Skip to content

[Windows] Add trailing slash to NSTemporaryDirectory result #2910

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
Oct 25, 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
15 changes: 9 additions & 6 deletions Sources/Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ let validPathSeps: [Character] = ["/"]
#endif

public func NSTemporaryDirectory() -> String {
func normalizedPath(with path: String) -> String {
if validPathSeps.contains(where: { path.hasSuffix(String($0)) }) {
return path
} else {
return path + String(validPathSeps.last!)
}
}
#if os(Windows)
let cchLength: DWORD = GetTempPathW(0, nil)
var wszPath: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(cchLength + 1))
guard GetTempPathW(DWORD(wszPath.count), &wszPath) <= cchLength else {
preconditionFailure("GetTempPathW mutation race")
}
return String(decodingCString: wszPath, as: UTF16.self).standardizingPath
return normalizedPath(with: String(decodingCString: wszPath, as: UTF16.self).standardizingPath)
#else
#if canImport(Darwin)
let safe_confstr = { (name: Int32, buf: UnsafeMutablePointer<Int8>?, len: Int) -> Int in
Expand Down Expand Up @@ -57,11 +64,7 @@ public func NSTemporaryDirectory() -> String {
}
#endif
if let tmpdir = ProcessInfo.processInfo.environment["TMPDIR"] {
if !validPathSeps.contains(where: { tmpdir.hasSuffix(String($0)) }) {
return tmpdir + "/"
} else {
return tmpdir
}
return normalizedPath(with: tmpdir)
}
#if os(Android)
// Bionic uses /data/local/tmp/ as temporary directory. TMPDIR is rarely
Expand Down
6 changes: 6 additions & 0 deletions Tests/Foundation/Tests/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class TestFileManager : XCTestCase {
let pathSep = "/"
#endif

func test_NSTemporaryDirectory() {
let tempDir = NSTemporaryDirectory()
XCTAssertTrue(validPathSeps.contains(where: { tempDir.hasSuffix(String($0)) }), "Temporary directory path must end with path separator")
}

func test_createDirectory() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "testdir\(NSUUID().uuidString)"
Expand Down Expand Up @@ -1974,6 +1979,7 @@ VIDEOS=StopgapVideos
/* ⚠️ */ ("test_replacement", testExpectedToFail(test_replacement,
/* ⚠️ */ "<https://bugs.swift.org/browse/SR-10819> Re-enable Foundation test TestFileManager.test_replacement")),
("test_concurrentGetItemReplacementDirectory", test_concurrentGetItemReplacementDirectory),
("test_NSTemporaryDirectory", test_NSTemporaryDirectory),
]

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