Updated cs_CZ translation from Tomáš Begeni.
[dcpomatic.git] / src / lib / ext.cc
index 5e2ff7d9cbd93a4463f4c5f05ae661a7f955b7b6..2ef3df6bb932f53a6a2b09ffb88e1d0135d9f956 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);
+                       }
                }
        }
 }
@@ -111,13 +113,13 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
        ext4_file out;
        int r = ext4_fopen(&out, to.generic_string().c_str(), "wb");
        if (r != EOK) {
-               throw CopyError (String::compose("Failed to open file %1", to.generic_string()), r);
+               throw CopyError(String::compose("Failed to open file %1", to.generic_string()), r, ext4_blockdev_errno);
        }
 
        dcp::File in(from, "rb");
        if (!in) {
                ext4_fclose (&out);
-               throw CopyError (String::compose("Failed to open file %1", from.string()), 0);
+               throw CopyError(String::compose("Failed to open file %1", from.string()), 0);
        }
 
        std::vector<uint8_t> buffer(block_size);
@@ -131,7 +133,7 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
                size_t read = in.read(buffer.data(), 1, this_time);
                if (read != this_time) {
                        ext4_fclose (&out);
-                       throw CopyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
+                       throw CopyError(String::compose("Short read; expected %1 but read %2", this_time, read), 0, ext4_blockdev_errno);
                }
 
                digester.add (buffer.data(), this_time);
@@ -140,18 +142,18 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
                r = ext4_fwrite (&out, buffer.data(), this_time, &written);
                if (r != EOK) {
                        ext4_fclose (&out);
-                       throw CopyError ("Write failed", r);
+                       throw CopyError("Write failed", r, ext4_blockdev_errno);
                }
                if (written != this_time) {
                        ext4_fclose (&out);
-                       throw CopyError (String::compose("Short write; expected %1 but wrote %2", this_time, written), 0);
+                       throw CopyError(String::compose("Short write; expected %1 but wrote %2", this_time, written), 0, ext4_blockdev_errno);
                }
                remaining -= this_time;
                total_remaining -= this_time;
 
                ++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);
                }
        }
 
@@ -235,7 +237,7 @@ copy (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_
        if (is_directory(from)) {
                int r = ext4_dir_mk (cr.generic_string().c_str());
                if (r != EOK) {
-                       throw CopyError (String::compose("Failed to create directory %1", cr.generic_string()), r);
+                       throw CopyError(String::compose("Failed to create directory %1", cr.generic_string()), r, ext4_blockdev_errno);
                }
                set_timestamps_to_now (cr);
 
@@ -270,16 +272,16 @@ 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
 {
@@ -315,7 +317,7 @@ try
 #endif
 
        if (!bd) {
-               throw CopyError ("Failed to open drive", 0);
+               throw CopyError("Failed to open drive", 0, ext4_blockdev_errno);
        }
        LOG_DISK_NC ("Opened drive");
 
@@ -328,14 +330,14 @@ try
        /* XXX: not sure if disk_id matters */
        int r = ext4_mbr_write (bd, &parts, 0);
        if (r) {
-               throw CopyError ("Failed to write MBR", r);
+               throw CopyError("Failed to write MBR", r, ext4_blockdev_errno);
        }
        LOG_DISK_NC ("Wrote MBR");
 
        struct ext4_mbr_bdevs bdevs;
        r = ext4_mbr_scan (bd, &bdevs);
        if (r != EOK) {
-               throw CopyError ("Failed to read MBR", r);
+               throw CopyError("Failed to read MBR", r, ext4_blockdev_errno);
        }
 
 #ifdef DCPOMATIC_LINUX
@@ -367,43 +369,45 @@ try
 #endif
 
        if (!bd) {
-               throw CopyError ("Failed to open partition", 0);
+               throw CopyError("Failed to open partition", 0, ext4_blockdev_errno);
        }
        LOG_DISK_NC ("Opened partition");
 
        r = ext4_mkfs(&fs, bd, &info, F_SET_EXT2, format_progress, nanomsg);
        if (r != EOK) {
-               throw CopyError ("Failed to make filesystem", r);
+               throw CopyError("Failed to make filesystem", r, ext4_blockdev_errno);
        }
        LOG_DISK_NC ("Made filesystem");
 
        r = ext4_device_register(bd, "ext4_fs");
        if (r != EOK) {
-               throw CopyError ("Failed to register device", r);
+               throw CopyError("Failed to register device", r, ext4_blockdev_errno);
        }
        LOG_DISK_NC ("Registered device");
 
        r = ext4_mount("ext4_fs", "/mp/", false);
        if (r != EOK) {
-               throw CopyError ("Failed to mount device", r);
+               throw CopyError("Failed to mount device", r, ext4_blockdev_errno);
        }
        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/");
        if (r != EOK) {
-               throw CopyError ("Failed to unmount device", r);
+               throw CopyError("Failed to unmount device", r, ext4_blockdev_errno);
        }
        r = ext4_mount("ext4_fs", "/mp/", false);
        if (r != EOK) {
-               throw CopyError ("Failed to mount device", r);
+               throw CopyError("Failed to mount device", r, ext4_blockdev_errno);
        }
        LOG_DISK_NC ("Re-mounted device");
 
@@ -411,29 +415,29 @@ try
 
        r = ext4_umount("/mp/");
        if (r != EOK) {
-               throw CopyError ("Failed to unmount device", r);
+               throw CopyError("Failed to unmount device", r, ext4_blockdev_errno);
        }
 
        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 ();
        }
 
        disk_write_finished ();
 } catch (CopyError& e) {
-       LOG_DISK("CopyError (from write): %1 %2", e.message(), e.number().get_value_or(0));
+       LOG_DISK("CopyError (from write): %1 %2 %3", e.message(), e.ext4_number().get_value_or(0), e.platform_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.ext4_number().get_value_or(0), e.platform_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(), 0).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, 0).write_to_nanomsg(*nanomsg, LONG_TIMEOUT);
        }
 }