Skip to content

Commit ee409a4

Browse files
committed
Auto merge of #44624 - tmerr:master, r=sfackler
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
2 parents 0962b8f + 6ae9fc2 commit ee409a4

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/libstd/fs.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,27 @@ mod tests {
21602160
check!(fs::remove_file(&filename));
21612161
}
21622162

2163+
#[test]
2164+
#[cfg(unix)]
2165+
fn set_get_unix_permissions() {
2166+
use os::unix::fs::PermissionsExt;
2167+
2168+
let tmpdir = tmpdir();
2169+
let filename = &tmpdir.join("set_get_unix_permissions");
2170+
check!(fs::create_dir(filename));
2171+
let mask = 0o7777;
2172+
2173+
check!(fs::set_permissions(filename,
2174+
fs::Permissions::from_mode(0)));
2175+
let metadata0 = check!(fs::metadata(filename));
2176+
assert_eq!(mask & metadata0.permissions().mode(), 0);
2177+
2178+
check!(fs::set_permissions(filename,
2179+
fs::Permissions::from_mode(0o1777)));
2180+
let metadata1 = check!(fs::metadata(filename));
2181+
assert_eq!(mask & metadata1.permissions().mode(), 0o1777);
2182+
}
2183+
21632184
#[test]
21642185
#[cfg(windows)]
21652186
fn file_test_io_seek_read_write() {

src/libstd/sys/unix/ext/fs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ impl FileExt for fs::File {
6868
/// Unix-specific extensions to `Permissions`
6969
#[stable(feature = "fs_ext", since = "1.1.0")]
7070
pub trait PermissionsExt {
71-
/// Returns the underlying raw `mode_t` bits that are the standard Unix
72-
/// permissions for this file.
71+
/// Returns the underlying raw `st_mode` bits that contain the standard
72+
/// Unix permissions for this file.
7373
///
7474
/// # Examples
7575
///

src/libstd/sys/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub struct DirBuilder { mode: mode_t }
9595
impl FileAttr {
9696
pub fn size(&self) -> u64 { self.stat.st_size as u64 }
9797
pub fn perm(&self) -> FilePermissions {
98-
FilePermissions { mode: (self.stat.st_mode as mode_t) & 0o777 }
98+
FilePermissions { mode: (self.stat.st_mode as mode_t) }
9999
}
100100

101101
pub fn file_type(&self) -> FileType {

0 commit comments

Comments
 (0)