Struct Mode

Source
pub struct Mode { /* private fields */ }
Expand description

Unix file mode.

All file type, special and protection bits described in sys/stat.h are represented.

The Mode can represent a file mode partially by the use of a bitmask. Only modified bits will be changed in the target file. Modifications specific only to directories (see ProtectionBit::Search) are handled correctly.

The Mode can be displayed as a string according to the POSIX standard for the ls command.

Mode bits can be set with chmod compatible string.

Implementations§

Source§

impl Mode

Source

pub fn new(mode: u32, mask: u32) -> Mode

Constructs Mode from mode value and mask representing which special and permissions bits will be applied to the target file mode.

Source

pub fn empty() -> Mode

Constructs Mode with no bits sets and empty mask.

Source

pub fn from_path(path: impl AsRef<Path>) -> Result<Mode, Error>

Constructs Mode from mode value of the file represented by the path.

The mask will be set to 0o7777 meaning that all the special and permissions bits will be applied to the target file mode.

Source

pub fn from_path_nofollow(path: impl AsRef<Path>) -> Result<Mode, Error>

Like Mode::from_path but will not follow symbolic links.

Source

pub fn from_file(file: &File) -> Result<Mode, Error>

Constructs Mode from mode value of the file.

The mask will be set to 0o7777 meaning that all the special and permissions bits will be applied to the target file mode.

Source

pub fn mode(&self) -> u32

Gets mode value.

Source

pub fn mode_mask(&self) -> (u32, u32)

Gets mode value and mask that represents bits of the mode that would be applied to the target file mode.

If the mode represents a directory the search bit will be handled accordingly.

Source

pub fn file_type(&self) -> Option<FileType>

Gets FileType if corresponding bit patterns are present and valid in the mode value.

Source

pub fn user_protection(&self, user: User) -> Protection

Gets Protection bits for User.

Source

pub fn user_special(&self, user: User) -> Special

Gets Special bits for User.

Source

pub fn add(&mut self, mode: &Mode)

Sets bits that are set in the given mode according to mask.

Source

pub fn sub(&mut self, mode: &Mode)

Clears bits that are set in the given mode according to mask.

Source

pub fn set(&mut self, mode: &Mode)

Sets values of bits that are set or clear in the given mode according to mask.

Source

pub fn forget(&mut self, mode: &Mode)

Forgets values of bits that are set or clear in the given mode by clearing bits in the mask.

Source

pub fn with_file_type(self, file_type: FileType) -> Mode

Returns self with given bit pattern representing FileType set.

Source

pub fn with_protection(self, user: User, protection: &Protection) -> Mode

Returns self with bit pattern and mask for Protection bits set for User.

Source

pub fn with_special(self, user: User, special: &Special) -> Mode

Returns self with bit pattern and mask for Special bits set for User.

Source

pub fn set_file_type(&mut self, file_type: FileType)

Sets bit pattern representing given FileType.

Source

pub fn set_protection(&mut self, user: User, protection: &Protection)

Sets bit pattern and mask for Protection bits for User.

Source

pub fn set_special(&mut self, user: User, special: &Special)

Sets bit pattern and mask for Special bits for User.

Source

pub fn set_str(&mut self, mode_str: &str) -> Result<(), ModeParseError>

Sets bits according to the mode string (as described in Linux chmod manual).

Only bits described by the string will be represented in the mask.

Note: Current value of mode is used as a reference for operations that use the user as the source of bits (like g+u). Consider constructing Mode using Mode::from_file or Mode::from_path before using this method.

Source

pub fn set_str_umask( &mut self, mode_str: &str, umask: u32, ) -> Result<(), ModeParseError>

Like Mode::set_str but allows to specify umask value to use instead of the calling process’s umask.

Source

pub fn apply_umask(&mut self, umask: u32)

Clears protection bits set in the umask value if they were set.

Source

pub fn apply_to(&self, mode: u32) -> u32

Applies mode changes to given mode value.

Only bits set in the mask are modified.

Returns the resulting mode value.

If the mode value passed as an argument represents a directory the search bit will be handled accordingly.

Source

pub fn apply_to_path(&self, path: impl AsRef<Path>) -> Result<u32, Error>

Applies mode changes to the mode of a file represented by the path.

Only bits set in the mask are modified.

Returns the resulting mode value.

If the mode value of the file passed as an argument represents a directory the search bit will be handled accordingly.

The file’s mode is not modified.

Source

pub fn apply_to_path_nofollow( &self, path: impl AsRef<Path>, ) -> Result<u32, Error>

Like Mode::apply_to_path but does not follow symbolic links.

Source

pub fn set_mode_path(&self, path: impl AsRef<Path>) -> Result<u32, Error>

Sets the mode of the file, represented by the path, by applying the mode changes to its current mode.

Only bits set in the mask are modified.

Returns the new file mode value.

If the mode value of the file passed as an argument represents a directory the search bit will be handled accordingly.

The file’s mode is modified.

Source

pub fn set_mode_path_nofollow( &self, path: impl AsRef<Path>, ) -> Result<u32, Error>

Like Mode::set_mode_path but does not follow symbolic links.

Source

pub fn set_mode_file(&self, file: &File) -> Result<u32, Error>

Sets the mode of the file by applying the mode changes to its current mode.

Only bits set in the mask are modified.

Returns the new file mode value.

If the mode value of the file passed as an argument represents a directory the search bit will be handled accordingly.

The file’s mode is modified.

Trait Implementations§

Source§

impl Clone for Mode

Source§

fn clone(&self) -> Mode

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Mode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<u32> for Mode

Source§

fn from(mode: u32) -> Mode

Converts to this type from the input type.
Source§

impl FromIterator<Mode> for Mode

Source§

fn from_iter<I: IntoIterator<Item = Mode>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for Mode

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Mode

Source§

fn eq(&self, other: &Mode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Mode

Source§

impl StructuralPartialEq for Mode

Auto Trait Implementations§

§

impl Freeze for Mode

§

impl RefUnwindSafe for Mode

§

impl Send for Mode

§

impl Sync for Mode

§

impl Unpin for Mode

§

impl UnwindSafe for Mode

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.