Skip to content

Commit 7aadbcd

Browse files
committed
Allow Foundation to create file with permissions respects process umask
**Problem** `Data.write(to:)` is a only method in the Foundation that can create a regular file. However, it ignores `uamask` and always set 0600 permission unlike macOS Foundation, which respects process `umask`. **Solution** 1. With `.atomic` write option It uses `mkstemp(3)` in `_NSCreateTemporaryFile`, which is always creating a file with 0600 permission, if the system follows the latest POSIX specification or the permission is undefined. On macOS Foundation, therefore `_NSCreateTemporaryFile` uses `mktemp(3)` and `open(2)` instead to respect `umask`. 2. Without `.atomic` write option It uses `0o600` even if it uses `open(2)` that respects `umask`. Simply gives `0o666` instead. This is a bug caused by previous commit in swiftlang#1876. JIRA: https://bugs.swift.org/browse/SR-13307
1 parent 38b6914 commit 7aadbcd

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Sources/Foundation/NSData.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
434434
}
435435

436436
let fm = FileManager.default
437-
// The destination file path may not exist so provide a default file permissions of RW user only
438-
let permissions = (try? fm._permissionsOfItem(atPath: path)) ?? 0o600
437+
let permissions = try? fm._permissionsOfItem(atPath: path)
439438

440439
if writeOptionsMask.contains(.atomic) {
441440
let (newFD, auxFilePath) = try _NSCreateTemporaryFile(path)
@@ -446,7 +445,9 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
446445
// requires that there be no open handles to the file
447446
fh.closeFile()
448447
try _NSCleanupTemporaryFile(auxFilePath, path)
449-
try fm.setAttributes([.posixPermissions: NSNumber(value: permissions)], ofItemAtPath: path)
448+
if let permissions = permissions {
449+
try fm.setAttributes([.posixPermissions: NSNumber(value: permissions)], ofItemAtPath: path)
450+
}
450451
} catch {
451452
let savedErrno = errno
452453
try? fm.removeItem(atPath: auxFilePath)
@@ -457,11 +458,15 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
457458
if writeOptionsMask.contains(.withoutOverwriting) {
458459
flags |= O_EXCL
459460
}
461+
let createMode = Int(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
460462

461-
guard let fh = FileHandle(path: path, flags: flags, createMode: permissions) else {
463+
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
462464
throw _NSErrorWithErrno(errno, reading: false, path: path)
463465
}
464466
try doWrite(fh)
467+
if let permissions = permissions {
468+
try fm.setAttributes([.posixPermissions: NSNumber(value: permissions)], ofItemAtPath: path)
469+
}
465470
}
466471
}
467472

Sources/Foundation/NSPathUtilities.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,10 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
760760
let maxLength = Int(PATH_MAX) + 1
761761
var buf = [Int8](repeating: 0, count: maxLength)
762762
let _ = template._nsObject.getFileSystemRepresentation(&buf, maxLength: maxLength)
763-
let fd = mkstemp(&buf)
763+
guard let name = mktemp(&buf) else {
764+
throw _NSErrorWithErrno(errno, reading: false, path: filePath)
765+
}
766+
let fd = open(name, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
764767
if fd == -1 {
765768
throw _NSErrorWithErrno(errno, reading: false, path: filePath)
766769
}

0 commit comments

Comments
 (0)