Skip to content

Commit 45ef4ff

Browse files
committed
feat: tempfile with permission support, adding new *_with_permissions(..., permissions) methods.
1 parent 8a62fb5 commit 45ef4ff

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

Cargo.lock

Lines changed: 14 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-tempfile/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ gix-fs = { version = "^0.10.0", path = "../gix-fs" }
3333
parking_lot = "0.12.1"
3434
dashmap = { version = "5.1.0", optional = true }
3535
once_cell = { version = "1.8.0", default-features = false, features = ["race", "std"] }
36-
tempfile = "3.4.0"
36+
tempfile = "3.10.0"
3737

3838
signal-hook = { version = "0.3.9", default-features = false, optional = true }
3939
signal-hook-registry = { version = "1.4.0", optional = true }

gix-tempfile/src/handle.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ pub(crate) enum Mode {
2222

2323
/// Utilities
2424
impl Handle<()> {
25-
fn at_path(path: &Path, directory: ContainingDirectory, cleanup: AutoRemove, mode: Mode) -> io::Result<usize> {
25+
fn at_path(
26+
path: &Path,
27+
directory: ContainingDirectory,
28+
cleanup: AutoRemove,
29+
mode: Mode,
30+
permissions: Option<std::fs::Permissions>,
31+
) -> io::Result<usize> {
2632
let tempfile = {
2733
let mut builder = tempfile::Builder::new();
2834
let dot_ext_storage;
@@ -34,6 +40,9 @@ impl Handle<()> {
3440
dot_ext_storage = format!(".{}", ext.to_string_lossy());
3541
builder.suffix(&dot_ext_storage);
3642
}
43+
if let Some(permissions) = permissions {
44+
builder.permissions(permissions);
45+
}
3746
let parent_dir = path.parent().expect("parent directory is present");
3847
let parent_dir = directory.resolve(parent_dir)?;
3948
ForksafeTempfile::new(builder.rand_bytes(0).tempfile_in(parent_dir)?, cleanup, mode)
@@ -76,7 +85,20 @@ impl Handle<Closed> {
7685
/// signal is encountered as destructors won't run. See [the top-level documentation](crate) for more.
7786
pub fn at(path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove) -> io::Result<Self> {
7887
Ok(Handle {
79-
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Closed)?,
88+
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Closed, None)?,
89+
_marker: Default::default(),
90+
})
91+
}
92+
93+
/// Like [`at`](Self::at()), but with support for filesystem `permissions`.
94+
pub fn at_with_permissions(
95+
path: impl AsRef<Path>,
96+
directory: ContainingDirectory,
97+
cleanup: AutoRemove,
98+
permissions: std::fs::Permissions,
99+
) -> io::Result<Self> {
100+
Ok(Handle {
101+
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Closed, Some(permissions))?,
80102
_marker: Default::default(),
81103
})
82104
}
@@ -104,7 +126,20 @@ impl Handle<Writable> {
104126
/// signal is encountered as destructors won't run. See [the top-level documentation](crate) for more.
105127
pub fn at(path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove) -> io::Result<Self> {
106128
Ok(Handle {
107-
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Writable)?,
129+
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Writable, None)?,
130+
_marker: Default::default(),
131+
})
132+
}
133+
134+
/// Like [`at`](Self::at()), but with support for filesystem `permissions`.
135+
pub fn at_with_permissions(
136+
path: impl AsRef<Path>,
137+
directory: ContainingDirectory,
138+
cleanup: AutoRemove,
139+
permissions: std::fs::Permissions,
140+
) -> io::Result<Self> {
141+
Ok(Handle {
142+
id: Handle::<()>::at_path(path.as_ref(), directory, cleanup, Mode::Writable, Some(permissions))?,
108143
_marker: Default::default(),
109144
})
110145
}

gix-tempfile/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ pub fn writable_at(
207207
Handle::<Writable>::at(path, directory, cleanup)
208208
}
209209

210+
/// Like [`writable_at`], but allows to set the given filesystem `permissions`.
211+
pub fn writable_at_with_permissions(
212+
path: impl AsRef<Path>,
213+
directory: ContainingDirectory,
214+
cleanup: AutoRemove,
215+
permissions: std::fs::Permissions,
216+
) -> io::Result<Handle<Writable>> {
217+
Handle::<Writable>::at_with_permissions(path, directory, cleanup, permissions)
218+
}
219+
210220
/// A shortcut to [`Handle::<Closed>::at()`] providing a closed temporary file to mark the presence of something.
211221
pub fn mark_at(
212222
path: impl AsRef<Path>,
@@ -215,3 +225,13 @@ pub fn mark_at(
215225
) -> io::Result<Handle<Closed>> {
216226
Handle::<Closed>::at(path, directory, cleanup)
217227
}
228+
229+
/// Like [`mark_at`], but allows to set the given filesystem `permissions`.
230+
pub fn mark_at_with_permissions(
231+
path: impl AsRef<Path>,
232+
directory: ContainingDirectory,
233+
cleanup: AutoRemove,
234+
permissions: std::fs::Permissions,
235+
) -> io::Result<Handle<Closed>> {
236+
Handle::<Closed>::at_with_permissions(path, directory, cleanup, permissions)
237+
}

0 commit comments

Comments
 (0)