void FileLogger::rotate() { // count current number of existing backup files int count=0; forever { QFile bakFile(QString("%1.%2").arg(fileName).arg(count+1)); if (bakFile.exists()) { ++count; } else { break; } } // Remove all old backup files that exceed the maximum number while (maxBackups>0 && count>=maxBackups) { QFile::remove(QString("%1.%2").arg(fileName).arg(count)); --count; } // Rotate backup files for (int i=count; i>0; --i) { QFile::rename(QString("%1.%2").arg(fileName).arg(i),QString("%1.%2").arg(fileName).arg(i+1)); } // Backup the current logfile QFile::rename(fileName,fileName+".1"); }
/** * Saves changes in the current database. * If successful, returns true and emits dbSaved() signal; * otherwise returns false and (sometimes) emits an appropriate error signal. */ Q_INVOKABLE bool PwDatabaseFacade::save() { if (!db) { LOG("Cannot save - no DB open"); return false; } if (isLocked()) { LOG("Cannot save - DB is locked"); return false; } emit dbAboutToSave(); // Encrypt and save to memory QByteArray outData; if (!db->save(outData)) { LOG("DB saving failed"); return false; } // Copy original DB to temporary timestamped file // (but when we create a DB, the original does not exist, and it is ok) QFile dbFile(db->getDatabaseFilePath()); QString bakFileName = makeBackupFilePath(db->getDatabaseFilePath()); if (dbFile.exists() && !dbFile.copy(bakFileName)) { LOG("Failed to backup the original DB: %s", dbFile.errorString().toUtf8().constData()); emit fileSaveError(tr("Cannot backup database file. Saving cancelled.", "An error message: failed to make a backup copy of the database file."), dbFile.errorString()); return false; } // Write the new data directly to the original file if (!dbFile.open(QIODevice::WriteOnly)) { LOG("Cannot open DB file: '%s' Error: %d. Message: %s", dbFile.fileName().toUtf8().constData(), dbFile.error(), dbFile.errorString().toUtf8().constData()); emit fileSaveError(tr("Cannot save database file", "An error message shown when the database file cannot be saved."), dbFile.errorString()); return false; } qint64 bytesWritten = dbFile.write(outData); if ((dbFile.error() != QFile::NoError) || (bytesWritten != outData.size())) { LOG("Cannot write to DB file. Error: %d. Message: %s", dbFile.error(), dbFile.errorString().toUtf8().constData()); emit fileSaveError(tr("Cannot write to database file", "An error message shown when the database file cannot be written to."), dbFile.errorString()); return false; } if (!dbFile.flush()) { LOG("Could not flush the DB file. Error: %d. Message: %s", dbFile.error(), dbFile.errorString().toUtf8().constData()); emit fileSaveError(tr("Error writing to database file", "An error message shown when the database file cannot be written to."), dbFile.errorString()); dbFile.close(); // could not flush -> possibly not completely written -> not saved return false; } dbFile.close(); if (!Settings::instance()->isBackupDatabaseOnSave()) { // Backup switched off, so remove the backup file // (there are no backup files for newly created DBs) QFile bakFile(bakFileName); if (bakFile.exists() && !bakFile.remove()) { // Could not delete backup. Not critical and nothing we/user can do about it - so just ignore it silently. LOG("Could not remove backup file: %s", bakFile.fileName().toUtf8().constData()); } } emit dbSaved(); return true; }