From 380d7b80283ba025c362f151811da89d0646602e Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 24 Oct 2018 15:42:42 +0200 Subject: [PATCH] Migration from 2.4: fallback to move file by file if directory move failled (#6807) Migration from 2.4: fallback to move file by file if directory move failed This can happen if the directory already exist because, say, it was created by the ownCloud outlook plugin which save its file in the same directory --- src/gui/application.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/gui/application.cpp b/src/gui/application.cpp index b1910be3c..310f055c1 100644 --- a/src/gui/application.cpp +++ b/src/gui/application.cpp @@ -202,9 +202,7 @@ Application::Application(int &argc, char **argv) setWindowIcon(_theme->applicationIcon()); setAttribute(Qt::AA_UseHighDpiPixmaps, true); - auto confDir = ConfigFile().configPath(); - if (confDir.endsWith('/')) confDir.chop(1); // macOS 10.11.x does not like trailing slash for rename/move. - if (!QFileInfo(confDir).isDir()) { + if (!ConfigFile().exists()) { // Migrate from version <= 2.4 setApplicationName(_theme->appNameGUI()); #ifndef QT_WARNING_DISABLE_DEPRECATED // Was added in Qt 5.9 @@ -219,9 +217,23 @@ Application::Application(int &argc, char **argv) QT_WARNING_POP setApplicationName(_theme->appName()); if (QFileInfo(oldDir).isDir()) { + auto confDir = ConfigFile().configPath(); + if (confDir.endsWith('/')) confDir.chop(1); // macOS 10.11.x does not like trailing slash for rename/move. qCInfo(lcApplication) << "Migrating old config from" << oldDir << "to" << confDir; + if (!QFile::rename(oldDir, confDir)) { - qCWarning(lcApplication) << "Failed to move the old config file to its new location (" << oldDir << "to" << confDir << ")"; + qCWarning(lcApplication) << "Failed to move the old config directory to its new location (" << oldDir << "to" << confDir << ")"; + + // Try to move the files one by one + if (QFileInfo(confDir).isDir() || QDir().mkdir(confDir)) { + const QStringList filesList = QDir(oldDir).entryList(QDir::Files); + qCInfo(lcApplication) << "Will move the individual files" << filesList; + for (const auto &name : filesList) { + if (!QFile::rename(oldDir + "/" + name, confDir + "/" + name)) { + qCWarning(lcApplication) << "Fallback move of " << name << "also failed"; + } + } + } } else { #ifndef Q_OS_WIN // Create a symbolic link so a downgrade of the client would still find the config.