Files
wasmtime/crates/test-programs/wasi-tests/src/bin/fd_advise.rs
Alex Crichton 1321c234e5 Remove dependency on more-asserts (#4408)
* Remove dependency on `more-asserts`

In my recent adventures to do a bit of gardening on our dependencies I
noticed that there's a new major version for the `more-asserts` crate.
Instead of updating to this though I've opted to instead remove the
dependency since I don't think we heavily lean on this crate and
otherwise one-off prints are probably sufficient to avoid the need for
pulling in a whole crate for this.

* Remove exemption for `more-asserts`
2022-07-26 16:47:33 +00:00

76 lines
2.3 KiB
Rust

use std::{env, process};
use wasi_tests::{open_scratch_directory, TESTCONFIG};
unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_ADVISE
| wasi::RIGHTS_FD_FILESTAT_GET
| wasi::RIGHTS_FD_FILESTAT_SET_SIZE
| wasi::RIGHTS_FD_ALLOCATE,
0,
0,
)
.expect("failed to open file");
assert!(
file_fd > libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Check file size
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat");
assert_eq!(stat.size, 0, "file size should be 0");
// set_size it bigger
wasi::fd_filestat_set_size(file_fd, 100).expect("setting size");
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 2");
assert_eq!(stat.size, 100, "file size should be 100");
// Advise the kernel
wasi::fd_advise(file_fd, 10, 50, wasi::ADVICE_NORMAL).expect("failed advise");
// Advise shouldnt change size
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 3");
assert_eq!(stat.size, 100, "file size should be 100");
if TESTCONFIG.support_fd_allocate() {
// Use fd_allocate to expand size to 200:
wasi::fd_allocate(file_fd, 100, 100).expect("allocating size");
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 3");
assert_eq!(stat.size, 200, "file size should be 200");
}
wasi::fd_close(file_fd).expect("failed to close");
wasi::path_unlink_file(dir_fd, "file").expect("failed to unlink");
}
fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
let arg = if let Some(arg) = args.next() {
arg
} else {
eprintln!("usage: {} <scratch directory>", prog);
process::exit(1);
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
process::exit(1)
}
};
// Run the tests.
unsafe { test_fd_advise(dir_fd) }
}