Skip to content

Commit cf3a823

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 f27b77b commit cf3a823

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@
1010
import CoreFoundation
1111

1212
public func NSTemporaryDirectory() -> String {
13-
#if os(macOS) || os(iOS)
13+
#if os(Windows)
14+
var cchLength: DWORD = GetTempPathW(0, nil)
15+
var wszPath: UnsafeMutableBufferPointer = UnsafeMutableBufferPointer<WCHAR>.allocate(capacity: Int(cchLength + 1))
16+
defer { wszPath.deallocate() }
17+
_ = GetTempPathW(cchLength + 1, wszPath.baseAddress)
18+
return String(decodingCString: wszPath.baseAddress!, as: UTF16.self)
19+
#else
20+
#if os(macOS) || os(iOS)
1421
var buf = [Int8](repeating: 0, count: 100)
1522
let r = confstr(_CS_DARWIN_USER_TEMP_DIR, &buf, buf.count)
1623
if r != 0 && r < buf.count {
1724
return String(cString: buf, encoding: .utf8)!
1825
}
19-
#endif
26+
#endif
2027
if let tmpdir = ProcessInfo.processInfo.environment["TMPDIR"] {
2128
if !tmpdir.hasSuffix("/") {
2229
return tmpdir + "/"
@@ -25,6 +32,7 @@ public func NSTemporaryDirectory() -> String {
2532
}
2633
}
2734
return "/tmp/"
35+
#endif
2836
}
2937

3038
extension String {

0 commit comments

Comments
 (0)