launcher/fs-tools.cpp

73 lines
1.7 KiB
C++

#include "fs-tools.h"
void emptyFolder(const QString& path)
{
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(".");
return;
}
QFileInfoList entries = dir.entryInfoList(
QDir::NoDotAndDotDot |
QDir::AllEntries
);
for (const QFileInfo &entry : entries) {
if (entry.isDir()) {
QDir(entry.absoluteFilePath()).removeRecursively();
} else {
QFile::remove(entry.absoluteFilePath());
}
}
}
QString checkFileHash(QString pathname)
{
QFile file(pathname);
if (!file.open(QFile::ReadOnly))
return QString();
QCryptographicHash hash(QCryptographicHash::Sha256);
while (!file.atEnd())
hash.addData(file.read(8192));
return hash.result().toHex();
}
QString checkDirHash(QString path) {
QCryptographicHash hash(QCryptographicHash::Sha256);
QDir dir(path);
if (!dir.exists()) {
qDebug() << "Directory not exists:" << path;
return QString();
}
QFileInfoList fileList = dir.entryInfoList(
QDir::Files | QDir::NoSymLinks | QDir::AllDirs | QDir::NoDotAndDotDot,
QDir::Name | QDir::DirsFirst
);
for (const QFileInfo &info : fileList) {
if (info.isDir()) {
QString subHash = checkDirHash(info.absoluteFilePath());
hash.addData(subHash.toUtf8());
} else if (info.isFile()) {
QFile file(info.absoluteFilePath());
if (file.open(QIODevice::ReadOnly)) {
while (!file.atEnd()) {
QByteArray chunk = file.read(8192);
hash.addData(chunk);
}
file.close();
}
}
}
return QString(hash.result().toHex());
}