Index: sources/storage/FileHandler.cpp =================================================================== diff -u -r3f60b9a1c0b3a43ec1c5c72955dce0d80354e35c -rcf51c19d82d667644d7f70eaa521b43f908c7068 --- sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 3f60b9a1c0b3a43ec1c5c72955dce0d80354e35c) +++ sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision cf51c19d82d667644d7f70eaa521b43f908c7068) @@ -1,31 +1,29 @@ /*! * - * Copyright (c) 2020-2022 Diality Inc. - All Rights Reserved. + * Copyright (c) 2020-2023 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) 10-Aug-2021 + * \date (last) 22-Jan-2023 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * */ #include "FileHandler.h" //Qt -#include #include #include #include -#include -#include #include // Project // #include "Logger.h" // logger should not be used in this class. +#include "StorageGlobals.h" // namespace using namespace Storage; @@ -42,17 +40,17 @@ { static uint count; static QString mCritical; - // coco begin validated : This has been manually test. Needs file system access to produce errors for hundred times. + // disabled coco begin validated : This has been manually test. Needs file system access to produce errors for hundred times. if (mCritical != vMessage || !(count % 1000)) { - // coco end + // disabled coco end count = 0; mCritical = vMessage; QTextStream err(stderr); err << "FS" << " " << QDate::currentDate().toString("yyyy_MM_dd") << " " << QTime::currentTime().toString("HH:mm:ss" ) << " " << mCritical - << endl; + << Qt::endl; } ++count; } @@ -72,14 +70,14 @@ QFile::OpenMode openMode = vAppend ? QFile::Text | QFile::Append : QFile::Text | QFile::WriteOnly; - // coco begin validated : This has been manually test. Needs file system access to make file the way it cannot be opened for writing. + // disabled coco begin validated : This has been manually test. Needs file system access to make file the way it cannot be opened for writing. if (! file.open(openMode)) { QString msg = QString("Cannot open file for write (%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; } - // coco end + // disabled coco end QTextStream out(&file); out << vContent; out.flush(); @@ -93,7 +91,7 @@ * \param vContent - The content of the file which will be set when done. * \return false if file cannot be opened. */ -bool FileHandler::read(const QString &vFileName, QString &vContent) +bool FileHandler::read(const QString &vFileName, QString &vContent, bool vAppend) { QFile file(vFileName); if (! file.open(QFile::Text | QFile::ReadOnly)) { @@ -103,7 +101,12 @@ return false; } QTextStream in(&file); - vContent = in.readAll(); + if ( vAppend ) { + vContent += in.readAll(); + } + else { + vContent = in.readAll(); + } return true; } @@ -144,15 +147,15 @@ */ int FileHandler::copyFolder(const QString &vSource, const QString &vDestination) { - // coco begin validated: This needs user interaction to export to USB device + // disabled coco begin validated: This needs user interaction to export to USB device // has been tested manually since currently it is the only place it has been used. QString cmd = "cp"; QStringList arguments; arguments << "-r" << vSource << vDestination; int result = QProcess::execute(cmd, arguments); return result; } -// coco end +// disabled coco end /*! * \brief FileHandler::moveFolder @@ -164,15 +167,15 @@ */ int FileHandler::moveFolder(const QString &vSource, const QString &vDestination) { - // coco begin validated: This needs user interaction to check the file system + // disabled coco begin validated: This needs user interaction to check the file system // has been tested manually since currently it is the only place it has been used. QString cmd = "mv"; QStringList arguments; arguments << vSource << vDestination; int result = QProcess::execute(cmd, arguments); return result; } -// coco end +// disabled coco end /*! * \brief FileHandler::removeFolder @@ -185,15 +188,15 @@ */ int FileHandler::removeFolder(const QString &vFolder) { - // coco begin validated: This needs user interaction to check the file system + // disabled coco begin validated: This needs user interaction to check the file system // has been tested manually since currently it is the only place it has been used. QString cmd = "rm"; QStringList arguments; arguments << "-frd" << vFolder; int result = QProcess::execute(cmd, arguments); return result; } -// coco end +// disabled coco end /*! * \brief FileHandler::removeFiles @@ -214,7 +217,7 @@ for (const auto &info : infoList) { QDateTime fileTime = info.lastModified(); QString fileName = info.absoluteFilePath(); - // coco begin validated : This has been manually tested since requires to change in file system to reproduce the error. + // disabled coco begin validated : This has been manually tested since requires to change in file system to reproduce the error. if (fileTime.isValid()) { if (fileTime.date() <= vDateOlderThan) { if (QFile::remove(fileName)) { @@ -229,7 +232,7 @@ else { errOut(QString("Cannot get last modified date of file : ") + fileName); } - // coco end + // disabled coco end } } return countRemoved; @@ -263,7 +266,6 @@ */ bool FileHandler::isMounted(const QString &vPath, bool *vIsReadOnly) { - // coco begin validated: Needed User Interaction to make the device not ready so tested manually bool mounted = false; // removing the extra '/' from the vPath if there is to be able to compare to the root path of the storage QString path = vPath.trimmed(); @@ -272,7 +274,11 @@ if (path.at(lastIndex) == "/") path.remove(lastIndex, 1); // check to see if the path in the list of mounted rootPaths /// DEBUG: qDebug() << " +++++ " << QStorageInfo::mountedVolumes(); - foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) { + // FIXME : This function blocks the Device controller thread + // It has been observed during the USB plug test and getting the drive info(space) that + // immediately after the mount this function laggs to get the information for about 2-5 sec. + auto mountedVolumes = QStorageInfo::mountedVolumes(); + foreach (const QStorageInfo &storage, mountedVolumes) { if (storage.isValid() && storage.isReady()) { if ( storage.rootPath() == path ) { if (vIsReadOnly) *vIsReadOnly = storage.isReadOnly(); @@ -283,9 +289,36 @@ } return mounted; } -// coco end /*! + * \brief FileHandler::tmpUsable + * \details Checks if the temp folder is available for read and write to file and directory + * \note This function will only chek the temp folder usability once + * and next call will just return the first result, + * assuming that the temp file situation is not going to change and is stable. + * \return true on success + */ +bool FileHandler::tmpUsable() +{ + static bool ok = false; + static bool tested = false; + if ( tested ) return ok; + + QString tmp = Storage::Standard_tmp; + QString tmpTestFolder = tmp + "tmp_test_folder" ; + QString tmpTestFile = tmp + "tmp_test_file" ; + QString tmpTestContent = "tmp_test_content"; + QDir dir(tmp); + if ( ! dir.exists() ) { ok = false; goto lOut; } + if ( ! FileHandler::makeFolder (tmpTestFolder )) { ok = false; goto lOut; } + if ( ! FileHandler::write (tmpTestFile , tmpTestContent )) { ok = false; goto lOut; } + if ( ! FileHandler::read (tmpTestFolder, tmpTestContent )) { ok = false; goto lOut; } +lOut: + tested = true; + return ok; +} + +/*! * \brief FileHandler::find * \details The function to find files. * It mainly been implemented to find the files are using some amount of total storage in the vPath by percentage, @@ -302,7 +335,7 @@ * if vRetainPercent is used then it contains list of the file(s) to be removed. */ QFileInfoList FileHandler::find(const QString &vPath, QStringList vNameFilters, quint8 vRetainPercent) { - // coco begin validated: Manually tested. Needs to fill up the storage to test some functionalities like vRetainPercent + // disabled coco begin validated: Manually tested. Needs to fill up the storage to test some functionalities like vRetainPercent QFileInfoList fileInfoList; // if all the files need to retain then no file shall be listed in the remove list. @@ -338,8 +371,8 @@ // checks if the total files size subtracted by the current file size will be less that limit // if it is breaks // else adds the file to the list and continues. - qDebug() << "%" << totalSizeStorage << totalSizeFiles << totalSizeRetain << totalSizeFiles - totalSizeRetain << vRetainPercent; - quint64 totalSizeRemoved = 0; + // DEBUG: qDebug() << "%" << totalSizeStorage << totalSizeFiles << totalSizeRetain << totalSizeFiles - totalSizeRetain << vRetainPercent; + quint64 totalSizeRemoved = 0; Q_UNUSED(totalSizeRemoved); for (auto it = fileInfoListAll.crbegin(); it != fileInfoListAll.crend(); ++it) { // (totalSizeFiles <= totalSizeRetain) has been checked above and didn't return; , // so at least one file should be checked and then check again in the loop. @@ -366,7 +399,7 @@ // ; return fileInfoList; } -// coco end +// disabled coco end /*! * \brief FileHandler::find @@ -377,7 +410,7 @@ * if vRetainPercent is used then it contains list of the file(s) to be removed. */ QFileInfoList FileHandler::find(const QString &vPath, QStringList vNameFilters) { - // coco begin validated: Needs to manually create specific folder with specific files to check the functionality + // disabled coco begin validated: Needs to manually create specific folder with specific files to check the functionality // manually tested QFileInfoList fileInfoList; QDir dir(vPath); @@ -393,17 +426,17 @@ ); return fileInfoList; } -// coco end +// disabled coco end quint64 FileHandler::totalSize(const QFileInfoList &vFileInfoList) { - // coco begin validated: Manually tested. requires list of files on file system to test and requires manual specific files for test. + // disabled coco begin validated: Manually tested. requires list of files on file system to test and requires manual specific files for test. quint64 total = 0; for (auto it = vFileInfoList.crbegin(); it != vFileInfoList.crend(); ++it) { total += it->size(); } return total; } -// coco end +// disabled coco end /*! * \brief FileHandler::subFolders