std::shared_ptr
[dcpomatic.git] / src / lib / scp_uploader.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "scp_uploader.h"
22 #include "exceptions.h"
23 #include "job.h"
24 #include "config.h"
25 #include "cross.h"
26 #include "compose.hpp"
27 #include "warnings.h"
28 #include <sys/stat.h>
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::min;
34 using std::shared_ptr;
35 using boost::function;
36
37 SCPUploader::SCPUploader (function<void (string)> set_status, function<void (float)> set_progress)
38         : Uploader (set_status, set_progress)
39 {
40         _session = ssh_new ();
41         if (!_session) {
42                 throw NetworkError (_("could not start SSH session"));
43         }
44
45         ssh_options_set (_session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
46         ssh_options_set (_session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
47         int const port = 22;
48         ssh_options_set (_session, SSH_OPTIONS_PORT, &port);
49
50         int r = ssh_connect (_session);
51         if (r != SSH_OK) {
52                 throw NetworkError (String::compose (_("Could not connect to server %1 (%2)"), Config::instance()->tms_ip(), ssh_get_error (_session)));
53         }
54
55 DCPOMATIC_DISABLE_WARNINGS
56         r = ssh_is_server_known (_session);
57         if (r == SSH_SERVER_ERROR) {
58                 throw NetworkError (String::compose (_("SSH error (%1)"), ssh_get_error (_session)));
59         }
60 DCPOMATIC_ENABLE_WARNINGS
61
62         r = ssh_userauth_password (_session, 0, Config::instance()->tms_password().c_str ());
63         if (r != SSH_AUTH_SUCCESS) {
64                 throw NetworkError (String::compose (_("Failed to authenticate with server (%1)"), ssh_get_error (_session)));
65         }
66
67         _scp = ssh_scp_new (_session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ());
68         if (!_scp) {
69                 throw NetworkError (String::compose (_("could not start SCP session (%1)"), ssh_get_error (_session)));
70         }
71
72         r = ssh_scp_init (_scp);
73         if (r != SSH_OK) {
74                 throw NetworkError (String::compose (_("Could not start SCP session (%1)"), ssh_get_error (_session)));
75         }
76 }
77
78 SCPUploader::~SCPUploader ()
79 {
80         ssh_scp_free (_scp);
81         ssh_disconnect (_session);
82         ssh_free (_session);
83 }
84
85 void
86 SCPUploader::create_directory (boost::filesystem::path directory)
87 {
88         /* Use generic_string so that we get forward-slashes in the path, even on Windows */
89         int const r = ssh_scp_push_directory (_scp, directory.generic_string().c_str(), S_IRWXU);
90         if (r != SSH_OK) {
91                 throw NetworkError (String::compose (_("Could not create remote directory %1 (%2)"), directory, ssh_get_error (_session)));
92         }
93 }
94
95 void
96 SCPUploader::upload_file (boost::filesystem::path from, boost::filesystem::path to, boost::uintmax_t& transferred, boost::uintmax_t total_size)
97 {
98         boost::uintmax_t to_do = boost::filesystem::file_size (from);
99         /* Use generic_string so that we get forward-slashes in the path, even on Windows */
100         ssh_scp_push_file (_scp, to.generic_string().c_str(), to_do, S_IRUSR | S_IWUSR);
101
102         FILE* f = fopen_boost (from, "rb");
103         if (f == 0) {
104                 throw NetworkError (String::compose (_("Could not open %1 to send"), from));
105         }
106
107         boost::uintmax_t buffer_size = 64 * 1024;
108         char buffer[buffer_size];
109
110         while (to_do > 0) {
111                 int const t = min (to_do, buffer_size);
112                 size_t const read = fread (buffer, 1, t, f);
113                 if (read != size_t (t)) {
114                         fclose (f);
115                         throw ReadFileError (from);
116                 }
117
118                 int const r = ssh_scp_write (_scp, buffer, t);
119                 if (r != SSH_OK) {
120                         fclose (f);
121                         throw NetworkError (String::compose (_("Could not write to remote file (%1)"), ssh_get_error (_session)));
122                 }
123                 to_do -= t;
124                 transferred += t;
125
126                 if (total_size > 0) {
127                         _set_progress ((double) transferred / total_size);
128                 }
129         }
130
131         fclose (f);
132 }