Struct devela::_std::fs::Permissions
1.0.0 · source · pub struct Permissions(/* private fields */);
Expand description
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§
source§impl Permissions
impl Permissions
sourcepub fn readonly(&self) -> bool
pub fn readonly(&self) -> bool
Returns true
if these permissions describe a readonly (unwritable) file.
Note
This function does not take Access Control Lists (ACLs) or Unix group membership into account.
Windows
On Windows this returns FILE_ATTRIBUTE_READONLY
.
If FILE_ATTRIBUTE_READONLY
is set then writes to the file will fail
but the user may still have permission to change this flag. If
FILE_ATTRIBUTE_READONLY
is 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 check if the current
user is in the file’s assigned group. It also does not check ACLs.
Therefore even if this returns true you may not be able to write to the
file, and vice versa. The PermissionsExt
trait gives direct access
to the permission bits but also does not read ACLs. If you need to
accurately know whether or not a file is writable use the access()
function from libc.
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(())
}
sourcepub fn set_readonly(&mut self, readonly: bool)
pub fn set_readonly(&mut self, readonly: bool)
Modifies the readonly flag for this set of permissions. If the
readonly
argument is true
, using the resulting Permission
will
update file permissions to forbid writing. Conversely, if it’s false
,
using the resulting Permission
will 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 Permissions
instance. To modify the files attributes use the set_permissions
function which commits these attribute changes to the file.
Note
set_readonly(false)
makes the file world-writable on Unix.
You can use the PermissionsExt
trait 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
.
If FILE_ATTRIBUTE_READONLY
is set then writes to the file will fail
but the user may still have permission to change this flag. If
FILE_ATTRIBUTE_READONLY
is 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>
or chmod a-w <file>
respectively. The latter will grant write access
to all users! You can use the PermissionsExt
trait 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(())
}
Trait Implementations§
source§impl Clone for Permissions
impl Clone for Permissions
source§fn clone(&self) -> Permissions
fn clone(&self) -> Permissions
source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Permissions
impl Debug for Permissions
source§impl PartialEq for Permissions
impl PartialEq for Permissions
1.1.0 · source§impl PermissionsExt for Permissions
impl PermissionsExt for Permissions
impl Eq for Permissions
impl StructuralEq for Permissions
impl StructuralPartialEq for Permissions
Auto Trait Implementations§
impl RefUnwindSafe for Permissions
impl Send for Permissions
impl Sync for Permissions
impl Unpin for Permissions
impl UnwindSafe for Permissions
Blanket Implementations§
source§impl<T> Also for T
impl<T> Also for T
source§impl<T> AnyExt for Twhere
T: Any,
impl<T> AnyExt for Twhere
T: Any,
source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
any
only.self
. Read moresource§fn as_any_ref(&self) -> &dyn Anywhere
Self: Sized,
fn as_any_ref(&self) -> &dyn Anywhere
Self: Sized,
any
only.source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
any
only.source§impl<T, Res> Apply<Res> for Twhere
T: ?Sized,
impl<T, Res> Apply<Res> for Twhere
T: ?Sized,
source§fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Reswhere
Self: Sized,
fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Reswhere
Self: Sized,
result
only.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
source§impl<T> Mem for Twhere
T: ?Sized,
impl<T> Mem for Twhere
T: ?Sized,
source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
mem
only.source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
mem
only.true
if dropping values of this type matters.source§fn mem_drop(self)where
Self: Sized,
fn mem_drop(self)where
Self: Sized,
mem
only.self
by running its destructor.source§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
mem
only.self
without running its destructor.source§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
mem
only.self
with other, returning the previous value of self
.source§fn mem_take(&mut self) -> Selfwhere
Self: Default,
fn mem_take(&mut self) -> Selfwhere
Self: Default,
mem
only.self
with its default value, returning the previous value of self
.source§fn mem_swap(&mut self, other: &mut Self)where
Self: Sized,
fn mem_swap(&mut self, other: &mut Self)where
Self: Sized,
mem
only.self
and other
without deinitializing either one.source§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
mem
and unsafe_mem
only.source§impl<T> Size for T
impl<T> Size for T
source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
mem
only.source§const BYTE_SIZE: usize = _
const BYTE_SIZE: usize = _
mem
only.source§const PTR_SIZE: usize = 4usize
const PTR_SIZE: usize = 4usize
mem
only.source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
mem
only.