mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
ls: display the correct permissions
This commit is contained in:
parent
ca6831ef91
commit
537ed734af
1 changed files with 81 additions and 7 deletions
88
src/ls/ls.rs
88
src/ls/ls.rs
|
@ -182,15 +182,17 @@ fn display_dir_entry(entry: DirEntry, options: &getopts::Matches) {
|
||||||
display_file_name(entry.file_name()));
|
display_file_name(entry.file_name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cstr2string(cstr: *const c_char) -> String {
|
|
||||||
unsafe { CStr::from_ptr(cstr).to_string_lossy().into_owned() }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Currently getpwuid is `linux` target only. If it's broken out into
|
// Currently getpwuid is `linux` target only. If it's broken out into
|
||||||
// a posix-compliant attribute this can be updated...
|
// a posix-compliant attribute this can be updated...
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use uucore::c_types::{getpwuid, getgrgid};
|
use uucore::c_types::{getpwuid, getgrgid};
|
||||||
|
|
||||||
|
// Only used in `display_uname` and `display_group`
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn cstr2string(cstr: *const c_char) -> String {
|
||||||
|
unsafe { CStr::from_ptr(cstr).to_string_lossy().into_owned() }
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn display_uname(metadata: &Metadata) -> String {
|
fn display_uname(metadata: &Metadata) -> String {
|
||||||
let pw = unsafe { getpwuid(metadata.uid()) };
|
let pw = unsafe { getpwuid(metadata.uid()) };
|
||||||
|
@ -272,13 +274,85 @@ fn display_permissions(metadata: &Metadata) -> String {
|
||||||
String::from("---------")
|
String::from("---------")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! has {
|
||||||
|
($mode:expr, $perm:expr) => (
|
||||||
|
$mode & $perm != 0
|
||||||
|
)
|
||||||
|
}
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
|
fn display_permissions(metadata: &Metadata) -> String {
|
||||||
|
let mode = metadata.mode() as mode_t;
|
||||||
|
let mut result = String::with_capacity(9);
|
||||||
|
result.push(if has!(mode, S_IRUSR) {
|
||||||
|
'r'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
result.push(if has!(mode, S_IWUSR) {
|
||||||
|
'w'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
result.push(if has!(mode, S_ISUID as mode_t) {
|
||||||
|
if has!(mode, S_IXUSR) {
|
||||||
|
's'
|
||||||
|
} else {
|
||||||
|
'S'
|
||||||
|
}
|
||||||
|
} else if has!(mode, S_IXUSR) {
|
||||||
|
'x'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
|
||||||
fn display_permissions(_metadata: &Metadata) -> String {
|
result.push(if has!(mode, S_IRGRP) {
|
||||||
//use std::os::unix::fs::PermissionsExt;
|
'r'
|
||||||
"xxxxxxxxx".to_string()
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
result.push(if has!(mode, S_IWGRP) {
|
||||||
|
'w'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
result.push(if has!(mode, S_ISGID as mode_t) {
|
||||||
|
if has!(mode, S_IXGRP) {
|
||||||
|
's'
|
||||||
|
} else {
|
||||||
|
'S'
|
||||||
|
}
|
||||||
|
} else if has!(mode, S_IXGRP) {
|
||||||
|
'x'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
|
||||||
|
result.push(if has!(mode, S_IROTH) {
|
||||||
|
'r'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
result.push(if has!(mode, S_IWOTH) {
|
||||||
|
'w'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
result.push(if has!(mode, S_ISVTX as mode_t) {
|
||||||
|
if has!(mode, S_IXOTH) {
|
||||||
|
't'
|
||||||
|
} else {
|
||||||
|
'T'
|
||||||
|
}
|
||||||
|
} else if has!(mode, S_IXOTH) {
|
||||||
|
'x'
|
||||||
|
} else {
|
||||||
|
'-'
|
||||||
|
});
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
fn display_item(item: &Path, options: &getopts::Matches) {
|
fn display_item(item: &Path, options: &getopts::Matches) {
|
||||||
// let fileType = item.file
|
// let fileType = item.file
|
||||||
// let mut fileMeta = String::new();
|
// let mut fileMeta = String::new();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue