/*! * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * \copyright \n * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, \n * IN PART OR IN WHOLE, \n * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n * * \file filehandler.cpp * \date 2019/09/30 * \author Behrouz NematiPour * */ #include "filehandler.h" //Qt #include #include #include #include #include #include // Project #include "logger.h" // namespace using namespace Storage; /*! * \brief FileHandler::write * \details Writes the content of vContent into the file vFileName. * \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. */ void FileHandler::errOut(const QString &vMessage) { QString mCritical = vMessage; QTextStream err(stderr); err << mCritical << endl; } 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; if (! file.open(openMode)) { QString msg = QString("Can't open file for write (%1).Possible corrupted file system").arg(vFileName); errOut (msg); LOG_DEBUG(msg); return false; } QTextStream out(&file); out << vContent; out.flush(); return true; } /*! * \brief FileHandler::read * \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. */ 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); errOut (msg); LOG_DEBUG(msg); return false; } QTextStream in(&file); vContent = in.readAll(); return true; } /*! * \brief FileHandler::copyFolder * \details Copies all the file and folders recursively. * \param vSource - The source folder * \param vDestination - The destination folder * \return Tue on successful execution. * \note This method uses the Linux "cp -r vSource vDestination" command * Not a Thread-Safe. * */ int FileHandler::copyFolder(const QString &vSource, const QString &vDestination ) { // 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 cp = "cp"; QStringList arguments; arguments << "-r" << vSource << vDestination; int result = QProcess::execute(cp, arguments); return result; } // coco end /*! * \brief FileHandler::removeFiles * \details * \param vFolder * \param vFilter * \param vDateOlderThan * \return */ int FileHandler::removeFiles(const QStringList &vFolders, const QStringList &vNameFilter, const QDate &vDateOlderThan) { int countRemoved = 0; for (const auto &folder : vFolders) { QDir dir(folder); dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); dir.setSorting(QDir::Time | QDir::Reversed); QFileInfoList infoList = dir.entryInfoList(vNameFilter); for (const auto &info : infoList) { QDateTime fileTime = info.lastModified(); QString fileName = info.absoluteFilePath(); if (fileTime.isValid()) { if (fileTime.date() <= vDateOlderThan) { if (QFile::remove(fileName)) { ++countRemoved; LOG_DEBUG(QString("%1 File(s) %2 removed").arg(countRemoved).arg(fileName)); } else { LOG_DEBUG(QString("Can't delete file : ") + fileName); } } } else { LOG_DEBUG(QString("Can't get last modified date of file : ") + fileName); } } } return countRemoved; } /*! * \brief FileHandler::makeFolder * \details Create the folder vFolder if it doesn't exist. * \param vFolder - the folder to create * \return true on successfull creation */ bool FileHandler::makeFolder(const QString &vFolder) { QDir dir(vFolder); if ( ! dir.exists(vFolder) ) { if ( ! dir.mkpath(vFolder) ) { QString msg = "Can't create folder " + vFolder; qDebug() << msg; LOG_DEBUG(msg); return false; } } return true; }