Skip to content

Commit 556b4a5

Browse files
alexcrichtonPat Hickey
and
Pat Hickey
authored
Fix file writes in preview 2 implementation (#7394) (#7425)
* 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 <[email protected]>
1 parent 67f8c7c commit 556b4a5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

crates/wasi/src/preview2/filesystem.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ enum OutputState {
189189
Ready,
190190
/// Allows join future to be awaited in a cancellable manner. Gone variant indicates
191191
/// no task is currently outstanding.
192-
Waiting(AbortOnDropJoinHandle<io::Result<()>>),
192+
Waiting(AbortOnDropJoinHandle<io::Result<usize>>),
193193
/// The last I/O operation failed with this error.
194194
Error(io::Error),
195195
Closed,
@@ -233,22 +233,26 @@ impl HostOutputStream for FileOutputStream {
233233
let m = self.mode;
234234
let task = spawn_blocking(move || match m {
235235
FileOutputMode::Position(mut p) => {
236+
let mut total = 0;
236237
let mut buf = buf;
237238
while !buf.is_empty() {
238239
let nwritten = f.write_at(buf.as_ref(), p)?;
239240
// afterwards buf contains [nwritten, len):
240241
let _ = buf.split_to(nwritten);
241242
p += nwritten as u64;
243+
total += nwritten;
242244
}
243-
Ok(())
245+
Ok(total)
244246
}
245247
FileOutputMode::Append => {
248+
let mut total = 0;
246249
let mut buf = buf;
247250
while !buf.is_empty() {
248251
let nwritten = f.append(buf.as_ref())?;
249252
let _ = buf.split_to(nwritten);
253+
total += nwritten;
250254
}
251-
Ok(())
255+
Ok(total)
252256
}
253257
});
254258
self.state = OutputState::Waiting(task);
@@ -285,7 +289,12 @@ impl Subscribe for FileOutputStream {
285289
async fn ready(&mut self) {
286290
if let OutputState::Waiting(task) = &mut self.state {
287291
self.state = match task.await {
288-
Ok(()) => OutputState::Ready,
292+
Ok(nwritten) => {
293+
if let FileOutputMode::Position(ref mut p) = &mut self.mode {
294+
*p += nwritten as u64;
295+
}
296+
OutputState::Ready
297+
}
289298
Err(e) => OutputState::Error(e),
290299
};
291300
}

0 commit comments

Comments
 (0)