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, open_scratch_directory};
5
test_directory_seek(dir_fd: wasip1::Fd)6 unsafe fn test_directory_seek(dir_fd: wasip1::Fd) {
7 // Create a directory in the scratch directory.
8 wasip1::path_create_directory(dir_fd, "dir").expect("failed to make directory");
9
10 // Open the directory and attempt to request rights for seeking.
11 let fd = wasip1::path_open(dir_fd, 0, "dir", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
12 .expect("failed to open file");
13 assert!(
14 fd > libc::STDERR_FILENO as wasip1::Fd,
15 "file descriptor range check",
16 );
17
18 // Attempt to seek.
19 assert_errno!(
20 wasip1::fd_seek(fd, 0, wasip1::WHENCE_CUR).expect_err("seek on a directory"),
21 wasip1::ERRNO_BADF
22 );
23
24 // Clean up.
25 wasip1::fd_close(fd).expect("failed to close fd");
26 wasip1::path_remove_directory(dir_fd, "dir").expect("failed to remove dir");
27 }
28
main()29 fn main() {
30 let mut args = env::args();
31 let prog = args.next().unwrap();
32 let arg = if let Some(arg) = args.next() {
33 arg
34 } else {
35 eprintln!("usage: {prog} <scratch directory>");
36 process::exit(1);
37 };
38
39 // Open scratch directory
40 let dir_fd = match open_scratch_directory(&arg) {
41 Ok(dir_fd) => dir_fd,
42 Err(err) => {
43 eprintln!("{err}");
44 process::exit(1)
45 }
46 };
47
48 // Run the tests.
49 unsafe { test_directory_seek(dir_fd) }
50 }
51