diff options
| -rw-r--r-- | src/libstd/fs.rs | 8 | ||||
| -rw-r--r-- | src/libstd/sys/unix/fs.rs | 8 |
2 files changed, 13 insertions, 3 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 1e692abaff2..a438b4afdd0 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -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. diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index 4e6fde5c29d..cb0f687e072 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -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; } } |
