Skip to content

Commit 6cf2852

Browse files
committed
Foundation: adjust NSTemporaryDirectory for Windows
Windows provides `GetTempPath` for getting the temporary directory which consults the environment variables and falls back through a cascading set ultimately ending up at `%SystemRoot%\Temp` if nothing else is found. The last path is not exactly great, but, we do not expect to end up there as modern Windows will use `%UserProfile%\Local Settings` before that.
1 parent e2dd087 commit 6cf2852

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
import CoreFoundation
1111

1212
public func NSTemporaryDirectory() -> String {
13+
#if os(Windows)
14+
let cchLength: DWORD = GetTempPathW(0, nil)
15+
var wszPath: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(cchLength + 1))
16+
guard GetTempPathW(DWORD(wszPath.count), &wszPath) == cchLength else {
17+
precondition(false, "GetTempPathW mutation race")
18+
}
19+
return String(decodingCString: wszPath, as: UTF16.self)
20+
#else
1321
#if canImport(Darwin)
1422
var length: Int = confstr(_CS_DARWIN_USER_TEMP_DIR, nil, 0)
1523
if length > 0 {
@@ -28,6 +36,7 @@ public func NSTemporaryDirectory() -> String {
2836
}
2937
}
3038
return "/tmp/"
39+
#endif
3140
}
3241

3342
extension String {

0 commit comments

Comments
 (0)