2 Copyright (C) 2019-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "compose.hpp"
24 #include "dcpomatic_log.h"
26 #include "disk_writer_messages.h"
27 #include "exceptions.h"
32 #ifdef DCPOMATIC_LINUX
34 #include <sys/ioctl.h>
36 #include <lwext4/file_dev.h>
42 #include <lwext4/file_dev.h>
46 #ifdef DCPOMATIC_WINDOWS
48 #include <lwext4/file_windows.h>
53 #include <lwext4/ext4.h>
54 #include <lwext4/ext4_debug.h>
55 #include <lwext4/ext4_errno.h>
56 #include <lwext4/ext4_fs.h>
57 #include <lwext4/ext4_mbr.h>
58 #include <lwext4/ext4_mkfs.h>
60 #include <boost/filesystem.hpp>
71 #define SHORT_TIMEOUT 100
72 #define LONG_TIMEOUT 2000
75 /* Use quite a big block size here, as ext4's fwrite() has quite a bit of overhead */
76 static uint64_t const block_size = 4096 * 4096;
81 count (boost::filesystem::path dir, uint64_t& total_bytes)
83 dir = dcp::fix_long_path (dir);
85 using namespace boost::filesystem;
86 for (auto i: directory_iterator(dir)) {
87 if (is_directory(i)) {
88 count (i, total_bytes);
90 total_bytes += file_size (i);
98 set_timestamps_to_now (boost::filesystem::path path)
100 auto const now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
101 ext4_mtime_set (path.generic_string().c_str(), now);
102 ext4_ctime_set (path.generic_string().c_str(), now);
103 ext4_atime_set (path.generic_string().c_str(), now);
109 write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, Nanomsg* nanomsg)
112 int r = ext4_fopen(&out, to.generic_string().c_str(), "wb");
114 throw CopyError (String::compose("Failed to open file %1", to.generic_string()), r);
117 dcp::File in(from, "rb");
120 throw CopyError (String::compose("Failed to open file %1", from.string()), 0);
123 std::vector<uint8_t> buffer(block_size);
126 int progress_frequency = 1;
127 int progress_count = 0;
128 uint64_t remaining = file_size (from);
129 while (remaining > 0) {
130 uint64_t const this_time = min(remaining, block_size);
131 size_t read = in.read(buffer.data(), 1, this_time);
132 if (read != this_time) {
134 throw CopyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
137 digester.add (buffer.data(), this_time);
140 r = ext4_fwrite (&out, buffer.data(), this_time, &written);
143 throw CopyError ("Write failed", r);
145 if (written != this_time) {
147 throw CopyError (String::compose("Short write; expected %1 but wrote %2", this_time, written), 0);
149 remaining -= this_time;
150 total_remaining -= this_time;
153 if ((progress_count % progress_frequency) == 0 && nanomsg) {
154 nanomsg->send(String::compose(DISK_WRITER_COPY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT);
160 set_timestamps_to_now (to);
162 return digester.get ();
168 read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, Nanomsg* nanomsg)
171 LOG_DISK("Opening %1 for read", to.generic_string());
172 int r = ext4_fopen(&in, to.generic_string().c_str(), "rb");
174 throw VerifyError (String::compose("Failed to open file %1", to.generic_string()), r);
176 LOG_DISK("Opened %1 for read", to.generic_string());
178 std::vector<uint8_t> buffer(block_size);
181 uint64_t remaining = file_size (from);
182 while (remaining > 0) {
183 uint64_t const this_time = min(remaining, block_size);
185 r = ext4_fread (&in, buffer.data(), this_time, &read);
186 if (read != this_time) {
188 throw VerifyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
191 digester.add (buffer.data(), this_time);
192 remaining -= this_time;
193 total_remaining -= this_time;
195 nanomsg->send(String::compose(DISK_WRITER_VERIFY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT);
201 return digester.get ();
208 CopiedFile (boost::filesystem::path from_, boost::filesystem::path to_, string write_digest_)
211 , write_digest (write_digest_)
214 boost::filesystem::path from;
215 boost::filesystem::path to;
216 /** digest calculated from data as it was read from the source during write */
221 /** @param from File to copy from.
222 * @param to Directory to copy to.
226 copy (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, vector<CopiedFile>& copied_files, Nanomsg* nanomsg)
228 LOG_DISK ("Copy %1 -> %2", from.string(), to.generic_string());
229 from = dcp::fix_long_path (from);
231 using namespace boost::filesystem;
233 path const cr = to / from.filename();
235 if (is_directory(from)) {
236 int r = ext4_dir_mk (cr.generic_string().c_str());
238 throw CopyError (String::compose("Failed to create directory %1", cr.generic_string()), r);
240 set_timestamps_to_now (cr);
242 for (auto i: directory_iterator(from)) {
243 copy (i.path(), cr, total_remaining, total, copied_files, nanomsg);
246 string const write_digest = write (from, cr, total_remaining, total, nanomsg);
247 LOG_DISK ("Wrote %1 %2 with %3", from.string(), cr.generic_string(), write_digest);
248 copied_files.push_back (CopiedFile(from, cr, write_digest));
255 verify (vector<CopiedFile> const& copied_files, uint64_t total, Nanomsg* nanomsg)
257 uint64_t total_remaining = total;
258 for (auto const& i: copied_files) {
259 string const read_digest = read (i.from, i.to, total_remaining, total, nanomsg);
260 LOG_DISK ("Read %1 %2 was %3 on write, now %4", i.from.string(), i.to.generic_string(), i.write_digest, read_digest);
261 if (read_digest != i.write_digest) {
262 throw VerifyError ("Hash of written data is incorrect", 0);
270 format_progress (void* context, float progress)
273 reinterpret_cast<Nanomsg*>(context)->send(String::compose(DISK_WRITER_FORMAT_PROGRESS "\n%1\n", progress), SHORT_TIMEOUT);
279 #ifdef DCPOMATIC_WINDOWS
280 dcpomatic::write (boost::filesystem::path dcp_path, string device, string, Nanomsg* nanomsg)
282 dcpomatic::write (boost::filesystem::path dcp_path, string device, string posix_partition, Nanomsg* nanomsg)
286 ext4_dmask_set (DEBUG_ALL);
288 /* We rely on static initialization for these */
289 static struct ext4_fs fs;
290 static struct ext4_mkfs_info info;
291 info.block_size = 4096;
292 info.inode_size = 128;
293 info.journal = false;
296 file_windows_name_set(device.c_str());
297 struct ext4_blockdev* bd = file_windows_dev_get();
299 file_dev_name_set (device.c_str());
300 struct ext4_blockdev* bd = file_dev_get ();
304 throw CopyError ("Failed to open drive", 0);
306 LOG_DISK_NC ("Opened drive");
308 struct ext4_mbr_parts parts;
309 parts.division[0] = 100;
310 parts.division[1] = 0;
311 parts.division[2] = 0;
312 parts.division[3] = 0;
314 /* XXX: not sure if disk_id matters */
315 int r = ext4_mbr_write (bd, &parts, 0);
317 throw CopyError ("Failed to write MBR", r);
319 LOG_DISK_NC ("Wrote MBR");
321 struct ext4_mbr_bdevs bdevs;
322 r = ext4_mbr_scan (bd, &bdevs);
324 throw CopyError ("Failed to read MBR", r);
327 #ifdef DCPOMATIC_LINUX
328 /* Re-read the partition table */
329 int fd = open(device.c_str(), O_RDONLY);
330 ioctl(fd, BLKRRPART, NULL);
334 LOG_DISK ("Writing to partition at %1 size %2; bd part size is %3", bdevs.partitions[0].part_offset, bdevs.partitions[0].part_size, bd->part_size);
336 #ifdef DCPOMATIC_WINDOWS
337 file_windows_partition_set (bdevs.partitions[0].part_offset, bdevs.partitions[0].part_size);
339 file_dev_name_set (posix_partition.c_str());
341 /* On macOS (at least) if you try to write to a drive that is sleeping the ext4_mkfs call
342 * below is liable to return EIO because it can't open the device. Try to work around that
343 * here by opening and closing the device, waiting 5 seconds if it fails.
345 int wake = open(posix_partition.c_str(), O_RDWR);
347 dcpomatic_sleep_seconds (5);
352 bd = file_dev_get ();
356 throw CopyError ("Failed to open partition", 0);
358 LOG_DISK_NC ("Opened partition");
360 r = ext4_mkfs(&fs, bd, &info, F_SET_EXT2, format_progress, nanomsg);
362 throw CopyError ("Failed to make filesystem", r);
364 LOG_DISK_NC ("Made filesystem");
366 r = ext4_device_register(bd, "ext4_fs");
368 throw CopyError ("Failed to register device", r);
370 LOG_DISK_NC ("Registered device");
372 r = ext4_mount("ext4_fs", "/mp/", false);
374 throw CopyError ("Failed to mount device", r);
376 LOG_DISK_NC ("Mounted device");
378 uint64_t total_bytes = 0;
379 count (dcp_path, total_bytes);
381 uint64_t total_remaining = total_bytes;
382 vector<CopiedFile> copied_files;
383 copy (dcp_path, "/mp", total_remaining, total_bytes, copied_files, nanomsg);
385 /* Unmount and re-mount to make sure the write has finished */
386 r = ext4_umount("/mp/");
388 throw CopyError ("Failed to unmount device", r);
390 r = ext4_mount("ext4_fs", "/mp/", false);
392 throw CopyError ("Failed to mount device", r);
394 LOG_DISK_NC ("Re-mounted device");
396 verify (copied_files, total_bytes, nanomsg);
398 r = ext4_umount("/mp/");
400 throw CopyError ("Failed to unmount device", r);
403 ext4_device_unregister("ext4_fs");
404 if (nanomsg && !nanomsg->send(DISK_WRITER_OK "\n", LONG_TIMEOUT)) {
405 throw CommunicationFailedError ();
408 disk_write_finished ();
409 } catch (CopyError& e) {
410 LOG_DISK("CopyError (from write): %1 %2", e.message(), e.number().get_value_or(0));
412 nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number().get_value_or(0)), LONG_TIMEOUT);
414 } catch (VerifyError& e) {
415 LOG_DISK("VerifyError (from write): %1 %2", e.message(), e.number());
417 nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number()), LONG_TIMEOUT);
419 } catch (exception& e) {
420 LOG_DISK("Exception (from write): %1", e.what());
422 nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n0\n", e.what()), LONG_TIMEOUT);