Skip to content

Commit 596fd8f

Browse files
committed
Implement NSTemporaryDirectory
* Darwin should check against `confstr` using `_CS_DARWIN_USER_TEMP_DIR` * All platforms check against TMPDIR environment variable * Fall back to /tmp/
1 parent 8c33544 commit 596fd8f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ import Darwin
1515
import Glibc
1616
#endif
1717

18+
public func NSTemporaryDirectory() -> String {
19+
#if os(OSX) || os(iOS)
20+
var buf = [Int8](count: 100, repeatedValue: 0)
21+
let r = confstr(_CS_DARWIN_USER_TEMP_DIR, &buf, buf.count)
22+
if r != 0 && r < buf.count {
23+
return String(CString: buf, encoding: NSUTF8StringEncoding)!
24+
}
25+
#endif
26+
if let tmpdir = NSProcessInfo.processInfo().environment["TMPDIR"] {
27+
return tmpdir
28+
}
29+
return "/tmp/"
30+
}
31+
1832
internal extension String {
1933

2034
internal var _startOfLastPathComponent : String.CharacterView.Index {

TestFoundation/TestNSURL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class TestNSURL : XCTestCase {
225225

226226
}
227227

228-
static let gBaseTemporaryDirectoryPath = "/tmp/" // TODO: NSTemporaryDirectory()
228+
static let gBaseTemporaryDirectoryPath = NSTemporaryDirectory()
229229
static var gBaseCurrentWorkingDirectoryPath : String {
230230
let count = Int(1024) // MAXPATHLEN is platform specific; this is the lowest common denominator for darwin and most linuxes
231231
var buf : [Int8] = Array(count: count, repeatedValue: 0)

0 commit comments

Comments
 (0)