mirror of
https://github.com/apple/swift-system
synced 2026-11-24 04:38:14 +00:00
Use the proper temporary directory on Darwin.
It seems we can get the temporary directory from confstr(). rdar://125087707
This commit is contained in:
1 parent
d4e2b09c37
commit
85de4e51e2
3 files changed
+47
No files matched your search
@@ -111,7 +111,33 @@ internal func recursiveRemove(at path: FilePath) throws {
|
||||
|
||||
#else
|
||||
internal func getTemporaryDirectory() throws -> FilePath {
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
var capacity = 1024
|
||||
while true {
|
||||
let path: FilePath? = withUnsafeTemporaryAllocation(
|
||||
of: CInterop.PlatformChar.self,
|
||||
capacity: 1024) { buffer in
|
||||
let len = system_confstr(SYSTEM_CS_DARWIN_USER_TEMP_DIR,
|
||||
buffer.baseAddress!,
|
||||
buffer.count)
|
||||
if len == 0 {
|
||||
// Fall back to "/tmp" if we can't read the temp directory
|
||||
return "/tmp"
|
||||
}
|
||||
// If it was truncated, increase capaciy and try again
|
||||
if len > buffer.count {
|
||||
capacity = len
|
||||
return nil
|
||||
}
|
||||
return FilePath(SystemString(platformString: buffer.baseAddress!))
|
||||
}
|
||||
if let path = path {
|
||||
return path
|
||||
}
|
||||
}
|
||||
#else
|
||||
return "/tmp"
|
||||
#endif
|
||||
}
|
||||
|
||||
internal func recursiveRemove(at path: FilePath) throws {
|
||||
|
||||
@@ -173,6 +173,18 @@ internal func system_rmdir(
|
||||
return rmdir(path)
|
||||
}
|
||||
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
internal let SYSTEM_CS_DARWIN_USER_TEMP_DIR = _CS_DARWIN_USER_TEMP_DIR
|
||||
|
||||
internal func system_confstr(
|
||||
_ name: CInt,
|
||||
_ buf: UnsafeMutablePointer<CInterop.PlatformChar>,
|
||||
_ len: Int
|
||||
) -> Int {
|
||||
return confstr(name, buf, len)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !os(Windows)
|
||||
internal let SYSTEM_AT_REMOVE_DIR = AT_REMOVEDIR
|
||||
internal let SYSTEM_DT_DIR = DT_DIR
|
||||
|
||||
@@ -16,6 +16,15 @@ import System
|
||||
#endif
|
||||
|
||||
final class TemporaryPathTest: XCTestCase {
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
func testNotInSlashTmp() throws {
|
||||
try withTemporaryPath(basename: "NotInSlashTmp") { path in
|
||||
// We shouldn't be using "/tmp" on Darwin
|
||||
XCTAssertNotEqual(path.components.first!, "tmp")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func testUnique() throws {
|
||||
try withTemporaryPath(basename: "test") { path in
|
||||
let strPath = String(decoding: path)
|
||||
|
||||
Reference in new issue
Block a user