1 use more_asserts::assert_gt;
2 use std::{env, process};
3 use wasi_tests::open_scratch_directory;
4 
test_directory_seek(dir_fd: wasi::Fd)5 unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
6     // Create a directory in the scratch directory.
7     wasi::path_create_directory(dir_fd, "dir").expect("failed to make directory");
8 
9     // Open the directory and attempt to request rights for seeking.
10     let fd = wasi::path_open(dir_fd, 0, "dir", 0, wasi::RIGHTS_FD_SEEK, 0, 0)
11         .expect("failed to open file");
12     assert_gt!(
13         fd,
14         libc::STDERR_FILENO as wasi::Fd,
15         "file descriptor range check",
16     );
17 
18     // Attempt to seek.
19     assert_eq!(
20         wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
21             .expect_err("seek on a directory")
22             .raw_error(),
23         wasi::ERRNO_NOTCAPABLE,
24         "errno should be ERRNO_NOTCAPABLE"
25     );
26 
27     // Check if we obtained the right to seek.
28     let fdstat = wasi::fd_fdstat_get(fd).expect("failed to fdstat");
29     assert_eq!(
30         fdstat.fs_filetype,
31         wasi::FILETYPE_DIRECTORY,
32         "expected the scratch directory to be a directory",
33     );
34     assert_eq!(
35         (fdstat.fs_rights_base & wasi::RIGHTS_FD_SEEK),
36         0,
37         "directory has the seek right",
38     );
39 
40     // Clean up.
41     wasi::fd_close(fd).expect("failed to close fd");
42     wasi::path_remove_directory(dir_fd, "dir").expect("failed to remove dir");
43 }
44 
main()45 fn main() {
46     let mut args = env::args();
47     let prog = args.next().unwrap();
48     let arg = if let Some(arg) = args.next() {
49         arg
50     } else {
51         eprintln!("usage: {} <scratch directory>", prog);
52         process::exit(1);
53     };
54 
55     // Open scratch directory
56     let dir_fd = match open_scratch_directory(&arg) {
57         Ok(dir_fd) => dir_fd,
58         Err(err) => {
59             eprintln!("{}", err);
60             process::exit(1)
61         }
62     };
63 
64     // Run the tests.
65     unsafe { test_directory_seek(dir_fd) }
66 }
67