From 10ae5ee26557107b22524b1a84ebb56ed7d23fb4 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Fri, 28 Apr 2023 12:16:17 -0700 Subject: fix(ext/io) several sync fs fixes (#18886) 2 fixes related to sync fs: * update the 2 sync methods on `Resource` trait to take `Rc` (consistent with other methods) * fix a bug in `StdFileResource::with_inner_and_metadata`, which currently can trigger a panic if a sync method is called on a file with a pending async operation. This could happen in the code path where `File::try_clone` [fails](https://github.com/denoland/deno/blob/39ece1fe0ddacc2cbf182403c9e7085bc01df5a6/ext/io/lib.rs#L485-L489). --- ext/io/lib.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'ext/io') diff --git a/ext/io/lib.rs b/ext/io/lib.rs index c85b4baf6..23c087e16 100644 --- a/ext/io/lib.rs +++ b/ext/io/lib.rs @@ -461,13 +461,13 @@ impl StdFileResource { ) -> Result, ) -> Option> { match self.cell.try_borrow_mut() { - Ok(mut cell) => { + Ok(mut cell) if cell.is_some() => { let mut file = cell.take().unwrap(); let result = action(&mut file.inner, &file.meta_data); cell.replace(file); Some(result) } - Err(_) => None, + _ => None, } } @@ -537,14 +537,14 @@ impl StdFileResource { .await } - fn read_byob_sync(&self, buf: &mut [u8]) -> Result { + fn read_byob_sync(self: Rc, buf: &mut [u8]) -> Result { self .with_inner_and_metadata(|inner, _| inner.read(buf)) .ok_or_else(resource_unavailable)? .map_err(Into::into) } - fn write_sync(&self, data: &[u8]) -> Result { + fn write_sync(self: Rc, data: &[u8]) -> Result { self .with_inner_and_metadata(|inner, _| inner.write_and_maybe_flush(data)) .ok_or_else(resource_unavailable)? @@ -694,12 +694,15 @@ impl Resource for StdFileResource { Box::pin(StdFileResource::write_all(self, view)) } - fn write_sync(&self, data: &[u8]) -> Result { + fn write_sync( + self: Rc, + data: &[u8], + ) -> Result { StdFileResource::write_sync(self, data) } fn read_byob_sync( - &self, + self: Rc, data: &mut [u8], ) -> Result { StdFileResource::read_byob_sync(self, data) -- cgit v1.2.3