Add DiskWriterBackendResponse::write_to_nanomsg() and use it
[dcpomatic.git] / src / lib / ext.cc
index 96753eca4927bc78a4d19fe7f6fc729e1a06f9af..29f44d90e8296cb7af98cbea7a1d97552901859e 100644 (file)
@@ -78,16 +78,18 @@ uint64_t constexpr block_size = 4096 * 4096;
 
 static
 void
-count (boost::filesystem::path dir, uint64_t& total_bytes)
+count (std::vector<boost::filesystem::path> dirs, uint64_t& total_bytes)
 {
-       dir = dcp::fix_long_path (dir);
-
        using namespace boost::filesystem;
-       for (auto i: directory_iterator(dir)) {
-               if (is_directory(i)) {
-                       count (i, total_bytes);
-               } else {
-                       total_bytes += file_size (i);
+
+       for (auto dir: dirs) {
+               dir = dcp::fix_long_path(dir);
+               for (auto path: directory_iterator(dir)) {
+                       if (is_directory(path)) {
+                               count({path}, total_bytes);
+                       } else {
+                               total_bytes += file_size(path);
+                       }
                }
        }
 }
@@ -151,7 +153,7 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
 
                ++progress_count;
                if ((progress_count % progress_frequency) == 0 && nanomsg) {
-                       nanomsg->send(String::compose(DISK_WRITER_COPY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT);
+                       DiskWriterBackEndResponse::copy_progress(1 - float(total_remaining) / total).write_to_nanomsg(*nanomsg, SHORT_TIMEOUT);
                }
        }
 
@@ -192,7 +194,7 @@ read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_
                remaining -= this_time;
                total_remaining -= this_time;
                if (nanomsg) {
-                       nanomsg->send(String::compose(DISK_WRITER_VERIFY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT);
+                       DiskWriterBackEndResponse::verify_progress(1 - float(total_remaining) / total).write_to_nanomsg(*nanomsg, SHORT_TIMEOUT);
                }
        }
 
@@ -270,27 +272,41 @@ void
 format_progress (void* context, float progress)
 {
        if (context) {
-               reinterpret_cast<Nanomsg*>(context)->send(String::compose(DISK_WRITER_FORMAT_PROGRESS "\n%1\n", progress), SHORT_TIMEOUT);
+               DiskWriterBackEndResponse::format_progress(progress).write_to_nanomsg(*reinterpret_cast<Nanomsg*>(context), SHORT_TIMEOUT);
        }
 }
 
 
 void
 #ifdef DCPOMATIC_WINDOWS
-dcpomatic::write (boost::filesystem::path dcp_path, string device, string, Nanomsg* nanomsg)
+dcpomatic::write (vector<boost::filesystem::path> dcp_paths, string device, string, Nanomsg* nanomsg)
 #else
-dcpomatic::write (boost::filesystem::path dcp_path, string device, string posix_partition, Nanomsg* nanomsg)
+dcpomatic::write (vector<boost::filesystem::path> dcp_paths, string device, string posix_partition, Nanomsg* nanomsg)
 #endif
 try
 {
        ext4_dmask_set (DEBUG_ALL);
 
-       /* We rely on static initialization for these */
-       static struct ext4_fs fs;
-       static struct ext4_mkfs_info info;
+       struct ext4_fs fs;
+       fs.read_only = false;
+       fs.bdev = nullptr;
+       fs.last_inode_bg_id = 0;
+       fs.jbd_fs = nullptr;
+       fs.jbd_journal = nullptr;
+       fs.curr_trans = nullptr;
+       struct ext4_mkfs_info info;
+       info.len = 0;
        info.block_size = 4096;
+       info.blocks_per_group = 0;
        info.inode_size = 128;
+       info.inodes = 0;
+       info.journal_blocks = 0;
+       info.dsc_size = 0;
+       for (int i = 0; i < UUID_SIZE; ++i) {
+               info.uuid[i] = rand() & 0xff;
+       }
        info.journal = false;
+       info.label = nullptr;
 
 #ifdef WIN32
        file_windows_name_set(device.c_str());
@@ -376,11 +392,13 @@ try
        LOG_DISK_NC ("Mounted device");
 
        uint64_t total_bytes = 0;
-       count (dcp_path, total_bytes);
+       count (dcp_paths, total_bytes);
 
        uint64_t total_remaining = total_bytes;
        vector<CopiedFile> copied_files;
-       copy (dcp_path, "/mp", total_remaining, total_bytes, copied_files, nanomsg);
+       for (auto dcp_path: dcp_paths) {
+               copy (dcp_path, "/mp", total_remaining, total_bytes, copied_files, nanomsg);
+       }
 
        /* Unmount and re-mount to make sure the write has finished */
        r = ext4_umount("/mp/");
@@ -401,7 +419,7 @@ try
        }
 
        ext4_device_unregister("ext4_fs");
-       if (nanomsg && !nanomsg->send(DISK_WRITER_OK "\n", LONG_TIMEOUT)) {
+       if (nanomsg && !DiskWriterBackEndResponse::ok().write_to_nanomsg(*nanomsg, LONG_TIMEOUT)) {
                throw CommunicationFailedError ();
        }
 
@@ -409,17 +427,17 @@ try
 } catch (CopyError& e) {
        LOG_DISK("CopyError (from write): %1 %2", e.message(), e.number().get_value_or(0));
        if (nanomsg) {
-               nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number().get_value_or(0)), LONG_TIMEOUT);
+               DiskWriterBackEndResponse::error(e.message(), e.number().get_value_or(0)).write_to_nanomsg(*nanomsg, LONG_TIMEOUT);
        }
 } catch (VerifyError& e) {
        LOG_DISK("VerifyError (from write): %1 %2", e.message(), e.number());
        if (nanomsg) {
-               nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number()), LONG_TIMEOUT);
+               DiskWriterBackEndResponse::error(e.message(), e.number()).write_to_nanomsg(*nanomsg, LONG_TIMEOUT);
        }
 } catch (exception& e) {
        LOG_DISK("Exception (from write): %1", e.what());
        if (nanomsg) {
-               nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n0\n", e.what()), LONG_TIMEOUT);
+               DiskWriterBackEndResponse::error(e.what(), 0).write_to_nanomsg(*nanomsg, LONG_TIMEOUT);
        }
 }