# HG changeset patch # User Chris Cannam # Date 1582802822 0 # Node ID 230e7433030d7e029bfc3ba78cfb2290899abf3b # Parent bdd4317e43de666efad9afc498dfa82900854250 Backup existing files, if found diff -r bdd4317e43de -r 230e7433030d installer.cpp --- a/installer.cpp Wed Feb 26 13:54:21 2020 +0000 +++ b/installer.cpp Thu Feb 27 11:27:02 2020 +0000 @@ -53,6 +53,7 @@ #include #include #include +#include #include @@ -553,6 +554,31 @@ return status; } +bool +backup(QString filePath, QString backupDir) +{ + QFileInfo file(filePath); + + if (!file.exists()) { + return true; + } + + if (!QDir(backupDir).exists()) { + QDir().mkpath(backupDir); + } + + QString backup = backupDir + "/" + file.fileName() + ".bak"; + SVCERR << "Note: existing file " << filePath + << " found, backing up to " << backup << endl; + if (!QFile(filePath).rename(backup)) { + SVCERR << "Failed to move " << filePath.toStdString() + << " to backup " << backup.toStdString() << endl; + return false; + } + + return true; +} + QString installLibrary(LibraryInfo info, QString targetDir) { @@ -560,15 +586,17 @@ QString source = ":out"; QFile f(source + "/" + library); QString destination = targetDir + "/" + library; + QString backupDir = targetDir + "/" + + QString("saved-%1").arg(QDateTime::currentDateTime().toString + ("yyyyMMdd-hhmmss")); - if (QFileInfo(destination).exists()) { - auto installed = getLibraryPluginVersions(destination); - } else { - SVCERR << "Note: library " << library - << " is not yet installed, not comparing versions" << endl; + if (!QDir(targetDir).exists()) { + QDir().mkpath(targetDir); } - //!!! if destination exists, move it aside + if (!backup(destination, backupDir)) { + return QObject::tr("Failed to move aside existing library"); + } SVCERR << "Copying " << library.toStdString() << " to " << destination.toStdString() << "..." << endl; @@ -593,6 +621,9 @@ for (auto e: entries) { if (e == library) continue; QString destination = targetDir + "/" + e; + if (!backup(destination, backupDir)) { + continue; + } SVCERR << "Copying " << e.toStdString() << " to " << destination.toStdString() << "..." << endl; if (!QFile(source + "/" + e).copy(destination)) { @@ -935,10 +966,6 @@ return 0; } - if (!QDir(target).exists()) { - QDir().mkpath(target); - } - QProgressDialog progress(QObject::tr("Installing..."), QObject::tr("Stop"), 0, toInstall.size() + 1); progress.setMinimumDuration(0);