2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "scp_uploader.h"
23 #include "exceptions.h"
27 #include "compose.hpp"
29 #include <dcp/filesystem.h>
30 #include <dcp/warnings.h>
38 using std::shared_ptr;
42 SCPUploader::SCPUploader (function<void (string)> set_status, function<void (float)> set_progress)
43 : Uploader (set_status, set_progress)
45 _session = ssh_new ();
47 throw NetworkError (String::compose(_("SSH error [%1]"), "ssh_new"));
50 ssh_options_set (_session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str());
51 ssh_options_set (_session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str());
53 ssh_options_set (_session, SSH_OPTIONS_PORT, &port);
55 int r = ssh_connect (_session);
57 throw NetworkError (String::compose(_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error(_session)));
60 LIBDCP_DISABLE_WARNINGS
61 r = ssh_is_server_known (_session);
62 if (r == SSH_SERVER_ERROR) {
63 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_is_server_known", ssh_get_error(_session)));
65 LIBDCP_ENABLE_WARNINGS
67 r = ssh_userauth_password (_session, 0, Config::instance()->tms_password().c_str ());
68 if (r != SSH_AUTH_SUCCESS) {
69 throw NetworkError (String::compose(_("Failed to authenticate with server (%1)"), ssh_get_error(_session)));
72 LIBDCP_DISABLE_WARNINGS
73 _scp = ssh_scp_new (_session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str());
74 LIBDCP_ENABLE_WARNINGS
76 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_scp_new", ssh_get_error(_session)));
79 LIBDCP_DISABLE_WARNINGS
80 r = ssh_scp_init (_scp);
81 LIBDCP_ENABLE_WARNINGS
83 throw NetworkError (String::compose(_("SSH error [%1] (%2)"), "ssh_scp_init", ssh_get_error(_session)));
88 SCPUploader::~SCPUploader ()
90 LIBDCP_DISABLE_WARNINGS
92 LIBDCP_ENABLE_WARNINGS
93 ssh_disconnect (_session);
99 SCPUploader::create_directory (boost::filesystem::path directory)
101 /* Use generic_string so that we get forward-slashes in the path, even on Windows */
102 LIBDCP_DISABLE_WARNINGS
103 int const r = ssh_scp_push_directory (_scp, directory.generic_string().c_str(), S_IRWXU);
104 LIBDCP_ENABLE_WARNINGS
106 throw NetworkError (String::compose(_("Could not create remote directory %1 (%2)"), directory, ssh_get_error(_session)));
112 SCPUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
114 auto to_do = dcp::filesystem::file_size(from);
115 /* Use generic_string so that we get forward-slashes in the path, even on Windows */
116 LIBDCP_DISABLE_WARNINGS
117 ssh_scp_push_file (_scp, to.generic_string().c_str(), to_do, S_IRUSR | S_IWUSR);
118 LIBDCP_ENABLE_WARNINGS
120 dcp::File f(from, "rb");
122 throw NetworkError (String::compose(_("Could not open %1 to send"), from));
125 boost::uintmax_t buffer_size = 64 * 1024;
126 char buffer[buffer_size];
129 int const t = min (to_do, buffer_size);
130 size_t const read = f.read(buffer, 1, t);
131 if (read != size_t (t)) {
132 throw ReadFileError (from);
135 LIBDCP_DISABLE_WARNINGS
136 int const r = ssh_scp_write (_scp, buffer, t);
137 LIBDCP_ENABLE_WARNINGS
139 throw NetworkError (String::compose(_("Could not write to remote file (%1)"), ssh_get_error(_session)));
144 if (total_size > 0) {
145 _set_progress ((double) transferred / total_size);