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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "dedup_cmd.h"
#include <zencore/blake3.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/iobuffer.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/timer.h>
#if ZEN_PLATFORM_WINDOWS
# include <ppl.h>
#endif
#include <list>
#include <unordered_map>
namespace zen {
////////////////////////////////////////////////////////////////////////////////
#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
namespace Concurrency {
template<typename T0, typename T1>
inline void parallel_invoke(T0 const& t0, T1 const& t1)
{
t0();
t1();
}
} // namespace Concurrency
#endif // ZEN_PLATFORM_LINUX/MAC
////////////////////////////////////////////////////////////////////////////////
DedupCommand::DedupCommand()
{
m_Options.add_options()("h,help", "Print help");
m_Options.add_options()("size", "Configure size threshold for dedup", cxxopts::value(m_SizeThreshold)->default_value("131072"));
m_Options.add_option("", "s", "source", "Copy source", cxxopts::value(m_DedupSource), "<file/directory>");
m_Options.add_option("", "t", "target", "Copy target", cxxopts::value(m_DedupTarget), "<file/directory>");
m_Options.add_option("", "", "positional", "Positional arguments", cxxopts::value(m_Positional), "");
m_Options.parse_positional({"source", "target", "positional"});
}
DedupCommand::~DedupCommand() = default;
void
DedupCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions);
if (!ParseOptions(argc, argv))
{
return;
}
// Validate arguments
const bool SourceGood = zen::SupportsBlockRefCounting(m_DedupSource);
const bool TargetGood = zen::SupportsBlockRefCounting(m_DedupTarget);
if (!SourceGood)
{
throw std::runtime_error(fmt::format("Source directory '{}' does not support deduplication", m_DedupSource));
}
if (!TargetGood)
{
throw std::runtime_error(fmt::format("Target directory '{}' does not support deduplication", m_DedupTarget));
}
ZEN_CONSOLE("Performing dedup operation between {} and {}, size threshold {}",
m_DedupSource,
m_DedupTarget,
zen::NiceBytes(m_SizeThreshold));
using DirEntryList_t = std::list<std::filesystem::directory_entry>;
zen::RwLock MapLock;
std::unordered_map<size_t, DirEntryList_t> FileSizeMap;
size_t CandidateCount = 0;
auto AddToList = [&](const std::filesystem::directory_entry& Entry) {
if (Entry.is_regular_file())
{
uintmax_t FileSize = Entry.file_size();
if (FileSize > m_SizeThreshold)
{
zen::RwLock::ExclusiveLockScope _(MapLock);
FileSizeMap[FileSize].push_back(Entry);
++CandidateCount;
}
}
};
std::filesystem::recursive_directory_iterator DirEnd;
ZEN_CONSOLE("Gathering file info from source: '{}'", m_DedupSource);
ZEN_CONSOLE("Gathering file info from target: '{}'", m_DedupTarget);
{
zen::Stopwatch Timer;
Concurrency::parallel_invoke(
[&] {
for (std::filesystem::recursive_directory_iterator DirIt1(m_DedupSource); DirIt1 != DirEnd; ++DirIt1)
{
AddToList(*DirIt1);
}
},
[&] {
for (std::filesystem::recursive_directory_iterator DirIt2(m_DedupTarget); DirIt2 != DirEnd; ++DirIt2)
{
AddToList(*DirIt2);
}
});
ZEN_CONSOLE("Gathered {} candidates across {} size buckets. Elapsed: {}",
CandidateCount,
FileSizeMap.size(),
zen::NiceTimeSpanMs(Timer.GetElapsedTimeMs()));
}
ZEN_CONSOLE("Sorting buckets by size");
zen::Stopwatch Timer;
uint64_t DupeBytes = 0;
struct SizeList
{
size_t Size;
DirEntryList_t* DirEntries;
};
std::vector<SizeList> SizeLists{FileSizeMap.size()};
{
int i = 0;
for (auto& kv : FileSizeMap)
{
ZEN_ASSERT(kv.first >= m_SizeThreshold);
SizeLists[i].Size = kv.first;
SizeLists[i].DirEntries = &kv.second;
++i;
}
}
std::sort(begin(SizeLists), end(SizeLists), [](const SizeList& Lhs, const SizeList& Rhs) { return Lhs.Size > Rhs.Size; });
ZEN_CONSOLE("Bucket summary:");
std::vector<size_t> BucketId;
std::vector<size_t> BucketOffsets;
std::vector<size_t> BucketSizes;
std::vector<size_t> BucketFileCounts;
size_t TotalFileSizes = 0;
size_t TotalFileCount = 0;
{
size_t CurrentPow2 = 0;
size_t BucketSize = 0;
size_t BucketFileCount = 0;
bool FirstBucket = true;
for (size_t i = 0; i < SizeLists.size(); ++i)
{
const size_t ThisSize = SizeLists[i].Size;
const size_t Pow2 = zen::NextPow2(ThisSize);
if (CurrentPow2 != Pow2)
{
CurrentPow2 = Pow2;
if (!FirstBucket)
{
BucketSizes.push_back(BucketSize);
BucketFileCounts.push_back(BucketFileCount);
}
BucketId.push_back(Pow2);
BucketOffsets.push_back(i);
FirstBucket = false;
BucketSize = 0;
BucketFileCount = 0;
}
BucketSize += ThisSize;
TotalFileSizes += ThisSize;
BucketFileCount += SizeLists[i].DirEntries->size();
TotalFileCount += SizeLists[i].DirEntries->size();
}
if (!FirstBucket)
{
BucketSizes.push_back(BucketSize);
BucketFileCounts.push_back(BucketFileCount);
}
ZEN_ASSERT(BucketOffsets.size() == BucketSizes.size());
ZEN_ASSERT(BucketOffsets.size() == BucketFileCounts.size());
}
for (size_t i = 0; i < BucketOffsets.size(); ++i)
{
ZEN_CONSOLE(" Bucket {} : {}, {} candidates", zen::NiceBytes(BucketId[i]), zen::NiceBytes(BucketSizes[i]), BucketFileCounts[i]);
}
ZEN_CONSOLE("Total : {}, {} candidates", zen::NiceBytes(TotalFileSizes), TotalFileCount);
std::string CurrentNice;
for (SizeList& Size : SizeLists)
{
std::string CurNice{zen::NiceBytes(zen::NextPow2(Size.Size))};
if (CurNice != CurrentNice)
{
CurrentNice = CurNice;
ZEN_CONSOLE("Now scanning bucket: {}", CurrentNice);
}
std::unordered_map<zen::BLAKE3, const std::filesystem::directory_entry*, zen::BLAKE3::Hasher> DedupMap;
for (const auto& Entry : *Size.DirEntries)
{
zen::BLAKE3 Hash;
if constexpr (true)
{
zen::BLAKE3Stream b3s;
if (std::error_code ScanEc =
zen::ScanFile(Entry.path(), 64 * 1024, [&](const void* Data, size_t Size) { b3s.Append(Data, Size); });
ScanEc)
{
throw std::system_error(ScanEc, fmt::format("Failed to scan file '{}'", Entry.path()));
}
Hash = b3s.GetHash();
}
else
{
zen::FileContents Contents = zen::ReadFile(Entry.path());
zen::BLAKE3Stream b3s;
for (zen::IoBuffer& Buffer : Contents.Data)
{
b3s.Append(Buffer.Data(), Buffer.Size());
}
Hash = b3s.GetHash();
}
if (const std::filesystem::directory_entry* Dupe = DedupMap[Hash])
{
std::string FileA = PathToUtf8(Dupe->path());
std::string FileB = PathToUtf8(Entry.path());
size_t MinLen = std::min(FileA.size(), FileB.size());
auto Its = std::mismatch(FileB.rbegin(), FileB.rbegin() + MinLen, FileA.rbegin());
if (Its.first != FileB.rbegin())
{
if (Its.first[-1] == '\\' || Its.first[-1] == '/')
--Its.first;
FileB = std::string(FileB.begin(), Its.first.base()) + "...";
}
ZEN_INFO("{} {} <-> {}", zen::NiceBytes(Entry.file_size()).c_str(), FileA.c_str(), FileB.c_str());
zen::CopyFileOptions Options;
Options.EnableClone = true;
Options.MustClone = true;
if (std::error_code Ec = zen::CopyFile(Dupe->path(), Entry.path(), Options); Ec)
{
ZEN_ERROR("Failed to clone '{}' to '{}': {}", Dupe->path(), Entry.path(), Ec.message());
continue;
}
DupeBytes += Entry.file_size();
}
else
{
DedupMap[Hash] = &Entry;
}
}
Size.DirEntries->clear();
}
ZEN_CONSOLE("Elapsed: {} Deduped: {}", zen::NiceTimeSpanMs(Timer.GetElapsedTimeMs()), zen::NiceBytes(DupeBytes));
}
} // namespace zen
|