Tidy up logging a bit. Make it configurable from the GUI.
[dcpomatic.git] / src / lib / scp_dcp_job.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/scp_dcp_job.cc
21  *  @brief A job to copy DCPs to a SCP-enabled server.
22  */
23
24 #include <iostream>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.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"
33 #include "config.h"
34 #include "log.h"
35 #include "film.h"
36 #include "cross.h"
37
38 #include "i18n.h"
39
40 #define LOG_GENERAL_NC(...) _film->log()->microsecond_log (__VA_ARGS__, Log::GENERAL);
41
42 using std::string;
43 using std::stringstream;
44 using std::min;
45 using boost::shared_ptr;
46
47 class SSHSession
48 {
49 public:
50         SSHSession ()
51                 : _connected (false)
52         {
53                 session = ssh_new ();
54                 if (session == 0) {
55                         throw NetworkError (_("could not start SSH session"));
56                 }
57         }
58
59         int connect ()
60         {
61                 int r = ssh_connect (session);
62                 if (r == 0) {
63                         _connected = true;
64                 }
65                 return r;
66         }
67
68         ~SSHSession ()
69         {
70                 if (_connected) {
71                         ssh_disconnect (session);
72                 }
73                 ssh_free (session);
74         }
75
76         ssh_session session;
77
78 private:        
79         bool _connected;
80 };
81
82 class SSHSCP
83 {
84 public:
85         SSHSCP (ssh_session s)
86         {
87                 scp = ssh_scp_new (s, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ());
88                 if (!scp) {
89                         throw NetworkError (String::compose (_("could not start SCP session (%1)"), ssh_get_error (s)));
90                 }
91         }
92
93         ~SSHSCP ()
94         {
95                 ssh_scp_free (scp);
96         }
97
98         ssh_scp scp;
99 };
100
101
102 SCPDCPJob::SCPDCPJob (shared_ptr<const Film> f)
103         : Job (f)
104         , _status (_("Waiting"))
105 {
106
107 }
108
109 string
110 SCPDCPJob::name () const
111 {
112         return _("Copy DCP to TMS");
113 }
114
115 string
116 SCPDCPJob::json_name () const
117 {
118         return N_("scp_dcp");
119 }
120
121 void
122 SCPDCPJob::run ()
123 {
124         LOG_GENERAL_NC (N_("SCP DCP job starting"));
125         
126         SSHSession ss;
127         
128         set_status (_("connecting"));
129         
130         ssh_options_set (ss.session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
131         ssh_options_set (ss.session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
132         int const port = 22;
133         ssh_options_set (ss.session, SSH_OPTIONS_PORT, &port);
134         
135         int r = ss.connect ();
136         if (r != SSH_OK) {
137                 throw NetworkError (String::compose (_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error (ss.session)));
138         }
139         
140         int const state = ssh_is_server_known (ss.session);
141         if (state == SSH_SERVER_ERROR) {
142                 throw NetworkError (String::compose (_("SSH error (%1)"), ssh_get_error (ss.session)));
143         }
144         
145         r = ssh_userauth_password (ss.session, 0, Config::instance()->tms_password().c_str ());
146         if (r != SSH_AUTH_SUCCESS) {
147                 throw NetworkError (String::compose (_("Failed to authenticate with server (%1)"), ssh_get_error (ss.session)));
148         }
149         
150         SSHSCP sc (ss.session);
151         
152         r = ssh_scp_init (sc.scp);
153         if (r != SSH_OK) {
154                 throw NetworkError (String::compose (_("Could not start SCP session (%1)"), ssh_get_error (ss.session)));
155         }
156         
157         r = ssh_scp_push_directory (sc.scp, _film->dcp_name().c_str(), S_IRWXU);
158         if (r != SSH_OK) {
159                 throw NetworkError (String::compose (_("Could not create remote directory %1 (%2)"), _film->dcp_name(), ssh_get_error (ss.session)));
160         }
161         
162         boost::filesystem::path const dcp_dir = _film->dir (_film->dcp_name());
163         
164         boost::uintmax_t bytes_to_transfer = 0;
165         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
166                 bytes_to_transfer += boost::filesystem::file_size (*i);
167         }
168         
169         boost::uintmax_t buffer_size = 64 * 1024;
170         char buffer[buffer_size];
171         boost::uintmax_t bytes_transferred = 0;
172         
173         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
174                 
175                 string const leaf = boost::filesystem::path(*i).leaf().generic_string ();
176                 
177                 set_status (String::compose (_("copying %1"), leaf));
178                 
179                 boost::uintmax_t to_do = boost::filesystem::file_size (*i);
180                 ssh_scp_push_file (sc.scp, leaf.c_str(), to_do, S_IRUSR | S_IWUSR);
181
182                 FILE* f = fopen_boost (boost::filesystem::path (*i), "rb");
183                 if (f == 0) {
184                         throw NetworkError (String::compose (_("Could not open %1 to send"), *i));
185                 }
186
187                 while (to_do > 0) {
188                         int const t = min (to_do, buffer_size);
189                         size_t const read = fread (buffer, 1, t, f);
190                         if (read != size_t (t)) {
191                                 fclose (f);
192                                 throw ReadFileError (boost::filesystem::path (*i).string());
193                         }
194                         
195                         r = ssh_scp_write (sc.scp, buffer, t);
196                         if (r != SSH_OK) {
197                                 fclose (f);
198                                 throw NetworkError (String::compose (_("Could not write to remote file (%1)"), ssh_get_error (ss.session)));
199                         }
200                         to_do -= t;
201                         bytes_transferred += t;
202
203                         if (bytes_to_transfer > 0) {
204                                 set_progress ((double) bytes_transferred / bytes_to_transfer);
205                         }
206                 }
207
208                 fclose (f);
209         }
210         
211         set_progress (1);
212         set_status (N_(""));
213         set_state (FINISHED_OK);
214 }
215
216 string
217 SCPDCPJob::status () const
218 {
219         boost::mutex::scoped_lock lm (_status_mutex);
220         stringstream s;
221         s << Job::status ();
222         if (!_status.empty ()) {
223                 s << N_("; ") << _status;
224         }
225         return s.str ();
226 }
227
228 void
229 SCPDCPJob::set_status (string s)
230 {
231         boost::mutex::scoped_lock lm (_status_mutex);
232         _status = s;
233 }