Stebalien / tempfile

Temporary file library for rust
http://stebalien.com/projects/tempfile-rs
Apache License 2.0
1.18k stars 115 forks source link

Set temp directory (override TMPDIR environment) #285

Closed surban closed 3 months ago

surban commented 3 months ago

Now that std::env::set_var is unsafe and essentially unusable anymore, it would be great if tempfile provided a method to override the global temporary directory on UNIX.

Stebalien commented 3 months ago

I'd like to hear a few more details on what you're trying to do here. In general:

  1. If you're writing your own program, you can safely use std::env::set_var early at startup. Although, TBH, you should really just use the default specified by the user.
  2. If you're not writing your own program but instead writing a library, you shouldn't be changing global defaults anyways.
surban commented 3 months ago

We are writing a Rust dynamic library that is loaded into a bigger program. We do not control when the library is loaded and initialized, thus multiple threads might already be running. For some OS (for example Android) we need to manually set the TMPDIR location, since the default path is inaccessible.

surban commented 3 months ago

If you don't like the idea, a workaround would be to use $HOME/cache/tmp on Android, since $HOME points to the package directory and thus should be writable. This would probably already account for 90% of the use cases.

Stebalien commented 3 months ago

We use https://doc.rust-lang.org/std/env/fn.temp_dir.html. Are you sure you're compiling for the right target?

surban commented 3 months ago

Yes, but /data/local/tmp is not accessible anymore in recent Android versions.

Stebalien commented 3 months ago

That's... really annoying. Apparently new enough versions of android may set TMPDIR (according to a random internet comment that I probably shouldn't trust).

I'll add an override.

Stebalien commented 3 months ago

@surban please review #286. Also, would you mind contributing a test case (make a PR against that PR)?

surban commented 3 months ago

That's... really annoying. Apparently new enough versions of android may set TMPDIR (according to a random internet comment that I probably shouldn't trust).

I'll add an override.

Tested with Google Pixel 8 and latest update. TMPDIR is not set in a package context, i.e. adb shell run-as <package> printenv TMPDIR is empty.

Stebalien commented 3 months ago

Tested with Google Pixel 8 and latest update. TMPDIR is not set in a package context, i.e. adb shell run-as printenv TMPDIR is empty.

Then I guess the random internet comment was a lie (and google really needs to fix their shit).