Merge master; fix destruction of Server; some test cleanups.
[dcpomatic.git] / src / lib / scp_dcp_job.cc
index a9fdfefda2f01671698c83279a624e5102ed894d..63784081356e9eb0fc4d81ea97d551a7078e224d 100644 (file)
 #include "config.h"
 #include "log.h"
 #include "film.h"
+#include "cross.h"
 
 #include "i18n.h"
 
+#define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_GENERAL);
+
 using std::string;
 using std::stringstream;
 using std::min;
@@ -96,7 +99,7 @@ public:
 };
 
 
-SCPDCPJob::SCPDCPJob (shared_ptr<Film> f)
+SCPDCPJob::SCPDCPJob (shared_ptr<const Film> f)
        : Job (f)
        , _status (_("Waiting"))
 {
@@ -109,10 +112,16 @@ SCPDCPJob::name () const
        return _("Copy DCP to TMS");
 }
 
+string
+SCPDCPJob::json_name () const
+{
+       return N_("scp_dcp");
+}
+
 void
 SCPDCPJob::run ()
 {
-       _film->log()->log (N_("SCP DCP job starting"));
+       LOG_GENERAL_NC (N_("SCP DCP job starting"));
        
        SSHSession ss;
        
@@ -150,7 +159,7 @@ SCPDCPJob::run ()
                throw NetworkError (String::compose (_("Could not create remote directory %1 (%2)"), _film->dcp_name(), ssh_get_error (ss.session)));
        }
        
-       string const dcp_dir = _film->dir (_film->dcp_name());
+       boost::filesystem::path const dcp_dir = _film->dir (_film->dcp_name());
        
        boost::uintmax_t bytes_to_transfer = 0;
        for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
@@ -170,7 +179,7 @@ SCPDCPJob::run ()
                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 (boost::filesystem::path (*i).string().c_str(), N_("rb"));
+               FILE* f = fopen_boost (boost::filesystem::path (*i), "rb");
                if (f == 0) {
                        throw NetworkError (String::compose (_("Could not open %1 to send"), *i));
                }
@@ -179,17 +188,21 @@ SCPDCPJob::run ()
                        int const t = min (to_do, buffer_size);
                        size_t const read = fread (buffer, 1, t, f);
                        if (read != size_t (t)) {
+                               fclose (f);
                                throw ReadFileError (boost::filesystem::path (*i).string());
                        }
                        
                        r = ssh_scp_write (sc.scp, buffer, t);
                        if (r != SSH_OK) {
+                               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);
@@ -218,4 +231,3 @@ SCPDCPJob::set_status (string s)
        boost::mutex::scoped_lock lm (_status_mutex);
        _status = s;
 }
-