Stebalien / tempfile

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

feat: add the ability to override the default temporary directory #286

Closed Stebalien closed 1 month ago

Stebalien commented 1 month ago

This adds a new env module with override_temp_dir and temp_dir functions.

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 commented 1 month ago

(needs test)

surban commented 1 month ago

Generally I am not sure if it makes sense to limit the override function to be only called once. Is there a technical reason why you want to prevent it? It was possible before to call set_env multiple times.

For example, a system utility running during boot (initrd) might need to change its temporary directory after mounting some file systems.

Stebalien commented 1 month ago

I can relax the constraints later if someone presents a strong use-case, but tightening them isn't possible so I'm starting with the strongest constraints.

My reason is that override_temp_dir exists only to handle the case where the OS doesn't set an appropriate temporary directory. It's a last-resort escape hatch. I don't want is multiple libraries calling override_temp_dir, then getting confused when their setting gets clobbered by some other library.

In general, applications needing to put specific files in specific directories should use tempfile_in and friends.

For example, a system utility running during boot (initrd) might need to change its temporary directory after mounting some file systems.

Init systems re-exec themselves for this reason. Changing the tempdir at runtime likely isn't going to have the desired result (old temporary files will still live in the old location).

Stebalien commented 1 month ago

Merging this for now. I'll release at the end of the week so we can continue our discussion until then (and possibly iterate on the design).