diff options
| author | Carl Hetherington <cth@carlh.net> | 2021-12-25 02:05:54 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2022-05-02 00:31:04 +0200 |
| commit | 266e3f1b80d1263ff3cc8b3afaecd6ca1f88983b (patch) | |
| tree | eeb717e9eaa72e1b70f0a9ff1f2e7bad4ddc2a5b /src/lib/find_missing.cc | |
| parent | 469a5dbd4251b094e6d4b668a763568c25947b88 (diff) | |
Extract and improve code to find missing files (#1940).
Diffstat (limited to 'src/lib/find_missing.cc')
| -rw-r--r-- | src/lib/find_missing.cc | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/lib/find_missing.cc b/src/lib/find_missing.cc new file mode 100644 index 000000000..3d61e74bd --- /dev/null +++ b/src/lib/find_missing.cc @@ -0,0 +1,75 @@ +/* + Copyright (C) 2021 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#include "content.h" +#include "find_missing.h" +#include "util.h" +#include <boost/filesystem.hpp> + + +using std::map; +using std::shared_ptr; +using std::vector; + + +typedef map<shared_ptr<Content>, vector<boost::filesystem::path>> Replacements; + + +static +void +search (Replacements& replacement_paths, boost::filesystem::path directory, int depth = 0) +{ + for (auto candidate: boost::filesystem::directory_iterator(directory)) { + if (boost::filesystem::is_regular_file(candidate.path())) { + for (auto& replacement: replacement_paths) { + for (auto& path: replacement.second) { + if (!boost::filesystem::exists(path) && path.filename() == candidate.path().filename()) { + path = candidate.path(); + } + } + } + } else if (boost::filesystem::is_directory(candidate.path()) && depth <= 2) { + search (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; + for (auto content: content_to_fix) { + replacement_paths[content] = content->paths(); + } + + search (replacement_paths, is_directory(clue) ? clue : clue.parent_path()); + + for (auto content: content_to_fix) { + auto const& repl = 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()) { + content->set_paths (repl); + } + } +} |
