Skip to content

Commit

Permalink
Fix file writes in preview 2 implementation (#7394) (#7425)
Browse files Browse the repository at this point in the history
* add a preview1_file_write test to show behavior of large file writes

reproduces #7390

* filesystem output-stream: update position cursor

Co-authored-by: Pat Hickey <phickey@fastly.com>
  • Loading branch information
alexcrichton and Pat Hickey authored Oct 31, 2023
1 parent 67f8c7c commit 556b4a5
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions crates/wasi/src/preview2/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ enum OutputState {
Ready,
/// Allows join future to be awaited in a cancellable manner. Gone variant indicates
/// no task is currently outstanding.
Waiting(AbortOnDropJoinHandle<io::Result<()>>),
Waiting(AbortOnDropJoinHandle<io::Result<usize>>),
/// The last I/O operation failed with this error.
Error(io::Error),
Closed,
Expand Down Expand Up @@ -233,22 +233,26 @@ impl HostOutputStream for FileOutputStream {
let m = self.mode;
let task = spawn_blocking(move || match m {
FileOutputMode::Position(mut p) => {
let mut total = 0;
let mut buf = buf;
while !buf.is_empty() {
let nwritten = f.write_at(buf.as_ref(), p)?;
// afterwards buf contains [nwritten, len):
let _ = buf.split_to(nwritten);
p += nwritten as u64;
total += nwritten;
}
Ok(())
Ok(total)
}
FileOutputMode::Append => {
let mut total = 0;
let mut buf = buf;
while !buf.is_empty() {
let nwritten = f.append(buf.as_ref())?;
let _ = buf.split_to(nwritten);
total += nwritten;
}
Ok(())
Ok(total)
}
});
self.state = OutputState::Waiting(task);
Expand Down Expand Up @@ -285,7 +289,12 @@ impl Subscribe for FileOutputStream {
async fn ready(&mut self) {
if let OutputState::Waiting(task) = &mut self.state {
self.state = match task.await {
Ok(()) => OutputState::Ready,
Ok(nwritten) => {
if let FileOutputMode::Position(ref mut p) = &mut self.mode {
*p += nwritten as u64;
}
OutputState::Ready
}
Err(e) => OutputState::Error(e),
};
}
Expand Down

0 comments on commit 556b4a5

Please sign in to comment.