Index: sources/storage/FileHandler.cpp =================================================================== diff -u -r4a67c01045f365be38f1a12a8572c0070d343e1e -rcf51c19d82d667644d7f70eaa521b43f908c7068 --- sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 4a67c01045f365be38f1a12a8572c0070d343e1e) +++ 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) 24-Feb-2022 + * \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; @@ -52,7 +50,7 @@ << QDate::currentDate().toString("yyyy_MM_dd") << " " << QTime::currentTime().toString("HH:mm:ss" ) << " " << mCritical - << endl; + << Qt::endl; } ++count; } @@ -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; } @@ -263,7 +266,6 @@ */ bool FileHandler::isMounted(const QString &vPath, bool *vIsReadOnly) { - // disabled 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; } -// disabled 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, @@ -339,7 +372,7 @@ // if it is breaks // else adds the file to the list and continues. // DEBUG: qDebug() << "%" << totalSizeStorage << totalSizeFiles << totalSizeRetain << totalSizeFiles - totalSizeRetain << vRetainPercent; - quint64 totalSizeRemoved = 0; + 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.