Fix errors when mapping DCPs referring to the same asset multiple times (#2542).
[dcpomatic.git] / src / lib / map_cli.cc
index c95d7f1a1aaa4c2f3bb2b5f2eda6ddf2b80c7bb0..c49964f80e7da03a80d927405804a276a0c3083f 100644 (file)
@@ -196,13 +196,20 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                CopyError(std::string message) : std::runtime_error(message) {}
        };
 
-       auto maybe_copy = [&assets, output_dir](
+       vector<string> already_copied;
+
+       auto maybe_copy = [&assets, &already_copied, output_dir](
                string asset_id,
                bool rename,
                bool hard_link,
                bool soft_link,
                boost::optional<boost::filesystem::path> extra = boost::none
                ) {
+
+               if (std::find(already_copied.begin(), already_copied.end(), asset_id) != already_copied.end()) {
+                       return;
+               }
+
                auto iter = std::find_if(assets.begin(), assets.end(), [asset_id](shared_ptr<const dcp::Asset> a) { return a->id() == asset_id; });
                if (iter != assets.end()) {
                        DCP_ASSERT((*iter)->file());
@@ -240,6 +247,7 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                                }
                        }
                        (*iter)->set_file(output_path);
+                       already_copied.push_back(asset_id);
                } else {
                        boost::system::error_code ec;
                        boost::filesystem::remove_all(*output_dir, ec);