Supporters update.
[dcpomatic.git] / src / lib / copy_to_drive_job.cc
1 /*
2     Copyright (C) 2019-2020 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
22 #include "compose.hpp"
23 #include "copy_to_drive_job.h"
24 #include "dcpomatic_log.h"
25 #include "disk_writer_messages.h"
26 #include "exceptions.h"
27 #include <dcp/raw_convert.h>
28 #include <nanomsg/nn.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <iostream>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "i18n.h"
37
38
39 using std::cout;
40 using std::min;
41 using std::shared_ptr;
42 using std::string;
43 using boost::optional;
44 using dcp::raw_convert;
45
46
47 CopyToDriveJob::CopyToDriveJob(std::vector<boost::filesystem::path> const& dcps, Drive drive, Nanomsg& nanomsg)
48         : Job (shared_ptr<Film>())
49         , _dcps (dcps)
50         , _drive (drive)
51         , _nanomsg (nanomsg)
52 {
53
54 }
55
56
57 string
58 CopyToDriveJob::name () const
59 {
60         if (_dcps.size() == 1) {
61                 return String::compose(_("Copying %1\nto %2"), _dcps[0].filename().string(), _drive.description());
62         }
63
64         return String::compose(_("Copying DCPs to %1"), _drive.description());
65 }
66
67
68 string
69 CopyToDriveJob::json_name () const
70 {
71         return N_("copy");
72 }
73
74 void
75 CopyToDriveJob::run ()
76 {
77         LOG_DISK("Sending write requests to disk %1 for:", _drive.device());
78         for (auto dcp: _dcps) {
79                 LOG_DISK("%1", dcp.string());
80         }
81
82         string request = String::compose(DISK_WRITER_WRITE "\n%1\n", _drive.device());
83         for (auto dcp: _dcps) {
84                 request += String::compose("%1\n", dcp.string());
85         }
86         request += "\n";
87         if (!_nanomsg.send(request, 2000)) {
88                 LOG_DISK_NC("Failed to send write request.");
89                 throw CommunicationFailedError ();
90         }
91
92         enum State {
93                 SETUP,
94                 FORMAT,
95                 COPY,
96                 VERIFY
97         } state = SETUP;
98
99         while (true) {
100                 auto response = DiskWriterBackEndResponse::read_from_nanomsg(_nanomsg, 10000);
101                 if (!response) {
102                         continue;
103                 }
104
105                 switch (response->type()) {
106                 case DiskWriterBackEndResponse::Type::OK:
107                         set_state (FINISHED_OK);
108                         return;
109                 case DiskWriterBackEndResponse::Type::PONG:
110                         break;
111                 case DiskWriterBackEndResponse::Type::ERROR:
112                         throw CopyError(response->error_message(), response->ext4_error_number(), response->platform_error_number());
113                 case DiskWriterBackEndResponse::Type::FORMAT_PROGRESS:
114                         if (state == SETUP) {
115                                 sub (_("Formatting drive"));
116                                 state = FORMAT;
117                         }
118                         set_progress(response->progress());
119                         break;
120                 case DiskWriterBackEndResponse::Type::COPY_PROGRESS:
121                         if (state == FORMAT) {
122                                 sub (_("Copying DCP"));
123                                 state = COPY;
124                         }
125                         set_progress(response->progress());
126                         break;
127                 case DiskWriterBackEndResponse::Type::VERIFY_PROGRESS:
128                         if (state == COPY) {
129                                 sub (_("Verifying copied files"));
130                                 state = VERIFY;
131                         }
132                         set_progress(response->progress());
133                         break;
134                 }
135         }
136 }