forked from theArcticOcean/CLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckFolderChange.cpp
98 lines (93 loc) · 2.63 KB
/
CheckFolderChange.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
#include <iostream>
#include <string>
#include <numeric>
#include <set>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <QJsonDocument>
#include <QByteArray>
#include <QFile>
#include <QTextStream>
#include <QJsonObject>
#include <QDebug>
#include <QJsonParseError>
#include <QDir>
#include <QDebug>
#include <QCryptographicHash>
using namespace std;
QList<QByteArray> sha256List1;
QList<QByteArray> sha256List2;
void CheckShaSum( const QDir &dir )
{
QFileInfoList entries = dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot, QDir::Name);
for( auto fileInfo: entries )
{
if( fileInfo.isDir() )
{
CheckShaSum( QDir( fileInfo.absoluteFilePath() ) );
}
else
{
QFile file( fileInfo.filePath() );
if( file.open( QIODevice::ReadOnly ) )
{
QByteArray bytes = file.readAll();
// the check sum is `sha256 result` + `file path string`
QByteArray hashValue = QCryptographicHash::hash( bytes, QCryptographicHash::Sha256 );
hashValue += fileInfo.filePath();
sha256List1.push_back( hashValue );
file.close();
}
}
}
}
int main(int argc, char** argv)
{
QString path = "/Users/weiyang/Desktop/files";
CheckShaSum( QDir( path ) );
for( auto it: sha256List1 )
{
qDebug() << it;
}
for( int i = 0; i < sha256List1.size(); ++i )
{
for( int j = i+1; j < sha256List1.size(); ++j )
{
if( sha256List1[i] == sha256List1[j] )
{
qDebug() << "there is same file";
}
}
}
/* // change txt1
QFile file( "/Users/weiyang/Desktop/files/txt1" );
if( file.open( QIODevice::ReadWrite ) )
{
QTextStream istream( &file );
istream << "world";
file.close();
}
for( int i = 0; i < entries.size(); ++i )
{
QFileInfo fileInfo = entries[i];
if( "." != fileInfo.fileName() && ".." != fileInfo.fileName() )
{
qDebug() << fileInfo.filePath();
QFile file( fileInfo.filePath() );
if( file.open( QIODevice::ReadOnly ) )
{
QByteArray bytes = file.readAll();
QByteArray hashValue = QCryptographicHash::hash( bytes, QCryptographicHash::Sha256 );
qDebug() << hashValue;
sha256List2.push_back( hashValue );
file.close();
}
}
}
for( int i = 0; i < sha256List1.size(); ++i )
{
qDebug() << ( sha256List1[i] == sha256List2[i] );
} */
return 0;
}