/
dirgrinder.cpp
164 lines (134 loc) · 4.98 KB
/
dirgrinder.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
#include <QCoreApplication>
#include <QDir>
#include <QCryptographicHash>
#include <iostream>
#include "dirgrinder.h"
#include "sqlgrinder.h"
#include <QCoreApplication>
#include <QStringList>
#include <iostream>
#include <QTextStream>
#include <QDebug>
#include <QDate>
#include <QtSql>
QMultiMap<QString,FileAttributes> ListFilesInDirectoryTest(QDir dir, bool Hash)
{
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
qt_ntfs_permission_lookup++; // turn checking on
QMultiMap<QString, FileAttributes> fileAttHashTable; //making hash table to store file attributes
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Name);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
QFileInfo fileInfo = list.at(i);
if (fileInfo.isFile())
{
FileAttributes tempFileAttributes;
QDateTime date = fileInfo.lastModified();
QString lastModified = date.toString();
tempFileAttributes.absoluteFilePath = fileInfo.absoluteFilePath();
tempFileAttributes.fileName = fileInfo.fileName();
tempFileAttributes.filePath= fileInfo.path();
if (Hash) tempFileAttributes.md5Hash = GetFileMd5hash(fileInfo.absoluteFilePath());
tempFileAttributes.lastModified = fileInfo.lastModified();
tempFileAttributes.lastRead = fileInfo.lastRead();
tempFileAttributes.created = fileInfo.created();
tempFileAttributes.isHidden = fileInfo.isHidden();
tempFileAttributes.size = fileInfo.size();
tempFileAttributes.owner = fileInfo.owner();
fileAttHashTable.insert(fileInfo.absoluteFilePath(),tempFileAttributes);
}
}
return fileAttHashTable;
}
QString GetFileMd5hash(QString path)
{
QFile file(path);
if(file.open(QIODevice::ReadOnly))
{
//reading hash file is limited to 20MB = 2621440 bytes
QString hash = QCryptographicHash::hash(file.read(2621440), QCryptographicHash::Md5).toHex().constData();
file.close();
//std::cout << hash.toStdString() << "\n";
return hash;
}
return "error geting md5 hash";
}
void ListContentOfDirectory(QDir dir, bool Recurse, bool Hash)
{
//ListFilesInDirectory(dir);
dir.setFilter( QDir::Hidden | QDir::NoSymLinks |QDir::AllDirs );
dir.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
QFileInfo fileInfo = list.at(i);
QString str1 = fileInfo.fileName();
QString str2 = ".";
QString str3 = "..";
if (!str1.compare(str2) || !str1.compare(str3) )
{
// std::cout << " found . or .. ";
}
else
{
ListFilesInDirectory(dir,Hash);
QDir NextDir(fileInfo.absoluteFilePath());
if (Recurse)
{
ListContentOfDirectory(NextDir,Recurse,Hash);
ListFilesInDirectory(dir,Hash);
}
}
}
}
void ListFilesInDirectory(QDir dir, bool Hash)
{
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
QFileInfo fileInfo = list.at(i);
if (fileInfo.isFile())
{
QDateTime date = fileInfo.lastModified();
QString lastModified = date.toString();
std::cout << qPrintable(QString("%1 lastModified=%2 ").arg(fileInfo.absoluteFilePath()).arg(lastModified)) << std::endl;
if (Hash) GetFileMd5hash(fileInfo.absoluteFilePath());
}
}
}
void scanDir(QDir dir, QSqlDatabase db, QDateTime currentDateTime )
{
// dir.setNameFilters(QStringList("*.nut"));
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks| QDir::Hidden );
QStringList fileList = dir.entryList();
QMultiMap<QString,FileAttributes> temp = ListFilesInDirectoryTest(dir.path(),0);
copyFilesAttHashTableToSqlLite(temp,db,currentDateTime);
dir.setFilter(QDir::AllDirs | QDir::Hidden | QDir::NoDotAndDotDot| QDir::NoSymLinks);
QStringList dirList = dir.entryList();
for (int i=0; i<dirList.size(); ++i)
{
QString newPath = QString("%1/%2").arg(dir.absolutePath()).arg(dirList.at(i));
scanDir(QDir(newPath),db,currentDateTime);
}
}
// ListDirectory list for given path
void ListDirectory(QDir dir, bool Recurse)
{
dir.setFilter( QDir::Hidden | QDir::NoSymLinks |QDir::AllDirs );
dir.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
QFileInfo fileInfo = list.at(i);
if (fileInfo.isFile())
{
std::cout << qPrintable(QString("%1").arg(fileInfo.absoluteFilePath())) << std::endl;
QDir NextDir(fileInfo.absoluteFilePath());
if (Recurse) ListDirectory(NextDir,1);
}
}
}