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";
114 _log->log ("SCP DCP job starting");
118 set_status ("connecting");
120 ssh_options_set (ss.session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
121 ssh_options_set (ss.session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
123 ssh_options_set (ss.session, SSH_OPTIONS_PORT, &port);
125 int r = ss.connect ();
128 s << "Could not connect to server " << Config::instance()->tms_ip() << " (" << ssh_get_error (ss.session) << ")";
129 throw NetworkError (s.str ());
132 int const state = ssh_is_server_known (ss.session);
133 if (state == SSH_SERVER_ERROR) {
135 s << "SSH error (" << ssh_get_error (ss.session) << ")";
136 throw NetworkError (s.str ());
139 r = ssh_userauth_password (ss.session, 0, Config::instance()->tms_password().c_str ());
140 if (r != SSH_AUTH_SUCCESS) {
142 s << "Failed to authenticate with server (" << ssh_get_error (ss.session) << ")";
143 throw NetworkError (s.str ());
146 SSHSCP sc (ss.session);
148 r = ssh_scp_init (sc.scp);
151 s << "Could not start SCP session (" << ssh_get_error (ss.session) << ")";
152 throw NetworkError (s.str ());
155 r = ssh_scp_push_directory (sc.scp, _fs->name.c_str(), S_IRWXU);
158 s << "Could not create remote directory " << _fs->name << "(" << ssh_get_error (ss.session) << ")";
159 throw NetworkError (s.str ());
162 string const dcp_dir = _fs->dir (_fs->name);
164 boost::uintmax_t bytes_to_transfer = 0;
165 for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) {
166 bytes_to_transfer += filesystem::file_size (*i);
169 boost::uintmax_t buffer_size = 64 * 1024;
170 char buffer[buffer_size];
171 boost::uintmax_t bytes_transferred = 0;
173 for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) {
175 /* Aah, the sweet smell of progress */
176 #if BOOST_FILESYSTEM_VERSION == 3
177 string const leaf = filesystem::path(*i).leaf().generic_string ();
179 string const leaf = i->leaf ();
182 set_status ("copying " + leaf);
184 boost::uintmax_t to_do = filesystem::file_size (*i);
185 ssh_scp_push_file (sc.scp, leaf.c_str(), to_do, S_IRUSR | S_IWUSR);
187 FILE* f = fopen (filesystem::path (*i).string().c_str(), "rb");
190 s << "Could not open " << *i << " to send";
191 throw NetworkError (s.str ());
195 int const t = min (to_do, buffer_size);
196 size_t const read = fread (buffer, 1, t, f);
197 if (read != size_t (t)) {
198 throw ReadFileError (filesystem::path (*i).string());
201 r = ssh_scp_write (sc.scp, buffer, t);
204 s << "Could not write to remote file (" << ssh_get_error (ss.session) << ")";
205 throw NetworkError (s.str ());
208 bytes_transferred += t;
210 set_progress ((double) bytes_transferred / bytes_to_transfer);
218 set_state (FINISHED_OK);
222 SCPDCPJob::status () const
224 boost::mutex::scoped_lock lm (_status_mutex);
227 if (!_status.empty ()) {
228 s << "; " << _status;
234 SCPDCPJob::set_status (string s)
236 boost::mutex::scoped_lock lm (_status_mutex);