Set up {m,c,a}times on copied files (#2145).
[dcpomatic.git] / src / lib / ext.cc
index 086afd91e60288101a030f218fd08a644b0dfa9a..ec34f147216d97af091fdaaeec060c9620403fef 100644 (file)
@@ -57,6 +57,7 @@ extern "C" {
 #include <lwext4/ext4_mkfs.h>
 }
 #include <boost/filesystem.hpp>
+#include <chrono>
 #include <string>
 
 
@@ -78,16 +79,30 @@ static
 void
 count (boost::filesystem::path dir, uint64_t& total_bytes)
 {
+       dir = fix_long_path (dir);
+
        using namespace boost::filesystem;
-       for (directory_iterator i = directory_iterator(dir); i != directory_iterator(); ++i) {
-               if (is_directory(*i)) {
-                       count (*i, total_bytes);
+       for (auto i: directory_iterator(dir)) {
+               if (is_directory(i)) {
+                       count (i, total_bytes);
                } else {
-                       total_bytes += file_size (*i);
+                       total_bytes += file_size (i);
                }
        }
 }
 
+
+static
+void
+set_timestamps_to_now (boost::filesystem::path path)
+{
+       auto const now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+       ext4_mtime_set (path.generic_string().c_str(), now);
+       ext4_ctime_set (path.generic_string().c_str(), now);
+       ext4_atime_set (path.generic_string().c_str(), now);
+}
+
+
 static
 string
 write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, Nanomsg* nanomsg)
@@ -149,6 +164,8 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
        ext4_fclose (&out);
        delete[] buffer;
 
+       set_timestamps_to_now (to);
+
        return digester.get ();
 }
 
@@ -218,6 +235,7 @@ void
 copy (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_remaining, uint64_t total, vector<CopiedFile>& copied_files, Nanomsg* nanomsg)
 {
        LOG_DISK ("Copy %1 -> %2", from.string(), to.generic_string());
+       from = fix_long_path (from);
 
        using namespace boost::filesystem;
 
@@ -228,6 +246,7 @@ copy (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_
                if (r != EOK) {
                        throw CopyError (String::compose("Failed to create directory %1", cr.generic_string()), r);
                }
+               set_timestamps_to_now (cr);
 
                for (directory_iterator i = directory_iterator(from); i != directory_iterator(); ++i) {
                        copy (i->path(), cr, total_remaining, total, copied_files, nanomsg);
@@ -255,6 +274,16 @@ verify (vector<CopiedFile> const& copied_files, uint64_t total, Nanomsg* nanomsg
 }
 
 
+static
+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);
+       }
+}
+
+
 void
 #ifdef DCPOMATIC_WINDOWS
 dcpomatic::write (boost::filesystem::path dcp_path, string device, string, Nanomsg* nanomsg)
@@ -291,10 +320,6 @@ try
        parts.division[2] = 0;
        parts.division[3] = 0;
 
-#ifdef DCPOMATIC_LINUX
-       PrivilegeEscalator e;
-#endif
-
        /* XXX: not sure if disk_id matters */
        int r = ext4_mbr_write (bd, &parts, 0);
        if (r) {
@@ -321,6 +346,18 @@ try
        file_windows_partition_set (bdevs.partitions[0].part_offset, bdevs.partitions[0].part_size);
 #else
        file_dev_name_set (posix_partition.c_str());
+
+       /* On macOS (at least) if you try to write to a drive that is sleeping the ext4_mkfs call
+        * below is liable to return EIO because it can't open the device.  Try to work around that
+        * here by opening and closing the device, waiting 5 seconds if it fails.
+        */
+       int wake = open(posix_partition.c_str(), O_RDWR);
+       if (wake == -1) {
+               dcpomatic_sleep_seconds (5);
+       } else {
+               close(wake);
+       }
+
        bd = file_dev_get ();
 #endif
 
@@ -329,11 +366,7 @@ try
        }
        LOG_DISK_NC ("Opened partition");
 
-       if (nanomsg) {
-               nanomsg->send(DISK_WRITER_FORMATTING "\n", SHORT_TIMEOUT);
-       }
-
-       r = ext4_mkfs(&fs, bd, &info, F_SET_EXT2);
+       r = ext4_mkfs(&fs, bd, &info, F_SET_EXT2, format_progress, nanomsg);
        if (r != EOK) {
                throw CopyError ("Failed to make filesystem", r);
        }