Skip to content

Commit

Permalink
feat: tempfile with permission support, adding new `*_with_permission…
Browse files Browse the repository at this point in the history
…s(..., permissions)` methods.
  • Loading branch information
Byron committed Feb 6, 2024
1 parent 8a62fb5 commit 45ef4ff
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
29 changes: 14 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gix-tempfile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ gix-fs = { version = "^0.10.0", path = "../gix-fs" }
parking_lot = "0.12.1"
dashmap = { version = "5.1.0", optional = true }
once_cell = { version = "1.8.0", default-features = false, features = ["race", "std"] }
tempfile = "3.4.0"
tempfile = "3.10.0"

signal-hook = { version = "0.3.9", default-features = false, optional = true }
signal-hook-registry = { version = "1.4.0", optional = true }
Expand Down
41 changes: 38 additions & 3 deletions gix-tempfile/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ pub(crate) enum Mode {

/// Utilities
impl Handle<()> {
fn at_path(path: &Path, directory: ContainingDirectory, cleanup: AutoRemove, mode: Mode) -> io::Result<usize> {
fn at_path(
path: &Path,
directory: ContainingDirectory,
cleanup: AutoRemove,
mode: Mode,
permissions: Option<std::fs::Permissions>,
) -> io::Result<usize> {
let tempfile = {
let mut builder = tempfile::Builder::new();
let dot_ext_storage;
Expand All @@ -34,6 +40,9 @@ impl Handle<()> {
dot_ext_storage = format!(".{}", ext.to_string_lossy());
builder.suffix(&dot_ext_storage);
}
if let Some(permissions) = permissions {
builder.permissions(permissions);
}
let parent_dir = path.parent().expect("parent directory is present");
let parent_dir = directory.resolve(parent_dir)?;
ForksafeTempfile::new(builder.rand_bytes(0).tempfile_in(parent_dir)?, cleanup, mode)
Expand Down Expand Up @@ -76,7 +85,20 @@ impl Handle<Closed> {
/// signal is encountered as destructors won't run. See [the top-level documentation](crate) for more.
pub fn at(path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove) -> io::Result<Self> {
Ok(Handle {
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Closed)?,
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Closed, None)?,
_marker: Default::default(),
})
}

/// Like [`at`](Self::at()), but with support for filesystem `permissions`.
pub fn at_with_permissions(
path: impl AsRef<Path>,
directory: ContainingDirectory,
cleanup: AutoRemove,
permissions: std::fs::Permissions,
) -> io::Result<Self> {
Ok(Handle {
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Closed, Some(permissions))?,
_marker: Default::default(),
})
}
Expand Down Expand Up @@ -104,7 +126,20 @@ impl Handle<Writable> {
/// signal is encountered as destructors won't run. See [the top-level documentation](crate) for more.
pub fn at(path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove) -> io::Result<Self> {
Ok(Handle {
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Writable)?,
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Writable, None)?,
_marker: Default::default(),
})
}

/// Like [`at`](Self::at()), but with support for filesystem `permissions`.
pub fn at_with_permissions(
path: impl AsRef<Path>,
directory: ContainingDirectory,
cleanup: AutoRemove,
permissions: std::fs::Permissions,
) -> io::Result<Self> {
Ok(Handle {
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Writable, Some(permissions))?,
_marker: Default::default(),
})
}
Expand Down
20 changes: 20 additions & 0 deletions gix-tempfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ pub fn writable_at(
Handle::<Writable>::at(path, directory, cleanup)
}

/// Like [`writable_at`], but allows to set the given filesystem `permissions`.
pub fn writable_at_with_permissions(
path: impl AsRef<Path>,
directory: ContainingDirectory,
cleanup: AutoRemove,
permissions: std::fs::Permissions,
) -> io::Result<Handle<Writable>> {
Handle::<Writable>::at_with_permissions(path, directory, cleanup, permissions)
}

/// A shortcut to [`Handle::<Closed>::at()`] providing a closed temporary file to mark the presence of something.
pub fn mark_at(
path: impl AsRef<Path>,
Expand All @@ -215,3 +225,13 @@ pub fn mark_at(
) -> io::Result<Handle<Closed>> {
Handle::<Closed>::at(path, directory, cleanup)
}

/// Like [`mark_at`], but allows to set the given filesystem `permissions`.
pub fn mark_at_with_permissions(
path: impl AsRef<Path>,
directory: ContainingDirectory,
cleanup: AutoRemove,
permissions: std::fs::Permissions,
) -> io::Result<Handle<Closed>> {
Handle::<Closed>::at_with_permissions(path, directory, cleanup, permissions)
}

0 comments on commit 45ef4ff

Please sign in to comment.