Index: sources/storage/FileHandler.cpp =================================================================== diff -u -r3d0a160e4e8c0348c688ed5efe4d86dc66121e6b -r5215e145a0f26a20c99e3f10af6ac8a9c3b67383 --- sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 3d0a160e4e8c0348c688ed5efe4d86dc66121e6b) +++ sources/storage/FileHandler.cpp (.../FileHandler.cpp) (revision 5215e145a0f26a20c99e3f10af6ac8a9c3b67383) @@ -23,6 +23,7 @@ #include #include + // Project #include "Logger.h" @@ -35,7 +36,7 @@ * \param vFileName - Source file name * \param vContent - The content which is going to be written in the file. * \param vAppend - if set to true the content will be appended at the end of the file. - * \return false if file can't be opened. + * \return false if file cannot be opened. */ void FileHandler::errOut(const QString &vMessage) { @@ -63,18 +64,18 @@ * \param vFileName - Source file name * \param vContent - content to be written into file. * \param vAppend - append (true) or overwrite (false) - * \return false if file can't be opened for write. + * \return false if file cannot be opened for write. */ bool FileHandler::write(const QString &vFileName, const QString &vContent, bool vAppend) { QFile file(vFileName); 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 can't be opened for writing. + // 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("Can't open file for write (%1).Possible corrupted file system").arg(vFileName); - // here can't use LOG_XXXX because if the folder can't be created then the log can't be written. + 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; } @@ -90,14 +91,14 @@ * \details reads file vFilename content into vContent variable. * \param vFileName - Source file name * \param vContent - The content of the file which will be set when done. - * \return false if file can't be opened. + * \return false if file cannot be opened. */ bool FileHandler::read(const QString &vFileName, QString &vContent) { QFile file(vFileName); if (! file.open(QFile::Text | QFile::ReadOnly)) { - QString msg = QString("Can't open file for read (%1).Possible corrupted file system").arg(vFileName); - // here can't use LOG_XXXX because if the folder can't be created then the log can't be written. + 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; } @@ -107,23 +108,27 @@ } /*! - * \brief FileHandler::readJSON + * \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 can't be read + * \return false if file cannot be read or parsed */ -bool FileHandler::readJSON(const QString &vFileName, QJsonObject &vContent) +bool FileHandler::read(const QString &vFileName, QJsonObject &vContent, QJsonParseError *error) { QFile file(vFileName); if (! file.open(QFile::Text | QFile::ReadOnly)) { - errOut(QObject::tr("Can't open file for read (%1).Possible corrupted file system").arg(vFileName)); + errOut(QObject::tr("Cannot open file for read (%1). Possible corrupted file system.").arg(vFileName)); return false; } QTextStream in(&file); QString content = in.readAll(); - - QJsonDocument doc = QJsonDocument::fromJson(content.toUtf8()); + QJsonParseError jsonParseError; + QJsonDocument doc = QJsonDocument::fromJson(content.toUtf8(), &jsonParseError); + if (jsonParseError.error) { + if (error) *error = jsonParseError; + return false; + } vContent = doc.object(); return true; } @@ -177,12 +182,12 @@ errOut(QString("%1 File(s) %2 removed").arg(countRemoved).arg(fileName)); } else { - errOut(QString("Can't delete file : ") + fileName); + errOut(QString("Cannot delete file : ") + fileName); } } } else { - errOut(QString("Can't get last modified date of file : ") + fileName); + errOut(QString("Cannot get last modified date of file : ") + fileName); } // coco end } @@ -202,8 +207,8 @@ if ( ! dir.exists(vFolder) ) { // coco begin validated: Has been validated manually if ( ! dir.mkpath(vFolder) ) { - QString msg = "Can't create folder " + vFolder; - // here can't use LOG_XXXX because if the folder can't be created then the log can't be written. + QString msg = "Cannot create folder " + vFolder; + // here cannot use LOG_XXXX because if the folder cannot be created then the log cannot be written. errOut(msg); return false; } @@ -240,3 +245,123 @@ return mounted; } // coco end + +/*! + * \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, + * and are the oldest. + * \param vPath - the path to search for the files + * \param vNameFilters - the files filter to search for. + * \param vRetainPercent - It means how many percentage of the space these file are able to retain. + * e.g. 90% retains means older files which use 10% of storage will be listed. + * e.g. 80% retains means older files which use 20% of storage will be listed. + * e.g. 0% retains means no file shall be retained, therefore all the files listed. + * e.g. 100% retains means all file shall be retained, therefore the list shall be empty. + * By default set t0 0 so don't have any size constraint + * \return list of the files found by their information. + * 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 + + QFileInfoList fileInfoList; + // if all the files need to retain then no file shall be listed in the remove list. + if ( vRetainPercent == 100 ) return fileInfoList; + + // if the path is incorrect return with empty list + QDir dir(vPath); + if (!dir.exists()) return fileInfoList; + + // get the storage total + QStorageInfo storage(dir); + quint64 totalSizeStorage = storage.bytesTotal(); + + // get list of all the files in the path by the filter + QFileInfoList fileInfoListAll = find(vPath, vNameFilters); + + // if there is no file in the path with that filter then return empty list. + if (fileInfoListAll.count() == 0) return fileInfoList; + + // if vRetainPercent is 0 means all needs to be removed. + if (vRetainPercent == 0) return fileInfoListAll; + + // get the files total + quint64 totalSizeFiles = totalSize(fileInfoListAll); + + // the total size that all the + quint64 totalSizeRetain = totalSizeStorage * (vRetainPercent / 100.0); + + // if already totalSizeFiles <= totalSizeRetain, don't go any further and return empty list. + if (totalSizeFiles <= totalSizeRetain) return fileInfoList; + + // gets each file size from oldest to newest + // 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; + 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. + quint64 size = it->size(); + totalSizeRemoved += size; + totalSizeFiles -= size; + fileInfoList += *it; + /// DEBUG: since it has been manually tested this will help next time for test. + /// debugging the find function + // qDebug() << QString("%1 , %2 , %3 , %4 , %5") + // .arg(totalSizeFiles , 12) + // .arg(size , 12) + // .arg(totalSizeRemoved , 12) + // .arg(it->lastModified().toString("yyyy-MM-dd-HH:mm")) + // .arg(it->fileName()) + // ; + if (totalSizeFiles <= totalSizeRetain) break; + } + /// DEBUG: since it has been manually tested this will help next time for test. + /// the total size & count removed. + // qDebug() << QString("%1 , %2") + // .arg(totalSizeRemoved , 12) + // .arg(fileInfoList.count(), 3) + // ; + return fileInfoList; +} +// coco end + +/*! + * \brief FileHandler::find + * \details The function to find files. + * \param vPath - the path to search for the files + * \param vNameFilters - the files filter to search for. + * \return list of the files found by their information. + * 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 + // manually tested + + QFileInfoList fileInfoList; + QDir dir(vPath); + if (!dir.exists()) return fileInfoList; + fileInfoList = dir.entryInfoList( + vNameFilters, + QDir::NoDotAndDotDot | QDir::Files, + // the sorting may require to change from QDir::Time to QDir::Name + // since the birthTime always returns invalid + // and as part of our log naming we have the birthTime in file name. + QDir::Time + ); + return fileInfoList; +} +// coco end + +quint64 FileHandler::totalSize(const QFileInfoList &vFileInfoList) { + // coco begin validation: 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