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::open_scratch_directory;
5
test_file_allocate(dir_fd: wasip1::Fd)6 unsafe fn test_file_allocate(dir_fd: wasip1::Fd) {
7 // Create a file in the scratch directory.
8 let file_fd = wasip1::path_open(
9 dir_fd,
10 0,
11 "file",
12 wasip1::OFLAGS_CREAT,
13 wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
14 0,
15 0,
16 )
17 .expect("opening a file");
18 assert!(
19 file_fd > libc::STDERR_FILENO as wasip1::Fd,
20 "file descriptor range check",
21 );
22
23 // Check file size
24 let mut stat = wasip1::fd_filestat_get(file_fd).expect("reading file stats");
25 assert_eq!(stat.size, 0, "file size should be 0");
26
27 let err = wasip1::fd_allocate(file_fd, 0, 100)
28 .err()
29 .expect("fd_allocate must fail");
30 assert_eq!(
31 err,
32 wasip1::ERRNO_NOTSUP,
33 "fd_allocate should fail with NOTSUP"
34 );
35
36 stat = wasip1::fd_filestat_get(file_fd).expect("reading file stats");
37 assert_eq!(stat.size, 0, "file size should still be 0");
38
39 wasip1::fd_close(file_fd).expect("closing a file");
40 wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
41 }
42
main()43 fn main() {
44 let mut args = env::args();
45 let prog = args.next().unwrap();
46 let arg = if let Some(arg) = args.next() {
47 arg
48 } else {
49 eprintln!("usage: {prog} <scratch directory>");
50 process::exit(1);
51 };
52
53 // Open scratch directory
54 let dir_fd = match open_scratch_directory(&arg) {
55 Ok(dir_fd) => dir_fd,
56 Err(err) => {
57 eprintln!("{err}");
58 process::exit(1)
59 }
60 };
61
62 // Run the tests.
63 unsafe { test_file_allocate(dir_fd) }
64 }
65