-
Notifications
You must be signed in to change notification settings - Fork 1
/
folderchecksum.cpp
201 lines (175 loc) · 4.74 KB
/
folderchecksum.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
#include "folderchecksum.h"
#include <QCryptographicHash>
#include <QDir>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
QMap<QString, QString> FolderCheckSum::checkSums() const
{
return _checkSums;
}
QString FolderCheckSum::removePath(const QString &path)
{
QString ret = path;
if (ret.startsWith(_path))
ret = ret.remove(0, _path.length());
if (ret.startsWith("/"))
ret = ret.remove(0, 1);
return ret;
}
QList<QString> FolderCheckSum::files() const
{
return _files;
}
QByteArray FolderCheckSum::toByteArray()
{
QJsonDocument doc;
QJsonArray filesArray;
foreach (QString f, _files)
filesArray.append(f);
QJsonObject hashObject;
QMapIterator<QString, QString> i(_checkSums);
while (i.hasNext()) {
i.next();
hashObject.insert(i.key(), QJsonValue(i.value()));
}
QJsonObject mainObject;
mainObject.insert("files", filesArray);
mainObject.insert("hashes", hashObject);
doc.setObject(mainObject);
return doc.toJson();
}
FolderCheckSum *FolderCheckSum::fromDir(const QString &path)
{
FolderCheckSum *r = new FolderCheckSum;
r->checkFolder(path);
return r;
}
FolderCheckSum *FolderCheckSum::fromByteArray(const QByteArray &array)
{
FolderCheckSum *r = new FolderCheckSum;
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonArray filesArray = doc.object().value("files").toArray();
foreach (QJsonValue v, filesArray)
r->_files.append(v.toString());
QJsonObject hashObject = doc.object().value("hashes").toObject();
foreach (QString key, hashObject.keys())
r->_checkSums.insert(key, hashObject[key].toString());
return r;
}
FolderCheckSum::FolderCheckSum()
{
}
FolderCheckSum::FolderCheckSum(QString path)
{
_path = path;
if (!_path.endsWith("/"))
_path.append("/");
}
void FolderCheckSum::calc()
{
checkFolder(_path);
}
void FolderCheckSum::checkFolder(const QString &p)
{
qDebug() << "Checking dir" << p;
QDir dir(p);
QStringList files = dir.entryList(QDir::Files);
foreach (QString f, files)
fileChecksum(p + "/" + f);
QStringList dirs = dir.entryList(QDir::Dirs);
foreach (QString d, dirs)
if (d != "." && d != "..")
checkFolder(p + "/" + d);
}
QByteArray FolderCheckSum::fileChecksum(const QString &fileName)
{
qDebug() << "Checking file" << fileName;
QFile f(fileName);
if (f.open(QFile::ReadOnly)) {
QCryptographicHash hash(QCryptographicHash::Sha224);
if (hash.addData(&f)) {
_files.append(fileName);
_checkSums[removePath(fileName)] = hash.result();
return hash.result();
}
}
qDebug() << " * NO HASH";
return QByteArray();
}
FolderInfo::FolderInfo()
{
}
FolderInfo *FolderInfo::fromDir(const QString &path)
{
FolderInfo *ret = new FolderInfo;
ret->checkFolder(path);
return ret;
}
FolderInfo *FolderInfo::fromJson(const QByteArray &array)
{
qDebug() << "reading from json"<<array;
FolderInfo *r = new FolderInfo;
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonObject o = doc.object();
foreach (QString key, o.keys()) {
FileInfo fi;
QJsonObject v = o.value(key).toObject();
fi.size = v.value("size").toInt();
fi.hash = v.value("hash").toString();
r->insert(key, fi);
}
return r;
}
QByteArray FolderInfo::toByteArray()
{
QJsonDocument doc;
QJsonObject mainObject;
const_iterator i;
for(i = begin(); i != end(); ++i) {
QJsonObject obj
{
{"size", QJsonValue::fromVariant((*i).size)},
{"hash", (*i).hash}
};
mainObject.insert(i.key(), obj);
}
doc.setObject(mainObject);
return doc.toJson();
}
QString FolderInfo::removePath(const QString &path)
{
QString ret = path;
if (ret.startsWith(_path))
ret = ret.remove(0, _path.length());
if (ret.startsWith("/"))
ret = ret.remove(0, 1);
return ret;
}
void FolderInfo::checkFolder(const QString &p)
{
qDebug() << "Checking dir" << p;
QDir dir(p);
QStringList files = dir.entryList(QDir::Files);
foreach (QString f, files)
checkFile(p + "/" + f);
QStringList dirs = dir.entryList(QDir::Dirs);
foreach (QString d, dirs)
if (d != "." && d != "..")
checkFolder(p + "/" + d);
}
void FolderInfo::checkFile(const QString &p)
{
FileInfo fi;// = new FileInfo;
QFile f(p);
if (f.open(QFile::ReadOnly)) {
fi.size = f.size();
// fi.fullPath = p;
QCryptographicHash hash(QCryptographicHash::Sha224);
if (hash.addData(&f)) {
fi.hash = QString(hash.result());
this->insert(removePath(p), fi);
}
}
}