diff options
| author | bors <bors@rust-lang.org> | 2017-08-16 06:56:11 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-08-16 06:56:11 +0000 |
| commit | 4fc3765c5477f98cfdf325475b2b01c6c094ed2d (patch) | |
| tree | fd60e890abe6577dcda55e49988eda766c68f1fc /src/libstd | |
| parent | 6f4ab9458a7ad06c8ce630604f533c8c0c0acef4 (diff) | |
| parent | 10a479ebe170398e70f872a7afa222d00a9c8f53 (diff) | |
| download | rust-4fc3765c5477f98cfdf325475b2b01c6c094ed2d.tar.gz rust-4fc3765c5477f98cfdf325475b2b01c6c094ed2d.zip | |
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.
Diffstat (limited to 'src/libstd')
| -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; } } |
