Missing files.
[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 #include "disk_writer_messages.h"
22 #include "copy_to_drive_job.h"
23 #include "compose.hpp"
24 #include "exceptions.h"
25 #include <dcp/raw_convert.h>
26 #include <nanomsg/nn.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <iostream>
33
34 #include "i18n.h"
35
36 using std::string;
37 using std::cout;
38 using std::min;
39 using boost::shared_ptr;
40 using boost::optional;
41 using dcp::raw_convert;
42
43 CopyToDriveJob::CopyToDriveJob (boost::filesystem::path dcp, Drive drive, Nanomsg& nanomsg)
44         : Job (shared_ptr<Film>())
45         , _dcp (dcp)
46         , _drive (drive)
47         , _nanomsg (nanomsg)
48 {
49
50 }
51
52 string
53 CopyToDriveJob::name () const
54 {
55         return String::compose (_("Copying %1 to %2"), _dcp.filename().string(), _drive.description());
56 }
57
58 string
59 CopyToDriveJob::json_name () const
60 {
61         return N_("copy");
62 }
63
64 void
65 CopyToDriveJob::run ()
66 {
67         if (!_nanomsg.send(String::compose(DISK_WRITER_WRITE "\n%1\n%2\n", _dcp.string(), _drive.device()), 2000)) {
68                 throw CommunicationFailedError ();
69         }
70
71         bool formatting = false;
72         while (true) {
73                 optional<string> s = _nanomsg.receive (10000);
74                 if (!s) {
75                         continue;
76                 }
77                 if (*s == DISK_WRITER_OK) {
78                         set_state (FINISHED_OK);
79                         return;
80                 } else if (*s == DISK_WRITER_ERROR) {
81                         optional<string> const m = _nanomsg.receive (500);
82                         optional<string> const n = _nanomsg.receive (500);
83                         throw CopyError (m.get_value_or("Unknown"), raw_convert<int>(n.get_value_or("0")));
84                 } else if (*s == DISK_WRITER_FORMATTING) {
85                         sub ("Formatting drive");
86                         set_progress_unknown ();
87                         formatting = true;
88                 } else if (*s == DISK_WRITER_PROGRESS) {
89                         if (formatting) {
90                                 sub ("Copying DCP");
91                                 formatting = false;
92                         }
93                         optional<string> progress = _nanomsg.receive (500);
94                         if (progress) {
95                                 set_progress (raw_convert<float>(*progress));
96                         }
97                 }
98         }
99 }