Skip to content

[5.0] SR-6531: FileManager copyItem() does not preserve permissions #1935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -664,7 +664,7 @@ open class FileManager : NSObject {
#endif
result[.modificationDate] = Date(timeIntervalSinceReferenceDate: ti)

result[.posixPermissions] = NSNumber(value: UInt64(s.st_mode & 0o7777))
result[.posixPermissions] = NSNumber(value: UInt64(s.st_mode & ~S_IFMT))
result[.referenceCount] = NSNumber(value: UInt64(s.st_nlink))
result[.systemNumber] = NSNumber(value: UInt64(s.st_dev))
result[.systemFileNumber] = NSNumber(value: UInt64(s.st_ino))
Expand Down Expand Up @@ -836,6 +836,13 @@ open class FileManager : NSObject {
}
defer { close(dstfd) }

// Set the file permissions using fchmod() instead of when open()ing to avoid umask() issues
let permissions = fileInfo.st_mode & ~S_IFMT
guard fchmod(dstfd, permissions) == 0 else {
throw _NSErrorWithErrno(errno, reading: false, path: dstPath,
extraUserInfo: extraErrorInfo(srcPath: srcPath, dstPath: dstPath, userVariant: variant))
}

if fileInfo.st_size == 0 {
// no copying required
return
Expand Down
45 changes: 45 additions & 0 deletions TestFoundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TestFileManager : XCTestCase {
("test_temporaryDirectoryForUser", test_temporaryDirectoryForUser),
("test_creatingDirectoryWithShortIntermediatePath", test_creatingDirectoryWithShortIntermediatePath),
("test_mountedVolumeURLs", test_mountedVolumeURLs),
("test_copyItemsPermissions", test_copyItemsPermissions),
]

#if !DEPLOYMENT_RUNTIME_OBJC && NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
Expand Down Expand Up @@ -1067,6 +1068,50 @@ class TestFileManager : XCTestCase {
XCTAssertFalse(fm.contentsEqual(atPath: dataFile1.path, andPath: dataFile2.path))
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path))
}

func test_copyItemsPermissions() throws {
let fm = FileManager.default
let tmpDir = fm.temporaryDirectory.appendingPathComponent("test_copyItemsPermissions")
try fm.createDirectory(at: tmpDir, withIntermediateDirectories: true)
defer { try? fm.removeItem(atPath: tmpDir.path) }

let srcFile = tmpDir.appendingPathComponent("file1.txt")
let destFile = tmpDir.appendingPathComponent("file2.txt")

let source = "This is the source file"
try? fm.removeItem(at: srcFile)
try source.write(toFile: srcFile.path, atomically: false, encoding: .utf8)

func testCopy() throws {
try? fm.removeItem(at: destFile)
try fm.copyItem(at: srcFile, to: destFile)
let copy = try String(contentsOf: destFile)
XCTAssertEqual(source, copy)
if let srcPerms = (try fm.attributesOfItem(atPath: srcFile.path)[.posixPermissions] as? NSNumber)?.intValue,
let destPerms = (try fm.attributesOfItem(atPath: destFile.path)[.posixPermissions] as? NSNumber)?.intValue {
XCTAssertEqual(srcPerms, destPerms)
} else {
XCTFail("Cant get file permissions")
}
}

try testCopy()

try fm.setAttributes([ .posixPermissions: 0o417], ofItemAtPath: srcFile.path)
try testCopy()

try fm.setAttributes([ .posixPermissions: 0o400], ofItemAtPath: srcFile.path)
try testCopy()

try fm.setAttributes([ .posixPermissions: 0o700], ofItemAtPath: srcFile.path)
try testCopy()

try fm.setAttributes([ .posixPermissions: 0o707], ofItemAtPath: srcFile.path)
try testCopy()

try fm.setAttributes([ .posixPermissions: 0o411], ofItemAtPath: srcFile.path)
try testCopy()
}

#if !DEPLOYMENT_RUNTIME_OBJC // XDG tests require swift-corelibs-foundation

Expand Down