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 6 unsafe fn test_dirfd_not_dir(dir_fd: wasip1::Fd) { 7 // Open a file. 8 let file_fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0) 9 .expect("opening a file"); 10 // Now try to open a file underneath it as if it were a directory. 11 assert_errno!( 12 wasip1::path_open(file_fd, 0, "foo", wasip1::OFLAGS_CREAT, 0, 0, 0) 13 .expect_err("non-directory base fd should get ERRNO_NOTDIR"), 14 wasip1::ERRNO_NOTDIR 15 ); 16 wasip1::fd_close(file_fd).expect("closing a file"); 17 } 18 19 fn main() { 20 let mut args = env::args(); 21 let prog = args.next().unwrap(); 22 let arg = if let Some(arg) = args.next() { 23 arg 24 } else { 25 eprintln!("usage: {prog} <scratch directory>"); 26 process::exit(1); 27 }; 28 29 // Open scratch directory 30 let dir_fd = match open_scratch_directory(&arg) { 31 Ok(dir_fd) => dir_fd, 32 Err(err) => { 33 eprintln!("{err}"); 34 process::exit(1) 35 } 36 }; 37 38 // Run the tests. 39 unsafe { test_dirfd_not_dir(dir_fd) } 40 } 41