-
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
Builder::permissions()
method. (#273)
* feat: Add `Builder::permissions()` method. With it it's possible to change the default mode of tempfiles on unix systems. The example also doesn't hide the fact that it is currently only useful on Unix, but as the ecosystem matures, thanks to the usage of `std::fs::Permissions` it should be able grow into more platforms over time. * Use `permissions` field of `Builder` on Unix to affect file mode The implementation favors the least invasive solution even though its forced to pull platform dependent code up. The alternative would have been to alter the method signatures of `create_named()` on all platforms. * Respect module boundaries and make all platforms permissions aware. This way, permissions can be passed and handled by platform specific code, which avoid having to use platform dependent code in the top-level. * Fail on Windows if somebody tries to set permissions there. That way, there will be no surprises as would be the case with doing nothing. * Add support for setting permissions on directories as well.
- Loading branch information
Showing
10 changed files
with
192 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::error::IoResultExt; | ||
use crate::TempDir; | ||
use std::path::PathBuf; | ||
use std::{fs, io}; | ||
|
||
fn not_supported<T>(msg: &str) -> io::Result<T> { | ||
Err(io::Error::new(io::ErrorKind::Other, msg)) | ||
} | ||
|
||
pub fn create(path: PathBuf, permissions: Option<&std::fs::Permissions>) -> io::Result<TempDir> { | ||
if permissions.map_or(false, |p| p.readonly()) { | ||
return not_supported("changing permissions is not supported on this platform"); | ||
} | ||
fs::create_dir(&path) | ||
.with_err_path(|| &path) | ||
.map(|_| TempDir { | ||
path: path.into_boxed_path(), | ||
}) | ||
} |
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,9 @@ | ||
#[cfg(unix)] | ||
mod unix; | ||
#[cfg(unix)] | ||
pub use unix::*; | ||
|
||
#[cfg(not(unix))] | ||
mod any; | ||
#[cfg(not(unix))] | ||
pub use any::*; |
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,21 @@ | ||
use crate::error::IoResultExt; | ||
use crate::TempDir; | ||
use std::io; | ||
use std::path::PathBuf; | ||
|
||
pub fn create(path: PathBuf, permissions: Option<&std::fs::Permissions>) -> io::Result<TempDir> { | ||
let mut dir_options = std::fs::DirBuilder::new(); | ||
#[cfg(not(target_os = "wasi"))] | ||
{ | ||
use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; | ||
if let Some(p) = permissions { | ||
dir_options.mode(p.mode()); | ||
} | ||
} | ||
dir_options | ||
.create(&path) | ||
.with_err_path(|| &path) | ||
.map(|_| TempDir { | ||
path: path.into_boxed_path(), | ||
}) | ||
} |
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
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