File indexing completed on 2024-05-12 03:54:54
0001 /* 0002 This file is part of the KDE libraries 0003 0004 SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org> 0005 SPDX-FileCopyrightText: 2006 Allen Winter <winter@kde.org> 0006 SPDX-FileCopyrightText: 2006 Gregory S. Hayes <syncomm@kde.org> 0007 SPDX-FileCopyrightText: 2006 Jaison Lee <lee.jaison@gmail.com> 0008 SPDX-FileCopyrightText: 2011 Romain Perier <bambi@ubuntu.com> 0009 0010 SPDX-License-Identifier: LGPL-2.0-only 0011 */ 0012 0013 #include "kbackup.h" 0014 0015 #include <QDir> 0016 #include <QFileInfo> 0017 0018 namespace KBackup 0019 { 0020 bool simpleBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension) 0021 { 0022 QString backupFileName = qFilename + backupExtension; 0023 0024 if (!backupDir.isEmpty()) { 0025 QFileInfo fileInfo(qFilename); 0026 backupFileName = backupDir + QLatin1Char('/') + fileInfo.fileName() + backupExtension; 0027 } 0028 0029 // qCDebug(KCOREADDONS_DEBUG) << "KBackup copying " << qFilename << " to " << backupFileName; 0030 QFile::remove(backupFileName); 0031 return QFile::copy(qFilename, backupFileName); 0032 } 0033 0034 bool numberedBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension, const uint maxBackups) 0035 { 0036 QFileInfo fileInfo(qFilename); 0037 0038 // The backup file name template. 0039 QString sTemplate; 0040 if (backupDir.isEmpty()) { 0041 sTemplate = qFilename + QLatin1String(".%1") + backupExtension; 0042 } else { 0043 sTemplate = backupDir + QLatin1Char('/') + fileInfo.fileName() + QLatin1String(".%1") + backupExtension; 0044 } 0045 0046 // First, search backupDir for numbered backup files to remove. 0047 // Remove all with number 'maxBackups' and greater. 0048 QDir d = backupDir.isEmpty() ? fileInfo.dir() : backupDir; 0049 d.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); 0050 const QStringList nameFilters = QStringList(fileInfo.fileName() + QLatin1String(".*") + backupExtension); 0051 d.setNameFilters(nameFilters); 0052 d.setSorting(QDir::Name); 0053 0054 uint maxBackupFound = 0; 0055 const QFileInfoList infoList = d.entryInfoList(); 0056 for (const QFileInfo &fi : infoList) { 0057 if (fi.fileName().endsWith(backupExtension)) { 0058 // sTemp holds the file name, without the ending backupExtension 0059 QString sTemp = fi.fileName(); 0060 sTemp.truncate(fi.fileName().length() - backupExtension.length()); 0061 // compute the backup number 0062 int idex = sTemp.lastIndexOf(QLatin1Char('.')); 0063 if (idex > 0) { 0064 bool ok; 0065 const uint num = QStringView(sTemp).mid(idex + 1).toUInt(&ok); 0066 if (ok) { 0067 if (num >= maxBackups) { 0068 QFile::remove(fi.filePath()); 0069 } else { 0070 maxBackupFound = qMax(maxBackupFound, num); 0071 } 0072 } 0073 } 0074 } 0075 } 0076 0077 // Next, rename max-1 to max, max-2 to max-1, etc. 0078 QString to = sTemplate.arg(maxBackupFound + 1); 0079 for (int i = maxBackupFound; i > 0; i--) { 0080 QString from = sTemplate.arg(i); 0081 // qCDebug(KCOREADDONS_DEBUG) << "KBackup renaming " << from << " to " << to; 0082 QFile::rename(from, to); 0083 to = from; 0084 } 0085 0086 // Finally create most recent backup by copying the file to backup number 1. 0087 // qCDebug(KCOREADDONS_DEBUG) << "KBackup copying " << qFilename << " to " << sTemplate.arg(1); 0088 return QFile::copy(qFilename, sTemplate.arg(1)); 0089 } 0090 0091 }