1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/*
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 <dcp/filesystem.h>
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)
{
boost::system::error_code ec;
for (auto candidate: dcp::filesystem::directory_iterator(directory, ec)) {
if (dcp::filesystem::is_regular_file(candidate.path())) {
for (auto& replacement: replacement_paths) {
for (auto& path: replacement.second) {
if (!dcp::filesystem::exists(path) && path.filename() == candidate.path().filename()) {
path = candidate.path();
}
}
}
} else if (dcp::filesystem::is_directory(candidate.path()) && depth <= 2) {
search (replacement_paths, candidate, depth + 1);
}
}
/* Just ignore errors when creating the directory_iterator; they can be triggered by things like
* macOS' love of creating random directories (see #2291).
*/
}
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);
}
}
}
|