From e3910794961b820defedd909b11e69681803e0db Mon Sep 17 00:00:00 2001 From: Trevor Merrifield Date: Mon, 11 Sep 2017 20:02:44 -0400 Subject: [PATCH 1/3] Retain suid/sgid/sticky bits in Metadata.permissions Most users would expect set_permissions(Metadata.permissions()) to be non-destructive. While we can't guarantee this, we can at least pass the needed info to chmod. Also update the PermissionsExt documentation to disambiguate what it contains, and to refer to the underlying value as `st_mode` rather than its type `mode_t`. Closes #44147 --- src/libstd/sys/unix/ext/fs.rs | 4 ++-- src/libstd/sys/unix/fs.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/sys/unix/ext/fs.rs b/src/libstd/sys/unix/ext/fs.rs index f44b9aa96156..96c0f4eab427 100644 --- a/src/libstd/sys/unix/ext/fs.rs +++ b/src/libstd/sys/unix/ext/fs.rs @@ -68,8 +68,8 @@ fn write_at(&self, buf: &[u8], offset: u64) -> io::Result { /// Unix-specific extensions to `Permissions` #[stable(feature = "fs_ext", since = "1.1.0")] pub trait PermissionsExt { - /// Returns the underlying raw `mode_t` bits that are the standard Unix - /// permissions for this file. + /// Returns the 12 least significant bits of `st_mode` which are the + /// standard Unix permissions for this file. /// /// # Examples /// diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index 13112fc1fa59..e0ce02873f5c 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -95,7 +95,7 @@ pub struct DirBuilder { mode: mode_t } impl FileAttr { pub fn size(&self) -> u64 { self.stat.st_size as u64 } pub fn perm(&self) -> FilePermissions { - FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o777 } + FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o7777 } } pub fn file_type(&self) -> FileType { From 04c01e0b1fc4830f4416c79d886c2f1a54986fe9 Mon Sep 17 00:00:00 2001 From: Trevor Merrifield Date: Sun, 17 Sep 2017 17:01:35 -0400 Subject: [PATCH 2/3] Add test case for unix permissions --- src/libstd/fs.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 2a916b819cca..991627660815 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -2160,6 +2160,27 @@ fn file_test_io_read_write_at() { check!(fs::remove_file(&filename)); } + #[test] + #[cfg(unix)] + fn set_get_unix_permissions() { + use os::unix::fs::PermissionsExt; + + let tmpdir = tmpdir(); + let filename = &tmpdir.join("set_get_unix_permissions"); + check!(fs::create_dir(filename)); + let mask = 0o7777; + + check!(fs::set_permissions(filename, + fs::Permissions::from_mode(0))); + let metadata0 = check!(fs::metadata(filename)); + assert_eq!(mask & metadata0.permissions().mode(), 0); + + check!(fs::set_permissions(filename, + fs::Permissions::from_mode(0o1777))); + let metadata1 = check!(fs::metadata(filename)); + assert_eq!(mask & metadata1.permissions().mode(), 0o1777); + } + #[test] #[cfg(windows)] fn file_test_io_seek_read_write() { From 6ae9fc277287e4f207012ab65be24058f143d42d Mon Sep 17 00:00:00 2001 From: Trevor Merrifield Date: Sun, 17 Sep 2017 17:42:58 -0400 Subject: [PATCH 3/3] Remove st_mode mask --- src/libstd/sys/unix/ext/fs.rs | 4 ++-- src/libstd/sys/unix/fs.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/sys/unix/ext/fs.rs b/src/libstd/sys/unix/ext/fs.rs index 96c0f4eab427..3e631ad40ac7 100644 --- a/src/libstd/sys/unix/ext/fs.rs +++ b/src/libstd/sys/unix/ext/fs.rs @@ -68,8 +68,8 @@ fn write_at(&self, buf: &[u8], offset: u64) -> io::Result { /// Unix-specific extensions to `Permissions` #[stable(feature = "fs_ext", since = "1.1.0")] pub trait PermissionsExt { - /// Returns the 12 least significant bits of `st_mode` which are the - /// standard Unix permissions for this file. + /// Returns the underlying raw `st_mode` bits that contain the standard + /// Unix permissions for this file. /// /// # Examples /// diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index e0ce02873f5c..c4616c3b395b 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -95,7 +95,7 @@ pub struct DirBuilder { mode: mode_t } impl FileAttr { pub fn size(&self) -> u64 { self.stat.st_size as u64 } pub fn perm(&self) -> FilePermissions { - FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o7777 } + FilePermissions { mode: (self.stat.st_mode as mode_t) } } pub fn file_type(&self) -> FileType {