summaryrefslogtreecommitdiff
path: root/src/lib/copy_to_drive_job.cc
blob: bd46ba46951b210647239a4e609e766dc4bce9c2 (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
/*
    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 "copy_to_drive_job.h"
#include "dcpomatic_log.h"
#include "disk_writer_messages.h"
#include "exceptions.h"
#include <nanomsg/nn.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "i18n.h"


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


CopyToDriveJob::CopyToDriveJob(std::vector<boost::filesystem::path> const& dcps, Drive drive, Nanomsg& nanomsg)
	: Job (shared_ptr<Film>())
	, _dcps (dcps)
	, _drive (drive)
	, _nanomsg (nanomsg)
{

}


string
CopyToDriveJob::name () const
{
	if (_dcps.size() == 1) {
		return fmt::format(_("Copying {}\nto {}"), _dcps[0].filename().string(), _drive.description());
	}

	return fmt::format(_("Copying DCPs to {}"), _drive.description());
}


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

void
CopyToDriveJob::run ()
{
	LOG_DISK("Sending write requests to disk {} for:", _drive.device());
	for (auto dcp: _dcps) {
		LOG_DISK("{}", dcp.string());
	}

	string request = fmt::format(DISK_WRITER_WRITE "\n{}\n", _drive.device());
	for (auto dcp: _dcps) {
		request += fmt::format("{}\n", dcp.string());
	}
	request += "\n";
	if (!_nanomsg.send(request, 2000)) {
		LOG_DISK("Failed to send write request.");
		throw CommunicationFailedError ();
	}

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

	while (true) {
		auto response = DiskWriterBackEndResponse::read_from_nanomsg(_nanomsg, 10000);
		if (!response) {
			continue;
		}

		switch (response->type()) {
		case DiskWriterBackEndResponse::Type::OK:
			set_state (FINISHED_OK);
			return;
		case DiskWriterBackEndResponse::Type::PONG:
			break;
		case DiskWriterBackEndResponse::Type::ERROR:
			throw CopyError(response->error_message(), response->ext4_error_number(), response->platform_error_number());
		case DiskWriterBackEndResponse::Type::FORMAT_PROGRESS:
			if (state == SETUP) {
				sub (_("Formatting drive"));
				state = FORMAT;
			}
			set_progress(response->progress());
			break;
		case DiskWriterBackEndResponse::Type::COPY_PROGRESS:
			if (state == FORMAT) {
				sub (_("Copying DCP"));
				state = COPY;
			}
			set_progress(response->progress());
			break;
		case DiskWriterBackEndResponse::Type::VERIFY_PROGRESS:
			if (state == COPY) {
				sub (_("Verifying copied files"));
				state = VERIFY;
			}
			set_progress(response->progress());
			break;
		}
	}
}