ef7c09060e01d154a94cbde2de6b3459867d2914
[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 "dist_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 <unistd.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::cout;
37 using std::min;
38 using boost::shared_ptr;
39 using dcp::raw_convert;
40
41 CopyToDriveJob::CopyToDriveJob (boost::filesystem::path dcp, Drive drive, boost::process::opstream& to_writer, boost::process::ipstream& from_writer)
42         : Job (shared_ptr<Film>())
43         , _dcp (dcp)
44         , _drive (drive)
45         , _to_writer (to_writer)
46         , _from_writer (from_writer)
47 {
48
49 }
50
51 string
52 CopyToDriveJob::name () const
53 {
54         return String::compose (_("Copying %1 to %2"), _dcp.filename().string(), _drive.description());
55 }
56
57 string
58 CopyToDriveJob::json_name () const
59 {
60         return N_("copy");
61 }
62
63 void
64 CopyToDriveJob::run ()
65 {
66         _to_writer << _dcp.string() << "\n";
67         _to_writer << _drive.internal_name() << "\n";
68         _to_writer.flush ();
69
70         while (true) {
71                 string s;
72                 getline (_from_writer, s);
73                 if (s == DIST_WRITER_OK) {
74                         set_state (FINISHED_OK);
75                         return;
76                 } else if (s == DIST_WRITER_ERROR) {
77                         string m;
78                         getline (_from_writer, m);
79                         string n;
80                         getline (_from_writer, n);
81                         throw CopyError (m, raw_convert<int>(n));
82                 } else if (s == DIST_WRITER_PROGRESS) {
83                         string p;
84                         getline (_from_writer, p);
85                         set_progress (raw_convert<float>(p));
86                 }
87         }
88 }