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::{assert_errno, open_scratch_directory};
5 
test_remove_nonempty_directory(dir_fd: wasip1::Fd)6 unsafe fn test_remove_nonempty_directory(dir_fd: wasip1::Fd) {
7     // Create a directory in the scratch directory.
8     wasip1::path_create_directory(dir_fd, "dir").expect("creating a directory");
9 
10     // Create a directory in the directory we just created.
11     wasip1::path_create_directory(dir_fd, "dir/nested").expect("creating a subdirectory");
12 
13     // Test that attempting to unlink the first directory returns the expected error code.
14     assert_errno!(
15         wasip1::path_remove_directory(dir_fd, "dir")
16             .expect_err("remove_directory on a directory should return ENOTEMPTY"),
17         wasip1::ERRNO_NOTEMPTY
18     );
19 
20     // Removing the directories.
21     wasip1::path_remove_directory(dir_fd, "dir/nested")
22         .expect("remove_directory on a nested directory should succeed");
23     wasip1::path_remove_directory(dir_fd, "dir").expect("removing a directory");
24 }
25 
main()26 fn main() {
27     let mut args = env::args();
28     let prog = args.next().unwrap();
29     let arg = if let Some(arg) = args.next() {
30         arg
31     } else {
32         eprintln!("usage: {prog} <scratch directory>");
33         process::exit(1);
34     };
35 
36     // Open scratch directory
37     let dir_fd = match open_scratch_directory(&arg) {
38         Ok(dir_fd) => dir_fd,
39         Err(err) => {
40             eprintln!("{err}");
41             process::exit(1)
42         }
43     };
44 
45     // Run the tests.
46     unsafe { test_remove_nonempty_directory(dir_fd) }
47 }
48