Struct Permissions
struct Permissions(_)
Representation of the various permissions on a file.
This module only currently provides one bit of information,
Permissions::readonly, which is exposed on all currently supported
platforms. Unix-specific functionality, such as mode bits, is available
through the PermissionsExt trait.
Implementations
impl Permissions
fn readonly(self: &Self) -> boolReturns
trueif these permissions describe a readonly (unwritable) file.Note
This function does not take Access Control Lists (ACLs), Unix group membership and other nuances into account. Therefore the return value of this function cannot be relied upon to predict whether attempts to read or write the file will actually succeed.
Windows
On Windows this returns
FILE_ATTRIBUTE_READONLY. IfFILE_ATTRIBUTE_READONLYis set then writes to the file will fail but the user may still have permission to change this flag. IfFILE_ATTRIBUTE_READONLYis not set then writes may still fail due to lack of write permission. The behavior of this attribute for directories depends on the Windows version.Unix (including macOS)
On Unix-based platforms this checks if any of the owner, group or others write permission bits are set. It does not consider anything else, including:
- Whether the current user is in the file's assigned group.
- Permissions granted by ACL.
- That
rootuser can write to files that do not have any write bits set. - Writable files on a filesystem that is mounted read-only.
The
PermissionsExttrait gives direct access to the permission bits but also does not read ACLs.Examples
use std::fs::File; fn main() -> std::io::Result<()> { let mut f = File::create("foo.txt")?; let metadata = f.metadata()?; assert_eq!(false, metadata.permissions().readonly()); Ok(()) }fn set_readonly(self: &mut Self, readonly: bool)Modifies the readonly flag for this set of permissions. If the
readonlyargument istrue, using the resultingPermissionwill update file permissions to forbid writing. Conversely, if it'sfalse, using the resultingPermissionwill update file permissions to allow writing.This operation does not modify the files attributes. This only changes the in-memory value of these attributes for this
Permissionsinstance. To modify the files attributes use theset_permissionsfunction which commits these attribute changes to the file.Note
set_readonly(false)makes the file world-writable on Unix. You can use thePermissionsExttrait on Unix to avoid this issue.It also does not take Access Control Lists (ACLs) or Unix group membership into account.
Windows
On Windows this sets or clears
FILE_ATTRIBUTE_READONLY. IfFILE_ATTRIBUTE_READONLYis set then writes to the file will fail but the user may still have permission to change this flag. IfFILE_ATTRIBUTE_READONLYis not set then the write may still fail if the user does not have permission to write to the file.In Windows 7 and earlier this attribute prevents deleting empty directories. It does not prevent modifying the directory contents. On later versions of Windows this attribute is ignored for directories.
Unix (including macOS)
On Unix-based platforms this sets or clears the write access bit for the owner, group and others, equivalent to
chmod a+w <file>orchmod a-w <file>respectively. The latter will grant write access to all users! You can use thePermissionsExttrait on Unix to avoid this issue.Examples
use std::fs::File; fn main() -> std::io::Result<()> { let f = File::create("foo.txt")?; let metadata = f.metadata()?; let mut permissions = metadata.permissions(); permissions.set_readonly(true); // filesystem doesn't change, only the in memory state of the // readonly permission assert_eq!(false, metadata.permissions().readonly()); // just this particular `permissions`. assert_eq!(true, permissions.readonly()); Ok(()) }
impl Clone for Permissions
fn clone(self: &Self) -> Permissions
impl Debug for Permissions
fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result
impl Eq for Permissions
impl Freeze for Permissions
impl PartialEq for Permissions
fn eq(self: &Self, other: &Permissions) -> bool
impl PermissionsExt for crate::fs::Permissions
fn mode(self: &Self) -> u32fn set_mode(self: &mut Self, mode: u32)fn from_mode(mode: u32) -> Permissions
impl RefUnwindSafe for Permissions
impl Send for Permissions
impl StructuralPartialEq for Permissions
impl Sync for Permissions
impl Unpin for Permissions
impl UnwindSafe for Permissions
impl<T> Any for Permissions
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Permissions
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Permissions
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Permissions
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Permissions
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Permissions
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Permissions
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Permissions
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Permissions
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>