Simple, more elegant implementation of setting uid/gid on copied files

(does not yet look up user or group names.)
This commit is contained in:
Steve Dodd 2025-01-29 17:26:58 +00:00
parent 196586a97d
commit cee3aa3dd6

View file

@ -4,6 +4,7 @@ use im::HashMap;
use itertools::Itertools; use itertools::Itertools;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs::{DirBuilder, Permissions}; use std::fs::{DirBuilder, Permissions};
use std::os::unix::fs as unixfs;
use std::os::unix::prelude::PermissionsExt; use std::os::unix::prelude::PermissionsExt;
use std::path; use std::path;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -352,6 +353,8 @@ fn create_etc_entry(
&entry.source.store_path.join(&entry.target), &entry.source.store_path.join(&entry.target),
&target_path, &target_path,
&entry.mode, &entry.mode,
entry.uid,
entry.gid,
old_state, old_state,
) { ) {
Ok(_) => Ok(new_state.register_managed_entry(&target_path)), Ok(_) => Ok(new_state.register_managed_entry(&target_path)),
@ -411,7 +414,8 @@ fn create_dir_recursively(dir: &Path, state: FileTree) -> EtcActivationResult {
new_state new_state
} }
fn copy_file(source: &Path, target: &Path, mode: &str, old_state: &FileTree) -> anyhow::Result<()> { fn copy_file(source: &Path, target: &Path, mode: &str,
uid: u32, gid: u32, old_state: &FileTree) -> anyhow::Result<()> {
let exists = target.try_exists()?; let exists = target.try_exists()?;
if !exists || old_state.is_managed(target) { if !exists || old_state.is_managed(target) {
log::debug!( log::debug!(
@ -422,6 +426,7 @@ fn copy_file(source: &Path, target: &Path, mode: &str, old_state: &FileTree) ->
fs::copy(source, target)?; fs::copy(source, target)?;
let mode_int = u32::from_str_radix(mode, 8)?; let mode_int = u32::from_str_radix(mode, 8)?;
fs::set_permissions(target, Permissions::from_mode(mode_int))?; fs::set_permissions(target, Permissions::from_mode(mode_int))?;
unixfs::chown(target, Some(uid), Some(gid))?;
Ok(()) Ok(())
} else { } else {
anyhow::bail!("File {} already exists, ignoring.", target.display()); anyhow::bail!("File {} already exists, ignoring.", target.display());