2 Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
23 #include "dcpomatic_log.h"
24 #include "find_missing.h"
26 #include <boost/filesystem.hpp>
30 using std::shared_ptr;
34 typedef map<shared_ptr<Content>, vector<boost::filesystem::path>> Replacements;
39 search (Replacements& replacement_paths, boost::filesystem::path directory, int depth = 0)
41 boost::system::error_code ec;
42 for (auto candidate: boost::filesystem::directory_iterator(directory, ec)) {
43 LOG_GENERAL("Have candidate %1", candidate.path().string());
44 if (boost::filesystem::is_regular_file(candidate.path())) {
45 for (auto& replacement: replacement_paths) {
46 for (auto& path: replacement.second) {
47 if (!boost::filesystem::exists(path) && path.filename() == candidate.path().filename()) {
48 LOG_GENERAL("Accept %1", candidate.path());
49 path = candidate.path();
51 LOG_GENERAL("Reject %1", candidate.path());
55 } else if (boost::filesystem::is_directory(candidate.path()) && depth <= 2) {
56 LOG_GENERAL("Recurse into %1", candidate);
57 search (replacement_paths, candidate, depth + 1);
59 LOG_GENERAL("Candidate was ignored; depth=%1", candidate.path().string());
63 /* Just ignore errors when creating the directory_iterator; they can be triggered by things like
64 * macOS' love of creating random directories (see #2291).
70 dcpomatic::find_missing (vector<shared_ptr<Content>> content_to_fix, boost::filesystem::path clue)
72 using namespace boost::filesystem;
74 LOG_GENERAL_NC("Find missing to fix:");
75 for (auto content: content_to_fix) {
76 for (auto file: content->paths()) {
77 LOG_GENERAL(" %1", file.string());
80 LOG_GENERAL("Clue is %1", clue);
82 Replacements replacement_paths;
83 for (auto content: content_to_fix) {
84 replacement_paths[content] = content->paths();
87 search (replacement_paths, is_directory(clue) ? clue : clue.parent_path());
89 for (auto content: content_to_fix) {
90 auto const& repl = replacement_paths[content];
91 bool const replacements_exist = std::find_if(repl.begin(), repl.end(), [](path p) { return !exists(p); }) == repl.end();
92 LOG_GENERAL("replacements_exist? %1", replacements_exist);
93 LOG_GENERAL("digest match? %1", simple_digest(replacement_paths[content]) == content->digest());
94 if (replacements_exist && simple_digest(replacement_paths[content]) == content->digest()) {
95 LOG_GENERAL_NC("Setting content paths:");
96 for (auto const& i: repl) {
97 LOG_GENERAL(" %1", i);
99 content->set_paths (repl);