KCoreAddons

kbackup.cpp
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
5 SPDX-FileCopyrightText: 2006 Allen Winter <winter@kde.org>
6 SPDX-FileCopyrightText: 2006 Gregory S. Hayes <syncomm@kde.org>
7 SPDX-FileCopyrightText: 2006 Jaison Lee <lee.jaison@gmail.com>
8 SPDX-FileCopyrightText: 2011 Romain Perier <bambi@ubuntu.com>
9
10 SPDX-License-Identifier: LGPL-2.0-only
11*/
12
13#include "kbackup.h"
14
15#include <QDir>
16#include <QFileInfo>
17
18namespace KBackup
19{
20bool simpleBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension)
21{
22 QString backupFileName = qFilename + backupExtension;
23
24 if (!backupDir.isEmpty()) {
25 QFileInfo fileInfo(qFilename);
26 backupFileName = backupDir + QLatin1Char('/') + fileInfo.fileName() + backupExtension;
27 }
28
29 // qCDebug(KCOREADDONS_DEBUG) << "KBackup copying " << qFilename << " to " << backupFileName;
30 QFile::remove(backupFileName);
31 return QFile::copy(qFilename, backupFileName);
32}
33
34bool numberedBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension, const uint maxBackups)
35{
36 QFileInfo fileInfo(qFilename);
37
38 // The backup file name template.
39 QString sTemplate;
40 if (backupDir.isEmpty()) {
41 sTemplate = qFilename + QLatin1String(".%1") + backupExtension;
42 } else {
43 sTemplate = backupDir + QLatin1Char('/') + fileInfo.fileName() + QLatin1String(".%1") + backupExtension;
44 }
45
46 // First, search backupDir for numbered backup files to remove.
47 // Remove all with number 'maxBackups' and greater.
48 QDir d = backupDir.isEmpty() ? fileInfo.dir() : backupDir;
50 const QStringList nameFilters = QStringList(fileInfo.fileName() + QLatin1String(".*") + backupExtension);
51 d.setNameFilters(nameFilters);
53
54 uint maxBackupFound = 0;
55 const QFileInfoList infoList = d.entryInfoList();
56 for (const QFileInfo &fi : infoList) {
57 if (fi.fileName().endsWith(backupExtension)) {
58 // sTemp holds the file name, without the ending backupExtension
59 QString sTemp = fi.fileName();
60 sTemp.truncate(fi.fileName().length() - backupExtension.length());
61 // compute the backup number
62 int idex = sTemp.lastIndexOf(QLatin1Char('.'));
63 if (idex > 0) {
64 bool ok;
65 const uint num = QStringView(sTemp).mid(idex + 1).toUInt(&ok);
66 if (ok) {
67 if (num >= maxBackups) {
68 QFile::remove(fi.filePath());
69 } else {
70 maxBackupFound = qMax(maxBackupFound, num);
71 }
72 }
73 }
74 }
75 }
76
77 // Next, rename max-1 to max, max-2 to max-1, etc.
78 QString to = sTemplate.arg(maxBackupFound + 1);
79 for (int i = maxBackupFound; i > 0; i--) {
80 QString from = sTemplate.arg(i);
81 // qCDebug(KCOREADDONS_DEBUG) << "KBackup renaming " << from << " to " << to;
82 QFile::rename(from, to);
83 to = from;
84 }
85
86 // Finally create most recent backup by copying the file to backup number 1.
87 // qCDebug(KCOREADDONS_DEBUG) << "KBackup copying " << qFilename << " to " << sTemplate.arg(1);
88 return QFile::copy(qFilename, sTemplate.arg(1));
89}
90
91}
Provides utility functions for backup of files.
Definition kbackup.cpp:19
bool simpleBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension)
Function to create a backup file for a given filename.
Definition kbackup.cpp:20
bool numberedBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension, const uint maxBackups)
Function to create a backup file for a given filename.
Definition kbackup.cpp:34
QFileInfoList entryInfoList(Filters filters, SortFlags sort) const const
void setFilter(Filters filters)
void setNameFilters(const QStringList &nameFilters)
void setSorting(SortFlags sort)
bool copy(const QString &fileName, const QString &newName)
bool remove()
bool rename(const QString &newName)
QDir dir() const const
QString fileName() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype length() const const
void truncate(qsizetype position)
QStringView mid(qsizetype start, qsizetype length) const const
uint toUInt(bool *ok, int base) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.