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, create_file, open_scratch_directory};
5 
test_unlink_file_trailing_slashes(dir_fd: wasip1::Fd)6 unsafe fn test_unlink_file_trailing_slashes(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     // Test that unlinking it fails.
11     assert_errno!(
12         wasip1::path_unlink_file(dir_fd, "dir")
13             .expect_err("unlink_file on a directory should fail"),
14         macos => wasip1::ERRNO_PERM,
15         unix => wasip1::ERRNO_ISDIR,
16         windows => wasip1::ERRNO_ACCES
17     );
18 
19     // Test that unlinking it with a trailing flash fails.
20     assert_errno!(
21         wasip1::path_unlink_file(dir_fd, "dir/")
22             .expect_err("unlink_file on a directory should fail"),
23         macos => wasip1::ERRNO_PERM,
24         unix => wasip1::ERRNO_ISDIR,
25         windows => wasip1::ERRNO_ACCES
26     );
27 
28     // Clean up.
29     wasip1::path_remove_directory(dir_fd, "dir").expect("removing a directory");
30 
31     // Create a temporary file.
32     create_file(dir_fd, "file");
33 
34     // Test that unlinking it with a trailing flash fails.
35     assert_errno!(
36         wasip1::path_unlink_file(dir_fd, "file/")
37             .expect_err("unlink_file with a trailing slash should fail"),
38         wasip1::ERRNO_NOTDIR
39     );
40 
41     // Test that unlinking it with no trailing flash succeeds.
42     wasip1::path_unlink_file(dir_fd, "file")
43         .expect("unlink_file with no trailing slash should succeed");
44 }
45 
main()46 fn main() {
47     let mut args = env::args();
48     let prog = args.next().unwrap();
49     let arg = if let Some(arg) = args.next() {
50         arg
51     } else {
52         eprintln!("usage: {prog} <scratch directory>");
53         process::exit(1);
54     };
55 
56     // Open scratch directory
57     let dir_fd = match open_scratch_directory(&arg) {
58         Ok(dir_fd) => dir_fd,
59         Err(err) => {
60             eprintln!("{err}");
61             process::exit(1)
62         }
63     };
64 
65     // Run the tests.
66     unsafe { test_unlink_file_trailing_slashes(dir_fd) }
67 }
68