mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 07:12:44 +00:00
fs: split get_file_display into its function
This commit is contained in:
parent
f76b53d969
commit
29a5a13ce6
1 changed files with 43 additions and 11 deletions
|
@ -465,16 +465,23 @@ pub fn display_permissions(metadata: &fs::Metadata, display_file_type: bool) ->
|
||||||
display_permissions_unix(mode, display_file_type)
|
display_permissions_unix(mode, display_file_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The logic below is more readable written this way.
|
/// Returns a character representation of the file type based on its mode.
|
||||||
#[allow(clippy::if_not_else)]
|
/// This function is specific to Unix-like systems.
|
||||||
#[allow(clippy::cognitive_complexity)]
|
///
|
||||||
|
/// - `mode`: The mode of the file, typically obtained from file metadata.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// - 'd' for directories
|
||||||
|
/// - 'c' for character devices
|
||||||
|
/// - 'b' for block devices
|
||||||
|
/// - '-' for regular files
|
||||||
|
/// - 'p' for FIFOs (named pipes)
|
||||||
|
/// - 'l' for symbolic links
|
||||||
|
/// - 's' for sockets
|
||||||
|
/// - '?' for any other unrecognized file types
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
/// Display the permissions of a file on a unix like system
|
fn get_file_display(mode: mode_t) -> char {
|
||||||
pub fn display_permissions_unix(mode: mode_t, display_file_type: bool) -> String {
|
match mode & S_IFMT {
|
||||||
let mut result;
|
|
||||||
if display_file_type {
|
|
||||||
result = String::with_capacity(10);
|
|
||||||
result.push(match mode & S_IFMT {
|
|
||||||
S_IFDIR => 'd',
|
S_IFDIR => 'd',
|
||||||
S_IFCHR => 'c',
|
S_IFCHR => 'c',
|
||||||
S_IFBLK => 'b',
|
S_IFBLK => 'b',
|
||||||
|
@ -484,7 +491,19 @@ pub fn display_permissions_unix(mode: mode_t, display_file_type: bool) -> String
|
||||||
S_IFSOCK => 's',
|
S_IFSOCK => 's',
|
||||||
// TODO: Other file types
|
// TODO: Other file types
|
||||||
_ => '?',
|
_ => '?',
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The logic below is more readable written this way.
|
||||||
|
#[allow(clippy::if_not_else)]
|
||||||
|
#[allow(clippy::cognitive_complexity)]
|
||||||
|
#[cfg(unix)]
|
||||||
|
/// Display the permissions of a file on a unix like system
|
||||||
|
pub fn display_permissions_unix(mode: mode_t, display_file_type: bool) -> String {
|
||||||
|
let mut result;
|
||||||
|
if display_file_type {
|
||||||
|
result = String::with_capacity(10);
|
||||||
|
result.push(get_file_display(mode));
|
||||||
} else {
|
} else {
|
||||||
result = String::with_capacity(9);
|
result = String::with_capacity(9);
|
||||||
}
|
}
|
||||||
|
@ -881,4 +900,17 @@ mod tests {
|
||||||
|
|
||||||
assert!(are_hardlinks_to_same_file(&path1, &path2));
|
assert!(are_hardlinks_to_same_file(&path1, &path2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[test]
|
||||||
|
fn test_get_file_display() {
|
||||||
|
assert_eq!(get_file_display(S_IFDIR | 0o755), 'd');
|
||||||
|
assert_eq!(get_file_display(S_IFCHR | 0o644), 'c');
|
||||||
|
assert_eq!(get_file_display(S_IFBLK | 0o600), 'b');
|
||||||
|
assert_eq!(get_file_display(S_IFREG | 0o777), '-');
|
||||||
|
assert_eq!(get_file_display(S_IFIFO | 0o666), 'p');
|
||||||
|
assert_eq!(get_file_display(S_IFLNK | 0o777), 'l');
|
||||||
|
assert_eq!(get_file_display(S_IFSOCK | 0o600), 's');
|
||||||
|
assert_eq!(get_file_display(0o777), '?');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue