3d61e74bd4f9b2b330f92ea7ba9c5e2bfc73293c
[dcpomatic.git] / src / lib / find_missing.cc
1 /*
2     Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "content.h"
23 #include "find_missing.h"
24 #include "util.h"
25 #include <boost/filesystem.hpp>
26
27
28 using std::map;
29 using std::shared_ptr;
30 using std::vector;
31
32
33 typedef map<shared_ptr<Content>, vector<boost::filesystem::path>> Replacements;
34
35
36 static
37 void
38 search (Replacements& replacement_paths, boost::filesystem::path directory, int depth = 0)
39 {
40         for (auto candidate: boost::filesystem::directory_iterator(directory)) {
41                 if (boost::filesystem::is_regular_file(candidate.path())) {
42                         for (auto& replacement: replacement_paths) {
43                                 for (auto& path: replacement.second) {
44                                         if (!boost::filesystem::exists(path) && path.filename() == candidate.path().filename()) {
45                                                 path = candidate.path();
46                                         }
47                                 }
48                         }
49                 } else if (boost::filesystem::is_directory(candidate.path()) && depth <= 2) {
50                         search (replacement_paths, candidate, depth + 1);
51                 }
52         }
53 }
54
55
56 void
57 dcpomatic::find_missing (vector<shared_ptr<Content>> content_to_fix, boost::filesystem::path clue)
58 {
59         using namespace boost::filesystem;
60
61         Replacements replacement_paths;
62         for (auto content: content_to_fix) {
63                 replacement_paths[content] = content->paths();
64         }
65
66         search (replacement_paths, is_directory(clue) ? clue : clue.parent_path());
67
68         for (auto content: content_to_fix) {
69                 auto const& repl = replacement_paths[content];
70                 bool const replacements_exist = std::find_if(repl.begin(), repl.end(), [](path p) { return !exists(p); }) == repl.end();
71                 if (replacements_exist && simple_digest(replacement_paths[content]) == content->digest()) {
72                         content->set_paths (repl);
73                 }
74         }
75 }