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::{create_file, open_scratch_directory};
5 
test_path_exists(dir_fd: wasip1::Fd)6 unsafe fn test_path_exists(dir_fd: wasip1::Fd) {
7     // Create a temporary directory
8     wasip1::path_create_directory(dir_fd, "subdir").expect("create directory");
9 
10     // Check directory exists:
11     let file_stat = wasip1::path_filestat_get(dir_fd, 0, "subdir").expect("reading file stats");
12     assert_eq!(file_stat.filetype, wasip1::FILETYPE_DIRECTORY);
13 
14     // Should still exist with symlink follow flag:
15     let file_stat = wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "subdir")
16         .expect("reading file stats");
17     assert_eq!(file_stat.filetype, wasip1::FILETYPE_DIRECTORY);
18 
19     // Create a file:
20     create_file(dir_fd, "subdir/file");
21     // Check directory exists:
22     let file_stat =
23         wasip1::path_filestat_get(dir_fd, 0, "subdir/file").expect("reading file stats");
24     assert_eq!(file_stat.filetype, wasip1::FILETYPE_REGULAR_FILE);
25 
26     // Should still exist with symlink follow flag:
27     let file_stat =
28         wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "subdir/file")
29             .expect("reading file stats");
30     assert_eq!(file_stat.filetype, wasip1::FILETYPE_REGULAR_FILE);
31 
32     // Create a symlink to a file:
33     wasip1::path_symlink("subdir/file", dir_fd, "link1").expect("create symlink");
34     // Check symlink exists:
35     let file_stat = wasip1::path_filestat_get(dir_fd, 0, "link1").expect("reading file stats");
36     assert_eq!(file_stat.filetype, wasip1::FILETYPE_SYMBOLIC_LINK);
37 
38     // Should still exist with symlink follow flag, pointing to regular file
39     let file_stat = wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "link1")
40         .expect("reading file stats");
41     assert_eq!(file_stat.filetype, wasip1::FILETYPE_REGULAR_FILE);
42 
43     // Create a symlink to a dir:
44     wasip1::path_symlink("subdir", dir_fd, "link2").expect("create symlink");
45     // Check symlink exists:
46     let file_stat = wasip1::path_filestat_get(dir_fd, 0, "link2").expect("reading file stats");
47     assert_eq!(file_stat.filetype, wasip1::FILETYPE_SYMBOLIC_LINK);
48 
49     // Should still exist with symlink follow flag, pointing to directory
50     let file_stat = wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "link2")
51         .expect("reading file stats");
52     assert_eq!(file_stat.filetype, wasip1::FILETYPE_DIRECTORY);
53 
54     wasip1::path_unlink_file(dir_fd, "link1").expect("clean up");
55     wasip1::path_unlink_file(dir_fd, "link2").expect("clean up");
56     wasip1::path_unlink_file(dir_fd, "subdir/file").expect("clean up");
57     wasip1::path_remove_directory(dir_fd, "subdir").expect("clean up");
58 }
59 
main()60 fn main() {
61     let mut args = env::args();
62     let prog = args.next().unwrap();
63     let arg = if let Some(arg) = args.next() {
64         arg
65     } else {
66         eprintln!("usage: {prog} <scratch directory>");
67         process::exit(1);
68     };
69 
70     // Open scratch directory
71     let dir_fd = match open_scratch_directory(&arg) {
72         Ok(dir_fd) => dir_fd,
73         Err(err) => {
74             eprintln!("{err}");
75             process::exit(1)
76         }
77     };
78     // Run the tests.
79     unsafe { test_path_exists(dir_fd) }
80 }
81