Skip to content

Reverse order of migrations #9226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 22, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ void migrateConfigFile(const QCoreApplication *app)
{
using namespace OCC;

static const auto logPrefix = QStringLiteral("Settings migration: ");

if (!ConfigFile::exists()) {
// check whether an old config location must be migrated
// we support multiple locations from old versions
// these are worked on in-order to upgrade from version to version
// the algorithm is the same for all these locations, thus we can use a loop
// note that we try to migrate in descending order, i.e., we try to migrate from the last release, then from the release before, ...
// this is done in order to avoid porting old configu
QStringList configLocationsToMigrate;

{
Expand All @@ -89,47 +93,62 @@ void migrateConfigFile(const QCoreApplication *app)

QCoreApplication::setApplicationName(Theme::instance()->appNameGUI());

// location used in versions from 2.5 to 2.8
configLocationsToMigrate.append(QStandardPaths::writableLocation(Utility::isWindows() ? QStandardPaths::AppDataLocation : QStandardPaths::AppConfigLocation));

// location used in versions <= 2.4
// We need to use the deprecated QDesktopServices::storageLocation because of its Qt4 behavior of adding "data" to the path
configLocationsToMigrate.append(QDesktopServices::storageLocation(QDesktopServices::DataLocation));

// location used in versions from 2.5 to 2.8
configLocationsToMigrate.append(QStandardPaths::writableLocation(Utility::isWindows() ? QStandardPaths::AppDataLocation : QStandardPaths::AppConfigLocation));
}


for (auto oldDir : configLocationsToMigrate) {
// prove me wrong!
bool success = false;

if (oldDir.endsWith('/')) {
// macOS 10.11.x does not like trailing slash for rename/move.
oldDir.chop(1);
}

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 (confDir.endsWith('/')) {
// macOS 10.11.x does not like trailing slash for rename/move.
confDir.chop(1);
}

qCInfo(lcApplication) << logPrefix << "Migrating old config from" << oldDir << "to" << confDir;

if (!QFile::rename(oldDir, confDir)) {
qCWarning(lcApplication) << "Failed to move the old config directory to its new location (" << oldDir << "to" << confDir << ")";
qCWarning(lcApplication) << logPrefix << "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().mkpath(confDir)) {
const auto filesList = QDir(oldDir).entryInfoList(QDir::Files);
qCInfo(lcApplication) << "Will move the individual files" << filesList;
qCInfo(lcApplication) << logPrefix << "Will move the individual files" << filesList;
for (const auto &fileInfo : filesList) {
if (!QFile::rename(fileInfo.canonicalFilePath(), confDir + "/" + fileInfo.fileName())) {
qCWarning(lcApplication) << "Fallback move of " << fileInfo.fileName() << "also failed";
qCWarning(lcApplication) << logPrefix << "Fallback move of " << fileInfo.fileName() << "also failed";
}
}
}
} else {
// migration worked!
success = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both branches migrate, I guess we should break in both?


#ifndef Q_OS_WIN
// Create a symbolic link so a downgrade of the client would still find the config.
QFile::link(confDir, oldDir);
#endif
}
}

if (success) {
// we found a suitable config directory to migrate, hence we can stop here
// if we continued to run, we would try to overwrite the working migration
break;
}
}
}
}
Expand Down