Commit 620abbd8 authored by Halla Rempt's avatar Halla Rempt
Browse files

Fork KBackup to investigate why numbered backups with [] fail

CCBUG:445500
(cherry picked from commit 7f92164d2f22f28ce511f33d38c0ec8fb88429c6)
parent 97008676
......@@ -45,6 +45,7 @@ set(kritaglobal_LIB_SRCS
KisRectsGrid.cpp
KisSynchronizedConnection.cpp
KisFilteredRollingMean.cpp
KisBackup.cpp
)
if(WIN32)
......
/*
This file is part of the KDE libraries
SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
SPDX-FileCopyrightText: 2006 Allen Winter <winter@kde.org>
SPDX-FileCopyrightText: 2006 Gregory S. Hayes <syncomm@kde.org>
SPDX-FileCopyrightText: 2006 Jaison Lee <lee.jaison@gmail.com>
SPDX-FileCopyrightText: 2011 Romain Perier <bambi@ubuntu.com>
SPDX-License-Identifier: LGPL-2.0-only
*/
#include "KisBackup.h"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
bool KisBackup::backupFile(const QString &qFilename, const QString &backupDir)
{
return (simpleBackupFile(qFilename, backupDir, QStringLiteral("~")));
}
bool KisBackup::simpleBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension)
{
QString backupFileName = qFilename + backupExtension;
if (!backupDir.isEmpty()) {
QFileInfo fileInfo(qFilename);
backupFileName = backupDir + QLatin1Char('/') + fileInfo.fileName() + backupExtension;
}
// qCDebug(KCOREADDONS_DEBUG) << "KisBackup copying " << qFilename << " to " << backupFileName;
QFile::remove(backupFileName);
return QFile::copy(qFilename, backupFileName);
}
bool KisBackup::numberedBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension, const uint maxBackups)
{
QFileInfo fileInfo(qFilename);
// The backup file name template.
QString sTemplate;
if (backupDir.isEmpty()) {
sTemplate = qFilename + QLatin1String(".%1") + backupExtension;
} else {
sTemplate = backupDir + QLatin1Char('/') + fileInfo.fileName() + QLatin1String(".%1") + backupExtension;
}
// First, search backupDir for numbered backup files to remove.
// Remove all with number 'maxBackups' and greater.
QDir d = backupDir.isEmpty() ? fileInfo.dir() : backupDir;
d.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
const QStringList nameFilters = QStringList(fileInfo.fileName() + QLatin1String(".*") + backupExtension);
d.setNameFilters(nameFilters);
d.setSorting(QDir::Name);
uint maxBackupFound = 0;
const QFileInfoList infoList = d.entryInfoList();
for (const QFileInfo &fi : infoList) {
if (fi.fileName().endsWith(backupExtension)) {
// sTemp holds the file name, without the ending backupExtension
QString sTemp = fi.fileName();
sTemp.truncate(fi.fileName().length() - backupExtension.length());
// compute the backup number
int idex = sTemp.lastIndexOf(QLatin1Char('.'));
if (idex > 0) {
bool ok;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const uint num = QStringView(sTemp).mid(idex + 1).toUInt(&ok);
#else
const uint num = sTemp.midRef(idex + 1).toUInt(&ok);
#endif
if (ok) {
if (num >= maxBackups) {
QFile::remove(fi.filePath());
} else {
maxBackupFound = qMax(maxBackupFound, num);
}
}
}
}
}
// Next, rename max-1 to max, max-2 to max-1, etc.
QString to = sTemplate.arg(maxBackupFound + 1);
for (int i = maxBackupFound; i > 0; i--) {
QString from = sTemplate.arg(i);
// qCDebug(KCOREADDONS_DEBUG) << "KisBackup renaming " << from << " to " << to;
QFile::rename(from, to);
to = from;
}
// Finally create most recent backup by copying the file to backup number 1.
// qCDebug(KCOREADDONS_DEBUG) << "KisBackup copying " << qFilename << " to " << sTemplate.arg(1);
bool r = QFile::copy(qFilename, sTemplate.arg(1));
qDebug() << "Copy from" << qFilename << "to" << sTemplate.arg(1) << "result" << r;
return r;
}
/*
This file is part of the KDE libraries
SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
SPDX-FileCopyrightText: 2006 Jaison Lee <lee.jaison@gmail.com>
SPDX-FileCopyrightText: 2011 Romain Perier <bambi@ubuntu.com>
SPDX-License-Identifier: LGPL-2.0-only
*/
#ifndef KISBACKUP_H
#define KISBACKUP_H
#include <QString>
#include <kritaglobal_export.h>
class KRITAGLOBAL_EXPORT KisBackup
{
public:
static bool backupFile(const QString &filename, const QString &backupDir = QString());
static bool simpleBackupFile(const QString &filename, const QString &backupDir = QString(), const QString &backupExtension = QStringLiteral("~"));
static bool numberedBackupFile(const QString &filename,
const QString &backupDir = QString(),
const QString &backupExtension = QStringLiteral("~"),
const uint maxBackups = 10);
};
#endif
......@@ -11,7 +11,6 @@
#include <kis_debug.h>
#include <KisTag.h>
#include <KisResourceLoaderRegistry.h>
#include <kbackup.h>
#include <KisGlobalResourcesInterface.h>
#include <kis_pointer_utils.h>
#include <KoMD5Generator.h>
......
......@@ -18,7 +18,7 @@
#include <KritaVersionWrapper.h>
#include <klocalizedstring.h>
#include <kbackup.h>
#include <KisBackup.h>
#include <kis_debug.h>
#include <KisUsageLogger.h>
......@@ -173,7 +173,7 @@ QSqlError createDatabase(const QString &location)
qWarning() << "Old schema:" << schemaVersion << "New schema:" << newSchemaVersionNumber;
schemaIsOutDated = true;
KBackup::numberedBackupFile(location + "/" + KisResourceCacheDb::resourceCacheDbFilename);
KisBackup::numberedBackupFile(location + "/" + KisResourceCacheDb::resourceCacheDbFilename);
if (newSchemaVersionNumber == QVersionNumber::fromString("0.0.17")
&& QVersionNumber::compare(oldSchemaVersionNumber, QVersionNumber::fromString("0.0.14")) > 0
......
......@@ -50,7 +50,7 @@
#include <KisAutoSaveRecoveryDialog.h>
#include <kdesktopfile.h>
#include <kconfiggroup.h>
#include <kbackup.h>
#include <KisBackup.h>
#include <KisView.h>
#include <QTextBrowser>
......@@ -742,7 +742,7 @@ bool KisDocument::exportDocumentImpl(const KritaUtils::ExportFileJob &job, KisPr
QString suffix = cfg.readEntry<QString>("backupfilesuffix", "~");
if (numOfBackupsKept == 1) {
if (!KBackup::simpleBackupFile(job.filePath, backupDir, suffix)) {
if (!KisBackup::simpleBackupFile(job.filePath, backupDir, suffix)) {
qWarning() << "Failed to create simple backup file!" << job.filePath << backupDir << suffix;
KisUsageLogger::log(QString("Failed to create a simple backup for %1 in %2.").arg(job.filePath).arg(backupDir.isEmpty() ? "the same location as the file" : backupDir));
return false;
......@@ -752,7 +752,7 @@ bool KisDocument::exportDocumentImpl(const KritaUtils::ExportFileJob &job, KisPr
}
}
else if (numOfBackupsKept > 1) {
if (!KBackup::numberedBackupFile(job.filePath, backupDir, suffix, numOfBackupsKept)) {
if (!KisBackup::numberedBackupFile(job.filePath, backupDir, suffix, numOfBackupsKept)) {
qWarning() << "Failed to create numbered backup file!" << job.filePath << backupDir << suffix;
KisUsageLogger::log(QString("Failed to create a numbered backup for %2.").arg(job.filePath).arg(backupDir.isEmpty() ? "the same location as the file" : backupDir));
return false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment