Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / scp_uploader.cc
1 /*
2     Copyright (C) 2012-2015 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 #include "scp_uploader.h"
21 #include "exceptions.h"
22 #include "job.h"
23 #include "config.h"
24 #include "cross.h"
25 #include "compose.hpp"
26 #include <sys/stat.h>
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::min;
32 using boost::shared_ptr;
33 using boost::function;
34
35 SCPUploader::SCPUploader (function<void (string)> set_status, function<void (float)> set_progress)
36         : Uploader (set_status, set_progress)
37 {
38         _session = ssh_new ();
39         if (!_session) {
40                 throw NetworkError (_("could not start SSH session"));
41         }
42
43         ssh_options_set (_session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
44         ssh_options_set (_session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
45         int const port = 22;
46         ssh_options_set (_session, SSH_OPTIONS_PORT, &port);
47
48         int r = ssh_connect (_session);
49         if (r != SSH_OK) {
50                 throw NetworkError (String::compose (_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error (_session)));
51         }
52
53         r = ssh_is_server_known (_session);
54         if (r == SSH_SERVER_ERROR) {
55                 throw NetworkError (String::compose (_("SSH error (%1)"), ssh_get_error (_session)));
56         }
57
58         r = ssh_userauth_password (_session, 0, Config::instance()->tms_password().c_str ());
59         if (r != SSH_AUTH_SUCCESS) {
60                 throw NetworkError (String::compose (_("Failed to authenticate with server (%1)"), ssh_get_error (_session)));
61         }
62
63         _scp = ssh_scp_new (_session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ());
64         if (!_scp) {
65                 throw NetworkError (String::compose (_("could not start SCP session (%1)"), ssh_get_error (_session)));
66         }
67
68         r = ssh_scp_init (_scp);
69         if (r != SSH_OK) {
70                 throw NetworkError (String::compose (_("Could not start SCP session (%1)"), ssh_get_error (_session)));
71         }
72 }
73
74 SCPUploader::~SCPUploader ()
75 {
76         ssh_scp_free (_scp);
77         ssh_disconnect (_session);
78         ssh_free (_session);
79 }
80
81 void
82 SCPUploader::create_directory (boost::filesystem::path directory)
83 {
84         int const r = ssh_scp_push_directory (_scp, directory.string().c_str(), S_IRWXU);
85         if (r != SSH_OK) {
86                 throw NetworkError (String::compose (_("Could not create remote directory %1 (%2)"), directory, ssh_get_error (_session)));
87         }
88 }
89
90 void
91 SCPUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
92 {
93         boost::uintmax_t to_do = boost::filesystem::file_size (from);
94         ssh_scp_push_file (_scp, to.string().c_str(), to_do, S_IRUSR | S_IWUSR);
95
96         FILE* f = fopen_boost (from, "rb");
97         if (f == 0) {
98                 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
99         }
100
101         boost::uintmax_t buffer_size = 64 * 1024;
102         char buffer[buffer_size];
103
104         while (to_do > 0) {
105                 int const t = min (to_do, buffer_size);
106                 size_t const read = fread (buffer, 1, t, f);
107                 if (read != size_t (t)) {
108                         fclose (f);
109                         throw ReadFileError (from);
110                 }
111
112                 int const r = ssh_scp_write (_scp, buffer, t);
113                 if (r != SSH_OK) {
114                         fclose (f);
115                         throw NetworkError (String::compose (_("Could not write to remote file (%1)"), ssh_get_error (_session)));
116                 }
117                 to_do -= t;
118                 transferred += t;
119
120                 if (total_size > 0) {
121                         _set_progress ((double) transferred / total_size);
122                 }
123         }
124
125         fclose (f);
126 }