Commit f556e8f5 authored by Knut Petter Svendsen's avatar Knut Petter Svendsen Committed by Orgad Shaneh
Browse files

Fix: Make sure umask is used when creating new files



When a new file was created from the file menu, the permissions
on *nix was always 0600 regardless of the proess' current umask.
Fixed by letting CorePlugin::initialize() initialize the umask in
Utils::SaveFile. Since getting the system's umask is not thread
safe this can't be done directly in SaveFile::open.

Task-number: QTCREATORBUG-6513
Change-Id: I10d8b2f4ab85574ed3004b5e646664c2255196b9
Reviewed-by: default avatarOswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: default avatarOrgad Shaneh <orgads@gmail.com>
parent 7fcc52bf
......@@ -34,10 +34,13 @@
# include <windows.h>
#else
# include <unistd.h>
# include <sys/stat.h>
#endif
namespace Utils {
QFile::Permissions SaveFile::m_umask = 0;
SaveFile::SaveFile(const QString &filename) :
m_finalFileName(filename), m_finalized(true), m_backup(false)
{
......@@ -65,8 +68,19 @@ bool SaveFile::open(OpenMode flags)
return false;
m_finalized = false; // needs clean up in the end
if (ofi.exists())
if (ofi.exists()) {
setPermissions(ofi.permissions()); // Ignore errors
} else {
Permissions permAll = QFile::ReadOwner
| QFile::ReadGroup
| QFile::ReadOther
| QFile::WriteOwner
| QFile::WriteGroup
| QFile::WriteOther;
// set permissions with respect to the current umask
setPermissions(permAll & ~m_umask);
}
return true;
}
......@@ -114,4 +128,27 @@ bool SaveFile::commit()
return true;
}
void SaveFile::initializeUmask()
{
#ifdef Q_OS_WIN
m_umask = QFile::WriteGroup | QFile::WriteOther;
#else
// Get the current process' file creation mask (umask)
// umask() is not thread safe so this has to be done by single threaded
// application initialization
mode_t mask = umask(0); // get current umask
umask(mask); // set it back
m_umask = ((mask & S_IRUSR) ? QFile::ReadOwner : QFlags<QFile::Permission>(0))
| ((mask & S_IWUSR) ? QFile::WriteOwner : QFlags<QFile::Permission>(0))
| ((mask & S_IXUSR) ? QFile::ExeOwner : QFlags<QFile::Permission>(0))
| ((mask & S_IRGRP) ? QFile::ReadGroup : QFlags<QFile::Permission>(0))
| ((mask & S_IWGRP) ? QFile::WriteGroup : QFlags<QFile::Permission>(0))
| ((mask & S_IXGRP) ? QFile::ExeGroup : QFlags<QFile::Permission>(0))
| ((mask & S_IROTH) ? QFile::ReadOther : QFlags<QFile::Permission>(0))
| ((mask & S_IWOTH) ? QFile::WriteOther : QFlags<QFile::Permission>(0))
| ((mask & S_IXOTH) ? QFile::ExeOther : QFlags<QFile::Permission>(0));
#endif
}
} // namespace Utils
......@@ -49,10 +49,13 @@ public:
void setBackup(bool backup) { m_backup = backup; }
static void initializeUmask();
private:
const QString m_finalFileName;
bool m_finalized;
bool m_backup;
static QFile::Permissions m_umask;
};
} // namespace Utils
......
......@@ -40,6 +40,7 @@
#include "infobar.h"
#include <extensionsystem/pluginmanager.h>
#include <utils/savefile.h>
#include <QtPlugin>
#include <QDebug>
......@@ -95,6 +96,10 @@ bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage)
m_designMode = new DesignMode;
InfoBar::initializeGloballySuppressed();
}
// Make sure we respect the process's umask when creating new files
Utils::SaveFile::initializeUmask();
return success;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment