readlink: get rid of weird partial-buffer semantics carried over from posix

but follow posix in returning ERANGE when the result is too big
This commit is contained in:
Pat Hickey
2021-01-04 16:41:24 -08:00
parent 84318024ef
commit 222a57868e
5 changed files with 14 additions and 51 deletions

View File

@@ -10,7 +10,7 @@ unsafe fn test_readlink(dir_fd: wasi::Fd) {
// Read link into the buffer
let buf = &mut [0u8; 10];
let mut bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len())
let bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len())
.expect("readlink should succeed");
assert_eq!(bufused, 6, "should use 6 bytes of the buffer");
assert_eq!(&buf[..6], b"target", "buffer should contain 'target'");
@@ -22,10 +22,14 @@ unsafe fn test_readlink(dir_fd: wasi::Fd) {
// Read link into smaller buffer than the actual link's length
let buf = &mut [0u8; 4];
bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len())
.expect("readlink should succeed");
assert_eq!(bufused, 4, "should use all 4 bytes of the buffer");
assert_eq!(buf, b"targ", "buffer should contain 'targ'");
let err = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len())
.err()
.expect("readlink with too-small buffer should fail");
assert_eq!(
err.raw_error(),
wasi::ERRNO_RANGE,
"readlink with too-small buffer should give ERANGE"
);
// Clean up.
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");

View File

@@ -1,41 +0,0 @@
use std::{env, process};
use wasi_tests::open_scratch_directory;
unsafe fn test_readlink_no_buffer(dir_fd: wasi::Fd) {
// First create a dangling symlink.
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
// Readlink it into a non-existent buffer.
let bufused = wasi::path_readlink(dir_fd, "symlink", (&mut []).as_mut_ptr(), 0)
.expect("readlink with a 0-sized buffer should succeed");
assert_eq!(
bufused, 0,
"readlink with a 0-sized buffer should return 'bufused' 0"
);
// Clean up.
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file");
}
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_readlink_no_buffer(dir_fd) }
}