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>
#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