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: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Knut Petter Svendsen
2013-01-28 21:15:04 +01:00
committed by Orgad Shaneh
parent 7fcc52bf47
commit f556e8f5f2
3 changed files with 46 additions and 1 deletions

View File

@@ -34,10 +34,13 @@
# include <windows.h> # include <windows.h>
#else #else
# include <unistd.h> # include <unistd.h>
# include <sys/stat.h>
#endif #endif
namespace Utils { namespace Utils {
QFile::Permissions SaveFile::m_umask = 0;
SaveFile::SaveFile(const QString &filename) : SaveFile::SaveFile(const QString &filename) :
m_finalFileName(filename), m_finalized(true), m_backup(false) m_finalFileName(filename), m_finalized(true), m_backup(false)
{ {
@@ -65,8 +68,19 @@ bool SaveFile::open(OpenMode flags)
return false; return false;
m_finalized = false; // needs clean up in the end m_finalized = false; // needs clean up in the end
if (ofi.exists()) if (ofi.exists()) {
setPermissions(ofi.permissions()); // Ignore errors 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; return true;
} }
@@ -114,4 +128,27 @@ bool SaveFile::commit()
return true; 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 } // namespace Utils

View File

@@ -49,10 +49,13 @@ public:
void setBackup(bool backup) { m_backup = backup; } void setBackup(bool backup) { m_backup = backup; }
static void initializeUmask();
private: private:
const QString m_finalFileName; const QString m_finalFileName;
bool m_finalized; bool m_finalized;
bool m_backup; bool m_backup;
static QFile::Permissions m_umask;
}; };
} // namespace Utils } // namespace Utils

View File

@@ -40,6 +40,7 @@
#include "infobar.h" #include "infobar.h"
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <utils/savefile.h>
#include <QtPlugin> #include <QtPlugin>
#include <QDebug> #include <QDebug>
@@ -95,6 +96,10 @@ bool CorePlugin::initialize(const QStringList &arguments, QString *errorMessage)
m_designMode = new DesignMode; m_designMode = new DesignMode;
InfoBar::initializeGloballySuppressed(); InfoBar::initializeGloballySuppressed();
} }
// Make sure we respect the process's umask when creating new files
Utils::SaveFile::initializeUmask();
return success; return success;
} }