-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cpp
155 lines (134 loc) · 3.98 KB
/
Worker.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
#include "Worker.h"
using namespace std;
WorkerThread::WorkerThread(string& directoryPath, string& hashStorageFile) : accumulator(suon::SUON(suon::storType::ASSOC)), dirToHash{ directoryPath }, hashFile{ hashStorageFile } {
numChanges = 0;
if (!filesystem::exists(filesystem::path(directoryPath)))
throw bad_exception();
if (!filesystem::exists(filesystem::path(hashStorageFile)))
throw exception();
scan = directoryPath.c_str();
store = hashStorageFile.c_str();
};
WorkerThread::WorkerThread(const char* directoryPath, const char* hashStorageFile) : accumulator(suon::SUON(suon::storType::ASSOC)), dirToHash{ directoryPath }, hashFile{ hashStorageFile } {
numChanges = 0;
if (!filesystem::exists(filesystem::path(directoryPath)))
throw bad_exception();
if (!filesystem::exists(filesystem::path(hashStorageFile)))
throw exception();
scan = directoryPath;
store = hashStorageFile;
};
WorkerThread::WorkerThread(QString directoryPath, QString hashStorageFile) : accumulator(suon::SUON(suon::storType::ASSOC)) {
dirToHash = directoryPath.toStdString();
hashFile = hashStorageFile.toStdString();
numChanges = 0;
if (!filesystem::exists(filesystem::path(dirToHash)))
throw bad_exception();
if (!filesystem::exists(filesystem::path(hashFile)))
throw exception();
scan = directoryPath;
store = hashStorageFile;
}
vector<string> WorkerThread::run(const bool& dumpToFile) {
return privateRun(dumpToFile);
}
size_t WorkerThread::size() const {
return numChanges;
};
vector<string> WorkerThread::privateRun(const bool& dumpToFile) {
suon::SUON currentHashes = hashDirectory(dirToHash);
vector<string> changes;
ofstream logfile = ofstream("C:/Users/Aidan/Desktop/log.log", std::ios::app);
try {
suon::SUON test(suon::storType::ASSOC);
test.loadFromFile(hashFile);
for (pair<string, pair<string, string>> i; currentHashes.hasNext(); i = currentHashes.next()) {
logfile << i.first << "\t" << i.second.second << "\n";
string hashAgainst = test[i.first];
if (i.second.second != hashAgainst) {
if (i.first == "")
continue;
changes.push_back(i.first);
}
}
/*for (pair<string, pair<string, string>> i; test.hasNext(); i = test.next()) {
string hashAgainst = currentHashes[i.first];
if (hashAgainst == suon::nfound) {
cout << i.first << " was deleted" << endl;
}
}*/
numChanges = changes.size();
#
if (dumpToFile)
currentHashes.dumpToFile(hashFile);
}
catch (...) {
if (dumpToFile)
currentHashes.dumpToFile(hashFile);
return vector<string>({ "GIVEN HASH FILE NOT FOUND" });
}
emit changedFilesReady(changes);
return changes;
}
suon::SUON WorkerThread::hashDirectory(const string& dp) {
size_t hashedFiles = 0;
filesystem::recursive_directory_iterator iterator(dp);
int noaccess = 0;
long long ct = 0;
filesystem::recursive_directory_iterator traversalForCount(dp);
while (true) {
try {
for (const auto& entry : traversalForCount) {
if (!filesystem::is_directory(entry)) {
ct++;
}
}
break;
}
catch (filesystem::filesystem_error ex) {
auto code = ex.code();
iterator.increment(code);
}
catch (system_error ex) {
auto code = ex.code();
iterator.increment(code);
}
}
emit totalFiles(ct);
while (true) {
try {
for (const auto& entry : iterator) {
emit hashedFileCountIncrement(hashedFiles);
if (!filesystem::is_directory(entry)) {
hashSingleFile(entry.path().u8string());
hashedFiles++;
}
}
break;
}
catch (filesystem::filesystem_error ex) {
noaccess++;
auto code = ex.code();
iterator.increment(code);
emit fileError(noaccess);
}
catch (system_error ex) {
auto code = ex.code();
iterator.increment(code);
}
}
return accumulator;
}
void WorkerThread::hashSingleFile(const string& fp) {
ifstream reader(fp);
SHA256 hasher;
char content;
while (reader.good()) {
content = reader.get();
hasher.add(&content, sizeof(content));
}
string hash = hasher.getHash();
reader.close();
pair<string, string> hashAppend(fp, hash);
accumulator.add(hashAppend);
}