Stop using static initialisation so that dcpomatic::write() can be called more than...
[dcpomatic.git] / src / lib / ext.cc
index 1af6c137bf3f2945d22a0655579dec340a564eaa..5e2ff7d9cbd93a4463f4c5f05ae661a7f955b7b6 100644 (file)
@@ -27,6 +27,7 @@
 #include "exceptions.h"
 #include "ext.h"
 #include "nanomsg.h"
+#include <dcp/file.h>
 
 #ifdef DCPOMATIC_LINUX
 #include <linux/fs.h>
@@ -72,14 +73,14 @@ using std::vector;
 
 
 /* Use quite a big block size here, as ext4's fwrite() has quite a bit of overhead */
-static uint64_t const block_size = 4096 * 4096;
+uint64_t constexpr block_size = 4096 * 4096;
 
 
 static
 void
 count (boost::filesystem::path dir, uint64_t& total_bytes)
 {
-       dir = fix_long_path (dir);
+       dir = dcp::fix_long_path (dir);
 
        using namespace boost::filesystem;
        for (auto i: directory_iterator(dir)) {
@@ -113,7 +114,7 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
                throw CopyError (String::compose("Failed to open file %1", to.generic_string()), r);
        }
 
-       FILE* in = fopen_boost (from, "rb");
+       dcp::File in(from, "rb");
        if (!in) {
                ext4_fclose (&out);
                throw CopyError (String::compose("Failed to open file %1", from.string()), 0);
@@ -127,9 +128,8 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
        uint64_t remaining = file_size (from);
        while (remaining > 0) {
                uint64_t const this_time = min(remaining, block_size);
-               size_t read = fread (buffer.data(), 1, this_time, in);
+               size_t read = in.read(buffer.data(), 1, this_time);
                if (read != this_time) {
-                       fclose (in);
                        ext4_fclose (&out);
                        throw CopyError (String::compose("Short read; expected %1 but read %2", this_time, read), 0);
                }
@@ -139,12 +139,10 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
                size_t written;
                r = ext4_fwrite (&out, buffer.data(), this_time, &written);
                if (r != EOK) {
-                       fclose (in);
                        ext4_fclose (&out);
                        throw CopyError ("Write failed", r);
                }
                if (written != this_time) {
-                       fclose (in);
                        ext4_fclose (&out);
                        throw CopyError (String::compose("Short write; expected %1 but wrote %2", this_time, written), 0);
                }
@@ -157,7 +155,6 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total
                }
        }
 
-       fclose (in);
        ext4_fclose (&out);
 
        set_timestamps_to_now (to);
@@ -229,7 +226,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);
+       from = dcp::fix_long_path (from);
 
        using namespace boost::filesystem;
 
@@ -288,12 +285,26 @@ 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());