summaryrefslogtreecommitdiff
path: root/src/lib/scp_dcp_job.cc
blob: 22129f56cdf05e2edadda7baedce3378e012160f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file src/scp_dcp_job.cc
 *  @brief A job to copy DCPs to a SCP-enabled server.
 */

#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <boost/filesystem.hpp>
#include <libssh/libssh.h>
#include "compose.hpp"
#include "scp_dcp_job.h"
#include "exceptions.h"
#include "config.h"
#include "log.h"
#include "film.h"

using std::string;
using std::stringstream;
using std::min;
using boost::shared_ptr;

class SSHSession
{
public:
	SSHSession ()
		: _connected (false)
	{
		session = ssh_new ();
		if (session == 0) {
			throw NetworkError ("Could not start SSH session");
		}
	}

	int connect ()
	{
		int r = ssh_connect (session);
		if (r == 0) {
			_connected = true;
		}
		return r;
	}

	~SSHSession ()
	{
		if (_connected) {
			ssh_disconnect (session);
		}
		ssh_free (session);
	}

	ssh_session session;

private:	
	bool _connected;
};

class SSHSCP
{
public:
	SSHSCP (ssh_session s)
	{
		scp = ssh_scp_new (s, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, Config::instance()->tms_path().c_str ());
		if (!scp) {
			throw NetworkError (String::compose ("Could not start SCP session (%1)", ssh_get_error (s)));
		}
	}

	~SSHSCP ()
	{
		ssh_scp_free (scp);
	}

	ssh_scp scp;
};


SCPDCPJob::SCPDCPJob (shared_ptr<Film> f, shared_ptr<Job> req)
	: Job (f, req)
	, _status ("Waiting")
{

}

string
SCPDCPJob::name () const
{
	return "Copy DCP to TMS";
}

void
SCPDCPJob::run ()
{
	_film->log()->log ("SCP DCP job starting");
	
	SSHSession ss;
	
	set_status ("connecting");
	
	ssh_options_set (ss.session, SSH_OPTIONS_HOST, Config::instance()->tms_ip().c_str ());
	ssh_options_set (ss.session, SSH_OPTIONS_USER, Config::instance()->tms_user().c_str ());
	int const port = 22;
	ssh_options_set (ss.session, SSH_OPTIONS_PORT, &port);
	
	int r = ss.connect ();
	if (r != SSH_OK) {
		throw NetworkError (String::compose ("Could not connect to server %1 (%2)", Config::instance()->tms_ip(), ssh_get_error (ss.session)));
	}
	
	int const state = ssh_is_server_known (ss.session);
	if (state == SSH_SERVER_ERROR) {
		throw NetworkError (String::compose ("SSH error (%1)", ssh_get_error (ss.session)));
	}
	
	r = ssh_userauth_password (ss.session, 0, Config::instance()->tms_password().c_str ());
	if (r != SSH_AUTH_SUCCESS) {
		throw NetworkError (String::compose ("Failed to authenticate with server (%1)", ssh_get_error (ss.session)));
	}
	
	SSHSCP sc (ss.session);
	
	r = ssh_scp_init (sc.scp);
	if (r != SSH_OK) {
		throw NetworkError (String::compose ("Could not start SCP session (%1)", ssh_get_error (ss.session)));
	}
	
	r = ssh_scp_push_directory (sc.scp, _film->dcp_name().c_str(), S_IRWXU);
	if (r != SSH_OK) {
		throw NetworkError (String::compose ("Could not create remote directory %1 (%2)", _film->dcp_name(), ssh_get_error (ss.session)));
	}
	
	string const dcp_dir = _film->dir (_film->dcp_name());
	
	boost::uintmax_t bytes_to_transfer = 0;
	for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
		bytes_to_transfer += boost::filesystem::file_size (*i);
	}
	
	boost::uintmax_t buffer_size = 64 * 1024;
	char buffer[buffer_size];
	boost::uintmax_t bytes_transferred = 0;
	
	for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
		
		/* Aah, the sweet smell of progress */
#if BOOST_FILESYSTEM_VERSION == 3		
		string const leaf = boost::filesystem::path(*i).leaf().generic_string ();
#else
		string const leaf = i->leaf ();
#endif
		
		set_status ("copying " + leaf);
		
		boost::uintmax_t to_do = boost::filesystem::file_size (*i);
		ssh_scp_push_file (sc.scp, leaf.c_str(), to_do, S_IRUSR | S_IWUSR);

		FILE* f = fopen (boost::filesystem::path (*i).string().c_str(), "rb");
		if (f == 0) {
			throw NetworkError (String::compose ("Could not open %1 to send", *i));
		}

		while (to_do > 0) {
			int const t = min (to_do, buffer_size);
			size_t const read = fread (buffer, 1, t, f);
			if (read != size_t (t)) {
				throw ReadFileError (boost::filesystem::path (*i).string());
			}
			
			r = ssh_scp_write (sc.scp, buffer, t);
			if (r != SSH_OK) {
				throw NetworkError (String::compose ("Could not write to remote file (%1)", ssh_get_error (ss.session)));
			}
			to_do -= t;
			bytes_transferred += t;
			
			set_progress ((double) bytes_transferred / bytes_to_transfer);
		}

		fclose (f);
	}
	
	set_progress (1);
	set_status ("");
	set_state (FINISHED_OK);
}

string
SCPDCPJob::status () const
{
	boost::mutex::scoped_lock lm (_status_mutex);
	stringstream s;
	s << Job::status ();
	if (!_status.empty ()) {
		s << "; " << _status;
	}
	return s.str ();
}

void
SCPDCPJob::set_status (string s)
{
	boost::mutex::scoped_lock lm (_status_mutex);
	_status = s;
}