1 #![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
2 
3 use std::{env, process};
4 use test_programs::preview1::{config, open_scratch_directory};
5 
test_dangling_fd(dir_fd: wasip1::Fd)6 unsafe fn test_dangling_fd(dir_fd: wasip1::Fd) {
7     if config().support_dangling_filesystem() {
8         // Create a file, open it, delete it without closing the handle,
9         // and then try creating it again
10         let fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0).unwrap();
11         wasip1::fd_close(fd).unwrap();
12         let file_fd = wasip1::path_open(dir_fd, 0, "file", 0, 0, 0, 0).expect("failed to open");
13         assert!(
14             file_fd > libc::STDERR_FILENO as wasip1::Fd,
15             "file descriptor range check",
16         );
17         wasip1::path_unlink_file(dir_fd, "file").expect("failed to unlink");
18         let fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0).unwrap();
19         wasip1::fd_close(fd).unwrap();
20 
21         // Now, repeat the same process but for a directory
22         wasip1::path_create_directory(dir_fd, "subdir").expect("failed to create dir");
23         let subdir_fd = wasip1::path_open(dir_fd, 0, "subdir", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
24             .expect("failed to open dir");
25         assert!(
26             subdir_fd > libc::STDERR_FILENO as wasip1::Fd,
27             "file descriptor range check",
28         );
29         wasip1::path_remove_directory(dir_fd, "subdir").expect("failed to remove dir 2");
30         wasip1::path_create_directory(dir_fd, "subdir").expect("failed to create dir 2");
31     }
32 }
33 
main()34 fn main() {
35     let mut args = env::args();
36     let prog = args.next().unwrap();
37     let arg = if let Some(arg) = args.next() {
38         arg
39     } else {
40         eprintln!("usage: {prog} <scratch directory>");
41         process::exit(1);
42     };
43 
44     // Open scratch directory
45     let dir_fd = match open_scratch_directory(&arg) {
46         Ok(dir_fd) => dir_fd,
47         Err(err) => {
48             eprintln!("{err}");
49             process::exit(1)
50         }
51     };
52 
53     // Run the tests.
54     unsafe { test_dangling_fd(dir_fd) }
55 }
56