Add disk writer tool.
[dcpomatic.git] / src / tools / dcpomatic_disk_writer.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 "lib/disk_writer_messages.h"
22 #include "lib/compose.hpp"
23 #include "lib/exceptions.h"
24 #include "lib/cross.h"
25 #include "lib/digester.h"
26 #include "lib/file_log.h"
27 #include "lib/dcpomatic_log.h"
28 #include "lib/nanomsg.h"
29 extern "C" {
30 #include <lwext4/ext4_mbr.h>
31 #include <lwext4/ext4_fs.h>
32 #include <lwext4/ext4_mkfs.h>
33 #include <lwext4/ext4_errno.h>
34 #include <lwext4/ext4_debug.h>
35 #include <lwext4/ext4.h>
36 }
37
38 #ifdef DCPOMATIC_POSIX
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #endif
43
44 #ifdef DCPOMATIC_OSX
45 #undef nil
46 extern "C" {
47 #include <lwext4/file_dev.h>
48 }
49 #endif
50
51 #ifdef DCPOMATIC_LINUX
52 #include <linux/fs.h>
53 #include <polkit/polkit.h>
54 extern "C" {
55 #include <lwext4/file_dev.h>
56 }
57 #include <poll.h>
58 #endif
59
60 #ifdef DCPOMATIC_WINDOWS
61 extern "C" {
62 #include <lwext4/file_windows.h>
63 }
64 #endif
65
66 #include <glibmm.h>
67 #include <unistd.h>
68 #include <sys/types.h>
69 #include <boost/filesystem.hpp>
70 #include <boost/algorithm/string.hpp>
71 #include <iostream>
72
73 using std::cin;
74 using std::min;
75 using std::string;
76 using std::runtime_error;
77 using std::exception;
78 using boost::optional;
79
80 #ifdef DCPOMATIC_LINUX
81 static PolkitAuthority* polkit_authority = 0;
82 #endif
83 static boost::filesystem::path dcp_path;
84 static std::string device;
85 static uint64_t const block_size = 4096;
86 static Nanomsg* nanomsg = 0;
87
88 static
89 void
90 count (boost::filesystem::path dir, uint64_t& total_bytes)
91 {
92         using namespace boost::filesystem;
93         for (directory_iterator i = directory_iterator(dir); i != directory_iterator(); ++i) {
94                 if (is_directory(*i)) {
95                         count (*i, total_bytes);
96                 } else {
97                         total_bytes += file_size (*i);
98                 }
99         }
100 }
101
102 static
103 string
104 write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total)
105 {
106         ext4_file out;
107         int r = ext4_fopen(&out, to.generic_string().c_str(), "wb");
108         if (r != EOK) {
109                 throw CopyError (String::compose("Failed to open file %1", to.generic_string()), r);
110         }
111
112         FILE* in = fopen_boost (from, "rb");
113         if (!in) {
114                 ext4_fclose (&out);
115                 throw CopyError (String::compose("Failed to open file %1", from.string()), 0);
116         }
117
118         uint8_t* buffer = new uint8_t[block_size];
119         Digester digester;
120
121         uint64_t remaining = file_size (from);
122         while (remaining > 0) {
123                 uint64_t const this_time = min(remaining, block_size);
124                 size_t read = fread (buffer, 1, this_time, in);
125                 if (read != this_time) {
126                         fclose (in);
127                         ext4_fclose (&out);
128                         delete[] buffer;
129                         throw CopyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
130                 }
131
132                 digester.add (buffer, this_time);
133
134                 size_t written;
135                 r = ext4_fwrite (&out, buffer, this_time, &written);
136                 if (r != EOK) {
137                         fclose (in);
138                         ext4_fclose (&out);
139                         delete[] buffer;
140                         throw CopyError ("Write failed", r);
141                 }
142                 if (written != this_time) {
143                         fclose (in);
144                         ext4_fclose (&out);
145                         delete[] buffer;
146                         throw CopyError (String::compose("Short write; expected %1 but wrote %2", this_time, written), 0);
147                 }
148                 remaining -= this_time;
149                 total_remaining -= this_time;
150                 nanomsg->blocking_send(String::compose(DISK_WRITER_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)));
151         }
152
153         fclose (in);
154         ext4_fclose (&out);
155         delete[] buffer;
156
157         return digester.get ();
158 }
159
160 static
161 string
162 read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total)
163 {
164         ext4_file in;
165         LOG_DISK("Opening %1 for read", to.generic_string());
166         int r = ext4_fopen(&in, to.generic_string().c_str(), "rb");
167         if (r != EOK) {
168                 throw VerifyError (String::compose("Failed to open file %1", to.generic_string()), r);
169         }
170         LOG_DISK("Opened %1 for read", to.generic_string());
171
172         uint8_t* buffer = new uint8_t[block_size];
173         Digester digester;
174
175         uint64_t remaining = file_size (from);
176         while (remaining > 0) {
177                 uint64_t const this_time = min(remaining, block_size);
178                 size_t read;
179                 r = ext4_fread (&in, buffer, this_time, &read);
180                 if (read != this_time) {
181                         ext4_fclose (&in);
182                         delete[] buffer;
183                         throw VerifyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
184                 }
185
186                 digester.add (buffer, this_time);
187                 remaining -= this_time;
188                 total_remaining -= this_time;
189                 nanomsg->blocking_send(String::compose(DISK_WRITER_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)));
190         }
191
192         ext4_fclose (&in);
193         delete[] buffer;
194
195         return digester.get ();
196 }
197
198
199 /** @param from File to copy from.
200  *  @param to Directory to copy to.
201  */
202 static
203 void
204 copy (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total)
205 {
206         LOG_DISK ("Copy %1 -> %2", from.string(), to.generic_string());
207
208         using namespace boost::filesystem;
209
210         path const cr = to / from.filename();
211
212         if (is_directory(from)) {
213                 int r = ext4_dir_mk (cr.generic_string().c_str());
214                 if (r != EOK) {
215                         throw CopyError (String::compose("Failed to create directory %1", cr.generic_string()), r);
216                 }
217
218                 for (directory_iterator i = directory_iterator(from); i != directory_iterator(); ++i) {
219                         copy (i->path(), cr, total_remaining, total);
220                 }
221         } else {
222                 string const write_digest = write (from, cr, total_remaining, total);
223                 LOG_DISK ("Wrote %1 %2 with %3", from.string(), cr.generic_string(), write_digest);
224                 string const read_digest = read (from, cr, total_remaining, total);
225                 LOG_DISK ("Read %1 %2 with %3", from.string(), cr.generic_string(), write_digest);
226                 if (write_digest != read_digest) {
227                         throw VerifyError ("Hash of written data is incorrect", 0);
228                 }
229         }
230 }
231
232 static
233 void
234 write ()
235 try
236 {
237 //      ext4_dmask_set (DEBUG_ALL);
238
239         /* We rely on static initialization for these */
240         static struct ext4_fs fs;
241         static struct ext4_mkfs_info info;
242         info.block_size = 1024;
243         info.inode_size = 128;
244         info.journal = false;
245
246 #ifdef WIN32
247         file_windows_name_set(device.c_str());
248         struct ext4_blockdev* bd = file_windows_dev_get();
249 #else
250         file_dev_name_set (device.c_str());
251         struct ext4_blockdev* bd = file_dev_get ();
252 #endif
253
254         if (!bd) {
255                 throw CopyError ("Failed to open drive", 0);
256         }
257         LOG_DISK_NC ("Opened drive");
258
259         struct ext4_mbr_parts parts;
260         parts.division[0] = 100;
261         parts.division[1] = 0;
262         parts.division[2] = 0;
263         parts.division[3] = 0;
264
265 #ifdef DCPOMATIC_LINUX
266         PrivilegeEscalator e;
267 #endif
268
269         /* XXX: not sure if disk_id matters */
270         int r = ext4_mbr_write (bd, &parts, 0);
271
272         if (r) {
273                 throw CopyError ("Failed to write MBR", r);
274         }
275         LOG_DISK_NC ("Wrote MBR");
276
277 #ifdef DCPOMATIC_WINDOWS
278         struct ext4_mbr_bdevs bdevs;
279         r = ext4_mbr_scan (bd, &bdevs);
280         if (r != EOK) {
281                 throw CopyError ("Failed to read MBR", r);
282         }
283
284         file_windows_partition_set (bdevs.partitions[0].part_offset, bdevs.partitions[0].part_size);
285 #endif
286
287 #ifdef DCPOMATIC_LINUX
288         /* Re-read the partition table */
289         int fd = open(device.c_str(), O_RDONLY);
290         ioctl(fd, BLKRRPART, NULL);
291         close(fd);
292 #endif
293
294 #ifdef DCPOMATIC_LINUX
295         string partition = device;
296         /* XXX: don't know if this logic is sensible */
297         if (partition.size() > 0 && isdigit(partition[partition.length() - 1])) {
298                 partition += "p1";
299         } else {
300                 partition += "1";
301         }
302         file_dev_name_set (partition.c_str());
303         bd = file_dev_get ();
304 #endif
305
306 #ifdef DCPOMATIC_OSX
307         string partition = device + "s1";
308         file_dev_name_set (partition.c_str());
309         bd = file_dev_get ();
310 #endif
311
312         if (!bd) {
313                 throw CopyError ("Failed to open partition", 0);
314         }
315         LOG_DISK_NC ("Opened partition");
316
317         nanomsg->blocking_send(DISK_WRITER_FORMATTING "\n");
318
319         r = ext4_mkfs(&fs, bd, &info, F_SET_EXT4);
320         if (r != EOK) {
321                 throw CopyError ("Failed to make filesystem", r);
322         }
323         LOG_DISK_NC ("Made filesystem");
324
325         r = ext4_device_register(bd, "ext4_fs");
326         if (r != EOK) {
327                 throw CopyError ("Failed to register device", r);
328         }
329         LOG_DISK_NC ("Registered device");
330
331         r = ext4_mount("ext4_fs", "/mp/", false);
332         if (r != EOK) {
333                 throw CopyError ("Failed to mount device", r);
334         }
335         LOG_DISK_NC ("Mounted device");
336
337         uint64_t total_bytes = 0;
338         count (dcp_path, total_bytes);
339
340         /* XXX: this is a hack.  We are going to "treat" every byte twice; write it, and then verify it.  Double the
341          * bytes totals so that progress works itself out (assuming write is the same speed as read).
342          */
343         total_bytes *= 2;
344         copy (dcp_path, "/mp", total_bytes, total_bytes);
345
346         r = ext4_umount("/mp/");
347         if (r != EOK) {
348                 throw CopyError ("Failed to unmount device", r);
349         }
350
351         ext4_device_unregister("ext4_fs");
352         nanomsg->blocking_send(DISK_WRITER_OK "\n");
353 } catch (CopyError& e) {
354         LOG_DISK("CopyError: %1 %2", e.message(), e.number());
355         nanomsg->blocking_send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number()));
356 } catch (VerifyError& e) {
357         LOG_DISK("VerifyError: %1 %2", e.message(), e.number());
358         nanomsg->blocking_send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number()));
359 } catch (exception& e) {
360         LOG_DISK("Exception: %1", e.what());
361         nanomsg->blocking_send(String::compose(DISK_WRITER_ERROR "\n%1\n0\n", e.what()));
362 }
363
364 #ifdef DCPOMATIC_LINUX
365 static
366 void
367 polkit_callback (GObject *, GAsyncResult* res, gpointer)
368 {
369         PolkitAuthorizationResult* result = polkit_authority_check_authorization_finish (polkit_authority, res, 0);
370         if (result && polkit_authorization_result_get_is_authorized(result)) {
371                 write ();
372         }
373         if (result) {
374                 g_object_unref (result);
375         }
376 }
377 #endif
378
379 bool
380 idle ()
381 {
382         using namespace boost::algorithm;
383
384         optional<string> s = nanomsg->nonblocking_get ();
385         if (!s) {
386                 return true;
387         }
388
389         if (*s == "Q") {
390                 exit (EXIT_SUCCESS);
391         } else if (*s == "W") {
392                 dcp_path = nanomsg->blocking_get();
393                 device = nanomsg->blocking_get();
394
395                 /* Do some basic sanity checks; this is a bit belt-and-braces but it can't hurt... */
396
397 #ifdef DCPOMATIC_OSX
398                 if (!starts_with(device, "/dev/disk")) {
399                         LOG_DISK ("Will not write to %1", device);
400                         nanomsg->blocking_send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n");
401                         return true;
402                 }
403 #endif
404 #ifdef DCPOMATIC_LINUX
405                 if (!starts_with(device, "/dev/sd") && !starts_with(device, "/dev/hd")) {
406                         LOG_DISK ("Will not write to %1", device);
407                         nanomsg->blocking_send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n");
408                         return true;
409                 }
410 #endif
411 #ifdef DCPOMATIC_WINDOWS
412                 if (!starts_with(device, "\\\\.\\PHYSICALDRIVE")) {
413                         LOG_DISK ("Will not write to %1", device);
414                         nanomsg->blocking_send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n");
415                         return true;
416                 }
417 #endif
418
419                 bool on_drive_list = false;
420                 bool mounted = false;
421                 for (auto const& i: get_drives()) {
422                         if (i.internal_name() == device) {
423                                 on_drive_list = true;
424                                 mounted = i.mounted();
425                         }
426                 }
427
428                 if (!on_drive_list) {
429                         LOG_DISK ("Will not write to %1 as it's not recognised as a drive", device);
430                         nanomsg->blocking_send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n");
431                         return true;
432                 }
433                 if (mounted) {
434                         LOG_DISK ("Will not write to %1 as it's mounted", device);
435                         nanomsg->blocking_send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n");
436                         return true;
437                 }
438
439                 LOG_DISK ("Here we go writing %1 to %2", dcp_path, device);
440
441 #ifdef DCPOMATIC_LINUX
442                 polkit_authority = polkit_authority_get_sync (0, 0);
443                 PolkitSubject* subject = polkit_unix_process_new (getppid());
444                 polkit_authority_check_authorization (
445                                 polkit_authority, subject, "com.dcpomatic.write-drive", 0, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, 0, polkit_callback, 0
446                                 );
447 #else
448                 write ();
449 #endif
450         }
451
452         return true;
453 }
454
455 int
456 main ()
457 {
458         /* XXX: this is a hack, but I expect we'll need logs and I'm not sure if there's
459          * a better place to put them.
460          */
461         dcpomatic_log.reset(new FileLog(config_path() / "disk_writer.log", LogEntry::TYPE_DISK));
462         LOG_DISK_NC("dcpomatic_disk_writer started");
463
464         try {
465                 nanomsg = new Nanomsg (false);
466         } catch (runtime_error& e) {
467                 LOG_DISK_NC("Could not set up nanomsg socket");
468                 exit (EXIT_FAILURE);
469         }
470
471         Glib::RefPtr<Glib::MainLoop> ml = Glib::MainLoop::create ();
472         Glib::signal_timeout().connect(sigc::ptr_fun(&idle), 500);
473         ml->run ();
474 }