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