Extend config backup method to make a copy of any given config file.
authorCamila <hello@camila.codes>
Tue, 27 Dec 2022 20:25:40 +0000 (21:25 +0100)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Mon, 30 Jan 2023 08:44:17 +0000 (09:44 +0100)
Signed-off-by: Camila <hello@camila.codes>
src/libsync/configfile.cpp
src/libsync/configfile.h

index 08064026f405875327bb65ffff8e0a29d1d7a660..631551a5e23143b8dfec23048ec41bc8d85d1ee0 100644 (file)
@@ -430,12 +430,15 @@ QString ConfigFile::excludeFileFromSystem()
     return fi.absoluteFilePath();
 }
 
-QString ConfigFile::backup() const
+QString ConfigFile::backup(const QString fileName) const
 {
-    QString baseFile = configFile();
+    QString baseFile = configPath() + fileName;
     auto versionString = clientVersionString();
-    if (!versionString.isEmpty())
+
+    if (!versionString.isEmpty()) {
         versionString.prepend('_');
+    }
+
     QString backupFile =
         QString("%1.backup_%2%3")
             .arg(baseFile)
@@ -445,10 +448,13 @@ QString ConfigFile::backup() const
     // If this exact file already exists it's most likely that a backup was
     // already done. (two backup calls directly after each other, potentially
     // even with source alterations in between!)
-    if (!QFile::exists(backupFile)) {
-        QFile f(baseFile);
-        f.copy(backupFile);
+    QFile f(baseFile);
+
+    // QFile does not overwrite it
+    if(!f.copy(backupFile)) {
+        qCWarning(lcConfigFile) << "Failed to create a backup of the config file" << baseFile;
     }
+
     return backupFile;
 }
 
index 719cce0835ada0e981e845bac75092ee1cd605e6..c0f1267369e97058f1751f7989581886ec731da3 100644 (file)
@@ -49,11 +49,11 @@ public:
     static QString excludeFileFromSystem(); // doesn't access config dir
 
     /**
-     * Creates a backup of the file
+     * Creates a backup of any given fileName in the config folder
      *
      * Returns the path of the new backup.
      */
-    [[nodiscard]] QString backup() const;
+    [[nodiscard]] QString backup(const QString fileName) const;
 
     bool exists();