Missed update to private test repo version.
[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         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();
47                                         }
48                                 }
49                         }
50                 } else if (boost::filesystem::is_directory(candidate.path()) && depth <= 2) {
51                         search (replacement_paths, candidate, depth + 1);
52                 }
53         }
54
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).
57          */
58 }
59
60
61 void
62 dcpomatic::find_missing (vector<shared_ptr<Content>> content_to_fix, boost::filesystem::path clue)
63 {
64         using namespace boost::filesystem;
65
66         Replacements replacement_paths;
67         for (auto content: content_to_fix) {
68                 replacement_paths[content] = content->paths();
69         }
70
71         search (replacement_paths, is_directory(clue) ? clue : clue.parent_path());
72
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);
78                 }
79         }
80 }