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

Fix permissions changing on database save #4833

Merged
merged 1 commit into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion src/core/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,14 @@ bool Database::saveAs(const QString& filePath, QString* error, bool atomic, bool

QFileInfo fileInfo(filePath);
auto realFilePath = fileInfo.exists() ? fileInfo.canonicalFilePath() : fileInfo.absoluteFilePath();
bool isNewFile = !QFile::exists(realFilePath);
bool ok = AsyncTask::runAndWaitForFuture([&] { return performSave(realFilePath, error, atomic, backup); });
if (ok) {
markAsClean();
setFilePath(filePath);
if (isNewFile) {
QFile::setPermissions(realFilePath, QFile::ReadUser | QFile::WriteUser);
}
m_fileWatcher->start(realFilePath, 30, 1);
} else {
// Saving failed, don't rewatch file since it does not represent our database
Expand Down Expand Up @@ -304,6 +308,7 @@ bool Database::performSave(const QString& filePath, QString* error, bool atomic,
}

// Delete the original db and move the temp file in place
auto perms = QFile::permissions(filePath);
QFile::remove(filePath);

// Note: call into the QFile rename instead of QTemporaryFile
Expand All @@ -312,6 +317,7 @@ bool Database::performSave(const QString& filePath, QString* error, bool atomic,
if (tempFile.QFile::rename(filePath)) {
// successfully saved the database
tempFile.setAutoRemove(false);
QFile::setPermissions(filePath, perms);
return true;
} else if (!backup || !restoreDatabase(filePath)) {
// Failed to copy new database in place, and
Expand Down Expand Up @@ -454,9 +460,12 @@ bool Database::backupDatabase(const QString& filePath)

auto match = re.match(filePath);
auto backupFilePath = filePath;
auto perms = QFile::permissions(filePath);
backupFilePath = backupFilePath.replace(re, "") + ".old" + match.captured(1);
QFile::remove(backupFilePath);
return QFile::copy(filePath, backupFilePath);
bool res = QFile::copy(filePath, backupFilePath);
QFile::setPermissions(backupFilePath, perms);
return res;
}

/**
Expand All @@ -472,11 +481,13 @@ bool Database::restoreDatabase(const QString& filePath)
static auto re = QRegularExpression("^(.*?)(\\.[^.]+)?$");

auto match = re.match(filePath);
auto perms = QFile::permissions(filePath);
auto backupFilePath = match.captured(1) + ".old" + match.captured(2);
// Only try to restore if the backup file actually exists
if (QFile::exists(backupFilePath)) {
QFile::remove(filePath);
return QFile::copy(backupFilePath, filePath);
QFile::setPermissions(filePath, perms);
}
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/keys/FileKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ bool FileKey::create(const QString& fileName, QString* errorMsg, int size)
}
create(&file, size);
file.close();
file.setPermissions(QFile::ReadUser);

if (file.error()) {
if (errorMsg) {
Expand Down
6 changes: 3 additions & 3 deletions tests/gui/TestGuiFdoSecrets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ void TestGuiFdoSecrets::testCollectionCreate()

QCOMPARE(spyCollectionCreated.count(), 1);
{
auto args = spyCollectionCreated.takeFirst();
args = spyCollectionCreated.takeFirst();
QCOMPARE(args.size(), 1);
QCOMPARE(args.at(0).value<Collection*>(), coll);
}
Expand Down Expand Up @@ -754,7 +754,7 @@ void TestGuiFdoSecrets::testCollectionDelete()

QCOMPARE(spyCollectionDeleted.count(), 1);
{
auto args = spyCollectionDeleted.takeFirst();
args = spyCollectionDeleted.takeFirst();
QCOMPARE(args.size(), 1);
QCOMPARE(args.at(0).value<Collection*>(), rawColl);
}
Expand Down Expand Up @@ -977,7 +977,7 @@ void TestGuiFdoSecrets::testItemDelete()

QCOMPARE(spyItemDeleted.count(), 1);
{
auto args = spyItemDeleted.takeFirst();
args = spyItemDeleted.takeFirst();
QCOMPARE(args.size(), 1);
QCOMPARE(args.at(0).value<Item*>(), rawItem);
}
Expand Down