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"
31 #ifdef DCPOMATIC_LINUX
33 #include <sys/ioctl.h>
35 #include <lwext4/file_dev.h>
41 #include <lwext4/file_dev.h>
45 #ifdef DCPOMATIC_WINDOWS
47 #include <lwext4/file_windows.h>
52 #include <lwext4/ext4.h>
53 #include <lwext4/ext4_debug.h>
54 #include <lwext4/ext4_errno.h>
55 #include <lwext4/ext4_fs.h>
56 #include <lwext4/ext4_mbr.h>
57 #include <lwext4/ext4_mkfs.h>
59 #include <boost/filesystem.hpp>
70 #define SHORT_TIMEOUT 100
71 #define LONG_TIMEOUT 2000
74 /* Use quite a big block size here, as ext4's fwrite() has quite a bit of overhead */
75 static uint64_t const block_size = 4096 * 4096;
80 count (boost::filesystem::path dir, uint64_t& total_bytes)
82 dir = dcp::fix_long_path (dir);
84 using namespace boost::filesystem;
85 for (auto i: directory_iterator(dir)) {
86 if (is_directory(i)) {
87 count (i, total_bytes);
89 total_bytes += file_size (i);
97 set_timestamps_to_now (boost::filesystem::path path)
99 auto const now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
100 ext4_mtime_set (path.generic_string().c_str(), now);
101 ext4_ctime_set (path.generic_string().c_str(), now);
102 ext4_atime_set (path.generic_string().c_str(), now);
108 write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, Nanomsg* nanomsg)
111 int r = ext4_fopen(&out, to.generic_string().c_str(), "wb");
113 throw CopyError (String::compose("Failed to open file %1", to.generic_string()), r);
116 dcp::File in(from, "rb");
119 throw CopyError (String::compose("Failed to open file %1", from.string()), 0);
122 std::vector<uint8_t> buffer(block_size);
125 int progress_frequency = 1;
126 int progress_count = 0;
127 uint64_t remaining = file_size (from);
128 while (remaining > 0) {
129 uint64_t const this_time = min(remaining, block_size);
130 size_t read = in.read(buffer.data(), 1, this_time);
131 if (read != this_time) {
133 throw CopyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
136 digester.add (buffer.data(), this_time);
139 r = ext4_fwrite (&out, buffer.data(), this_time, &written);
142 throw CopyError ("Write failed", r);
144 if (written != this_time) {
146 throw CopyError (String::compose("Short write; expected %1 but wrote %2", this_time, written), 0);
148 remaining -= this_time;
149 total_remaining -= this_time;
152 if ((progress_count % progress_frequency) == 0 && nanomsg) {
153 nanomsg->send(String::compose(DISK_WRITER_COPY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT);
159 set_timestamps_to_now (to);
161 return digester.get ();
167 read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, Nanomsg* nanomsg)
170 LOG_DISK("Opening %1 for read", to.generic_string());
171 int r = ext4_fopen(&in, to.generic_string().c_str(), "rb");
173 throw VerifyError (String::compose("Failed to open file %1", to.generic_string()), r);
175 LOG_DISK("Opened %1 for read", to.generic_string());
177 std::vector<uint8_t> buffer(block_size);
180 uint64_t remaining = file_size (from);
181 while (remaining > 0) {
182 uint64_t const this_time = min(remaining, block_size);
184 r = ext4_fread (&in, buffer.data(), this_time, &read);
185 if (read != this_time) {
187 throw VerifyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
190 digester.add (buffer.data(), this_time);
191 remaining -= this_time;
192 total_remaining -= this_time;
194 nanomsg->send(String::compose(DISK_WRITER_VERIFY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT);
200 return digester.get ();
207 CopiedFile (boost::filesystem::path from_, boost::filesystem::path to_, string write_digest_)
210 , write_digest (write_digest_)
213 boost::filesystem::path from;
214 boost::filesystem::path to;
215 /** digest calculated from data as it was read from the source during write */
220 /** @param from File to copy from.
221 * @param to Directory to copy to.
225 copy (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, vector<CopiedFile>& copied_files, Nanomsg* nanomsg)
227 LOG_DISK ("Copy %1 -> %2", from.string(), to.generic_string());
228 from = dcp::fix_long_path (from);
230 using namespace boost::filesystem;
232 path const cr = to / from.filename();
234 if (is_directory(from)) {
235 int r = ext4_dir_mk (cr.generic_string().c_str());
237 throw CopyError (String::compose("Failed to create directory %1", cr.generic_string()), r);
239 set_timestamps_to_now (cr);
241 for (auto i: directory_iterator(from)) {
242 copy (i.path(), cr, total_remaining, total, copied_files, nanomsg);
245 string const write_digest = write (from, cr, total_remaining, total, nanomsg);
246 LOG_DISK ("Wrote %1 %2 with %3", from.string(), cr.generic_string(), write_digest);
247 copied_files.push_back (CopiedFile(from, cr, write_digest));
254 verify (vector<CopiedFile> const& copied_files, uint64_t total, Nanomsg* nanomsg)
256 uint64_t total_remaining = total;
257 for (auto const& i: copied_files) {
258 string const read_digest = read (i.from, i.to, total_remaining, total, nanomsg);
259 LOG_DISK ("Read %1 %2 was %3 on write, now %4", i.from.string(), i.to.generic_string(), i.write_digest, read_digest);
260 if (read_digest != i.write_digest) {
261 throw VerifyError ("Hash of written data is incorrect", 0);
269 format_progress (void* context, float progress)
272 reinterpret_cast<Nanomsg*>(context)->send(String::compose(DISK_WRITER_FORMAT_PROGRESS "\n%1\n", progress), SHORT_TIMEOUT);
278 #ifdef DCPOMATIC_WINDOWS
279 dcpomatic::write (boost::filesystem::path dcp_path, string device, string, Nanomsg* nanomsg)
281 dcpomatic::write (boost::filesystem::path dcp_path, string device, string posix_partition, Nanomsg* nanomsg)
285 ext4_dmask_set (DEBUG_ALL);
287 /* We rely on static initialization for these */
288 static struct ext4_fs fs;
289 static struct ext4_mkfs_info info;
290 info.block_size = 4096;
291 info.inode_size = 128;
292 info.journal = false;
295 file_windows_name_set(device.c_str());
296 struct ext4_blockdev* bd = file_windows_dev_get();
298 file_dev_name_set (device.c_str());
299 struct ext4_blockdev* bd = file_dev_get ();
303 throw CopyError ("Failed to open drive", 0);
305 LOG_DISK_NC ("Opened drive");
307 struct ext4_mbr_parts parts;
308 parts.division[0] = 100;
309 parts.division[1] = 0;
310 parts.division[2] = 0;
311 parts.division[3] = 0;
313 /* XXX: not sure if disk_id matters */
314 int r = ext4_mbr_write (bd, &parts, 0);
316 throw CopyError ("Failed to write MBR", r);
318 LOG_DISK_NC ("Wrote MBR");
320 struct ext4_mbr_bdevs bdevs;
321 r = ext4_mbr_scan (bd, &bdevs);
323 throw CopyError ("Failed to read MBR", r);
326 #ifdef DCPOMATIC_LINUX
327 /* Re-read the partition table */
328 int fd = open(device.c_str(), O_RDONLY);
329 ioctl(fd, BLKRRPART, NULL);
333 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);
335 #ifdef DCPOMATIC_WINDOWS
336 file_windows_partition_set (bdevs.partitions[0].part_offset, bdevs.partitions[0].part_size);
338 file_dev_name_set (posix_partition.c_str());
340 /* On macOS (at least) if you try to write to a drive that is sleeping the ext4_mkfs call
341 * below is liable to return EIO because it can't open the device. Try to work around that
342 * here by opening and closing the device, waiting 5 seconds if it fails.
344 int wake = open(posix_partition.c_str(), O_RDWR);
346 dcpomatic_sleep_seconds (5);
351 bd = file_dev_get ();
355 throw CopyError ("Failed to open partition", 0);
357 LOG_DISK_NC ("Opened partition");
359 r = ext4_mkfs(&fs, bd, &info, F_SET_EXT2, format_progress, nanomsg);
361 throw CopyError ("Failed to make filesystem", r);
363 LOG_DISK_NC ("Made filesystem");
365 r = ext4_device_register(bd, "ext4_fs");
367 throw CopyError ("Failed to register device", r);
369 LOG_DISK_NC ("Registered device");
371 r = ext4_mount("ext4_fs", "/mp/", false);
373 throw CopyError ("Failed to mount device", r);
375 LOG_DISK_NC ("Mounted device");
377 uint64_t total_bytes = 0;
378 count (dcp_path, total_bytes);
380 uint64_t total_remaining = total_bytes;
381 vector<CopiedFile> copied_files;
382 copy (dcp_path, "/mp", total_remaining, total_bytes, copied_files, nanomsg);
384 /* Unmount and re-mount to make sure the write has finished */
385 r = ext4_umount("/mp/");
387 throw CopyError ("Failed to unmount device", r);
389 r = ext4_mount("ext4_fs", "/mp/", false);
391 throw CopyError ("Failed to mount device", r);
393 LOG_DISK_NC ("Re-mounted device");
395 verify (copied_files, total_bytes, nanomsg);
397 r = ext4_umount("/mp/");
399 throw CopyError ("Failed to unmount device", r);
402 ext4_device_unregister("ext4_fs");
403 if (nanomsg && !nanomsg->send(DISK_WRITER_OK "\n", LONG_TIMEOUT)) {
404 throw CommunicationFailedError ();
407 disk_write_finished ();
408 } catch (CopyError& e) {
409 LOG_DISK("CopyError (from write): %1 %2", e.message(), e.number().get_value_or(0));
411 nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number().get_value_or(0)), LONG_TIMEOUT);
413 } catch (VerifyError& e) {
414 LOG_DISK("VerifyError (from write): %1 %2", e.message(), e.number());
416 nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number()), LONG_TIMEOUT);
418 } catch (exception& e) {
419 LOG_DISK("Exception (from write): %1", e.what());
421 nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n0\n", e.what()), LONG_TIMEOUT);