Skip to content

Commit

Permalink
feat: add *_with_permissions(..., permissions) methods.
Browse files Browse the repository at this point in the history
That way it's possible to create lock files with non-default permissions
as well.
  • Loading branch information
Byron committed Feb 6, 2024
1 parent d83e45d commit 719fc81
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion gix-lock/src/acquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,26 @@ impl File {
lock_path,
})
}

/// Like [`acquire_to_update_resource()`](File::acquire_to_update_resource), but allows to set filesystem permissions using `make_permissions`.
pub fn acquire_to_update_resource_with_permissions(
at_path: impl AsRef<Path>,
mode: Fail,
boundary_directory: Option<PathBuf>,
make_permissions: impl Fn() -> std::fs::Permissions,
) -> Result<File, Error> {
let (lock_path, handle) = lock_with_mode(at_path.as_ref(), mode, boundary_directory, &|p, d, c| {
gix_tempfile::writable_at_with_permissions(p, d, c, make_permissions())
})?;
Ok(File {
inner: handle,
lock_path,
})
}
}

impl Marker {
/// Like [`acquire_to_update_resource()`][File::acquire_to_update_resource()] but _without_ the possibility to make changes
/// Like [`acquire_to_update_resource()`](File::acquire_to_update_resource()) but _without_ the possibility to make changes
/// and commit them.
///
/// If `boundary_directory` is given, non-existing directories will be created automatically and removed in the case of
Expand Down Expand Up @@ -118,6 +134,23 @@ impl Marker {
lock_path,
})
}

/// Like [`acquire_to_hold_resource()`](Marker::acquire_to_hold_resource), but allows to set filesystem permissions using `make_permissions`.
pub fn acquire_to_hold_resource_with_permissions(
at_path: impl AsRef<Path>,
mode: Fail,
boundary_directory: Option<PathBuf>,
make_permissions: impl Fn() -> std::fs::Permissions,
) -> Result<Marker, Error> {
let (lock_path, handle) = lock_with_mode(at_path.as_ref(), mode, boundary_directory, &|p, d, c| {
gix_tempfile::mark_at_with_permissions(p, d, c, make_permissions())
})?;
Ok(Marker {
created_from_file: false,
inner: handle,
lock_path,
})
}
}

fn dir_cleanup(boundary: Option<PathBuf>) -> (ContainingDirectory, AutoRemove) {
Expand Down

0 comments on commit 719fc81

Please sign in to comment.