Use dcp::file_to_string().
[dcpomatic.git] / src / lib / scp_uploader.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "scp_uploader.h"
23 #include "exceptions.h"
24 #include "job.h"
25 #include "config.h"
26 #include "cross.h"
27 #include "compose.hpp"
28 #include "warnings.h"
29 #include <sys/stat.h>
30
31 #include "i18n.h"
32
33
34 using std::function;
35 using std::min;
36 using std::shared_ptr;
37 using std::string;
38
39
40 SCPUploader::SCPUploader (function<void (string)> set_status, function<void (float)> set_progress)
41         : Uploader (set_status, set_progress)
42 {
43         _session = ssh_new ();
44         if (!_session) {
45                 throw NetworkError (String::compose(_("SSH error [%1]"), "ssh_new"));
46         }
47
48         ssh_options_set (_session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str());
49         ssh_options_set (_session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str());
50         int const port = 22;
51         ssh_options_set (_session, SSH_OPTIONS_PORT, &port);
52
53         int r = ssh_connect (_session);
54         if (r != SSH_OK) {
55                 throw NetworkError (String::compose(_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error(_session)));
56         }
57
58 DCPOMATIC_DISABLE_WARNINGS
59         r = ssh_is_server_known (_session);
60         if (r == SSH_SERVER_ERROR) {
61                 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_is_server_known", ssh_get_error(_session)));
62         }
63 DCPOMATIC_ENABLE_WARNINGS
64
65         r = ssh_userauth_password (_session, 0, Config::instance()->tms_password().c_str ());
66         if (r != SSH_AUTH_SUCCESS) {
67                 throw NetworkError (String::compose(_("Failed to authenticate with server (%1)"), ssh_get_error(_session)));
68         }
69
70         _scp = ssh_scp_new (_session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str());
71         if (!_scp) {
72                 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_scp_new", ssh_get_error(_session)));
73         }
74
75         r = ssh_scp_init (_scp);
76         if (r != SSH_OK) {
77                 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_scp_init", ssh_get_error(_session)));
78         }
79 }
80
81
82 SCPUploader::~SCPUploader ()
83 {
84         ssh_scp_free (_scp);
85         ssh_disconnect (_session);
86         ssh_free (_session);
87 }
88
89
90 void
91 SCPUploader::create_directory (boost::filesystem::path directory)
92 {
93         /* Use generic_string so that we get forward-slashes in the path, even on Windows */
94         int const r = ssh_scp_push_directory (_scp, directory.generic_string().c_str(), S_IRWXU);
95         if (r != SSH_OK) {
96                 throw NetworkError (String::compose(_("Could not create remote directory %1 (%2)"), directory, ssh_get_error(_session)));
97         }
98 }
99
100
101 void
102 SCPUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
103 {
104         auto to_do = boost::filesystem::file_size (from);
105         /* Use generic_string so that we get forward-slashes in the path, even on Windows */
106         ssh_scp_push_file (_scp, to.generic_string().c_str(), to_do, S_IRUSR | S_IWUSR);
107
108         auto f = fopen_boost (from, "rb");
109         if (f == nullptr) {
110                 throw NetworkError (String::compose(_("Could not open %1 to send"), from));
111         }
112
113         boost::uintmax_t buffer_size = 64 * 1024;
114         char buffer[buffer_size];
115
116         while (to_do > 0) {
117                 int const t = min (to_do, buffer_size);
118                 size_t const read = fread (buffer, 1, t, f);
119                 if (read != size_t (t)) {
120                         fclose (f);
121                         throw ReadFileError (from);
122                 }
123
124                 int const r = ssh_scp_write (_scp, buffer, t);
125                 if (r != SSH_OK) {
126                         fclose (f);
127                         throw NetworkError (String::compose(_("Could not write to remote file (%1)"), ssh_get_error(_session)));
128                 }
129                 to_do -= t;
130                 transferred += t;
131
132                 if (total_size > 0) {
133                         _set_progress ((double) transferred / total_size);
134                 }
135         }
136
137         fclose (f);
138 }