aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-01-22 14:01:34 +0000
committersaturneric <[email protected]>2024-01-22 14:01:34 +0000
commit0ff4345f4ce20be2f0d20a597da324d4f4855999 (patch)
tree0ef6472ac1f0fb87b5c42cce7344652affc2a3f1
parentfix: QStringList is not equal to QList<QVector> (diff)
downloadGpgFrontend-0ff4345f4ce20be2f0d20a597da324d4f4855999.tar.gz
GpgFrontend-0ff4345f4ce20be2f0d20a597da324d4f4855999.zip
fix: stop reading all data into memory at file hash calculation
-rw-r--r--src/core/utils/IOUtils.cpp82
-rw-r--r--src/ui/widgets/FileTreeView.cpp22
2 files changed, 66 insertions, 38 deletions
diff --git a/src/core/utils/IOUtils.cpp b/src/core/utils/IOUtils.cpp
index 54fbb6c9..1541d726 100644
--- a/src/core/utils/IOUtils.cpp
+++ b/src/core/utils/IOUtils.cpp
@@ -29,9 +29,23 @@
#include "IOUtils.h"
#include "core/GpgModel.h"
+#include "core/utils/FilesystemUtils.h"
namespace GpgFrontend {
+auto GetFileChecksum(const QString& file_name,
+ QCryptographicHash::Algorithm hashAlgorithm)
+ -> QByteArray {
+ QFile f(file_name);
+ if (f.open(QFile::ReadOnly)) {
+ QCryptographicHash hash(hashAlgorithm);
+ if (hash.addData(&f)) {
+ return hash.result();
+ }
+ }
+ return {};
+}
+
auto ReadFile(const QString& file_name, QByteArray& data) -> bool {
QFile file(file_name);
if (!file.open(QIODevice::ReadOnly)) {
@@ -72,45 +86,41 @@ auto CalculateHash(const QString& file_path) -> QString {
QTextStream ss(&buffer);
if (info.isFile() && info.isReadable()) {
- ss << "[#] " << QObject::tr("File Hash Information") << Qt::endl;
- ss << " " << QObject::tr("filename") << QObject::tr(": ")
+ ss << "# " << QObject::tr("File Hash Information") << Qt::endl;
+ ss << "- " << QObject::tr("Filename") << QObject::tr(": ")
<< info.fileName() << Qt::endl;
- QFile f(info.filePath());
- if (f.open(QFile::ReadOnly)) {
- // read all data
- auto buffer = f.readAll();
- ss << " " << QObject::tr("file size(bytes)") << QObject::tr(": ")
- << buffer.size() << Qt::endl;
-
- // md5
- auto hash_md5 = QCryptographicHash(QCryptographicHash::Md5);
- hash_md5.addData(buffer);
- auto md5 = hash_md5.result().toHex();
- GF_CORE_LOG_DEBUG("md5 {}", md5);
- ss << " "
- << "md5" << QObject::tr(": ") << md5 << Qt::endl;
-
- // sha1
- auto hash_sha1 = QCryptographicHash(QCryptographicHash::Sha1);
- hash_sha1.addData(buffer);
- auto sha1 = hash_sha1.result().toHex();
- GF_CORE_LOG_DEBUG("sha1 {}", sha1);
- ss << " "
- << "sha1" << QObject::tr(": ") << sha1 << Qt::endl;
-
- // sha1
- auto hash_sha256 = QCryptographicHash(QCryptographicHash::Sha256);
- hash_sha256.addData(buffer);
- auto sha256 = hash_sha256.result().toHex();
- GF_CORE_LOG_DEBUG("sha256 {}", sha256);
- ss << " "
- << "sha256" << QObject::tr(": ") << sha256 << Qt::endl;
-
- ss << Qt::endl;
- }
+ // read all data
+ ss << "- " << QObject::tr("File Size (bytes)") << QObject::tr(": ")
+ << QString::number(info.size()) << Qt::endl;
+
+ ss << "- " << QObject::tr("File Size") << QObject::tr(": ")
+ << GetHumanFriendlyFileSize(info.size()) << Qt::endl;
+
+ // md5
+ ss << "- "
+ << "MD5" << QObject::tr(": ")
+ << GetFileChecksum(file_path, QCryptographicHash::Md5).toHex()
+ << Qt::endl;
+
+ // sha1
+ ss << "- "
+ << "SHA1" << QObject::tr(": ")
+ << GetFileChecksum(file_path, QCryptographicHash::Sha1).toHex()
+ << Qt::endl;
+
+ // sha1
+ ss << "- "
+ << "SHA256" << QObject::tr(": ")
+ << GetFileChecksum(file_path, QCryptographicHash::Sha256).toHex()
+ << Qt::endl;
+
+ ss << Qt::endl;
+
} else {
- ss << "[#] " << QObject::tr("Error in Calculating File Hash ") << Qt::endl;
+ ss << "# " << QObject::tr("Error: cannot read target file") << Qt::endl;
+ ss << "- " << QObject::tr("Filename") << QObject::tr(": ")
+ << info.fileName() << Qt::endl;
}
return ss.readAll();
diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp
index 41cd9aff..0c299a97 100644
--- a/src/ui/widgets/FileTreeView.cpp
+++ b/src/ui/widgets/FileTreeView.cpp
@@ -28,8 +28,10 @@
#include "FileTreeView.h"
+#include "core/utils/AsyncUtils.h"
#include "core/utils/IOUtils.h"
#include "ui/UISignalStation.h"
+#include "ui/UserInterfaceUtils.h"
namespace GpgFrontend::UI {
@@ -372,8 +374,24 @@ void FileTreeView::slot_show_custom_context_menu(const QPoint& point) {
}
void FileTreeView::slot_calculate_hash() {
- emit UISignalStation::GetInstance()->SignalRefreshInfoBoard(
- CalculateHash(this->GetSelectedPath()), InfoBoardStatus::INFO_ERROR_OK);
+ CommonUtils::WaitForOpera(
+ this->parentWidget(), tr("Calculating"), [=](const OperaWaitingHd& hd) {
+ RunOperaAsync(
+ [=](DataObjectPtr data_object) {
+ data_object->Swap({CalculateHash(this->GetSelectedPath())});
+ return 0;
+ },
+ [hd](int rtn, DataObjectPtr data_object) {
+ hd();
+ if (rtn < 0 || !data_object->Check<QString>()) {
+ return;
+ }
+ auto result = ExtractParams<QString>(data_object, 0);
+ emit UISignalStation::GetInstance()->SignalRefreshInfoBoard(
+ result, InfoBoardStatus::INFO_ERROR_OK);
+ },
+ "calculate_file_hash");
+ });
}
void FileTreeView::slot_compress_files() {}