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

Conversation

natikgadzhi
Copy link
Contributor

Motivation:

This PR aligns what temp directory NIOFileSystem will return first:

  • First try ${TMPDIR} as it's an expressed user preference
  • On Darwin, try _CS_DARWIN_USER_TMP_DIR next. If it's set, return that.
  • Finally, fall back on /tmp on Darwin and Linux, and /data/local/tmp on Android.

Closes #2861.

Modifications:

  • Reworks NIOFileSystem.temporaryDirectory.

Result:

  • NIOFileSystem will now try ${TMPDIR} first for the temporary directory.

Caveats:

Motivation:

This PR aligns what temp directory NIOFileSystem will return first:
- First try `${TMPDIR}` as it's an expressed user preference
- On Darwin, try `_CS_DARWIN_USER_TMP_DIR` next. If it's set, return that.
- Finally, fall back on `/tmp` on Darwin and Linux, and `/data/local/tmp` on Android.

Closes apple#2861.

Modifications:

- Reworks `NIOFileSystem.temporaryDirectory`.

Result:

- NIOFileSystem will now try `${TMPDIR}` first for the temporary directory.

Caveats:
- We might want to align how Swift-NIO and FoundationEssentials resolve temp directory,
  and [FoundationEssentials have a different approach](https://github.com/swiftlang/swift-foundation/blob/9d57f36de757b3d5e3a2f7ffcf27aaec3033509f/Sources/FoundationEssentials/String/String%2BPath.swift#L484-L533). But, I agree with [this comment](apple#2861 (comment)), it feels like TMPDIR is an expected default.
@natikgadzhi natikgadzhi marked this pull request as ready for review January 17, 2025 05:19
@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jan 17, 2025
@Lukasa Lukasa requested a review from glbrntt January 17, 2025 17:18
@Lukasa
Copy link
Contributor

Lukasa commented Jan 17, 2025

Thanks for this patch! I've asked @glbrntt to review, as he's most familiar with this section of the code.

@finagolfin
Copy link
Contributor

Thanks, I prefer this for Android and currently patch this to use Foundation's temporaryDirectory logic instead, both on my Android CI and when testing this repo natively in the Termux app, which I can finally remove once this goes in. 👍

Copy link
Contributor

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

Thanks, the "actual" change looks great. I left a comment on the test though.

Comment on lines +1685 to +1695
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)
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NIOFileSystem.temporaryDirectory: Consider falling back to TMPDIR
4 participants