1- //@ignore-target-windows: no libc on Windows
1+ //@ignore-target-windows: File handling is not implemented yet
22//@compile-flags: -Zmiri-disable-isolation
33
44#![ feature( io_error_more) ]
55#![ feature( io_error_uncategorized) ]
66
77use std:: ffi:: { CStr , CString , OsString } ;
8- use std:: fs:: { canonicalize, remove_dir_all , remove_file, File } ;
8+ use std:: fs:: { canonicalize, remove_file, File } ;
99use std:: io:: { Error , ErrorKind , Write } ;
1010use std:: os:: unix:: ffi:: OsStrExt ;
1111use std:: os:: unix:: io:: AsRawFd ;
@@ -21,7 +21,6 @@ fn main() {
2121 test_ftruncate :: < libc:: off_t > ( libc:: ftruncate) ;
2222 #[ cfg( target_os = "linux" ) ]
2323 test_ftruncate :: < libc:: off64_t > ( libc:: ftruncate64) ;
24- test_readlink ( ) ;
2524 test_file_open_unix_allow_two_args ( ) ;
2625 test_file_open_unix_needs_three_args ( ) ;
2726 test_file_open_unix_extra_third_arg ( ) ;
@@ -38,33 +37,8 @@ fn main() {
3837 test_isatty ( ) ;
3938}
4039
41- /// Prepare: compute filename and make sure the file does not exist.
42- fn prepare ( filename : & str ) -> PathBuf {
43- let path = utils:: tmp ( ) . join ( filename) ;
44- // Clean the paths for robustness.
45- remove_file ( & path) . ok ( ) ;
46- path
47- }
48-
49- /// Prepare directory: compute directory name and make sure it does not exist.
50- #[ allow( unused) ]
51- fn prepare_dir ( dirname : & str ) -> PathBuf {
52- let path = utils:: tmp ( ) . join ( & dirname) ;
53- // Clean the directory for robustness.
54- remove_dir_all ( & path) . ok ( ) ;
55- path
56- }
57-
58- /// Prepare like above, and also write some initial content to the file.
59- fn prepare_with_content ( filename : & str , content : & [ u8 ] ) -> PathBuf {
60- let path = prepare ( filename) ;
61- let mut file = File :: create ( & path) . unwrap ( ) ;
62- file. write ( content) . unwrap ( ) ;
63- path
64- }
65-
6640fn test_file_open_unix_allow_two_args ( ) {
67- let path = prepare_with_content ( "test_file_open_unix_allow_two_args.txt" , & [ ] ) ;
41+ let path = utils :: prepare_with_content ( "test_file_open_unix_allow_two_args.txt" , & [ ] ) ;
6842
6943 let mut name = path. into_os_string ( ) ;
7044 name. push ( "\0 " ) ;
@@ -73,7 +47,7 @@ fn test_file_open_unix_allow_two_args() {
7347}
7448
7549fn test_file_open_unix_needs_three_args ( ) {
76- let path = prepare_with_content ( "test_file_open_unix_needs_three_args.txt" , & [ ] ) ;
50+ let path = utils :: prepare_with_content ( "test_file_open_unix_needs_three_args.txt" , & [ ] ) ;
7751
7852 let mut name = path. into_os_string ( ) ;
7953 name. push ( "\0 " ) ;
@@ -82,7 +56,7 @@ fn test_file_open_unix_needs_three_args() {
8256}
8357
8458fn test_file_open_unix_extra_third_arg ( ) {
85- let path = prepare_with_content ( "test_file_open_unix_extra_third_arg.txt" , & [ ] ) ;
59+ let path = utils :: prepare_with_content ( "test_file_open_unix_extra_third_arg.txt" , & [ ] ) ;
8660
8761 let mut name = path. into_os_string ( ) ;
8862 name. push ( "\0 " ) ;
@@ -106,49 +80,9 @@ fn test_canonicalize_too_long() {
10680 assert ! ( canonicalize( too_long) . is_err( ) ) ;
10781}
10882
109- fn test_readlink ( ) {
110- let bytes = b"Hello, World!\n " ;
111- let path = prepare_with_content ( "miri_test_fs_link_target.txt" , bytes) ;
112- let expected_path = path. as_os_str ( ) . as_bytes ( ) ;
113-
114- let symlink_path = prepare ( "miri_test_fs_symlink.txt" ) ;
115- std:: os:: unix:: fs:: symlink ( & path, & symlink_path) . unwrap ( ) ;
116-
117- // Test that the expected string gets written to a buffer of proper
118- // length, and that a trailing null byte is not written.
119- let symlink_c_str = CString :: new ( symlink_path. as_os_str ( ) . as_bytes ( ) ) . unwrap ( ) ;
120- let symlink_c_ptr = symlink_c_str. as_ptr ( ) ;
121-
122- // Make the buf one byte larger than it needs to be,
123- // and check that the last byte is not overwritten.
124- let mut large_buf = vec ! [ 0xFF ; expected_path. len( ) + 1 ] ;
125- let res =
126- unsafe { libc:: readlink ( symlink_c_ptr, large_buf. as_mut_ptr ( ) . cast ( ) , large_buf. len ( ) ) } ;
127- // Check that the resolved path was properly written into the buf.
128- assert_eq ! ( & large_buf[ ..( large_buf. len( ) - 1 ) ] , expected_path) ;
129- assert_eq ! ( large_buf. last( ) , Some ( & 0xFF ) ) ;
130- assert_eq ! ( res, large_buf. len( ) as isize - 1 ) ;
131-
132- // Test that the resolved path is truncated if the provided buffer
133- // is too small.
134- let mut small_buf = [ 0u8 ; 2 ] ;
135- let res =
136- unsafe { libc:: readlink ( symlink_c_ptr, small_buf. as_mut_ptr ( ) . cast ( ) , small_buf. len ( ) ) } ;
137- assert_eq ! ( small_buf, & expected_path[ ..small_buf. len( ) ] ) ;
138- assert_eq ! ( res, small_buf. len( ) as isize ) ;
139-
140- // Test that we report a proper error for a missing path.
141- let bad_path = CString :: new ( "MIRI_MISSING_FILE_NAME" ) . unwrap ( ) ;
142- let res = unsafe {
143- libc:: readlink ( bad_path. as_ptr ( ) , small_buf. as_mut_ptr ( ) . cast ( ) , small_buf. len ( ) )
144- } ;
145- assert_eq ! ( res, -1 ) ;
146- assert_eq ! ( Error :: last_os_error( ) . kind( ) , ErrorKind :: NotFound ) ;
147- }
148-
14983fn test_rename ( ) {
150- let path1 = prepare ( "miri_test_libc_fs_source.txt" ) ;
151- let path2 = prepare ( "miri_test_libc_fs_rename_destination.txt" ) ;
84+ let path1 = utils :: prepare ( "miri_test_libc_fs_source.txt" ) ;
85+ let path2 = utils :: prepare ( "miri_test_libc_fs_rename_destination.txt" ) ;
15286
15387 let file = File :: create ( & path1) . unwrap ( ) ;
15488 drop ( file) ;
@@ -178,7 +112,7 @@ fn test_ftruncate<T: From<i32>>(
178112 // https://docs.rs/libc/latest/i686-unknown-linux-gnu/libc/type.off_t.html
179113
180114 let bytes = b"hello" ;
181- let path = prepare ( "miri_test_libc_fs_ftruncate.txt" ) ;
115+ let path = utils :: prepare ( "miri_test_libc_fs_ftruncate.txt" ) ;
182116 let mut file = File :: create ( & path) . unwrap ( ) ;
183117 file. write ( bytes) . unwrap ( ) ;
184118 file. sync_all ( ) . unwrap ( ) ;
@@ -209,7 +143,7 @@ fn test_ftruncate<T: From<i32>>(
209143fn test_o_tmpfile_flag ( ) {
210144 use std:: fs:: { create_dir, OpenOptions } ;
211145 use std:: os:: unix:: fs:: OpenOptionsExt ;
212- let dir_path = prepare_dir ( "miri_test_fs_dir" ) ;
146+ let dir_path = utils :: prepare_dir ( "miri_test_fs_dir" ) ;
213147 create_dir ( & dir_path) . unwrap ( ) ;
214148 // test that the `O_TMPFILE` custom flag gracefully errors instead of stopping execution
215149 assert_eq ! (
0 commit comments