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, create_file, open_scratch_directory};
5 
test_path_symlink_trailing_slashes(dir_fd: wasip1::Fd)6 unsafe fn test_path_symlink_trailing_slashes(dir_fd: wasip1::Fd) {
7     if config().support_dangling_filesystem() {
8         // Dangling symlink: Link destination shouldn't end with a slash.
9         assert_errno!(
10             wasip1::path_symlink("source", dir_fd, "target/")
11                 .expect_err("link destination ending with a slash should fail"),
12             wasip1::ERRNO_NOENT
13         );
14 
15         // Dangling symlink: Without the trailing slash, this should succeed.
16         wasip1::path_symlink("source", dir_fd, "target")
17             .expect("link destination ending with a slash");
18         wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
19     }
20 
21     // Link destination already exists, target has trailing slash.
22     wasip1::path_create_directory(dir_fd, "target").expect("creating a directory");
23     assert_errno!(
24         wasip1::path_symlink("source", dir_fd, "target/")
25             .expect_err("link destination already exists"),
26         unix => wasip1::ERRNO_EXIST,
27         windows => wasip1::ERRNO_NOENT
28     );
29     wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
30 
31     // Link destination already exists, target has no trailing slash.
32     wasip1::path_create_directory(dir_fd, "target").expect("creating a directory");
33     assert_errno!(
34         wasip1::path_symlink("source", dir_fd, "target")
35             .expect_err("link destination already exists"),
36         unix => wasip1::ERRNO_EXIST,
37         windows => wasip1::ERRNO_NOENT
38     );
39     wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
40 
41     // Link destination already exists, target has trailing slash.
42     create_file(dir_fd, "target");
43 
44     assert_errno!(
45         wasip1::path_symlink("source", dir_fd, "target/")
46             .expect_err("link destination already exists"),
47         unix => wasip1::ERRNO_NOTDIR,
48         windows => wasip1::ERRNO_NOENT
49     );
50     wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
51 
52     // Link destination already exists, target has no trailing slash.
53     create_file(dir_fd, "target");
54 
55     assert_errno!(
56         wasip1::path_symlink("source", dir_fd, "target")
57             .expect_err("link destination already exists"),
58         unix => wasip1::ERRNO_EXIST,
59         windows => wasip1::ERRNO_NOENT
60     );
61     wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
62 }
63 
main()64 fn main() {
65     let mut args = env::args();
66     let prog = args.next().unwrap();
67     let arg = if let Some(arg) = args.next() {
68         arg
69     } else {
70         eprintln!("usage: {prog} <scratch directory>");
71         process::exit(1);
72     };
73 
74     // Open scratch directory
75     let dir_fd = match open_scratch_directory(&arg) {
76         Ok(dir_fd) => dir_fd,
77         Err(err) => {
78             eprintln!("{err}");
79             process::exit(1)
80         }
81     };
82 
83     // Run the tests.
84     unsafe { test_path_symlink_trailing_slashes(dir_fd) }
85 }
86