mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
chmod/chown/chgrp: deduplicate get metadata
This commit is contained in:
parent
1a0f41e4cc
commit
bb3741067a
2 changed files with 13 additions and 12 deletions
|
@ -344,14 +344,9 @@ impl Chmoder {
|
|||
}
|
||||
#[cfg(unix)]
|
||||
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 = if self.dereference {
|
||||
file.metadata() // Follow symlinks
|
||||
} else {
|
||||
file.symlink_metadata() // Act on the symlink itself
|
||||
};
|
||||
let metadata = get_metadata(file, self.dereference);
|
||||
|
||||
let fperm = match metadata {
|
||||
Ok(meta) => meta.mode() & 0o7777,
|
||||
|
|
|
@ -250,6 +250,14 @@ fn is_root(path: &Path, would_traverse_symlink: bool) -> bool {
|
|||
false
|
||||
}
|
||||
|
||||
pub fn get_metadata(file: &Path, follow: bool) -> Result<Metadata, std::io::Error> {
|
||||
if follow {
|
||||
file.metadata()
|
||||
} else {
|
||||
file.symlink_metadata()
|
||||
}
|
||||
}
|
||||
|
||||
impl ChownExecutor {
|
||||
pub fn exec(&self) -> UResult<()> {
|
||||
let mut ret = 0;
|
||||
|
@ -417,11 +425,9 @@ impl ChownExecutor {
|
|||
|
||||
fn obtain_meta<P: AsRef<Path>>(&self, path: P, follow: bool) -> Option<Metadata> {
|
||||
let path = path.as_ref();
|
||||
let meta = if follow {
|
||||
path.metadata()
|
||||
} else {
|
||||
path.symlink_metadata()
|
||||
};
|
||||
|
||||
let meta = get_metadata(path, follow);
|
||||
|
||||
match meta {
|
||||
Err(e) => {
|
||||
match self.verbosity.level {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue