Skip to content

Commit 87b5c70

Browse files
authored
Revert "Create a file with permissions that respects umask"
1 parent 7dbc009 commit 87b5c70

File tree

3 files changed

+5
-70
lines changed

3 files changed

+5
-70
lines changed

Sources/Foundation/NSData.swift

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

436436
let fm = FileManager.default
437-
let permissions = try? fm._permissionsOfItem(atPath: path)
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
438439

439440
if writeOptionsMask.contains(.atomic) {
440441
let (newFD, auxFilePath) = try _NSCreateTemporaryFile(path)
@@ -445,9 +446,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
445446
// requires that there be no open handles to the file
446447
fh.closeFile()
447448
try _NSCleanupTemporaryFile(auxFilePath, path)
448-
if let permissions = permissions {
449-
try fm.setAttributes([.posixPermissions: NSNumber(value: permissions)], ofItemAtPath: path)
450-
}
449+
try fm.setAttributes([.posixPermissions: NSNumber(value: permissions)], ofItemAtPath: path)
451450
} catch {
452451
let savedErrno = errno
453452
try? fm.removeItem(atPath: auxFilePath)
@@ -458,15 +457,11 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
458457
if writeOptionsMask.contains(.withoutOverwriting) {
459458
flags |= O_EXCL
460459
}
461-
let createMode = Int(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
462460

463-
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
461+
guard let fh = FileHandle(path: path, flags: flags, createMode: permissions) else {
464462
throw _NSErrorWithErrno(errno, reading: false, path: path)
465463
}
466464
try doWrite(fh)
467-
if let permissions = permissions {
468-
try fm.setAttributes([.posixPermissions: NSNumber(value: permissions)], ofItemAtPath: path)
469-
}
470465
}
471466
}
472467

Sources/Foundation/NSPathUtilities.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,7 @@ 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-
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)
763+
let fd = mkstemp(&buf)
767764
if fd == -1 {
768765
throw _NSErrorWithErrno(errno, reading: false, path: filePath)
769766
}

Tests/Foundation/Tests/TestNSData.swift

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
import CoreFoundation
11-
12-
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
13-
#if canImport(SwiftFoundation) && !DEPLOYMENT_RUNTIME_OBJC
14-
@testable import SwiftFoundation
15-
#else
16-
@testable import Foundation
17-
#endif
18-
#endif
19-
2010
class TestNSData: LoopbackServerTest {
2111

2212
class AllOnesImmutableData : NSData {
@@ -233,8 +223,6 @@ class TestNSData: LoopbackServerTest {
233223
("test_limitDebugDescription", test_limitDebugDescription),
234224
("test_edgeDebugDescription", test_edgeDebugDescription),
235225
("test_writeToURLOptions", test_writeToURLOptions),
236-
("test_writeToURLPermissions", test_writeToURLPermissions),
237-
("test_writeToURLPermissionsWithAtomic", test_writeToURLPermissionsWithAtomic),
238226
("test_edgeNoCopyDescription", test_edgeNoCopyDescription),
239227
("test_initializeWithBase64EncodedDataGetsDecodedData", test_initializeWithBase64EncodedDataGetsDecodedData),
240228
("test_initializeWithBase64EncodedDataWithNonBase64CharacterIsNil", test_initializeWithBase64EncodedDataWithNonBase64CharacterIsNil),
@@ -563,51 +551,6 @@ class TestNSData: LoopbackServerTest {
563551
}
564552
}
565553

566-
#if !os(Windows)
567-
// NOTE: `umask(3)` is process global. Therefore, the behavior is unknown if `withUmask(_:_:)` is used simultaniously.
568-
private func withUmask(_ mode: mode_t, _ block: () -> Void) {
569-
let original = umask(mode)
570-
block()
571-
umask(original)
572-
}
573-
#endif
574-
575-
func test_writeToURLPermissions() {
576-
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Windows)
577-
withUmask(0) {
578-
do {
579-
let data = Data()
580-
let url = URL(fileURLWithPath: NSTemporaryDirectory() + "meow")
581-
try data.write(to: url)
582-
let fileManager = FileManager.default
583-
let permission = try fileManager._permissionsOfItem(atPath: url.path)
584-
XCTAssertEqual(0o666, permission)
585-
try! fileManager.removeItem(atPath: url.path)
586-
} catch {
587-
XCTFail()
588-
}
589-
}
590-
#endif
591-
}
592-
593-
func test_writeToURLPermissionsWithAtomic() {
594-
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Windows)
595-
withUmask(0) {
596-
do {
597-
let data = Data()
598-
let url = URL(fileURLWithPath: NSTemporaryDirectory() + "meow")
599-
try data.write(to: url, options: .atomic)
600-
let fileManager = FileManager.default
601-
let permission = try fileManager._permissionsOfItem(atPath: url.path)
602-
XCTAssertEqual(0o666, permission)
603-
try! fileManager.removeItem(atPath: url.path)
604-
} catch {
605-
XCTFail()
606-
}
607-
}
608-
#endif
609-
}
610-
611554
func test_emptyDescription() {
612555
let expected = "<>"
613556

0 commit comments

Comments
 (0)