Use dcp::filesystem to wrap filesystem calls and fix_long_path
[dcpomatic.git] / src / lib / map_cli.cc
index c49964f80e7da03a80d927405804a276a0c3083f..368c3572554a6df81db67f5acbeef96d93b8b974 100644 (file)
@@ -25,6 +25,7 @@
 #include <dcp/cpl.h>
 #include <dcp/dcp.h>
 #include <dcp/interop_subtitle_asset.h>
+#include <dcp/filesystem.h>
 #include <dcp/font_asset.h>
 #include <dcp/mono_picture_asset.h>
 #include <dcp/reel.h>
@@ -150,7 +151,7 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                return string{"Missing -o or --output"};
        }
 
-       if (boost::filesystem::exists(*output_dir)) {
+       if (dcp::filesystem::exists(*output_dir)) {
                return String::compose("Output directory %1 already exists.", *output_dir);
        }
 
@@ -159,7 +160,7 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
        }
 
        boost::system::error_code ec;
-       boost::filesystem::create_directory(*output_dir, ec);
+       dcp::filesystem::create_directory(*output_dir, ec);
        if (ec) {
                return String::compose("Could not create output directory %1: %2", *output_dir, ec.message());
        }
@@ -221,36 +222,36 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                        }
 
                        if (rename) {
-                               output_path /= String::compose("%1%2", (*iter)->id(), boost::filesystem::extension((*iter)->file().get()));
+                               output_path /= String::compose("%1%2", (*iter)->id(), dcp::filesystem::extension((*iter)->file().get()));
                                (*iter)->rename_file(output_path);
                        } else {
                                output_path /= (*iter)->file()->filename();
                        }
 
-                       boost::filesystem::create_directories(output_path.parent_path());
+                       dcp::filesystem::create_directories(output_path.parent_path());
 
                        boost::system::error_code ec;
                        if (hard_link) {
-                               boost::filesystem::create_hard_link(input_path, output_path, ec);
+                               dcp::filesystem::create_hard_link(input_path, output_path, ec);
                                if (ec) {
                                        throw CopyError(String::compose("Could not hard-link asset %1: %2", input_path.string(), ec.message()));
                                }
                        } else if (soft_link) {
-                               boost::filesystem::create_symlink(input_path, output_path, ec);
+                               dcp::filesystem::create_symlink(input_path, output_path, ec);
                                if (ec) {
                                        throw CopyError(String::compose("Could not soft-link asset %1: %2", input_path.string(), ec.message()));
                                }
                        } else {
-                               boost::filesystem::copy_file(input_path, output_path, ec);
+                               dcp::filesystem::copy_file(input_path, output_path, ec);
                                if (ec) {
                                        throw CopyError(String::compose("Could not copy asset %1: %2", input_path.string(), ec.message()));
                                }
                        }
-                       (*iter)->set_file(output_path);
+                       (*iter)->set_file_preserving_hash(output_path);
                        already_copied.push_back(asset_id);
                } else {
                        boost::system::error_code ec;
-                       boost::filesystem::remove_all(*output_dir, ec);
+                       dcp::filesystem::remove_all(*output_dir, ec);
                        throw CopyError(String::compose("Could not find required asset %1", asset_id));
                }
        };
@@ -267,25 +268,31 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                }
        };
 
+       auto maybe_copy_font = [&maybe_copy](shared_ptr<const dcp::SubtitleAsset> asset, bool rename, bool hard_link, bool soft_link) {
+               auto interop = dynamic_pointer_cast<const dcp::InteropSubtitleAsset>(asset);
+               boost::optional<boost::filesystem::path> extra;
+               if (interop) {
+                       extra = interop->id();
+                       for (auto font_asset: interop->font_assets()) {
+                               maybe_copy(font_asset->id(), rename, hard_link, soft_link, extra);
+                       }
+               }
+               return extra;
+       };
+
        /* Copy assets that the CPLs need */
        try {
                for (auto cpl: cpls) {
                        for (auto reel: cpl->reels()) {
                                maybe_copy_from_reel(reel->main_picture(), rename, hard_link, soft_link);
                                maybe_copy_from_reel(reel->main_sound(), rename, hard_link, soft_link);
-                               boost::optional<boost::filesystem::path> extra;
                                if (reel->main_subtitle()) {
-                                       auto interop = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(reel->main_subtitle()->asset());
-                                       if (interop) {
-                                               extra = interop->id();
-                                               for (auto font_asset: interop->font_assets()) {
-                                                       maybe_copy(font_asset->id(), rename, hard_link, soft_link, extra);
-                                               }
-                                       }
+                                       auto extra = maybe_copy_font(reel->main_subtitle()->asset(), rename, hard_link, soft_link);
+                                       maybe_copy_from_reel(reel->main_subtitle(), rename, hard_link, soft_link, extra);
                                }
-                               maybe_copy_from_reel(reel->main_subtitle(), rename, hard_link, soft_link, extra);
                                for (auto ccap: reel->closed_captions()) {
-                                       maybe_copy_from_reel(ccap, rename, hard_link, soft_link);
+                                       auto extra = maybe_copy_font(ccap->asset(), rename, hard_link, soft_link);
+                                       maybe_copy_from_reel(ccap, rename, hard_link, soft_link, extra);
                                }
                                maybe_copy_from_reel(reel->atmos(), rename, hard_link, soft_link);
                        }
@@ -298,7 +305,13 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
 
        dcp.resolve_refs(assets);
        dcp.set_annotation_text(cpls[0]->annotation_text().get_value_or(""));
-       dcp.write_xml(Config::instance()->signer_chain());
+       try {
+               dcp.set_creator(Config::instance()->dcp_creator());
+               dcp.set_issuer(Config::instance()->dcp_issuer());
+               dcp.write_xml(Config::instance()->signer_chain());
+       } catch (dcp::UnresolvedRefError& e) {
+               return String::compose("%1\nPerhaps you need to give a -d parameter to say where this asset is located.", e.what());
+       }
 
        return {};
 }