Skip to content
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

Save to canonical path when dealing with symlinks #3463

Merged
merged 1 commit into from
Aug 31, 2019
Merged
Changes from all 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
12 changes: 7 additions & 5 deletions src/core/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ bool Database::save(const QString& filePath, QString* error, bool atomic, bool b
return false;
}

auto& canonicalFilePath = QFileInfo::exists(filePath) ? QFileInfo(filePath).canonicalFilePath() : filePath;

if (atomic) {
QSaveFile saveFile(filePath);
if (saveFile.open(QIODevice::WriteOnly)) {
Expand All @@ -220,7 +222,7 @@ bool Database::save(const QString& filePath, QString* error, bool atomic, bool b
}

if (backup) {
backupDatabase(filePath);
backupDatabase(canonicalFilePath);
}

if (saveFile.commit()) {
Expand All @@ -244,21 +246,21 @@ bool Database::save(const QString& filePath, QString* error, bool atomic, bool b
tempFile.close(); // flush to disk

if (backup) {
backupDatabase(filePath);
backupDatabase(canonicalFilePath);
}

// Delete the original db and move the temp file in place
QFile::remove(filePath);
QFile::remove(canonicalFilePath);

// Note: call into the QFile rename instead of QTemporaryFile
// due to an undocumented difference in how the function handles
// errors. This prevents errors when saving across file systems.
if (tempFile.QFile::rename(filePath)) {
if (tempFile.QFile::rename(canonicalFilePath)) {
// successfully saved the database
tempFile.setAutoRemove(false);
setFilePath(filePath);
return true;
} else if (!backup || !restoreDatabase(filePath)) {
} else if (!backup || !restoreDatabase(canonicalFilePath)) {
// Failed to copy new database in place, and
// failed to restore from backup or backups disabled
tempFile.setAutoRemove(false);
Expand Down