summaryrefslogtreecommitdiff
path: root/src/lib/find_missing.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2025-01-19 20:50:35 +0100
committerCarl Hetherington <cth@carlh.net>2025-01-19 20:50:35 +0100
commit9f2595c22d10a64f561379847192ad64001ac9f1 (patch)
tree9e23d9aa21797dabe298723f79f36ee9795fb080 /src/lib/find_missing.cc
parent033efb9ebc5c5d15b850fda0c770fb429f4e827c (diff)
Add search by digest if search by name fails (#2935).
Diffstat (limited to 'src/lib/find_missing.cc')
-rw-r--r--src/lib/find_missing.cc55
1 files changed, 50 insertions, 5 deletions
diff --git a/src/lib/find_missing.cc b/src/lib/find_missing.cc
index 73eb50460..65776d02e 100644
--- a/src/lib/find_missing.cc
+++ b/src/lib/find_missing.cc
@@ -58,23 +58,68 @@ search_by_name(Replacements& replacement_paths, boost::filesystem::path director
}
+static
+void
+search_by_digest(Replacements& replacement_paths, boost::filesystem::path directory, int depth = 0)
+{
+ boost::system::error_code ec;
+ for (auto candidate: dcp::filesystem::directory_iterator(directory, ec)) {
+ if (dcp::filesystem::is_regular_file(candidate.path())) {
+ auto const candidate_digest = simple_digest({candidate.path()});
+ for (auto& replacement: replacement_paths) {
+ DCPOMATIC_ASSERT(replacement.first->number_of_paths() == 1)
+ if (replacement.first->digest() == candidate_digest) {
+ replacement.second = { candidate.path() };
+ }
+ }
+ } else if (dcp::filesystem::is_directory(candidate.path()) && depth <= 2) {
+ search_by_digest(replacement_paths, candidate, depth + 1);
+ }
+ }
+}
+
+
void
dcpomatic::find_missing (vector<shared_ptr<Content>> content_to_fix, boost::filesystem::path clue)
{
using namespace boost::filesystem;
- Replacements replacement_paths;
+ Replacements name_replacement_paths;
for (auto content: content_to_fix) {
- replacement_paths[content] = content->paths();
+ name_replacement_paths[content] = content->paths();
}
- search_by_name(replacement_paths, is_directory(clue) ? clue : clue.parent_path());
+ /* Look for replacements with the same filename */
+ search_by_name(name_replacement_paths, is_directory(clue) ? clue : clue.parent_path());
+ /* Fix any content that can be fixed with those, making a note of those that cannot */
+ Replacements digest_replacement_paths;
for (auto content: content_to_fix) {
- auto const& repl = replacement_paths[content];
+ auto const& repl = name_replacement_paths[content];
bool const replacements_exist = std::find_if(repl.begin(), repl.end(), [](path p) { return !exists(p); }) == repl.end();
- if (replacements_exist && simple_digest(replacement_paths[content]) == content->digest()) {
+ if (replacements_exist && simple_digest(name_replacement_paths[content]) == content->digest()) {
content->set_paths (repl);
+ } else {
+ /* Put it on the list to look for by digest, if possible */
+ if (content->number_of_paths() == 1) {
+ digest_replacement_paths[content] = name_replacement_paths[content];
+ }
+ }
+ }
+
+ if (!digest_replacement_paths.empty()) {
+ /* Search for content with just one path by digest */
+ search_by_digest(digest_replacement_paths, is_directory(clue) ? clue : clue.parent_path());
+
+ for (auto content: content_to_fix) {
+ auto iter = digest_replacement_paths.find(content);
+ if (iter != digest_replacement_paths.end()) {
+ auto const& repl = iter->second;
+ bool const replacements_exist = std::find_if(repl.begin(), repl.end(), [](path p) { return !exists(p); }) == repl.end();
+ if (replacements_exist) {
+ content->set_paths(repl);
+ }
+ }
}
}
}