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
impl Mode
Sourcepub fn new(mode: u32, mask: u32) -> Mode
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.
Sourcepub fn from_path(path: impl AsRef<Path>) -> Result<Mode, Error>
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.
Sourcepub fn from_path_nofollow(path: impl AsRef<Path>) -> Result<Mode, Error>
pub fn from_path_nofollow(path: impl AsRef<Path>) -> Result<Mode, Error>
Like Mode::from_path but will not follow symbolic links.
Sourcepub fn from_file(file: &File) -> Result<Mode, Error>
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.
Sourcepub fn mode_mask(&self) -> (u32, u32)
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.
Sourcepub fn file_type(&self) -> Option<FileType>
pub fn file_type(&self) -> Option<FileType>
Gets FileType if corresponding bit patterns are present and valid in the mode value.
Sourcepub fn user_protection(&self, user: User) -> Protection
pub fn user_protection(&self, user: User) -> Protection
Gets Protection bits for User.
Sourcepub fn user_special(&self, user: User) -> Special
pub fn user_special(&self, user: User) -> Special
- For User::Owner the SpecialBit::SetId bit will represent the
set-user-ID
mode bit. - For User::Group the SpecialBit::SetId bit will represent the
set-group-ID
mode bit.
Sourcepub fn sub(&mut self, mode: &Mode)
pub fn sub(&mut self, mode: &Mode)
Clears bits that are set in the given mode according to mask.
Sourcepub fn set(&mut self, mode: &Mode)
pub fn set(&mut self, mode: &Mode)
Sets values of bits that are set or clear in the given mode according to mask.
Sourcepub fn forget(&mut self, mode: &Mode)
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.
Sourcepub fn with_file_type(self, file_type: FileType) -> Mode
pub fn with_file_type(self, file_type: FileType) -> Mode
Returns self with given bit pattern representing FileType set.
Sourcepub fn with_protection(self, user: User, protection: &Protection) -> Mode
pub fn with_protection(self, user: User, protection: &Protection) -> Mode
Returns self with bit pattern and mask for Protection bits set for User.
Sourcepub fn with_special(self, user: User, special: &Special) -> Mode
pub fn with_special(self, user: User, special: &Special) -> Mode
Returns self with bit pattern and mask for Special bits set for User.
- For User::Owner the SpecialBit::SetId bit will represent the
set-user-ID
mode bit. - For User::Group the SpecialBit::SetId bit will represent the
set-group-ID
mode bit.
Sourcepub fn set_file_type(&mut self, file_type: FileType)
pub fn set_file_type(&mut self, file_type: FileType)
Sets bit pattern representing given FileType.
Sourcepub fn set_protection(&mut self, user: User, protection: &Protection)
pub fn set_protection(&mut self, user: User, protection: &Protection)
Sets bit pattern and mask for Protection bits for User.
Sourcepub fn set_special(&mut self, user: User, special: &Special)
pub fn set_special(&mut self, user: User, special: &Special)
Sets bit pattern and mask for Special bits for User.
- For User::Owner the SpecialBit::SetId bit will represent the
set-user-ID
mode bit. - For User::Group the SpecialBit::SetId bit will represent the
set-group-ID
mode bit.
Sourcepub fn set_str(&mut self, mode_str: &str) -> Result<(), ModeParseError>
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.
Sourcepub fn set_str_umask(
&mut self,
mode_str: &str,
umask: u32,
) -> Result<(), ModeParseError>
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.
Sourcepub fn apply_umask(&mut self, umask: u32)
pub fn apply_umask(&mut self, umask: u32)
Clears protection bits set in the umask value if they were set.
Sourcepub fn apply_to(&self, mode: u32) -> u32
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.
Sourcepub fn apply_to_path(&self, path: impl AsRef<Path>) -> Result<u32, Error>
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.
Sourcepub fn apply_to_path_nofollow(
&self,
path: impl AsRef<Path>,
) -> Result<u32, Error>
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.
Sourcepub fn set_mode_path(&self, path: impl AsRef<Path>) -> Result<u32, Error>
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.
Sourcepub fn set_mode_path_nofollow(
&self,
path: impl AsRef<Path>,
) -> Result<u32, Error>
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.
Sourcepub fn set_mode_file(&self, file: &File) -> Result<u32, Error>
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.