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:
parent
d5f52ef7ac
commit
93b8c72d89
2 changed files with 11 additions and 8 deletions
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue