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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "hash.h"
#include <zencore/blake3.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zencore/timer.h>
#if ZEN_PLATFORM_WINDOWS
# include <ppl.h>
#endif
namespace zen {
////////////////////////////////////////////////////////////////////////////////
#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
namespace Concurrency {
template<typename IterType, typename LambdaType>
void parallel_for_each(IterType Cursor, IterType End, const LambdaType& Lambda)
{
for (; Cursor < End; ++Cursor)
{
Lambda(*Cursor);
}
}
template<typename T>
struct combinable
{
combinable<T>& local() { return *this; }
void operator+=(T Rhs) { Value += Rhs; }
template<typename LambdaType>
void combine_each(const LambdaType& Lambda)
{
Lambda(Value);
}
T Value = 0;
};
} // namespace Concurrency
#endif // ZEN_PLATFORM_LINUX|MAC
////////////////////////////////////////////////////////////////////////////////
HashCommand::HashCommand()
{
m_Options.add_options()("d,dir", "Directory to scan", cxxopts::value<std::string>(m_ScanDirectory))(
"o,output",
"Output file",
cxxopts::value<std::string>(m_OutputFile));
}
HashCommand::~HashCommand() = default;
int
HashCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions);
auto result = m_Options.parse(argc, argv);
bool valid = m_ScanDirectory.length();
if (!valid)
throw cxxopts::OptionParseException("Chunk command requires a directory to scan");
// Gather list of files to process
ZEN_INFO("Gathering files from {}", m_ScanDirectory);
struct FileEntry
{
std::filesystem::path FilePath;
zen::BLAKE3 FileHash;
};
std::vector<FileEntry> FileList;
uint64_t FileBytes = 0;
std::filesystem::path ScanDirectoryPath{m_ScanDirectory};
for (const std::filesystem::directory_entry& Entry : std::filesystem::recursive_directory_iterator(ScanDirectoryPath))
{
if (Entry.is_regular_file())
{
FileList.push_back({Entry.path()});
FileBytes += Entry.file_size();
}
}
ZEN_INFO("Gathered {} files, total size {}", FileList.size(), zen::NiceBytes(FileBytes));
Concurrency::combinable<uint64_t> TotalBytes;
auto hashFile = [&](FileEntry& File) {
InternalFile InputFile;
InputFile.OpenRead(File.FilePath);
const uint8_t* DataPointer = (const uint8_t*)InputFile.MemoryMapFile();
const size_t DataSize = InputFile.GetFileSize();
File.FileHash = zen::BLAKE3::HashMemory(DataPointer, DataSize);
TotalBytes.local() += DataSize;
};
// Process them as quickly as possible
zen::Stopwatch Timer;
#if 1
Concurrency::parallel_for_each(begin(FileList), end(FileList), [&](auto& file) { hashFile(file); });
#else
for (const auto& file : FileList)
{
hashFile(file);
}
#endif
size_t TotalByteCount = 0;
TotalBytes.combine_each([&](size_t Total) { TotalByteCount += Total; });
const uint64_t ElapsedMs = Timer.GetElapsedTimeMs();
ZEN_INFO("Scanned {} files in {}", FileList.size(), zen::NiceTimeSpanMs(ElapsedMs));
ZEN_INFO("Total bytes {} ({})", zen::NiceBytes(TotalByteCount), zen::NiceByteRate(TotalByteCount, ElapsedMs));
InternalFile Output;
if (m_OutputFile.empty())
{
// TEMPORARY -- should properly open stdout
Output.OpenWrite("CONOUT$", false);
}
else
{
Output.OpenWrite(m_OutputFile, true);
}
zen::ExtendableStringBuilder<256> Line;
uint64_t CurrentOffset = 0;
for (const auto& File : FileList)
{
Line.Append(File.FilePath.generic_u8string().c_str());
Line.Append(',');
File.FileHash.ToHexString(Line);
Line.Append('\n');
Output.Write(Line.Data(), Line.Size(), CurrentOffset);
CurrentOffset += Line.Size();
Line.Reset();
}
// TODO: implement snapshot enumeration and display
return 0;
}
} // namespace zen
|