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