Index: sources/storage/FileHandler.cpp =================================================================== diff -u -r702c25ffe096fb2c8c8e5b344b25759b1c218bce -r5687815256ae070a9a207107088e3f72dd464da0 --- sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 702c25ffe096fb2c8c8e5b344b25759b1c218bce) +++ sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 5687815256ae070a9a207107088e3f72dd464da0) @@ -7,7 +7,7 @@ * * \file FileHandler.cpp * \author (last) Behrouz NematiPour - * \date (last) 14-Sep-2023 + * \date (last) 23-Apr-2024 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * @@ -19,6 +19,7 @@ #include #include #include +#include // Project @@ -97,7 +98,7 @@ { QFile file(vFileName); if (! file.open(QFile::Text | QFile::ReadOnly)) { - QString msg = QString("Cannot open file for write (%1). Possible corrupted file system").arg(vFileName); + QString msg = QString("Cannot open file for read (%1). Possible corrupted file system").arg(vFileName); // here cannot use LOG_XXXX because if the folder cannot be created then the log cannot be written. errOut (msg); return false; @@ -113,29 +114,20 @@ } /*! - * \brief FileHandler::read - * \details reads the provided filename's JSON content into the vContent variable. - * \param vFileName - Source file name - * \param vContent - The content of the file which will be written to when done. - * \return false if file cannot be read or parsed + * \brief FileHandler::backupFile + * \details Calls the gzip command + * the file is compressed and will be deleted by gzip + * the destination file is determined by gzip + * \param vSource - file to be compressed + * \return non-zero if successfull. */ -bool FileHandler::read(const QString &vFileName, QJsonObject &vContent, QJsonParseError *error) +int FileHandler::backupFile(const QString &vSource) { - QFile file(vFileName); - if (! file.open(QFile::Text | QFile::ReadOnly)) { - errOut(QObject::tr("Cannot open file for read (%1). Possible corrupted file system.").arg(vFileName)); - return false; - } - QTextStream in(&file); - QString content = in.readAll(); - QJsonParseError jsonParseError; - QJsonDocument doc = QJsonDocument::fromJson(content.toUtf8(), &jsonParseError); - if (jsonParseError.error) { - if (error) *error = jsonParseError; - return false; - } - vContent = doc.object(); - return true; + QString cmd = "gzip"; + QStringList arguments; + arguments << vSource; + int result = QProcess::execute(cmd, arguments); + return result; } /*! @@ -464,3 +456,22 @@ QFileInfo fileInfo(vFilePath); return fileInfo.canonicalFilePath() != fileInfo.filePath(); } + +/*! + * \brief FileHandler::sha256sum + * \param vFileName - the file name including path to generate the sha256sum for + * \param vOk - if used will contain the success/true, fail/false of the checksum generation + * \return The checksum result in hex sting. + */ +QString FileHandler::sha256sum(QString vFileName, bool *vOk) { + bool ok = true; + QByteArray shasum; + QCryptographicHash hash(QCryptographicHash::Sha256); + QFile file(vFileName); + if ( ! file.open(QIODevice::ReadOnly)) { ok = false; goto lOut; } + hash.addData(&file); + shasum = hash.result().toHex(); +lOut: + if (vOk) *vOk = ok; + return shasum; +}