1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00

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<Self>`
(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](39ece1fe0d/ext/io/lib.rs (L485-L489)).
This commit is contained in:
Igor Zinkovsky 2023-04-28 12:16:17 -07:00 committed by Levente Kurusa
parent d5f52ef7ac
commit 93b8c72d89
No known key found for this signature in database
GPG key ID: 9F72F3C05BA137C4
2 changed files with 11 additions and 8 deletions

View file

@ -155,13 +155,13 @@ pub trait Resource: Any + 'static {
}
/// The same as [`read_byob()`][Resource::read_byob], but synchronous.
fn read_byob_sync(&self, data: &mut [u8]) -> Result<usize, Error> {
fn read_byob_sync(self: Rc<Self>, data: &mut [u8]) -> Result<usize, Error> {
_ = data;
Err(not_supported())
}
/// The same as [`write()`][Resource::write], but synchronous.
fn write_sync(&self, data: &[u8]) -> Result<usize, Error> {
fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, Error> {
_ = data;
Err(not_supported())
}

View file

@ -461,13 +461,13 @@ impl StdFileResource {
) -> Result<TResult, E>,
) -> Option<Result<TResult, E>> {
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<usize, AnyError> {
fn read_byob_sync(self: Rc<Self>, buf: &mut [u8]) -> Result<usize, AnyError> {
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<usize, AnyError> {
fn write_sync(self: Rc<Self>, data: &[u8]) -> Result<usize, AnyError> {
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<usize, deno_core::anyhow::Error> {
fn write_sync(
self: Rc<Self>,
data: &[u8],
) -> Result<usize, deno_core::anyhow::Error> {
StdFileResource::write_sync(self, data)
}
fn read_byob_sync(
&self,
self: Rc<Self>,
data: &mut [u8],
) -> Result<usize, deno_core::anyhow::Error> {
StdFileResource::read_byob_sync(self, data)