Fix crashes when using templates in some cases (#2491).
[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 <dcp/file.h>
29 #include <dcp/warnings.h>
30 #include <sys/stat.h>
31
32 #include "i18n.h"
33
34
35 using std::function;
36 using std::min;
37 using std::shared_ptr;
38 using std::string;
39
40
41 SCPUploader::SCPUploader (function<void (string)> set_status, function<void (float)> set_progress)
42         : Uploader (set_status, set_progress)
43 {
44         _session = ssh_new ();
45         if (!_session) {
46                 throw NetworkError (String::compose(_("SSH error [%1]"), "ssh_new"));
47         }
48
49         ssh_options_set (_session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str());
50         ssh_options_set (_session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str());
51         int const port = 22;
52         ssh_options_set (_session, SSH_OPTIONS_PORT, &port);
53
54         int r = ssh_connect (_session);
55         if (r != SSH_OK) {
56                 throw NetworkError (String::compose(_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error(_session)));
57         }
58
59 LIBDCP_DISABLE_WARNINGS
60         r = ssh_is_server_known (_session);
61         if (r == SSH_SERVER_ERROR) {
62                 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_is_server_known", ssh_get_error(_session)));
63         }
64 LIBDCP_ENABLE_WARNINGS
65
66         r = ssh_userauth_password (_session, 0, Config::instance()->tms_password().c_str ());
67         if (r != SSH_AUTH_SUCCESS) {
68                 throw NetworkError (String::compose(_("Failed to authenticate with server (%1)"), ssh_get_error(_session)));
69         }
70
71 LIBDCP_DISABLE_WARNINGS
72         _scp = ssh_scp_new (_session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str());
73 LIBDCP_ENABLE_WARNINGS
74         if (!_scp) {
75                 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_scp_new", ssh_get_error(_session)));
76         }
77
78 LIBDCP_DISABLE_WARNINGS
79         r = ssh_scp_init (_scp);
80 LIBDCP_ENABLE_WARNINGS
81         if (r != SSH_OK) {
82                 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_scp_init", ssh_get_error(_session)));
83         }
84 }
85
86
87 SCPUploader::~SCPUploader ()
88 {
89 LIBDCP_DISABLE_WARNINGS
90         ssh_scp_free (_scp);
91 LIBDCP_ENABLE_WARNINGS
92         ssh_disconnect (_session);
93         ssh_free (_session);
94 }
95
96
97 void
98 SCPUploader::create_directory (boost::filesystem::path directory)
99 {
100         /* Use generic_string so that we get forward-slashes in the path, even on Windows */
101 LIBDCP_DISABLE_WARNINGS
102         int const r = ssh_scp_push_directory (_scp, directory.generic_string().c_str(), S_IRWXU);
103 LIBDCP_ENABLE_WARNINGS
104         if (r != SSH_OK) {
105                 throw NetworkError (String::compose(_("Could not create remote directory %1 (%2)"), directory, ssh_get_error(_session)));
106         }
107 }
108
109
110 void
111 SCPUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
112 {
113         auto to_do = boost::filesystem::file_size (from);
114         /* Use generic_string so that we get forward-slashes in the path, even on Windows */
115 LIBDCP_DISABLE_WARNINGS
116         ssh_scp_push_file (_scp, to.generic_string().c_str(), to_do, S_IRUSR | S_IWUSR);
117 LIBDCP_ENABLE_WARNINGS
118
119         dcp::File f(from, "rb");
120         if (!f) {
121                 throw NetworkError (String::compose(_("Could not open %1 to send"), from));
122         }
123
124         boost::uintmax_t buffer_size = 64 * 1024;
125         char buffer[buffer_size];
126
127         while (to_do > 0) {
128                 int const t = min (to_do, buffer_size);
129                 size_t const read = f.read(buffer, 1, t);
130                 if (read != size_t (t)) {
131                         throw ReadFileError (from);
132                 }
133
134 LIBDCP_DISABLE_WARNINGS
135                 int const r = ssh_scp_write (_scp, buffer, t);
136 LIBDCP_ENABLE_WARNINGS
137                 if (r != SSH_OK) {
138                         throw NetworkError (String::compose(_("Could not write to remote file (%1)"), ssh_get_error(_session)));
139                 }
140                 to_do -= t;
141                 transferred += t;
142
143                 if (total_size > 0) {
144                         _set_progress ((double) transferred / total_size);
145                 }
146         }
147 }