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