1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

chmod/chown/chgrp: deduplicate get metadata

This commit is contained in:
Sylvestre Ledru 2024-12-30 14:42:27 +01:00
parent 1a0f41e4cc
commit bb3741067a
2 changed files with 13 additions and 12 deletions

View file

@ -344,14 +344,9 @@ impl Chmoder {
} }
#[cfg(unix)] #[cfg(unix)]
fn chmod_file(&self, file: &Path) -> UResult<()> { fn chmod_file(&self, file: &Path) -> UResult<()> {
use uucore::mode::get_umask; use uucore::{mode::get_umask, perms::get_metadata};
// Determine metadata based on dereference flag let metadata = get_metadata(file, self.dereference);
let metadata = if self.dereference {
file.metadata() // Follow symlinks
} else {
file.symlink_metadata() // Act on the symlink itself
};
let fperm = match metadata { let fperm = match metadata {
Ok(meta) => meta.mode() & 0o7777, Ok(meta) => meta.mode() & 0o7777,

View file

@ -250,6 +250,14 @@ fn is_root(path: &Path, would_traverse_symlink: bool) -> bool {
false false
} }
pub fn get_metadata(file: &Path, follow: bool) -> Result<Metadata, std::io::Error> {
if follow {
file.metadata()
} else {
file.symlink_metadata()
}
}
impl ChownExecutor { impl ChownExecutor {
pub fn exec(&self) -> UResult<()> { pub fn exec(&self) -> UResult<()> {
let mut ret = 0; let mut ret = 0;
@ -417,11 +425,9 @@ impl ChownExecutor {
fn obtain_meta<P: AsRef<Path>>(&self, path: P, follow: bool) -> Option<Metadata> { fn obtain_meta<P: AsRef<Path>>(&self, path: P, follow: bool) -> Option<Metadata> {
let path = path.as_ref(); let path = path.as_ref();
let meta = if follow {
path.metadata() let meta = get_metadata(path, follow);
} else {
path.symlink_metadata()
};
match meta { match meta {
Err(e) => { Err(e) => {
match self.verbosity.level { match self.verbosity.level {