2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/scp_dcp_job.cc
21 * @brief A job to copy DCPs to a SCP-enabled server.
26 #include <sys/types.h>
28 #include <boost/filesystem.hpp>
29 #include <libssh/libssh.h>
30 #include "compose.hpp"
31 #include "scp_dcp_job.h"
32 #include "exceptions.h"
40 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_GENERAL);
44 using boost::shared_ptr;
54 throw NetworkError (_("could not start SSH session"));
60 int r = ssh_connect (session);
70 ssh_disconnect (session);
84 SSHSCP (ssh_session s)
86 scp = ssh_scp_new (s, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ());
88 throw NetworkError (String::compose (_("could not start SCP session (%1)"), ssh_get_error (s)));
101 SCPDCPJob::SCPDCPJob (shared_ptr<const Film> film)
103 , _status (_("Waiting"))
109 SCPDCPJob::name () const
111 return _("Copy DCP to TMS");
115 SCPDCPJob::json_name () const
117 return N_("scp_dcp");
123 LOG_GENERAL_NC (N_("SCP DCP job starting"));
127 set_status (_("connecting"));
129 ssh_options_set (ss.session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
130 ssh_options_set (ss.session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
132 ssh_options_set (ss.session, SSH_OPTIONS_PORT, &port);
134 int r = ss.connect ();
136 throw NetworkError (String::compose (_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error (ss.session)));
139 int const state = ssh_is_server_known (ss.session);
140 if (state == SSH_SERVER_ERROR) {
141 throw NetworkError (String::compose (_("SSH error (%1)"), ssh_get_error (ss.session)));
144 r = ssh_userauth_password (ss.session, 0, Config::instance()->tms_password().c_str ());
145 if (r != SSH_AUTH_SUCCESS) {
146 throw NetworkError (String::compose (_("Failed to authenticate with server (%1)"), ssh_get_error (ss.session)));
149 SSHSCP sc (ss.session);
151 r = ssh_scp_init (sc.scp);
153 throw NetworkError (String::compose (_("Could not start SCP session (%1)"), ssh_get_error (ss.session)));
156 r = ssh_scp_push_directory (sc.scp, _film->dcp_name().c_str(), S_IRWXU);
158 throw NetworkError (String::compose (_("Could not create remote directory %1 (%2)"), _film->dcp_name(), ssh_get_error (ss.session)));
161 boost::filesystem::path const dcp_dir = _film->dir (_film->dcp_name());
163 boost::uintmax_t bytes_to_transfer = 0;
164 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
165 bytes_to_transfer += boost::filesystem::file_size (*i);
168 boost::uintmax_t buffer_size = 64 * 1024;
169 char buffer[buffer_size];
170 boost::uintmax_t bytes_transferred = 0;
172 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
174 string const leaf = boost::filesystem::path(*i).leaf().generic_string ();
176 set_status (String::compose (_("copying %1"), leaf));
178 boost::uintmax_t to_do = boost::filesystem::file_size (*i);
179 ssh_scp_push_file (sc.scp, leaf.c_str(), to_do, S_IRUSR | S_IWUSR);
181 FILE* f = fopen_boost (boost::filesystem::path (*i), "rb");
183 throw NetworkError (String::compose (_("Could not open %1 to send"), *i));
187 int const t = min (to_do, buffer_size);
188 size_t const read = fread (buffer, 1, t, f);
189 if (read != size_t (t)) {
191 throw ReadFileError (boost::filesystem::path (*i).string());
194 r = ssh_scp_write (sc.scp, buffer, t);
197 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), ssh_get_error (ss.session)));
200 bytes_transferred += t;
202 if (bytes_to_transfer > 0) {
203 set_progress ((double) bytes_transferred / bytes_to_transfer);
212 set_state (FINISHED_OK);
216 SCPDCPJob::status () const
218 boost::mutex::scoped_lock lm (_status_mutex);
219 string s = Job::status ();
220 if (!_status.empty ()) {
221 s += N_("; ") + _status;
227 SCPDCPJob::set_status (string s)
229 boost::mutex::scoped_lock lm (_status_mutex);