Skip to content

Commit 4fc3765

Browse files
committed
Auto merge of #43883 - frewsxcv:frewsxcv-set-readonly-clarification, r=QuietMisdreavus
Clarify writable behavior of readonly-named `Permissions` methods. Opened primarily to fix #41984.
2 parents 6f4ab94 + 10a479e commit 4fc3765

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/libstd/fs.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ impl AsInner<fs_imp::FileAttr> for Metadata {
916916
}
917917

918918
impl Permissions {
919-
/// Returns whether these permissions describe a readonly file.
919+
/// Returns whether these permissions describe a readonly (unwritable) file.
920920
///
921921
/// # Examples
922922
///
@@ -934,7 +934,11 @@ impl Permissions {
934934
#[stable(feature = "rust1", since = "1.0.0")]
935935
pub fn readonly(&self) -> bool { self.0.readonly() }
936936

937-
/// Modifies the readonly flag for this set of permissions.
937+
/// Modifies the readonly flag for this set of permissions. If the
938+
/// `readonly` argument is `true`, using the resulting `Permission` will
939+
/// update file permissions to forbid writing. Conversely, if it's `false`,
940+
/// using the resulting `Permission` will update file permissions to allow
941+
/// writing.
938942
///
939943
/// This operation does **not** modify the filesystem. To modify the
940944
/// filesystem use the `fs::set_permissions` function.

src/libstd/sys/unix/fs.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,17 @@ impl AsInner<stat64> for FileAttr {
170170
}
171171

172172
impl FilePermissions {
173-
pub fn readonly(&self) -> bool { self.mode & 0o222 == 0 }
173+
pub fn readonly(&self) -> bool {
174+
// check if any class (owner, group, others) has write permission
175+
self.mode & 0o222 == 0
176+
}
177+
174178
pub fn set_readonly(&mut self, readonly: bool) {
175179
if readonly {
180+
// remove write permission for all classes; equivalent to `chmod a-w <file>`
176181
self.mode &= !0o222;
177182
} else {
183+
// add write permission for all classes; equivalent to `chmod a+w <file>`
178184
self.mode |= 0o222;
179185
}
180186
}

0 commit comments

Comments
 (0)