Index: sources/storage/FileHandler.cpp =================================================================== diff -u -r340cae5888596d927601687476618d0ede343a35 -r5687815256ae070a9a207107088e3f72dd464da0 --- sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 340cae5888596d927601687476618d0ede343a35) +++ sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 5687815256ae070a9a207107088e3f72dd464da0) @@ -1,13 +1,13 @@ /*! * - * Copyright (c) 2020-2023 Diality Inc. - All Rights Reserved. + * Copyright (c) 2020-2024 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file FileHandler.cpp * \author (last) Behrouz NematiPour - * \date (last) 18-Jul-2023 + * \date (last) 23-Apr-2024 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * @@ -19,12 +19,15 @@ #include #include #include +#include // Project // #include "Logger.h" // logger should not be used in this class. #include "StorageGlobals.h" +extern QString gStandard_tmp; + // namespace using namespace Storage; @@ -95,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; @@ -111,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; } /*! @@ -304,7 +298,7 @@ static bool tested = false; if ( tested ) return ok; - QString tmp = Storage::Standard_tmp; + QString tmp = gStandard_tmp; QString tmpTestFolder = tmp + "tmp_test_folder" ; QString tmpTestFile = tmp + "tmp_test_file" ; QString tmpTestContent = "tmp_test_content"; @@ -462,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; +}