File tree 1 file changed +26
-0
lines changed
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -823,6 +823,32 @@ mod tests {
823
823
assert_eq ! ( contents, original_contents) ;
824
824
}
825
825
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
+
826
852
#[ test]
827
853
fn join_paths_lists_paths_on_error ( ) {
828
854
let valid_paths = vec ! [ "/testing/one" , "/testing/two" ] ;
You can’t perform that action at this time.
0 commit comments