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

Set temp directory (override TMPDIR environment) #285

Closed
surban opened this issue Jul 25, 2024 · 9 comments · Fixed by #286
Closed

Set temp directory (override TMPDIR environment) #285

surban opened this issue Jul 25, 2024 · 9 comments · Fixed by #286

Comments

@surban
Copy link
Contributor

surban commented Jul 25, 2024

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
Copy link
Owner

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
Copy link
Contributor Author

surban commented Jul 25, 2024

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
Copy link
Contributor Author

surban commented Jul 25, 2024

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
Copy link
Owner

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

@surban
Copy link
Contributor Author

surban commented Jul 26, 2024

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

@Stebalien
Copy link
Owner

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 added a commit that referenced this issue Jul 26, 2024
This adds a new `env` module with `override_temp_dir` and `temp_dir`
functions.

- `temp_dir` will defer to `std::env::temp_dir` by default unless the
temporary directory has been overridden.
- `override_temp_dir` allows the user to override the default temporary
directory ONCE. Once this value has been set, it cannot be changed Care
should be taken to ensure that the chosen directory is actually writable.

This API is designed for use by the application author when the
application may run in an environment without a reliable global
temporary directory (e.g., Android).

fixes #285
@Stebalien
Copy link
Owner

Stebalien commented Jul 26, 2024

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

Stebalien added a commit that referenced this issue Jul 26, 2024
This adds a new `env` module with `override_temp_dir` and `temp_dir`
functions.

- `temp_dir` will defer to `std::env::temp_dir` by default unless the
temporary directory has been overridden.
- `override_temp_dir` allows the user to override the default temporary
directory ONCE. Once this value has been set, it cannot be changed Care
should be taken to ensure that the chosen directory is actually writable.

This API is designed for use by the application author when the
application may run in an environment without a reliable global
temporary directory (e.g., Android).

fixes #285
@surban
Copy link
Contributor Author

surban commented Jul 29, 2024

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 added a commit that referenced this issue Jul 29, 2024
This adds a new `env` module with `override_temp_dir` and `temp_dir`
functions.

- `temp_dir` will defer to `std::env::temp_dir` by default unless the
temporary directory has been overridden.
- `override_temp_dir` allows the user to override the default temporary
directory ONCE. Once this value has been set, it cannot be changed Care
should be taken to ensure that the chosen directory is actually writable.

This API is designed for use by the application author when the
application may run in an environment without a reliable global
temporary directory (e.g., Android).

fixes #285
@Stebalien
Copy link
Owner

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants