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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/*
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::string;
using std::vector;
typedef map<shared_ptr<const Content>, vector<boost::filesystem::path>> Replacements;
static
void
search_by_name(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) {
/* Extract a filename as if this path were from a platform with a different
* separator.
*/
string other = path.string();
#ifdef DCPOMATIC_POSIX
std::replace(other.begin(), other.end(), '\\', '/');
#else
std::replace(other.begin(), other.end(), '/', '\\');
#endif
boost::filesystem::path other_path(other);
if (
!dcp::filesystem::exists(path) &&
(path.filename() == candidate.path().filename() || other_path.filename() == candidate.path().filename())) {
path = candidate.path();
}
}
}
} else if (dcp::filesystem::is_directory(candidate.path()) && depth <= 2) {
search_by_name(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).
*/
}
static
void
search_by_digest(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())) {
auto const candidate_digest = simple_digest({candidate.path()});
for (auto& replacement: replacement_paths) {
DCPOMATIC_ASSERT(replacement.first->number_of_paths() == 1)
if (replacement.first->digest() == candidate_digest) {
replacement.second = { candidate.path() };
}
}
} else if (dcp::filesystem::is_directory(candidate.path()) && depth <= 2) {
search_by_digest(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 name_replacement_paths;
for (auto content: content_to_fix) {
name_replacement_paths[content] = content->paths();
}
/* Look for replacements with the same filename */
search_by_name(name_replacement_paths, is_directory(clue) ? clue : clue.parent_path());
/* Fix any content that can be fixed with those, making a note of those that cannot */
Replacements digest_replacement_paths;
for (auto content: content_to_fix) {
auto const& repl = name_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(name_replacement_paths[content]) == content->digest()) {
content->set_paths (repl);
} else {
/* Put it on the list to look for by digest, if possible */
if (content->number_of_paths() == 1) {
digest_replacement_paths[content] = name_replacement_paths[content];
}
}
}
if (!digest_replacement_paths.empty()) {
/* Search for content with just one path by digest */
search_by_digest(digest_replacement_paths, is_directory(clue) ? clue : clue.parent_path());
for (auto content: content_to_fix) {
auto iter = digest_replacement_paths.find(content);
if (iter != digest_replacement_paths.end()) {
auto const& repl = iter->second;
bool const replacements_exist = std::find_if(repl.begin(), repl.end(), [](path p) { return !exists(p); }) == repl.end();
if (replacements_exist) {
content->set_paths(repl);
}
}
}
}
}
|