9414561448bd084fcd08fbc9e9d533c1c86db845
[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 "dcpomatic_log.h"
24 #include "find_missing.h"
25 #include "util.h"
26 #include <boost/filesystem.hpp>
27
28
29 using std::map;
30 using std::shared_ptr;
31 using std::vector;
32
33
34 typedef map<shared_ptr<Content>, vector<boost::filesystem::path>> Replacements;
35
36
37 static
38 void
39 search (Replacements& replacement_paths, boost::filesystem::path directory, int depth = 0)
40 {
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();
50                                         } else {
51                                                 LOG_GENERAL("Reject %1", candidate.path());
52                                         }
53                                 }
54                         }
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);
58                 } else {
59                         LOG_GENERAL("Candidate was ignored; depth=%1", candidate.path().string());
60                 }
61         }
62
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).
65          */
66 }
67
68
69 void
70 dcpomatic::find_missing (vector<shared_ptr<Content>> content_to_fix, boost::filesystem::path clue)
71 {
72         using namespace boost::filesystem;
73
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());
78                 }
79         }
80         LOG_GENERAL("Clue is %1", clue);
81
82         Replacements replacement_paths;
83         for (auto content: content_to_fix) {
84                 replacement_paths[content] = content->paths();
85         }
86
87         search (replacement_paths, is_directory(clue) ? clue : clue.parent_path());
88
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);
98                         }
99                         content->set_paths (repl);
100                 }
101         }
102 }