Auto merge of #43883 - frewsxcv:frewsxcv-set-readonly-clarification, r=QuietMisdreavus

Clarify writable behavior of readonly-named `Permissions` methods.

Opened primarily to fix https://github.com/rust-lang/rust/issues/41984.
This commit is contained in:
bors 2017-08-16 06:56:11 +00:00
commit 4fc3765c54
2 changed files with 13 additions and 3 deletions

View File

@ -916,7 +916,7 @@ impl AsInner<fs_imp::FileAttr> for Metadata {
}
impl Permissions {
/// Returns whether these permissions describe a readonly file.
/// Returns whether these permissions describe a readonly (unwritable) file.
///
/// # Examples
///
@ -934,7 +934,11 @@ impl Permissions {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn readonly(&self) -> bool { self.0.readonly() }
/// Modifies the readonly flag for this set of permissions.
/// 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 filesystem. To modify the
/// filesystem use the `fs::set_permissions` function.

View File

@ -170,11 +170,17 @@ impl AsInner<stat64> for FileAttr {
}
impl FilePermissions {
pub fn readonly(&self) -> bool { self.mode & 0o222 == 0 }
pub fn readonly(&self) -> bool {
// check if any class (owner, group, others) has write permission
self.mode & 0o222 == 0
}
pub fn set_readonly(&mut self, readonly: bool) {
if readonly {
// remove write permission for all classes; equivalent to `chmod a-w <file>`
self.mode &= !0o222;
} else {
// add write permission for all classes; equivalent to `chmod a+w <file>`
self.mode |= 0o222;
}
}