Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIOFileSystem: Try ${TMPDIR} first for temporary directory #3067

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions Sources/NIOFileSystem/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -636,31 +636,35 @@ public struct FileSystem: Sendable, FileSystemProtocol {
///
/// #### Implementation details
///
/// On Darwin this function uses `confstr(3)` and gets the value of `_CS_DARWIN_USER_TEMP_DIR`;
/// the users temporary directory. Typically items are removed after three days if they are not
/// accessed.
///
/// On Linux this returns "/tmp".
/// On all platforms, this function first attempts to read the `TMPDIR` environment variable.
/// If that fails:
/// - On Darwin this function uses `confstr(3)` and gets the value of `_CS_DARWIN_USER_TEMP_DIR`;
/// the users temporary directory. Typically items are removed after three days if they are not
/// accessed.
/// - On Android this returns "/data/local/tmp".
/// - On other platforms this returns "/tmp".
///
/// - Returns: The path to a temporary directory.
public var temporaryDirectory: FilePath {
get async throws {
if let tmpdir = getenv("TMPDIR") {
return FilePath(String(cString: tmpdir))
}

#if canImport(Darwin)
return try await self.threadPool.runIfActive {
try Libc.constr(_CS_DARWIN_USER_TEMP_DIR).map { path in
FilePath(path)
}.mapError { errno in
FileSystemError.confstr(
name: "_CS_DARWIN_USER_TEMP_DIR",
errno: errno,
location: .here()
)
}.get()
let result = Libc.constr(_CS_DARWIN_USER_TEMP_DIR)
switch result {
case .success(let path):
return FilePath(path)
case .failure(_):
return FilePath("/tmp")
}
}
#elseif os(Android)
return "/data/local/tmp"
return FilePath("/data/local/tmp")
#else
return "/tmp"
return FilePath("/tmp")
#endif
}
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/NIOFileSystemIntegrationTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,22 @@ extension FileSystemTests {
XCTAssertGreaterThan(info.size, 0)
}

func testTemporaryDirectoryRespectsEnvironmentVariable() async throws {
let originalTMPDIR = getenv("TMPDIR")
defer {
if let originalTMPDIR {
setenv("TMPDIR", String(cString: originalTMPDIR), 1)
} else {
unsetenv("TMPDIR")
}
}

let customTmpDir = "/custom/tmp/dir"
setenv("TMPDIR", customTmpDir, 1)
Comment on lines +1685 to +1695
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit hesitant to mess around with TMPDIR like this in case other tests running in parallel are relying on it.

II think my preference here would be to check if TMPDIR is set and then check fs.temporaryDirectory if it was set.

let tmpDir = try await fs.temporaryDirectory
XCTAssertEqual(tmpDir.string, customTmpDir)
}

func testReadChunksRange() async throws {
try await self.fs.withFileHandle(forReadingAt: FilePath(#filePath)) { handle in
let info = try await handle.info()
Expand Down
Loading