#unix

umask

utility to deal with unix access mode

13 releases (4 stable)

2.1.0 Mar 29, 2023
2.0.0 May 11, 2022
1.0.1 Jan 28, 2022
1.0.0 May 20, 2020
0.1.6 Jul 5, 2019

#152 in Unix APIs

Download history 2312/week @ 2024-06-07 1756/week @ 2024-06-14 2184/week @ 2024-06-21 2117/week @ 2024-06-28 2437/week @ 2024-07-05 2232/week @ 2024-07-12 2395/week @ 2024-07-19 2527/week @ 2024-07-26 1985/week @ 2024-08-02 1916/week @ 2024-08-09 2525/week @ 2024-08-16 2347/week @ 2024-08-23 2789/week @ 2024-08-30 2670/week @ 2024-09-06 2246/week @ 2024-09-13 2647/week @ 2024-09-20

10,791 downloads per month
Used in 25 crates (8 directly)

MIT license

19KB
333 lines

MIT Latest Version docs Chat on Miaou

umask

A light utility helping with unix mode representation, with strong types to avoid misusing constants.

The Mode struct implements Display and prints as "rwxrwxrwx"

Import

In Cargo.toml:

umask = "'2.0"

Usage

use umask::*;

// You can build from a number:
assert_eq!("rw-r--r--", Mode::from(0b110100100).to_string());
assert_eq!("rw-r--r--", Mode::from(0o644).to_string());

// You may use `|` to combine class permissions:
let mu = Mode::from(0o640);
let mo = Mode::from(0o044);
assert_eq!("rw-r--r--", (mu | mo).to_string());
assert_eq!("---r-----", (mu & mo).to_string());

// You can use more semantic constructs:
let m = Mode::all()
    .without(ALL_EXEC);
assert_eq!("rw-rw-rw-", m.to_string());
let mut m = Mode::new()
    .with_class_perm(ALL, READ)
    .with_class_perm(USER, WRITE);
assert_eq!("rw-r--r--", m.to_string());
// (semantic functions can be used in const declarations)

// Or
m |= ALL_EXEC;
assert_eq!("rwxr-xr-x", m.to_string());
let m = ALL_READ | USER_WRITE;
assert_eq!("rw-r--r--", m.to_string());

// Displaying the mode can be done with the `Display`
// implementation but also bit per bit for more control
assert_eq!(
    m.to_string().chars().next().unwrap(), // first char: 'r' or '-'
    if m.has(USER_READ) { 'r' } else { '-' },
);

// The `Display` implementation shows the extra permission bits
// (setuid, setgid and sticky):
let mut m = Mode::all()
    .with_extra(STICKY)
    .with_extra(SETUID)
    .with_extra(SETGID);
assert_eq!("rwsrwsrwt", m.to_string());

// But you can remove those bits for display if you want the
// sometimes more familiar 'x' for execution:
assert_eq!("rwxrwxrwx", m.without_any_extra().to_string());

Dependencies

~270–730KB
~17K SLoC