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, config, open_scratch_directory};
5 
test_dangling_symlink(dir_fd: wasip1::Fd)6 unsafe fn test_dangling_symlink(dir_fd: wasip1::Fd) {
7     if config().support_dangling_filesystem() {
8         // First create a dangling symlink.
9         wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
10 
11         // Try to open it as a directory with O_NOFOLLOW.
12         assert_errno!(
13             wasip1::path_open(dir_fd, 0, "symlink", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
14                 .expect_err("opening a dangling symlink as a directory"),
15             wasip1::ERRNO_NOTDIR,
16             wasip1::ERRNO_LOOP,
17             wasip1::ERRNO_NOENT
18         );
19 
20         // Try to open it as a file with O_NOFOLLOW.
21         assert_errno!(
22             wasip1::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
23                 .expect_err("opening a dangling symlink as a file"),
24             wasip1::ERRNO_LOOP,
25             wasip1::ERRNO_NOENT
26         );
27 
28         // Clean up.
29         wasip1::path_unlink_file(dir_fd, "symlink").expect("failed to remove file");
30     }
31 }
32 
main()33 fn main() {
34     let mut args = env::args();
35     let prog = args.next().unwrap();
36     let arg = if let Some(arg) = args.next() {
37         arg
38     } else {
39         eprintln!("usage: {prog} <scratch directory>");
40         process::exit(1);
41     };
42 
43     // Open scratch directory
44     let dir_fd = match open_scratch_directory(&arg) {
45         Ok(dir_fd) => dir_fd,
46         Err(err) => {
47             eprintln!("{err}");
48             process::exit(1)
49         }
50     };
51 
52     // Run the tests.
53     unsafe { test_dangling_symlink(dir_fd) }
54 }
55