Skip to content

Commit

Permalink
Clear the magic number.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbings committed Aug 11, 2024
1 parent bf16e63 commit a672f35
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
68 changes: 53 additions & 15 deletions src/find/matchers/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,70 @@ use super::{Matcher, MatcherIO};

#[cfg(unix)]
fn format_permissions(mode: u32) -> String {
let file_type = if mode & 0o170000 == 0o040000 {
"d"
} else if mode & 0o170000 == 0o100000 {
"-"
} else {
"?"
let file_type = match mode & uucore::libc::S_IFMT {
uucore::libc::S_IFDIR => "d",
uucore::libc::S_IFREG => "-",
_ => "?",

Check warning on line 20 in src/find/matchers/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/ls.rs#L20

Added line #L20 was not covered by tests
};

// S_$$USR means "user permissions"
let user_perms = format!(
"{}{}{}",
if mode & 0o0400 != 0 { "r" } else { "-" },
if mode & 0o0200 != 0 { "w" } else { "-" },
if mode & 0o0100 != 0 { "x" } else { "-" }
if mode & uucore::libc::S_IRUSR != 0 {
"r"
} else {
"-"

Check warning on line 29 in src/find/matchers/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/ls.rs#L29

Added line #L29 was not covered by tests
},
if mode & uucore::libc::S_IWUSR != 0 {
"w"
} else {
"-"

Check warning on line 34 in src/find/matchers/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/ls.rs#L34

Added line #L34 was not covered by tests
},
if mode & uucore::libc::S_IXUSR != 0 {
"x"
} else {
"-"
}
);

// S_$$GRP means "group permissions"
let group_perms = format!(
"{}{}{}",
if mode & 0o0040 != 0 { "r" } else { "-" },
if mode & 0o0020 != 0 { "w" } else { "-" },
if mode & 0o0010 != 0 { "x" } else { "-" }
if mode & uucore::libc::S_IRGRP != 0 {
"r"
} else {
"-"

Check warning on line 49 in src/find/matchers/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/ls.rs#L49

Added line #L49 was not covered by tests
},
if mode & uucore::libc::S_IWGRP != 0 {
"w"
} else {
"-"
},
if mode & uucore::libc::S_IXGRP != 0 {
"x"
} else {
"-"
}
);

// S_$$OTH means "other permissions"
let other_perms = format!(
"{}{}{}",
if mode & 0o0004 != 0 { "r" } else { "-" },
if mode & 0o0002 != 0 { "w" } else { "-" },
if mode & 0o0001 != 0 { "x" } else { "-" }
if mode & uucore::libc::S_IROTH != 0 {
"r"
} else {
"-"

Check warning on line 69 in src/find/matchers/ls.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/ls.rs#L69

Added line #L69 was not covered by tests
},
if mode & uucore::libc::S_IWOTH != 0 {
"w"
} else {
"-"
},
if mode & uucore::libc::S_IXOTH != 0 {
"x"
} else {
"-"
}
);

format!("{}{}{}{}", file_type, user_perms, group_perms, other_perms)
Expand All @@ -50,6 +87,7 @@ fn format_permissions(mode: u32) -> String {
fn format_permissions(file_attributes: u32) -> String {
let mut attributes = Vec::new();

// https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
if file_attributes & 0x0001 != 0 {
attributes.push("read-only");
}
Expand Down
2 changes: 1 addition & 1 deletion src/find/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ mod tests {

let _ = fs::remove_file("test_data/find_fprint");
}

#[test]
#[cfg(unix)]
fn test_ls() {
Expand Down

0 comments on commit a672f35

Please sign in to comment.