2 Copyright (C) 2012 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 "scp_dcp_job.h"
31 #include "exceptions.h"
34 #include "film_state.h"
37 using namespace boost;
47 throw NetworkError ("Could not start SSH session");
53 int r = ssh_connect (session);
63 ssh_disconnect (session);
77 SSHSCP (ssh_session s)
79 scp = ssh_scp_new (s, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ());
82 s << "Could not start SCP session (" << ssh_get_error (s) << ")";
83 throw NetworkError (s.str ());
96 SCPDCPJob::SCPDCPJob (shared_ptr<const FilmState> s, Log* l)
97 : Job (s, shared_ptr<const Options> (), l)
104 SCPDCPJob::name () const
107 s << "Copy DCP to TMS";
115 _log->log ("SCP DCP job starting");
119 set_status ("Connecting");
121 ssh_options_set (ss.session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
122 ssh_options_set (ss.session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
124 ssh_options_set (ss.session, SSH_OPTIONS_PORT, &port);
126 int r = ss.connect ();
129 s << "Could not connect to server " << Config::instance()->tms_ip() << " (" << ssh_get_error (ss.session) << ")";
130 throw NetworkError (s.str ());
133 int const state = ssh_is_server_known (ss.session);
134 if (state == SSH_SERVER_ERROR) {
136 s << "SSH error (" << ssh_get_error (ss.session) << ")";
137 throw NetworkError (s.str ());
140 r = ssh_userauth_password (ss.session, 0, Config::instance()->tms_password().c_str ());
141 if (r != SSH_AUTH_SUCCESS) {
143 s << "Failed to authenticate with server (" << ssh_get_error (ss.session) << ")";
144 throw NetworkError (s.str ());
147 SSHSCP sc (ss.session);
149 r = ssh_scp_init (sc.scp);
152 s << "Could not start SCP session (" << ssh_get_error (ss.session) << ")";
153 throw NetworkError (s.str ());
156 r = ssh_scp_push_directory (sc.scp, _fs->name.c_str(), S_IRWXU);
159 s << "Could not create remote directory " << _fs->name << "(" << ssh_get_error (ss.session) << ")";
160 throw NetworkError (s.str ());
163 string const dcp_dir = _fs->dir (_fs->name);
165 int bytes_to_transfer = 0;
166 for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) {
167 bytes_to_transfer += filesystem::file_size (*i);
170 int buffer_size = 64 * 1024;
171 char buffer[buffer_size];
172 int bytes_transferred = 0;
174 for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) {
176 /* Aah, the sweet smell of progress */
177 #if BOOST_FILESYSTEM_VERSION == 3
178 string const leaf = filesystem::path(*i).leaf().generic_string ();
180 string const leaf = i->leaf ();
183 set_status ("Copying " + leaf);
185 int to_do = filesystem::file_size (*i);
186 ssh_scp_push_file (sc.scp, leaf.c_str(), to_do, S_IRUSR | S_IWUSR);
188 int fd = open (filesystem::path (*i).string().c_str(), O_RDONLY);
191 s << "Could not open " << *i << " to send";
192 throw NetworkError (s.str ());
196 int const t = min (to_do, buffer_size);
197 read (fd, buffer, t);
198 r = ssh_scp_write (sc.scp, buffer, t);
201 s << "Could not write to remote file (" << ssh_get_error (ss.session) << ")";
202 throw NetworkError (s.str ());
205 bytes_transferred += t;
207 set_progress ((double) bytes_transferred / bytes_to_transfer);
213 set_state (FINISHED_OK);
215 } catch (std::exception& e) {
219 set_state (FINISHED_ERROR);
220 set_status (e.what ());
222 s << "SCP DCP job failed (" << e.what() << ")";
223 _log->log (s.str ());
230 SCPDCPJob::status () const
232 boost::mutex::scoped_lock lm (_status_mutex);
237 SCPDCPJob::set_status (string s)
239 boost::mutex::scoped_lock lm (_status_mutex);