diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2021-11-10 16:52:07 +0100 |
---|---|---|
committer | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2021-11-26 03:39:33 +0100 |
commit | 174af05400f6344a11f4aa2228244c954cbbca97 (patch) | |
tree | df5edd252f5d0bd4b865c3ace2473fbbfb80d087 /src/corelib/io/qfilesystemengine_unix.cpp | |
parent | 8c9875893bbd7b1355e36d9501d8cb1f84cb6b4d (diff) |
QDir: Add support for setting directory permissions to mkdir()
This patch adds an overload of the QDir::mkdir() method that
accepts permissions. This allows setting of the directory
permissions at the time of its creation.
[ChangeLog][QtCore][QDir] Added QDir::mdkir() overload that
accepts permissions argument.
Task-number: QTBUG-79750
Change-Id: Ic9db723b94ff0d2da6e0b819ac2e5d1f9a4e2049
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/io/qfilesystemengine_unix.cpp')
-rw-r--r-- | src/corelib/io/qfilesystemengine_unix.cpp | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 8c2df84b72..6e8229daec 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -1110,7 +1110,8 @@ bool QFileSystemEngine::cloneFile(int srcfd, int dstfd, const QFileSystemMetaDat // Note: if \a shouldMkdirFirst is false, we assume the caller did try to mkdir // before calling this function. -static bool createDirectoryWithParents(const QByteArray &nativeName, bool shouldMkdirFirst = true) +static bool createDirectoryWithParents(const QByteArray &nativeName, mode_t mode, + bool shouldMkdirFirst = true) { // helper function to check if a given path is a directory, since mkdir can // fail if the dir already exists (it may have been created by another @@ -1120,7 +1121,7 @@ static bool createDirectoryWithParents(const QByteArray &nativeName, bool should return QT_STAT(nativeName.constData(), &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR; }; - if (shouldMkdirFirst && QT_MKDIR(nativeName, 0777) == 0) + if (shouldMkdirFirst && QT_MKDIR(nativeName, mode) == 0) return true; if (errno == EISDIR) return true; @@ -1135,17 +1136,18 @@ static bool createDirectoryWithParents(const QByteArray &nativeName, bool should return false; QByteArray parentNativeName = nativeName.left(slash); - if (!createDirectoryWithParents(parentNativeName)) + if (!createDirectoryWithParents(parentNativeName, mode)) return false; // try again - if (QT_MKDIR(nativeName, 0777) == 0) + if (QT_MKDIR(nativeName, mode) == 0) return true; return errno == EEXIST && isDir(nativeName); } //static -bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool createParents) +bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool createParents, + std::optional<QFile::Permissions> permissions) { QString dirName = entry.filePath(); Q_CHECK_FILE_NAME(dirName, false); @@ -1156,12 +1158,13 @@ bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool crea // try to mkdir this directory QByteArray nativeName = QFile::encodeName(dirName); - if (QT_MKDIR(nativeName, 0777) == 0) + mode_t mode = permissions ? QtPrivate::toMode_t(*permissions) : 0777; + if (QT_MKDIR(nativeName, mode) == 0) return true; if (!createParents) return false; - return createDirectoryWithParents(nativeName, false); + return createDirectoryWithParents(nativeName, mode, false); } //static |