Skip to content

Commit

Permalink
imagewriter: Force permissions on settings to 600
Browse files Browse the repository at this point in the history
Use this mechanism to replace an older scheme that fully read and then fully wrote the file.

Resolves raspberrypi#770
  • Loading branch information
tdewey-rpi committed Jan 5, 2024
1 parent b918a7e commit 80768f3
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/imagewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,15 @@ ImageWriter::ImageWriter(QObject *parent)
#ifdef Q_OS_WIN
_taskbarButton = nullptr;
#endif

if (!_settings.isWritable() && !_settings.fileName().isEmpty())
{
/* Settings file is not writable, probably run by root previously */
QString settingsFile = _settings.fileName();
qDebug() << "Settings file" << settingsFile << "not writable. Recreating it";
QFile f(_settings.fileName());
QByteArray oldsettings;
const auto expectedPermissions = (QFileDevice::WriteOwner | QFileDevice::ReadOwner);
auto settingsFile = QFile(_settings.fileName());

if (f.open(f.ReadOnly))
{
oldsettings = f.readAll();
f.close();
}
f.remove();
if (f.open(f.WriteOnly))
{
f.write(oldsettings);
f.close();
_settings.sync();
}
else
{
qDebug() << "Error deleting and recreating settings file. Please remove manually.";
if (settingsFile.exists() && settingsFile.permissions() != expectedPermissions) {
if (!settingsFile.setPermissions(expectedPermissions)) {
qDebug() << "Failed to set expected permissions for settings file. Errant behaviour may follow, including settings failing to persist";
qDebug() << "To prevent future messages of this form, please delete " << _settings.fileName();
}
}
}

Expand Down

0 comments on commit 80768f3

Please sign in to comment.