summaryrefslogtreecommitdiff
path: root/src/lib/copy_to_drive_job.cc
blob: 539afb61f9cbd2504bf92eda7b6b40af6dddb026 (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
/*
    Copyright (C) 2019-2020 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic 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.

    DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "disk_writer_messages.h"
#include "copy_to_drive_job.h"
#include "exceptions.h"
#include "dcpomatic_log.h"
#include <dcp/compose.h>
#include <dcp/raw_convert.h>
#include <nanomsg/nn.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

#include "i18n.h"

using std::string;
using std::cout;
using std::min;
using std::shared_ptr;
using boost::optional;
using dcp::raw_convert;

CopyToDriveJob::CopyToDriveJob (boost::filesystem::path dcp, Drive drive, Nanomsg& nanomsg)
	: Job (shared_ptr<Film>())
	, _dcp (dcp)
	, _drive (drive)
	, _nanomsg (nanomsg)
{

}

string
CopyToDriveJob::name () const
{
	return dcp::compose (_("Copying %1\nto %2"), _dcp.filename().string(), _drive.description());
}

string
CopyToDriveJob::json_name () const
{
	return N_("copy");
}

void
CopyToDriveJob::run ()
{
	LOG_DISK("Sending write request to disk writer for %1 %2", _dcp.string(), _drive.device());
	if (!_nanomsg.send(dcp::compose(DISK_WRITER_WRITE "\n%1\n%2\n", _dcp.string(), _drive.device()), 2000)) {
		LOG_DISK_NC("Failed to send write request.");
		throw CommunicationFailedError ();
	}

	enum State {
		SETUP,
		FORMAT,
		COPY,
		VERIFY
	} state = SETUP;

	while (true) {
		optional<string> s = _nanomsg.receive (10000);
		if (!s) {
			continue;
		}
		if (*s == DISK_WRITER_OK) {
			set_state (FINISHED_OK);
			return;
		} else if (*s == DISK_WRITER_ERROR) {
			auto const m = _nanomsg.receive (500);
			auto const n = _nanomsg.receive (500);
			throw CopyError (m.get_value_or("Unknown"), raw_convert<int>(n.get_value_or("0")));
		} else if (*s == DISK_WRITER_FORMAT_PROGRESS) {
			if (state == SETUP) {
				sub (_("Formatting drive"));
				state = FORMAT;
			}
			auto progress = _nanomsg.receive (500);
			if (progress) {
				set_progress (raw_convert<float>(*progress));
			}
		} else if (*s == DISK_WRITER_COPY_PROGRESS) {
			if (state == FORMAT) {
				sub (_("Copying DCP"));
				state = COPY;
			}
			auto progress = _nanomsg.receive (500);
			if (progress) {
				set_progress (raw_convert<float>(*progress));
			}
		} else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
			if (state == COPY) {
				sub (_("Verifying copied files"));
				state = VERIFY;
			}
			auto progress = _nanomsg.receive (500);
			if (progress) {
				set_progress (raw_convert<float>(*progress));
			}
		}
	}
}