X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fscp_dcp_job.cc;h=86eec16b24771b3844e31bd07585706a6dac7938;hb=e60bb3e51bd1508b149e6b8f6608f09b5196ae26;hp=300a8fe33c0dbec65140ea53f1b3160a30656873;hpb=068bb5b95652c022419c64f3d29cc42c4610ae93;p=dcpomatic.git diff --git a/src/lib/scp_dcp_job.cc b/src/lib/scp_dcp_job.cc index 300a8fe33..86eec16b2 100644 --- a/src/lib/scp_dcp_job.cc +++ b/src/lib/scp_dcp_job.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Carl Hetherington + Copyright (C) 2012-2015 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,14 +27,21 @@ #include #include #include +#include "compose.hpp" #include "scp_dcp_job.h" #include "exceptions.h" #include "config.h" #include "log.h" -#include "film_state.h" +#include "film.h" +#include "cross.h" -using namespace std; -using namespace boost; +#include "i18n.h" + +#define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_GENERAL); + +using std::string; +using std::min; +using boost::shared_ptr; class SSHSession { @@ -44,7 +51,7 @@ public: { session = ssh_new (); if (session == 0) { - throw NetworkError ("Could not start SSH session"); + throw NetworkError (_("could not start SSH session")); } } @@ -67,7 +74,7 @@ public: ssh_session session; -private: +private: bool _connected; }; @@ -78,7 +85,7 @@ public: { scp = ssh_scp_new (s, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ()); if (!scp) { - throw NetworkError (String::compose ("Could not start SCP session (%1)", ssh_get_error (s))); + throw NetworkError (String::compose (_("could not start SCP session (%1)"), ssh_get_error (s))); } } @@ -91,9 +98,9 @@ public: }; -SCPDCPJob::SCPDCPJob (shared_ptr s, Log* l, shared_ptr req) - : Job (s, shared_ptr (), l, req) - , _status ("Waiting") +SCPDCPJob::SCPDCPJob (shared_ptr film) + : Job (film) + , _status (_("Waiting")) { } @@ -101,102 +108,107 @@ SCPDCPJob::SCPDCPJob (shared_ptr s, Log* l, shared_ptr req string SCPDCPJob::name () const { - return "Copy DCP to TMS"; + return _("Copy DCP to TMS"); +} + +string +SCPDCPJob::json_name () const +{ + return N_("scp_dcp"); } void SCPDCPJob::run () { - _log->log ("SCP DCP job starting"); - + LOG_GENERAL_NC (N_("SCP DCP job starting")); + SSHSession ss; - - set_status ("connecting"); - + + set_status (_("connecting")); + ssh_options_set (ss.session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ()); ssh_options_set (ss.session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ()); int const port = 22; ssh_options_set (ss.session, SSH_OPTIONS_PORT, &port); - + int r = ss.connect (); if (r != SSH_OK) { - throw NetworkError (String::compose ("Could not connect to server %1 (%2)", Config::instance()->tms_ip(), ssh_get_error (ss.session))); + throw NetworkError (String::compose (_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error (ss.session))); } - + int const state = ssh_is_server_known (ss.session); if (state == SSH_SERVER_ERROR) { - throw NetworkError (String::compose ("SSH error (%1)", ssh_get_error (ss.session))); + throw NetworkError (String::compose (_("SSH error (%1)"), ssh_get_error (ss.session))); } - + r = ssh_userauth_password (ss.session, 0, Config::instance()->tms_password().c_str ()); if (r != SSH_AUTH_SUCCESS) { - throw NetworkError (String::compose ("Failed to authenticate with server (%1)", ssh_get_error (ss.session))); + throw NetworkError (String::compose (_("Failed to authenticate with server (%1)"), ssh_get_error (ss.session))); } - + SSHSCP sc (ss.session); - + r = ssh_scp_init (sc.scp); if (r != SSH_OK) { - throw NetworkError (String::compose ("Could not start SCP session (%1)", ssh_get_error (ss.session))); + throw NetworkError (String::compose (_("Could not start SCP session (%1)"), ssh_get_error (ss.session))); } - - r = ssh_scp_push_directory (sc.scp, _fs->dcp_name().c_str(), S_IRWXU); + + r = ssh_scp_push_directory (sc.scp, _film->dcp_name().c_str(), S_IRWXU); if (r != SSH_OK) { - throw NetworkError (String::compose ("Could not create remote directory %1 (%2)", _fs->dcp_name(), ssh_get_error (ss.session))); + throw NetworkError (String::compose (_("Could not create remote directory %1 (%2)"), _film->dcp_name(), ssh_get_error (ss.session))); } - - string const dcp_dir = _fs->dir (_fs->dcp_name()); - + + boost::filesystem::path const dcp_dir = _film->dir (_film->dcp_name()); + boost::uintmax_t bytes_to_transfer = 0; - for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) { - bytes_to_transfer += filesystem::file_size (*i); + for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) { + bytes_to_transfer += boost::filesystem::file_size (*i); } - + boost::uintmax_t buffer_size = 64 * 1024; char buffer[buffer_size]; boost::uintmax_t bytes_transferred = 0; - - for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) { - - /* Aah, the sweet smell of progress */ -#if BOOST_FILESYSTEM_VERSION == 3 - string const leaf = filesystem::path(*i).leaf().generic_string (); -#else - string const leaf = i->leaf (); -#endif - - set_status ("copying " + leaf); - - boost::uintmax_t to_do = filesystem::file_size (*i); + + for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) { + + string const leaf = boost::filesystem::path(*i).leaf().generic_string (); + + set_status (String::compose (_("copying %1"), leaf)); + + boost::uintmax_t to_do = boost::filesystem::file_size (*i); ssh_scp_push_file (sc.scp, leaf.c_str(), to_do, S_IRUSR | S_IWUSR); - FILE* f = fopen (filesystem::path (*i).string().c_str(), "rb"); + FILE* f = fopen_boost (boost::filesystem::path (*i), "rb"); if (f == 0) { - throw NetworkError (String::compose ("Could not open %1 to send", *i)); + throw NetworkError (String::compose (_("Could not open %1 to send"), *i)); } while (to_do > 0) { int const t = min (to_do, buffer_size); size_t const read = fread (buffer, 1, t, f); if (read != size_t (t)) { - throw ReadFileError (filesystem::path (*i).string()); + fclose (f); + throw ReadFileError (boost::filesystem::path (*i).string()); } - + r = ssh_scp_write (sc.scp, buffer, t); if (r != SSH_OK) { - throw NetworkError (String::compose ("Could not write to remote file (%1)", ssh_get_error (ss.session))); + fclose (f); + throw NetworkError (String::compose (_("Could not write to remote file (%1)"), ssh_get_error (ss.session))); } to_do -= t; bytes_transferred += t; - - set_progress ((double) bytes_transferred / bytes_to_transfer); + + if (bytes_to_transfer > 0) { + set_progress ((double) bytes_transferred / bytes_to_transfer); + } } fclose (f); } - + set_progress (1); - set_status (""); + set_status (N_("")); set_state (FINISHED_OK); } @@ -204,12 +216,11 @@ string SCPDCPJob::status () const { boost::mutex::scoped_lock lm (_status_mutex); - stringstream s; - s << Job::status (); + string s = Job::status (); if (!_status.empty ()) { - s << "; " << _status; + s += N_("; ") + _status; } - return s.str (); + return s; } void @@ -218,4 +229,3 @@ SCPDCPJob::set_status (string s) boost::mutex::scoped_lock lm (_status_mutex); _status = s; } -