Skip to content

Commit

Permalink
Merge pull request #191 from srd424/chown-v2
Browse files Browse the repository at this point in the history
Simple, more elegant implementation of setting uid/gid on copied files
  • Loading branch information
r-vdp authored Feb 3, 2025
2 parents 3d2748c + cee3aa3 commit de28d23
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/activate/etc_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use im::HashMap;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::fs::{DirBuilder, Permissions};
use std::os::unix::fs as unixfs;
use std::os::unix::prelude::PermissionsExt;
use std::path;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -352,6 +353,8 @@ fn create_etc_entry(
&entry.source.store_path.join(&entry.target),
&target_path,
&entry.mode,
entry.uid,
entry.gid,
old_state,
) {
Ok(_) => Ok(new_state.register_managed_entry(&target_path)),
Expand Down Expand Up @@ -411,7 +414,8 @@ fn create_dir_recursively(dir: &Path, state: FileTree) -> EtcActivationResult {
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()?;
if !exists || old_state.is_managed(target) {
log::debug!(
Expand All @@ -422,6 +426,7 @@ fn copy_file(source: &Path, target: &Path, mode: &str, old_state: &FileTree) ->
fs::copy(source, target)?;
let mode_int = u32::from_str_radix(mode, 8)?;
fs::set_permissions(target, Permissions::from_mode(mode_int))?;
unixfs::chown(target, Some(uid), Some(gid))?;
Ok(())
} else {
anyhow::bail!("File {} already exists, ignoring.", target.display());
Expand Down

0 comments on commit de28d23

Please sign in to comment.