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 "find_missing.h"
25 #include <boost/filesystem.hpp>
29 using std::shared_ptr;
33 typedef map<shared_ptr<Content>, vector<boost::filesystem::path>> Replacements;
38 search (Replacements& replacement_paths, boost::filesystem::path directory, int depth = 0)
40 boost::system::error_code ec;
41 for (auto candidate: boost::filesystem::directory_iterator(directory, ec)) {
42 if (boost::filesystem::is_regular_file(candidate.path())) {
43 for (auto& replacement: replacement_paths) {
44 for (auto& path: replacement.second) {
45 if (!boost::filesystem::exists(path) && path.filename() == candidate.path().filename()) {
46 path = candidate.path();
50 } else if (boost::filesystem::is_directory(candidate.path()) && depth <= 2) {
51 search (replacement_paths, candidate, depth + 1);
55 /* Just ignore errors when creating the directory_iterator; they can be triggered by things like
56 * macOS' love of creating random directories (see #2291).
62 dcpomatic::find_missing (vector<shared_ptr<Content>> content_to_fix, boost::filesystem::path clue)
64 using namespace boost::filesystem;
66 Replacements replacement_paths;
67 for (auto content: content_to_fix) {
68 replacement_paths[content] = content->paths();
71 search (replacement_paths, is_directory(clue) ? clue : clue.parent_path());
73 for (auto content: content_to_fix) {
74 auto const& repl = replacement_paths[content];
75 bool const replacements_exist = std::find_if(repl.begin(), repl.end(), [](path p) { return !exists(p); }) == repl.end();
76 if (replacements_exist && simple_digest(replacement_paths[content]) == content->digest()) {
77 content->set_paths (repl);