Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discard 0-sized writes to files #7638

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion crates/test-programs/src/bin/preview1_file_pread_pwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,55 @@ unsafe fn test_file_pread_pwrite(dir_fd: wasi::Fd) {
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}

unsafe fn test_file_pwrite_and_file_pos(dir_fd: wasi::Fd) {
let path = "file2";
let file_fd = wasi::path_open(
dir_fd,
0,
path,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
)
.expect("opening a file");
assert!(
file_fd > libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);

// Perform a 0-sized pwrite at an offset beyond the end of the file. Unix
// semantics should pop out where nothing is actually written and the size
// of the file isn't modified.
assert_eq!(wasi::fd_tell(file_fd).unwrap(), 0);
let ciovec = wasi::Ciovec {
buf: [].as_ptr(),
buf_len: 0,
};
let n = wasi::fd_pwrite(file_fd, &mut [ciovec], 50).expect("writing bytes at offset 2");
assert_eq!(n, 0);

assert_eq!(wasi::fd_tell(file_fd).unwrap(), 0);
let stat = wasi::fd_filestat_get(file_fd).unwrap();
assert_eq!(stat.size, 0);

// Now write a single byte and make sure it actually works
let buf = [0];
let ciovec = wasi::Ciovec {
buf: buf.as_ptr(),
buf_len: buf.len(),
};
let n = wasi::fd_pwrite(file_fd, &mut [ciovec], 50).expect("writing bytes at offset 2");
assert_eq!(n, 1);

assert_eq!(wasi::fd_tell(file_fd).unwrap(), 0);
let stat = wasi::fd_filestat_get(file_fd).unwrap();
assert_eq!(stat.size, 51);

wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, path).expect("removing a file");
}

fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
Expand All @@ -148,5 +197,8 @@ fn main() {
};

// Run the tests.
unsafe { test_file_pread_pwrite(dir_fd) }
unsafe {
test_file_pread_pwrite(dir_fd);
test_file_pwrite_and_file_pos(dir_fd);
}
}
3 changes: 3 additions & 0 deletions crates/wasi-common/src/sync/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ impl WasiFile for File {
bufs: &[io::IoSlice<'a>],
offset: u64,
) -> Result<u64, Error> {
if bufs.iter().map(|i| i.len()).sum::<usize>() == 0 {
return Ok(0);
}
let n = self.0.write_vectored_at(bufs, offset)?;
Ok(n.try_into()?)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/wasi-common/src/tokio/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ macro_rules! wasi_file_impl {
bufs: &[io::IoSlice<'a>],
offset: u64,
) -> Result<u64, Error> {
if bufs.iter().map(|i| i.len()).sum::<usize>() == 0 {
return Ok(0);
}
block_on_dummy_executor(move || self.0.write_vectored_at(bufs, offset))
}
async fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
Expand Down
4 changes: 4 additions & 0 deletions crates/wasi/src/preview2/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ impl HostOutputStream for FileOutputStream {
}
}

if buf.is_empty() {
return Ok(());
}

let f = Arc::clone(&self.file);
let m = self.mode;
let task = spawn_blocking(move || match m {
Expand Down