Move toMode_t() from qfilesystemengine_unix.cpp to qfiledevice_p.h

The function is needed in multiple files to implement QDir::mkdir()
and QFile::open() methods that accept a permission argument on Unix.

Task-number: QTBUG-79750
Change-Id: Ib0853f7d12bcb5d6f1116b43ec1aa07b6554bb93
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ievgenii Meshcheriakov 2021-10-26 14:52:36 +02:00
parent 7db0eeae34
commit 1cc9ac71ac
2 changed files with 37 additions and 28 deletions

View File

@ -54,6 +54,10 @@
#include "private/qiodevice_p.h" #include "private/qiodevice_p.h"
#include <memory> #include <memory>
#ifdef Q_OS_UNIX
# include <sys/types.h> // for mode_t
# include <sys/stat.h> // for mode_t constants
#endif
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -98,6 +102,36 @@ inline bool QFileDevicePrivate::ensureFlushed() const
return true; return true;
} }
#ifdef Q_OS_UNIX
namespace QtPrivate {
constexpr mode_t toMode_t(QFileDevice::Permissions permissions)
{
mode_t mode = 0;
if (permissions & (QFileDevice::ReadOwner | QFileDevice::ReadUser))
mode |= S_IRUSR;
if (permissions & (QFileDevice::WriteOwner | QFileDevice::WriteUser))
mode |= S_IWUSR;
if (permissions & (QFileDevice::ExeOwner | QFileDevice::ExeUser))
mode |= S_IXUSR;
if (permissions & QFileDevice::ReadGroup)
mode |= S_IRGRP;
if (permissions & QFileDevice::WriteGroup)
mode |= S_IWGRP;
if (permissions & QFileDevice::ExeGroup)
mode |= S_IXGRP;
if (permissions & QFileDevice::ReadOther)
mode |= S_IROTH;
if (permissions & QFileDevice::WriteOther)
mode |= S_IWOTH;
if (permissions & QFileDevice::ExeOther)
mode |= S_IXOTH;
return mode;
}
} // namespace QtPrivate
#endif // Q_OS_UNIX
QT_END_NAMESPACE QT_END_NAMESPACE
#endif // QFILEDEVICE_P_H #endif // QFILEDEVICE_P_H

View File

@ -46,6 +46,7 @@
#include <QtCore/qoperatingsystemversion.h> #include <QtCore/qoperatingsystemversion.h>
#include <QtCore/private/qcore_unix_p.h> #include <QtCore/private/qcore_unix_p.h>
#include <QtCore/private/qfiledevice_p.h>
#include <QtCore/qvarlengtharray.h> #include <QtCore/qvarlengtharray.h>
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
# include <QtCore/qstandardpaths.h> # include <QtCore/qstandardpaths.h>
@ -53,8 +54,6 @@
#include <pwd.h> #include <pwd.h>
#include <stdlib.h> // for realpath() #include <stdlib.h> // for realpath()
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -1527,36 +1526,12 @@ bool QFileSystemEngine::removeFile(const QFileSystemEntry &entry, QSystemError &
} }
static mode_t toMode_t(QFile::Permissions permissions)
{
mode_t mode = 0;
if (permissions & (QFile::ReadOwner | QFile::ReadUser))
mode |= S_IRUSR;
if (permissions & (QFile::WriteOwner | QFile::WriteUser))
mode |= S_IWUSR;
if (permissions & (QFile::ExeOwner | QFile::ExeUser))
mode |= S_IXUSR;
if (permissions & QFile::ReadGroup)
mode |= S_IRGRP;
if (permissions & QFile::WriteGroup)
mode |= S_IWGRP;
if (permissions & QFile::ExeGroup)
mode |= S_IXGRP;
if (permissions & QFile::ReadOther)
mode |= S_IROTH;
if (permissions & QFile::WriteOther)
mode |= S_IWOTH;
if (permissions & QFile::ExeOther)
mode |= S_IXOTH;
return mode;
}
//static //static
bool QFileSystemEngine::setPermissions(const QFileSystemEntry &entry, QFile::Permissions permissions, QSystemError &error, QFileSystemMetaData *data) bool QFileSystemEngine::setPermissions(const QFileSystemEntry &entry, QFile::Permissions permissions, QSystemError &error, QFileSystemMetaData *data)
{ {
Q_CHECK_FILE_NAME(entry, false); Q_CHECK_FILE_NAME(entry, false);
mode_t mode = toMode_t(permissions); mode_t mode = QtPrivate::toMode_t(permissions);
bool success = ::chmod(entry.nativeFilePath().constData(), mode) == 0; bool success = ::chmod(entry.nativeFilePath().constData(), mode) == 0;
if (success && data) { if (success && data) {
data->entryFlags &= ~QFileSystemMetaData::Permissions; data->entryFlags &= ~QFileSystemMetaData::Permissions;
@ -1571,7 +1546,7 @@ bool QFileSystemEngine::setPermissions(const QFileSystemEntry &entry, QFile::Per
//static //static
bool QFileSystemEngine::setPermissions(int fd, QFile::Permissions permissions, QSystemError &error, QFileSystemMetaData *data) bool QFileSystemEngine::setPermissions(int fd, QFile::Permissions permissions, QSystemError &error, QFileSystemMetaData *data)
{ {
mode_t mode = toMode_t(permissions); mode_t mode = QtPrivate::toMode_t(permissions);
bool success = ::fchmod(fd, mode) == 0; bool success = ::fchmod(fd, mode) == 0;
if (success && data) { if (success && data) {