-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the ability to override the default temporary directory (#286)
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
- Loading branch information
Showing
9 changed files
with
80 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::env; | ||
use std::path::{Path, PathBuf}; | ||
|
||
// Once rust 1.70 is wide-spread (Debian stable), we can use OnceLock from stdlib. | ||
use once_cell::sync::OnceCell as OnceLock; | ||
|
||
static DEFAULT_TEMPDIR: OnceLock<PathBuf> = OnceLock::new(); | ||
|
||
/// Override the default temporary directory (defaults to [`std::env::temp_dir`]). This function | ||
/// changes the _global_ default temporary directory for the entire program and should not be called | ||
/// except in exceptional cases where it's not configured correctly by the platform. | ||
/// | ||
/// Only the first call to this function will succeed. All further calls will fail with `Err(path)` | ||
/// where `path` is previously set default temporary directory override. | ||
/// | ||
/// **NOTE:** This function does not check if the specified directory exists and/or is writable. | ||
pub fn override_temp_dir(path: &Path) -> Result<(), PathBuf> { | ||
let mut we_set = false; | ||
let val = DEFAULT_TEMPDIR.get_or_init(|| { | ||
we_set = true; | ||
path.to_path_buf() | ||
}); | ||
if we_set { | ||
Ok(()) | ||
} else { | ||
Err(val.to_owned()) | ||
} | ||
} | ||
|
||
/// Returns the default temporary directory, used for both temporary directories and files if no | ||
/// directory is explicitly specified. | ||
/// | ||
/// This function simply delegates to [`std::env::temp_dir`] unless the default temporary directory | ||
/// has been override by a call to [`override_temp_dir`]. | ||
/// | ||
/// **NOTE:** This function does check if the returned directory exists and/or is writable. | ||
pub fn temp_dir() -> PathBuf { | ||
DEFAULT_TEMPDIR | ||
.get() | ||
.map(|p| p.to_owned()) | ||
// Don't cache this in case the user uses std::env::set to change the temporary directory. | ||
.unwrap_or_else(env::temp_dir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters