Move things round a bit.
[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 "scp_dcp_job.h"
31 #include "exceptions.h"
32 #include "config.h"
33 #include "log.h"
34 #include "film_state.h"
35
36 using namespace std;
37 using namespace boost;
38
39 class SSHSession
40 {
41 public:
42         SSHSession ()
43                 : _connected (false)
44         {
45                 session = ssh_new ();
46                 if (session == 0) {
47                         throw NetworkError ("Could not start SSH session");
48                 }
49         }
50
51         int connect ()
52         {
53                 int r = ssh_connect (session);
54                 if (r == 0) {
55                         _connected = true;
56                 }
57                 return r;
58         }
59
60         ~SSHSession ()
61         {
62                 if (_connected) {
63                         ssh_disconnect (session);
64                 }
65                 ssh_free (session);
66         }
67
68         ssh_session session;
69
70 private:        
71         bool _connected;
72 };
73
74 class SSHSCP
75 {
76 public:
77         SSHSCP (ssh_session s)
78         {
79                 scp = ssh_scp_new (s, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ());
80                 if (!scp) {
81                         stringstream s;
82                         s << "Could not start SCP session (" << ssh_get_error (s) << ")";
83                         throw NetworkError (s.str ());
84                 }
85         }
86
87         ~SSHSCP ()
88         {
89                 ssh_scp_free (scp);
90         }
91
92         ssh_scp scp;
93 };
94
95
96 SCPDCPJob::SCPDCPJob (shared_ptr<const FilmState> s, Log* l)
97         : Job (s, shared_ptr<const Options> (), l)
98         , _status ("Waiting")
99 {
100
101 }
102
103 string
104 SCPDCPJob::name () const
105 {
106         stringstream s;
107         s << "Copy DCP to TMS";
108         return s.str ();
109 }
110
111 void
112 SCPDCPJob::run ()
113 {
114         try {
115                 _log->log ("SCP DCP job starting");
116
117                 SSHSession ss;
118
119                 set_status ("Connecting");
120
121                 ssh_options_set (ss.session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
122                 ssh_options_set (ss.session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
123                 int const port = 22;
124                 ssh_options_set (ss.session, SSH_OPTIONS_PORT, &port);
125                 
126                 int r = ss.connect ();
127                 if (r != SSH_OK) {
128                         stringstream s;
129                         s << "Could not connect to server " << Config::instance()->tms_ip() << " (" << ssh_get_error (ss.session) << ")";
130                         throw NetworkError (s.str ());
131                 }
132
133                 int const state = ssh_is_server_known (ss.session);
134                 if (state == SSH_SERVER_ERROR) {
135                         stringstream s;
136                         s << "SSH error (" << ssh_get_error (ss.session) << ")";
137                         throw NetworkError (s.str ());
138                 }
139
140                 r = ssh_userauth_password (ss.session, 0, Config::instance()->tms_password().c_str ());
141                 if (r != SSH_AUTH_SUCCESS) {
142                         stringstream s;
143                         s << "Failed to authenticate with server (" << ssh_get_error (ss.session) << ")";
144                         throw NetworkError (s.str ());
145                 }
146                 
147                 SSHSCP sc (ss.session);
148
149                 r = ssh_scp_init (sc.scp);
150                 if (r != SSH_OK) {
151                         stringstream s;
152                         s << "Could not start SCP session (" << ssh_get_error (ss.session) << ")";
153                         throw NetworkError (s.str ());
154                 }
155
156                 r = ssh_scp_push_directory (sc.scp, _fs->name.c_str(), S_IRWXU);
157                 if (r != SSH_OK) {
158                         stringstream s;
159                         s << "Could not create remote directory " << _fs->name << "(" << ssh_get_error (ss.session) << ")";
160                         throw NetworkError (s.str ());
161                 }
162
163                 string const dcp_dir = _fs->dir (_fs->name);
164
165                 int bytes_to_transfer = 0;
166                 for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) {
167                         bytes_to_transfer += filesystem::file_size (*i);
168                 }
169                 
170                 int buffer_size = 64 * 1024;
171                 char buffer[buffer_size];
172                 int bytes_transferred = 0;
173                 
174                 for (filesystem::directory_iterator i = filesystem::directory_iterator (dcp_dir); i != filesystem::directory_iterator(); ++i) {
175
176                         /* Aah, the sweet smell of progress */
177 #if BOOST_FILESYSTEM_VERSION == 3               
178                         string const leaf = filesystem::path(*i).leaf().generic_string ();
179 #else
180                         string const leaf = i->leaf ();
181 #endif
182
183                         set_status ("Copying " + leaf);
184
185                         int to_do = filesystem::file_size (*i);
186                         ssh_scp_push_file (sc.scp, leaf.c_str(), to_do, S_IRUSR | S_IWUSR);
187
188                         int fd = open (filesystem::path (*i).string().c_str(), O_RDONLY);
189                         if (fd == 0) {
190                                 stringstream s;
191                                 s << "Could not open " << *i << " to send";
192                                 throw NetworkError (s.str ());
193                         }
194
195                         while (to_do > 0) {
196                                 int const t = min (to_do, buffer_size);
197                                 read (fd, buffer, t);
198                                 r = ssh_scp_write (sc.scp, buffer, t);
199                                 if (r != SSH_OK) {
200                                         stringstream s;
201                                         s << "Could not write to remote file (" << ssh_get_error (ss.session) << ")";
202                                         throw NetworkError (s.str ());
203                                 }
204                                 to_do -= t;
205                                 bytes_transferred += t;
206
207                                 set_progress ((double) bytes_transferred / bytes_to_transfer);
208                         }
209                 }
210
211                 set_progress (1);
212                 set_status ("OK");
213                 set_state (FINISHED_OK);
214
215         } catch (std::exception& e) {
216
217                 stringstream s;
218                 set_progress (1);
219                 set_state (FINISHED_ERROR);
220                 set_status (e.what ());
221
222                 s << "SCP DCP job failed (" << e.what() << ")";
223                 _log->log (s.str ());
224
225                 throw;
226         }
227 }
228
229 string
230 SCPDCPJob::status () const
231 {
232         boost::mutex::scoped_lock lm (_status_mutex);
233         return _status;
234 }
235
236 void
237 SCPDCPJob::set_status (string s)
238 {
239         boost::mutex::scoped_lock lm (_status_mutex);
240         _status = s;
241 }
242