mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-15 12:21:30 +00:00
impl proxmox::tools::fs::CreateOptions::apply_to()
Split out code to apply CreateOptions. Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
parent
28e1d4c342
commit
8bcd8e5357
@ -134,30 +134,18 @@ pub fn make_tmp_file<P: AsRef<Path>>(
|
|||||||
// use mkstemp here, because it works with different processes, threads, even tokio tasks
|
// use mkstemp here, because it works with different processes, threads, even tokio tasks
|
||||||
let mut template = path.to_owned();
|
let mut template = path.to_owned();
|
||||||
template.set_extension("tmp_XXXXXX");
|
template.set_extension("tmp_XXXXXX");
|
||||||
let (file, tmp_path) = match unistd::mkstemp(&template) {
|
let (mut file, tmp_path) = match unistd::mkstemp(&template) {
|
||||||
Ok((fd, path)) => (unsafe { File::from_raw_fd(fd) }, path),
|
Ok((fd, path)) => (unsafe { File::from_raw_fd(fd) }, path),
|
||||||
Err(err) => bail!("mkstemp {:?} failed: {}", template, err),
|
Err(err) => bail!("mkstemp {:?} failed: {}", template, err),
|
||||||
};
|
};
|
||||||
|
|
||||||
// clippy bug?: from_bits_truncate is actually a const fn...
|
match options.apply_to(&mut file, &tmp_path) {
|
||||||
#[allow(clippy::or_fun_call)]
|
Ok(()) => Ok((file, tmp_path)),
|
||||||
let mode: stat::Mode = options
|
Err(err) => {
|
||||||
.perm
|
|
||||||
.unwrap_or(stat::Mode::from_bits_truncate(0o644));
|
|
||||||
|
|
||||||
if let Err(err) = stat::fchmod(file.as_raw_fd(), mode) {
|
|
||||||
let _ = unistd::unlink(&tmp_path);
|
|
||||||
bail!("fchmod {:?} failed: {}", tmp_path, err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.owner.is_some() || options.group.is_some() {
|
|
||||||
if let Err(err) = fchown(file.as_raw_fd(), options.owner, options.group) {
|
|
||||||
let _ = unistd::unlink(&tmp_path);
|
let _ = unistd::unlink(&tmp_path);
|
||||||
bail!("fchown {:?} failed: {}", tmp_path, err);
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((file, tmp_path))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Atomically replace a file.
|
/// Atomically replace a file.
|
||||||
@ -382,6 +370,25 @@ impl CreateOptions {
|
|||||||
self.owner(nix::unistd::ROOT)
|
self.owner(nix::unistd::ROOT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn apply_to(&self, file: &mut File, path: &Path) -> Result<(), Error> {
|
||||||
|
|
||||||
|
// clippy bug?: from_bits_truncate is actually a const fn...
|
||||||
|
#[allow(clippy::or_fun_call)]
|
||||||
|
let mode: stat::Mode = self.perm
|
||||||
|
.unwrap_or(stat::Mode::from_bits_truncate(0o644));
|
||||||
|
|
||||||
|
if let Err(err) = stat::fchmod(file.as_raw_fd(), mode) {
|
||||||
|
bail!("fchmod {:?} failed: {}", path, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.owner.is_some() || self.group.is_some() {
|
||||||
|
if let Err(err) = fchown(file.as_raw_fd(), self.owner, self.group) {
|
||||||
|
bail!("fchown {:?} failed: {}", path, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: once 'nix' has `const fn` constructors for Uid and Gid we can enable these:
|
// TODO: once 'nix' has `const fn` constructors for Uid and Gid we can enable these:
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user