Index: sources/device/DeviceController.cpp =================================================================== diff -u -r752204b5cb6f5feb1a434dc3226618a4534495e7 -r170fd9a04fab2e2ba96bdc16ca39f86789895937 --- sources/device/DeviceController.cpp (.../DeviceController.cpp) (revision 752204b5cb6f5feb1a434dc3226618a4534495e7) +++ sources/device/DeviceController.cpp (.../DeviceController.cpp) (revision 170fd9a04fab2e2ba96bdc16ca39f86789895937) @@ -7,7 +7,7 @@ * * \file DeviceController.cpp * \author (last) Behrouz NematiPour - * \date (last) 01-Mar-2024 + * \date (last) 24-Apr-2024 * \author (original) Behrouz NematiPour * \date (original) 03-Jun-2021 * @@ -262,8 +262,10 @@ QString device = ""; usbSpaceCheck(); if (usbSeek(device)) { - if (! _umounted ) { // avoid to mount the USB which has just been unmounted - usbMount(device); + if (! _umounted ) { // avoid to mount the USB which has just been unmounted + if (! _mounted ) { // avoid to mount the USB which has just been mounted + usbMount(device); + } } else { // the umount is requested? usbUmount(USB_Mount_Point); } @@ -319,8 +321,8 @@ emit didSDCardSpaceTooLow(_minRequiredAvailableSpacePercent); } - /// DEBUG: qDebug() << Storage::SDCard_Base_Path_Name << mCIsReady << mOTotal << mCTotal << (mOTotal == mCTotal) << mOAvailable << mCAvailable << (mOAvailable == mCAvailable) << mPercent << mCIsReadOnly; - if (mOPercent != mCPercent && mOAvailable != mCAvailable ) { + //DEBUG: qDebug() << Storage::SDCard_Base_Path_Name << mCIsReady << mCTotal << mOAvailable << mCAvailable << (mOAvailable == mCAvailable) << mOPercent << mCPercent << mCIsReadOnly; + if (mOPercent != mCPercent || mOAvailable != mCAvailable ) { mOPercent = mCPercent ; mOAvailable = mCAvailable ; emit didSDCardSpaceChange(mCIsReady, mCTotal, mCAvailable, mCPercent); @@ -877,11 +879,14 @@ { DeviceError::Scripts_Error_Enum err = DeviceError::eDevice_OK; if ( vCreate ) { - if ( ! FileHandler::write( vFile, "", false) ) { err = DeviceError::eDevice_Watch_Error_NotCreate; goto lErr; } - } else { - if ( ! QFileInfo::exists ( vFile ) ) { err = DeviceError::eDevice_Watch_Error_NotFound ; goto lErr; } - } - if ( ! _fileSystemWatcher.addPath( vFile ) ) { err = DeviceError::eDevice_Watch_Error_NotAdded ; goto lErr; } + if ( ! FileHandler::write ( vFile, "", false) ) { err = DeviceError::eDevice_Watch_Error_NotCreate; goto lErr; }} + else { + if ( ! QFileInfo::exists ( vFile ) ) { err = DeviceError::eDevice_Watch_Error_NotFound ; goto lErr; }} + if ( ! _fileSystemWatcher.removePath ( vFile ) ) { LOG_APPED_UI(QString("Device NOT watching %1").arg(vFile)); } + else { LOG_APPED_UI(QString("Device watch removed %1").arg(vFile)); } + if ( ! _fileSystemWatcher.addPath ( vFile ) ) { err = DeviceError::eDevice_Watch_Error_NotAdded ; goto lErr; } + + LOG_APPED_UI(QString("Device watch %1").arg(vFile)); return; lErr: LOG_DEBUG(DeviceError::deviceErrorText(err, 0)); @@ -975,25 +980,50 @@ bool DeviceController::logBackup(const QString &vFileName) { - if ( ! gLongLogName ) return false; + if ( ! gLogUpload ) return false; // no log backup bool ok = true; + QString fileSrc = vFileName; QFileInfo fileInfo(vFileName); QString filePath(fileInfo.absolutePath()); QString fileBase(fileInfo.baseName()); QString fileSufx(fileInfo.completeSuffix().prepend(_Logger.logFileNamePendingSubExt())); QString fileDest(QString("%1/%2.%3").arg(filePath, fileBase, fileSufx)); - // DEBUG - // qDebug() << ""; - // qDebug() << vFileName; - // qDebug() << fileDest; - ok = QFile::rename(vFileName, fileDest); + QString gzipSufx(gLogCompress ? Storage::gzipExt : ""); + + bool isBootPOST = _Logger.isBootPOST( vFileName ); + bool isCurrent = fileInfo.lastModified().date() == QDate::currentDate(); + if ( isBootPOST && isCurrent ) { + // no log backup if it is the BootPOST log file, only rename and add the serial to it. + fileDest = QString("%1/%2.%3").arg(filePath, fileBase.prepend(_Logger.logFileNameHDSN() + _Logger.fileSeparator()), fileInfo.completeSuffix()); + ok = FileHandler::append(fileSrc, fileDest, true); + return false; + } + + if ( gLogCompress ) { + ok = FileHandler::backupFile(fileSrc); + } + ok = QFile::rename(fileSrc + gzipSufx, fileDest + gzipSufx); + + // Now the serial received and the system is logging in the log with the serial number, + // therefore it is not bootPOST. + // Now try to back up the skipped bootPOST log. + if ( ! isBootPOST ) { + fileSrc .replace( _Logger.logFileNameHDSN(), _Logger.logFileNameHDSN() + _Logger.fileSeparator() + _Logger.logFileNameHDSN_default() ); + fileDest.replace( _Logger.logFileNameHDSN(), _Logger.logFileNameHDSN() + _Logger.fileSeparator() + _Logger.logFileNameHDSN_default() ); + if ( ! QFile::exists(fileSrc) ) goto lOut; + + ok = FileHandler::backupFile(fileSrc); + ok = QFile::rename(fileSrc + gzipSufx, fileDest + gzipSufx); + } + +lOut: return ok; } void DeviceController::onLogBackup(const QString &vFileName) { - if ( ! gLongLogName ) return; + if ( ! gLogUpload ) return; // no log backup ( slot ) bool ok = true; ok = logBackup( vFileName); @@ -1002,7 +1032,7 @@ bool DeviceController::logUpload(const QString &vFileName) { - if ( ! gLongLogName ) return false; + if ( ! gLogUpload ) return false; // no log Uploaded rename bool ok = true; QFileInfo fileInfo(vFileName); @@ -1015,10 +1045,7 @@ Logger::LogType logType = _Logger.logFileLogType(vFileName, filePath); ok = logType != Logger::eLogNone; if ( ! ok ) { LOG_APPED_UI(QString("CS Incorrect log upload type [%1]").arg(fileSufx)); goto lOut; } - // DEBUG - // qDebug() << ""; - // qDebug() << vFileName; - // qDebug() << fileDest; + // DEBUG qDebug() << __FUNCTION__ << "\n" << vFileName << "\n" << fileDest; ok = QFile::rename(filePath + vFileName, filePath + fileDest); lOut: @@ -1027,7 +1054,7 @@ void DeviceController::onLogUpload(const QString &vFileName) { - if ( ! gLongLogName ) return; + if ( ! gLogUpload ) return; // no log uploaded rename ( slot ) bool ok = true; ok = logUpload( vFileName); @@ -1076,7 +1103,7 @@ */ void DeviceController::findPendingLogs() { - if ( ! gLongLogName ) return; + if ( ! gLogUpload ) return; // no log upload pending detection static QString pendingLog = ""; if( _pendingCounter ) { @@ -1096,13 +1123,20 @@ // if there are pending files, // send a request only for the top on the list /// Note I thought it makes sense to send the oldest on the application and service logs - /// but there some conversation about the situation if something happens on the device, + /// but there are some conversation about the situation if something happens on the device, /// and it would be a critical situation to get the recent/top log and won't wait for the old ones to upload. // * When gets uploaded, moves from pending then next one comes to top // the process repeats until there is no file in pending + + bool uploadOldestFirst = true; //TODO: make if configurable(cfg, or cli) + if ( pendingFiles.count() ) { - // the most recent/first Tx file, to first ask for the current treatment which has just saved as pending on screen - _pendingLog = pendingFiles.first().absoluteFilePath(); + // the most recent/first log file, to first ask for the current log which has just been saved as pending + if ( uploadOldestFirst ) { + _pendingLog = pendingFiles.last().absoluteFilePath(); + } else { + _pendingLog = pendingFiles.first().absoluteFilePath(); + } QString message = pendingLog; LOG_DEBUG(message); emit didPendingLog( _pendingLog, FileHandler::sha256sum( _pendingLog ) ); @@ -1279,7 +1313,7 @@ bool ok = ! vStatus; QString usbDevice = _deviceUSBMountRequest._data.usbDevice; if(_deviceUSBMountRequest._data.isMountRequest) { // *** USB Mount - if ( ok ) { + if ( ok && ! _mounted ) { _mounted = true; _removed = false; LOG_DEBUG(QString("USB flash drive %1 has been mounted on %2").arg(usbDevice).arg(USB_Mount_Point)); @@ -1288,7 +1322,7 @@ usbError(usbDevice); } } else { // *** USB Unmount - if ( ok ) { + if ( ok && _mounted ) { _mounted = false; // _umounted = true; // I think it might be needed, but needs more testing. LOG_DEBUG(QString("USB drive %2 unmounted").arg(usbDevice));