-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeduplicate.cpp
More file actions
155 lines (114 loc) · 4.57 KB
/
deduplicate.cpp
File metadata and controls
155 lines (114 loc) · 4.57 KB
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
// cl /EHsc /nologo /W4 /MTd deduplicate.cpp /I boost-1.46.1\include /link /libpath:boost-1.46.1\lib bcrypt.lib
#pragma warning(disable: 4512) // assignment operator could not be generated
#include <stddef.h>
#include <fstream>
#include <ios>
#include <iostream>
#include <ostream>
#include <regex>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/bimap.hpp>
#include <boost/filesystem.hpp>
#include <boost/scope_exit.hpp>
#include <windows.h>
#include <bcrypt.h> // http://msdn.microsoft.com/en-us/library/aa376217(VS.85).aspx
using namespace std;
using namespace boost;
using namespace boost::filesystem;
vector<unsigned char> sha256(const vector<unsigned char>& v) {
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_HASH_HANDLE hHash = NULL;
DWORD cbData = 0;
DWORD cbHash = 0;
DWORD cbHashObject = 0;
BOOST_SCOPE_EXIT((&hAlg)) {
if (hAlg) {
BCryptCloseAlgorithmProvider(hAlg, 0);
}
} BOOST_SCOPE_EXIT_END
BOOST_SCOPE_EXIT((&hHash)) {
if (hHash) {
BCryptDestroyHash(hHash);
}
} BOOST_SCOPE_EXIT_END
if (BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, NULL, 0) < 0) {
throw runtime_error("BCryptOpenAlgorithmProvider() failed.");
}
if (BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, reinterpret_cast<unsigned char *>(&cbHashObject), sizeof(DWORD), &cbData, 0) < 0) {
throw runtime_error("BCryptGetProperty() failed.");
}
vector<unsigned char> HashObject(cbHashObject);
if (BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, reinterpret_cast<unsigned char *>(&cbHash), sizeof(DWORD), &cbData, 0) < 0) {
throw runtime_error("BCryptGetProperty() failed.");
}
vector<unsigned char> ret(cbHash);
if (BCryptCreateHash(hAlg, &hHash, HashObject.data(), HashObject.size(), NULL, 0, 0) < 0) {
throw runtime_error("BCryptCreateHash() failed.");
}
if (BCryptHashData(hHash, const_cast<unsigned char *>(v.data()), v.size(), 0) < 0) {
throw runtime_error("BCryptHashData() failed.");
}
if (BCryptFinishHash(hHash, ret.data(), ret.size(), 0) < 0) {
throw runtime_error("BCryptFinishHash() failed.");
}
return ret;
}
string hexify(const vector<unsigned char>& v) {
string s(2 * v.size(), 'x');
string::iterator k = s.begin();
for (auto i = v.begin(); i != v.end(); ++i) {
*k++ = "0123456789ABCDEF"[*i >> 4];
*k++ = "0123456789ABCDEF"[*i & 0x0F];
}
return s;
}
vector<unsigned char> read_file(const string& name) {
ifstream f(name, ios_base::binary);
vector<unsigned char> ret;
const int N = 64 * 1024;
vector<char> buf;
while (f) {
buf.resize(N);
f.read(&buf[0], N);
buf.resize(static_cast<size_t>(f.gcount()));
ret.insert(ret.end(), buf.begin(), buf.end());
}
return ret;
}
void read_batch_file(const string& batch_file_name, bimap<string, string>& hash_file) {
ifstream batch(batch_file_name);
const regex r("@REM ([[:xdigit:]]{64}) (.+)");
for (string s; getline(batch, s); ) {
smatch m;
if (regex_match(s, m, r)) {
hash_file.insert(bimap<string, string>::value_type(m[1], m[2]));
}
}
}
void write_batch_file(const string& batch_file_name, bimap<string, string>& hash_file) {
ofstream batch(batch_file_name, ios_base::app);
for (recursive_directory_iterator i(initial_path()), end; i != end; ++i) {
const string f = i->path().string().substr(initial_path().string().size() + 1);
if (!is_regular_file(i->path()) || f == batch_file_name || hash_file.right.find(f) != hash_file.right.end()) {
continue;
}
const string h = hexify(sha256(read_file(f)));
const bimap<string, string>::left_const_iterator j = hash_file.left.find(h);
if (j == hash_file.left.end()) {
hash_file.insert(bimap<string, string>::value_type(h, f));
batch << "@REM " << h << " " << f << endl;
} else {
cout << "del \"" << f << "\"" << endl;
batch << "@copy \"" << j->second << "\" \"" << f << "\"" << endl;
}
}
batch << "@REM **********" << endl;
}
int main() {
const string batch_file_name("reduplicate.bat");
bimap<string, string> hash_file;
read_batch_file(batch_file_name, hash_file);
write_batch_file(batch_file_name, hash_file);
}