-
Notifications
You must be signed in to change notification settings - Fork 122
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
feat: add the ability to override the default temporary directory #286
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,40 @@ | ||
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, returning `Ok(path)`. All further calls will | ||
/// fail with `Err(path)`. In both cases, `path` is the default temporary directory (on success, the | ||
/// new one; on failure, the existing one). | ||
pub fn override_temp_dir(path: &Path) -> Result<&'static Path, &'static Path> { | ||
let mut we_set = false; | ||
let val = DEFAULT_TEMPDIR.get_or_init(|| { | ||
we_set = true; | ||
path.to_path_buf() | ||
}); | ||
if we_set { | ||
Ok(val) | ||
} else { | ||
Err(val) | ||
} | ||
} | ||
|
||
/// 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`]. | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered treating this as a fallback (TMPDIR -> the override -> "/tmp"), but handling all the platform-specific cases got hairy.
I also considered testing if the various temporary directories were writable.... but honestly, I don't want to be in the position of "guessing" where temporary files should be written (although I'll revisit that later if this keeps coming up).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest
Result<(), TempDirAlreadyOverridden>
as the return type.In the success case there is not point in returning the value just passed in.
In the error case it is problematic that
Path
does not implementDisplay
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs: Is there a requirement that the directory already exists (I think so) or will it be created (recursively) if non-existent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns a static reference, which could be convenient if you need to know where the temporary directory is. But... it's inconsistent with
temp_dir
, so it's probably not all that useful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. Ok, I'm going to change it to return
Result<(), PathBuf>
, but I actually don't want the user tounwrap()
this error, they need to handle it.