File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -11,11 +11,14 @@ import CoreFoundation
11
11
12
12
public func NSTemporaryDirectory( ) -> String {
13
13
#if os(macOS) || os(iOS)
14
- var buf = [ Int8] ( repeating: 0 , count: 100 )
15
- let r = confstr ( _CS_DARWIN_USER_TEMP_DIR, & buf, buf. count)
16
- if r != 0 && r < buf. count {
17
- return String ( cString: buf, encoding: . utf8) !
18
- }
14
+ var length : Int = confstr ( _CS_DARWIN_USER_TEMP_DIR, nil , 0 )
15
+ guard length > 0 else { return " /tmp " }
16
+
17
+ var buffer : [ Int8 ] = Array < Int8 > ( repeating: 0 , count: length)
18
+ length = confstr ( _CS_DARWIN_USER_TEMP_DIR, & buffer, buffer. count)
19
+ precondition ( length > 0 && length < buffer. count,
20
+ " _CS_DARWIN_USER_TEMP_DIR mutation race " )
21
+ return String ( cString: buffer, encoding: . utf8) !
19
22
#elseif os(Windows)
20
23
var cchLength : DWORD = GetTempPathW ( 0 , nil )
21
24
var wszPath : UnsafeMutableBufferPointer = UnsafeMutableBufferPointer< WCHAR> . allocate( capacity: Int ( cchLength + 1 ) )
You can’t perform that action at this time.
0 commit comments