-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
267 lines (228 loc) · 9.73 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/regex.hpp>
#include <boost/crc.hpp>
#include <sodium.h>
#define CRC32 "crc32"
#define SODIUM "sodium"
//#define _DEBUG
namespace po = boost::program_options;
namespace fs = boost::filesystem;
static inline void trim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
void scanDir(const fs::path dirPath, std::list<fs::path> &fileList, const int level, const std::list<fs::path> &excludeDirs, const std::string &fmask, const int minFSize)
{
const boost::regex fileMask( fmask );
fs::directory_iterator it{dirPath};
while (it != fs::directory_iterator{})
{
if (fs::is_symlink(*it))
{
*it++;
continue;
}
bool foundExcluded = (std::find(excludeDirs.begin(), excludeDirs.end(), *it) != excludeDirs.end());
if (level == 1 && fs::is_directory(*it))
{
if (!foundExcluded)
scanDir(*it, fileList, level, excludeDirs, fmask, minFSize);
}
if (fs::is_regular_file(*it))
{
boost::smatch what;
if ( fmask.length() != 0
&& boost::regex_match( static_cast<fs::path>(*it).filename().string(), what, fileMask )
&& fs::file_size(*it) >= minFSize )
fileList.push_back(*it);
}
*it++;
}
}
unsigned char getHash(char *buffer, const std::string &hashType, const int blockSize, const int lastReadBlockSize)
{
if (lastReadBlockSize < blockSize)
for (int i = blockSize - 1; i > blockSize - lastReadBlockSize; --i)
buffer[i] = 0; //or '0' ?
if (hashType == CRC32)
{
boost::crc_32_type result;
result.process_bytes(buffer, sizeof(buffer));
return result.checksum();
}
else if (hashType == SODIUM)
{
//https://libsodium.gitbook.io/doc/hashing/generic_hashing
unsigned char hash[crypto_generichash_BYTES];
int result = crypto_generichash(hash, sizeof hash,
reinterpret_cast<unsigned char*>(buffer), sizeof(buffer),
NULL, 0);
return reinterpret_cast<unsigned char>(*hash);
}
return char();
}
//$ bayan
int main(int argc, const char *argv[])
{
//https://libsodium.gitbook.io/doc/usage
if (sodium_init() == -1) {
return 1;
}
po::options_description desc{"Options"};
#ifdef _DEBUG
desc.add_options()
("help,h", "This screen")
("scandirs", po::value<std::string>()->default_value("./testdata1,./testdata2"), "Directories for scan")
("excldirs", po::value<std::string>()->default_value("./testdata1/exclude,./testdata2/exclude"), "Directories for exclude")
("level", po::value<int>()->default_value(1), "Scan level: 1 - with subdirs, 0 - only current")
("minfsize", po::value<int>()->default_value(1), "Minimal file size in bytes")
("fmask", po::value<std::string>()->default_value(".*\\.txt"), "File mask for scan")
("blocksize", po::value<int>()->default_value(5), "Reading block size, in bytes")
("hashtype", po::value<std::string>()->default_value(SODIUM), "Hash algorithm type (sodium or crc32)");
#else
desc.add_options()
("help,h", "This screen")
("scandirs", po::value<std::string>(), "Directories for scan")
("excldirs", po::value<std::string>(), "Directories for exclude")
("level", po::value<int>()->default_value(1), "Scan level: 1 - with subdirs, 0 - only current")
("minfsize", po::value<int>()->default_value(1), "Minimal file size in bytes")
("fmask", po::value<std::string>()->default_value(".*\\.txt"), "File mask for scan")
("blocksize", po::value<int>()->default_value(5), "Reading block size, in bytes")
("hashtype", po::value<std::string>()->default_value(SODIUM), "Hash algorithm type (sodium or crc32)");
#endif
po::variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
if (vm.count("help") || !vm.count("scandirs"))
{
std::cout << desc << std::endl;
return 0;
}
//validate some input params
if (vm["hashtype"].as<std::string>() != CRC32
&& vm["hashtype"].as<std::string>() != SODIUM)
{
std::cout << "Wrong hash type" << std::endl;
std::cout << desc << std::endl;
return 1;
}
//int blockSize = vm["blocksize"].as<int>();
//std::cout << "BlockSize " << vm["blocksize"].as<int>() << std::endl;
std::list<fs::path> scanDirs;
std::list<fs::path> excludeDirs;
boost::tokenizer<boost::char_separator<char>> scanDirsTokenizer{vm["scandirs"].as<std::string>(), boost::char_separator<char>{","}};
for (auto line: scanDirsTokenizer)
{
trim(line);
scanDirs.push_back(fs::canonical(line));
}
if (!vm["excldirs"].empty())
{
boost::tokenizer<boost::char_separator<char>> exclDirsTokenizer{vm["excldirs"].as<std::string>(), boost::char_separator<char>{","}};
for (auto line: exclDirsTokenizer)
{
trim(line);
excludeDirs.push_back(fs::canonical(line));
}
}
std::list<fs::path> fullScanFileList;
for (auto dir: scanDirs)
{
scanDir(dir, fullScanFileList,
vm["level"].as<int>(),
excludeDirs,
vm["fmask"].as<std::string>(),
vm["minfsize"].as<int>());
}
std::vector<fs::path> foundDuplicates;
for (auto testFile: fullScanFileList)
{
std::vector<unsigned char> testFileHashes;
std::ifstream testFileStream;
testFileStream.rdbuf()->pubsetbuf(nullptr, 0);
testFileStream.open(testFile.string(), std::ios::binary | std::ios::ate);
testFileStream.seekg(0);
if (!testFileStream.is_open())
{
std::cout << "Error on open file" << testFile << std::endl;
return 1;
}
char *testFileBuffer = new char[vm["blocksize"].as<int>() + 1];
testFileBuffer[vm["blocksize"].as<int>()] = '\0'; //for debug
bool foundInDuplicates = (std::find(foundDuplicates.begin(), foundDuplicates.end(), testFile) != foundDuplicates.end());
if (foundInDuplicates)
{
std::cout << "Skip duplicate file: " << testFile.string() << std::endl;
continue;
}
std::cout << "Check file: " << testFile.string() << ":" << std::endl;
for (auto subTestFile: fullScanFileList)
{
//std::cout << testFile << subTestFile << std::endl;
if (testFile == subTestFile)
continue;
std::vector<unsigned char> subTestFileHashes;
std::ifstream subTestFileStream;
subTestFileStream.rdbuf()->pubsetbuf(nullptr, 0);
subTestFileStream.open(subTestFile.string(), std::ios::binary | std::ios::ate);
subTestFileStream.seekg(0);
if (!subTestFileStream.is_open())
{
std::cout << "Error on open sub-file" << subTestFile << std::endl;
return 1;
}
char *subTestFileBuffer = new char[vm["blocksize"].as<int>() + 1];
subTestFileBuffer[vm["blocksize"].as<int>()] = '\0'; //for debug
//std::cout << subTestFileStream.gcount() << std::endl;
bool diffFound = false;
while (subTestFileStream.read(subTestFileBuffer, vm["blocksize"].as<int>()) || subTestFileStream.gcount())
{
subTestFileHashes.push_back(getHash(subTestFileBuffer, vm["hashtype"].as<std::string>(), vm["blocksize"].as<int>(), subTestFileStream.gcount()));
//std::cout << "sub " << subTestFileBuffer << std::endl;
if (subTestFileHashes.size() > testFileHashes.size())
{
testFileStream.read(testFileBuffer, vm["blocksize"].as<int>());
testFileHashes.push_back(getHash(testFileBuffer, vm["hashtype"].as<std::string>(), vm["blocksize"].as<int>(), testFileStream.gcount()));
//std::cout << "tst " << testFileBuffer << std::endl;
}
//if sub-file > main-file or one of hashes not equal
if ( ( subTestFileHashes.size() > testFileHashes.size() )
|| (subTestFileHashes[subTestFileHashes.size()-1] != testFileHashes[subTestFileHashes.size()-1]) )
{
diffFound = true;
break;
}
}
delete[] subTestFileBuffer;
//if sub-file < main-file
if (subTestFileHashes.size() == testFileHashes.size())
{
testFileStream.get(testFileBuffer, vm["blocksize"].as<int>());
if (testFileStream.gcount() > 0)
{
diffFound = true;
}
}
//difference between files not found
if (!diffFound)
{
std::cout << " + " << subTestFile.string() << std::endl;
foundDuplicates.push_back(subTestFile);
}
else
{
std::cout << " - " << subTestFile.string() << std::endl;
}
}
delete[] testFileBuffer;
//std::cout << "testFileHashes.size " << testFileHashes.size() << std::endl;
}
return 0;
}