1 // ignore-windows: File handling is not implemented yet
2 // compile-flags: -Zmiri-disable-isolation
5 File, create_dir, OpenOptions, read_dir, remove_dir, remove_dir_all, remove_file, rename,
7 use std::io::{Read, Write, ErrorKind, Result, Seek, SeekFrom};
8 use std::path::{PathBuf, Path};
13 test_file_create_new();
25 std::env::var("MIRI_TEMP").map(PathBuf::from).unwrap_or_else(|_| std::env::temp_dir())
28 /// Prepare: compute filename and make sure the file does not exist.
29 fn prepare(filename: &str) -> PathBuf {
30 let path = tmp().join(filename);
31 // Clean the paths for robustness.
32 remove_file(&path).ok();
36 /// Prepare directory: compute directory name and make sure it does not exist.
37 fn prepare_dir(dirname: &str) -> PathBuf {
38 let path = tmp().join(&dirname);
39 // Clean the directory for robustness.
40 remove_dir_all(&path).ok();
44 /// Prepare like above, and also write some initial content to the file.
45 fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf {
46 let path = prepare(filename);
47 let mut file = File::create(&path).unwrap();
48 file.write(content).unwrap();
53 let bytes = b"Hello, World!\n";
54 let path = prepare("miri_test_fs_file.txt");
56 // Test creating, writing and closing a file (closing is tested when `file` is dropped).
57 let mut file = File::create(&path).unwrap();
58 // Writing 0 bytes should not change the file contents.
59 file.write(&mut []).unwrap();
60 assert_eq!(file.metadata().unwrap().len(), 0);
62 file.write(bytes).unwrap();
63 assert_eq!(file.metadata().unwrap().len(), bytes.len() as u64);
64 // Test opening, reading and closing a file.
65 let mut file = File::open(&path).unwrap();
66 let mut contents = Vec::new();
67 // Reading 0 bytes should not move the file pointer.
68 file.read(&mut []).unwrap();
69 // Reading until EOF should get the whole text.
70 file.read_to_end(&mut contents).unwrap();
71 assert_eq!(bytes, contents.as_slice());
73 // Removing file should succeed.
74 remove_file(&path).unwrap();
77 fn test_file_clone() {
78 let bytes = b"Hello, World!\n";
79 let path = prepare_with_content("miri_test_fs_file_clone.txt", bytes);
81 // Cloning a file should be successful.
82 let file = File::open(&path).unwrap();
83 let mut cloned = file.try_clone().unwrap();
84 // Reading from a cloned file should get the same text.
85 let mut contents = Vec::new();
86 cloned.read_to_end(&mut contents).unwrap();
87 assert_eq!(bytes, contents.as_slice());
89 // Removing file should succeed.
90 remove_file(&path).unwrap();
93 fn test_file_create_new() {
94 let path = prepare("miri_test_fs_file_create_new.txt");
96 // Creating a new file that doesn't yet exist should succeed.
97 OpenOptions::new().write(true).create_new(true).open(&path).unwrap();
98 // Creating a new file that already exists should fail.
99 assert_eq!(ErrorKind::AlreadyExists, OpenOptions::new().write(true).create_new(true).open(&path).unwrap_err().kind());
100 // Optionally creating a new file that already exists should succeed.
101 OpenOptions::new().write(true).create(true).open(&path).unwrap();
104 remove_file(&path).unwrap();
108 let bytes = b"Hello, entire World!\n";
109 let path = prepare_with_content("miri_test_fs_seek.txt", bytes);
111 let mut file = File::open(&path).unwrap();
112 let mut contents = Vec::new();
113 file.read_to_end(&mut contents).unwrap();
114 assert_eq!(bytes, contents.as_slice());
115 // Test that seeking to the beginning and reading until EOF gets the text again.
116 file.seek(SeekFrom::Start(0)).unwrap();
117 let mut contents = Vec::new();
118 file.read_to_end(&mut contents).unwrap();
119 assert_eq!(bytes, contents.as_slice());
120 // Test seeking relative to the end of the file.
121 file.seek(SeekFrom::End(-1)).unwrap();
122 let mut contents = Vec::new();
123 file.read_to_end(&mut contents).unwrap();
124 assert_eq!(&bytes[bytes.len() - 1..], contents.as_slice());
125 // Test seeking relative to the current position.
126 file.seek(SeekFrom::Start(5)).unwrap();
127 file.seek(SeekFrom::Current(-3)).unwrap();
128 let mut contents = Vec::new();
129 file.read_to_end(&mut contents).unwrap();
130 assert_eq!(&bytes[2..], contents.as_slice());
132 // Removing file should succeed.
133 remove_file(&path).unwrap();
136 fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
137 // Test that the file metadata is correct.
138 let metadata = path.metadata()?;
139 // `path` should point to a file.
140 assert!(metadata.is_file());
141 // The size of the file must be equal to the number of written bytes.
142 assert_eq!(bytes.len() as u64, metadata.len());
147 let bytes = b"Hello, meta-World!\n";
148 let path = prepare_with_content("miri_test_fs_metadata.txt", bytes);
150 // Test that metadata of an absolute path is correct.
151 check_metadata(bytes, &path).unwrap();
152 // Test that metadata of a relative path is correct.
153 std::env::set_current_dir(path.parent().unwrap()).unwrap();
154 check_metadata(bytes, Path::new(path.file_name().unwrap())).unwrap();
156 // Removing file should succeed.
157 remove_file(&path).unwrap();
160 fn test_file_set_len() {
161 let bytes = b"Hello, World!\n";
162 let path = prepare_with_content("miri_test_fs_set_len.txt", bytes);
164 // Test extending the file
165 let mut file = OpenOptions::new().read(true).write(true).open(&path).unwrap();
166 let bytes_extended = b"Hello, World!\n\x00\x00\x00\x00\x00\x00";
167 file.set_len(20).unwrap();
168 let mut contents = Vec::new();
169 file.read_to_end(&mut contents).unwrap();
170 assert_eq!(bytes_extended, contents.as_slice());
172 // Test truncating the file
173 file.seek(SeekFrom::Start(0)).unwrap();
174 file.set_len(10).unwrap();
175 let mut contents = Vec::new();
176 file.read_to_end(&mut contents).unwrap();
177 assert_eq!(&bytes[..10], contents.as_slice());
179 // Can't use set_len on a file not opened for writing
180 let file = OpenOptions::new().read(true).open(&path).unwrap();
181 assert_eq!(ErrorKind::InvalidInput, file.set_len(14).unwrap_err().kind());
183 remove_file(&path).unwrap();
186 fn test_file_sync() {
187 let bytes = b"Hello, World!\n";
188 let path = prepare_with_content("miri_test_fs_sync.txt", bytes);
190 // Test that we can call sync_data and sync_all (can't readily test effects of this operation)
191 let file = OpenOptions::new().write(true).open(&path).unwrap();
192 file.sync_data().unwrap();
193 file.sync_all().unwrap();
195 // Test that we can call sync_data and sync_all on a file opened for reading.
196 let file = File::open(&path).unwrap();
197 file.sync_data().unwrap();
198 file.sync_all().unwrap();
200 remove_file(&path).unwrap();
204 let bytes = b"Hello, World!\n";
205 let path = prepare_with_content("miri_test_fs_link_target.txt", bytes);
206 let symlink_path = prepare("miri_test_fs_symlink.txt");
208 // Creating a symbolic link should succeed.
210 std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
212 std::os::windows::fs::symlink_file(&path, &symlink_path).unwrap();
213 // Test that the symbolic link has the same contents as the file.
214 let mut symlink_file = File::open(&symlink_path).unwrap();
215 let mut contents = Vec::new();
216 symlink_file.read_to_end(&mut contents).unwrap();
217 assert_eq!(bytes, contents.as_slice());
218 // Test that metadata of a symbolic link is correct.
219 check_metadata(bytes, &symlink_path).unwrap();
220 // Test that the metadata of a symbolic link is correct when not following it.
221 assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
222 // Removing symbolic link should succeed.
223 remove_file(&symlink_path).unwrap();
225 // Removing file should succeed.
226 remove_file(&path).unwrap();
230 let bytes = b"Hello, World!\n";
231 let path = prepare("miri_test_fs_errors.txt");
233 // The following tests also check that the `__errno_location()` shim is working properly.
234 // Opening a non-existing file should fail with a "not found" error.
235 assert_eq!(ErrorKind::NotFound, File::open(&path).unwrap_err().kind());
236 // Removing a non-existing file should fail with a "not found" error.
237 assert_eq!(ErrorKind::NotFound, remove_file(&path).unwrap_err().kind());
238 // Reading the metadata of a non-existing file should fail with a "not found" error.
239 assert_eq!(ErrorKind::NotFound, check_metadata(bytes, &path).unwrap_err().kind());
243 // Renaming a file should succeed.
244 let path1 = prepare("miri_test_fs_rename_source.txt");
245 let path2 = prepare("miri_test_fs_rename_destination.txt");
247 let file = File::create(&path1).unwrap();
250 // Renaming should succeed
251 rename(&path1, &path2).unwrap();
252 // Check that the old file path isn't present
253 assert_eq!(ErrorKind::NotFound, path1.metadata().unwrap_err().kind());
254 // Check that the file has moved successfully
255 assert!(path2.metadata().unwrap().is_file());
257 // Renaming a nonexistent file should fail
258 assert_eq!(ErrorKind::NotFound, rename(&path1, &path2).unwrap_err().kind());
260 remove_file(&path2).unwrap();
263 fn test_directory() {
264 let dir_path = prepare_dir("miri_test_fs_dir");
265 // Creating a directory should succeed.
266 create_dir(&dir_path).unwrap();
267 // Test that the metadata of a directory is correct.
268 assert!(dir_path.metadata().unwrap().is_dir());
269 // Creating a directory when it already exists should fail.
270 assert_eq!(ErrorKind::AlreadyExists, create_dir(&dir_path).unwrap_err().kind());
272 // Create some files inside the directory
273 let path_1 = dir_path.join("test_file_1");
274 drop(File::create(&path_1).unwrap());
275 let path_2 = dir_path.join("test_file_2");
276 drop(File::create(&path_2).unwrap());
277 // Test that the files are present inside the directory
278 let dir_iter = read_dir(&dir_path).unwrap();
279 let mut file_names = dir_iter.map(|e| e.unwrap().file_name()).collect::<Vec<_>>();
280 file_names.sort_unstable();
281 assert_eq!(file_names, vec!["test_file_1", "test_file_2"]);
282 // Clean up the files in the directory
283 remove_file(&path_1).unwrap();
284 remove_file(&path_2).unwrap();
285 // Now there should be nothing left in the directory.
286 let dir_iter = read_dir(&dir_path).unwrap();
287 let file_names = dir_iter.map(|e| e.unwrap().file_name()).collect::<Vec<_>>();
288 assert!(file_names.is_empty());
290 // Deleting the directory should succeed.
291 remove_dir(&dir_path).unwrap();
292 // Reading the metadata of a non-existent directory should fail with a "not found" error.
293 assert_eq!(ErrorKind::NotFound, check_metadata(&[], &dir_path).unwrap_err().kind());