Skip to content

Commit 038ccd2

Browse files
committed
test: write_atomic changes file permissions to 0o600 on unix
1 parent 4de0094 commit 038ccd2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,32 @@ mod tests {
823823
assert_eq!(contents, original_contents);
824824
}
825825

826+
#[test]
827+
#[cfg(unix)]
828+
fn write_atomic_permissions() {
829+
use std::os::unix::fs::PermissionsExt;
830+
831+
let original_perms = std::fs::Permissions::from_mode(u32::from(
832+
libc::S_IRWXU | libc::S_IRGRP | libc::S_IWGRP | libc::S_IROTH,
833+
));
834+
835+
let tmp = tempfile::Builder::new().tempfile().unwrap();
836+
837+
// need to set the permissions after creating the file to avoid umask
838+
tmp.as_file()
839+
.set_permissions(original_perms.clone())
840+
.unwrap();
841+
842+
// after this call, the file at `tmp.path()` will not be the same as the file held by `tmp`
843+
write_atomic(tmp.path(), "new").unwrap();
844+
assert_eq!(std::fs::read_to_string(tmp.path()).unwrap(), "new");
845+
846+
let new_perms = std::fs::metadata(tmp.path()).unwrap().permissions();
847+
848+
let mask = u32::from(libc::S_IRWXU | libc::S_IRWXG | libc::S_IRWXO);
849+
assert_eq!(0o600, new_perms.mode() & mask);
850+
}
851+
826852
#[test]
827853
fn join_paths_lists_paths_on_error() {
828854
let valid_paths = vec!["/testing/one", "/testing/two"];

0 commit comments

Comments
 (0)