Skip to content

[5.3] Return success from FileManager recursive directory creation even if a directory was created by another thread concurrently. #2833

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 1 commit into from
Jul 9, 2020
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
12 changes: 10 additions & 2 deletions Sources/Foundation/FileManager+POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,16 @@ extension FileManager {
try createDirectory(atPath: parent, withIntermediateDirectories: true, attributes: attributes)
}
if mkdir(pathFsRep, S_IRWXU | S_IRWXG | S_IRWXO) != 0 {
throw _NSErrorWithErrno(errno, reading: false, path: path)
} else if let attr = attributes {
let posixError = errno
if posixError == EEXIST && fileExists(atPath: path, isDirectory: &isDir) && isDir.boolValue {
// Continue; if there is an existing file and it is a directory, that is still a success.
// There can be an existing file if another thread or process concurrently creates the
// same file.
} else {
throw _NSErrorWithErrno(posixError, reading: false, path: path)
}
}
if let attr = attributes {
try self.setAttributes(attr, ofItemAtPath: path)
}
} else if isDir.boolValue {
Expand Down
46 changes: 46 additions & 0 deletions Tests/Foundation/Tests/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#endif
#endif

import Dispatch

class TestFileManager : XCTestCase {
#if os(Windows)
let pathSep = "\\"
Expand Down Expand Up @@ -1755,6 +1757,49 @@ VIDEOS=StopgapVideos
}
#endif
}

/**
Tests that we can get an item replacement directory concurrently.

- Bug: [SR-12272](https://bugs.swift.org/browse/SR-12272)
*/
func test_concurrentGetItemReplacementDirectory() throws {
let fileManager = FileManager.default

let operationCount = 10

var directoryURLs = [URL?](repeating: nil, count: operationCount)
var errors = [Error?](repeating: nil, count: operationCount)

let dispatchGroup = DispatchGroup()
for operationIndex in 0..<operationCount {
DispatchQueue.global().async(group: dispatchGroup) {
do {
let directory = try fileManager.url(for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: URL(fileURLWithPath: NSTemporaryDirectory(),
isDirectory: true),
create: true)
directoryURLs[operationIndex] = directory
} catch {
errors[operationIndex] = error
}
}
}
dispatchGroup.wait()

for directoryURL in directoryURLs {
if let directoryURL = directoryURL {
try? fileManager.removeItem(at: directoryURL)
}
}

for error in errors {
if let error = error {
XCTFail("One of the concurrent calls to get the item replacement directory failed: \(error)")
}
}
}

// -----

Expand Down Expand Up @@ -1812,6 +1857,7 @@ VIDEOS=StopgapVideos
("test_contentsEqual", test_contentsEqual),
/* ⚠️ */ ("test_replacement", testExpectedToFail(test_replacement,
/* ⚠️ */ "<https://bugs.swift.org/browse/SR-10819> Re-enable Foundation test TestFileManager.test_replacement")),
("test_concurrentGetItemReplacementDirectory", test_concurrentGetItemReplacementDirectory),
]

#if !DEPLOYMENT_RUNTIME_OBJC && NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Android)
Expand Down