From 82d13fd45b6fa8da5d390e26a349522e93811639 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Tue, 15 Oct 2024 15:36:11 -0700 Subject: refactor(ext/io): use concrete error types (#26187) --- ext/io/fs.rs | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) (limited to 'ext/io/fs.rs') diff --git a/ext/io/fs.rs b/ext/io/fs.rs index 3798c1429..06fc3da09 100644 --- a/ext/io/fs.rs +++ b/ext/io/fs.rs @@ -6,10 +6,6 @@ use std::rc::Rc; use std::time::SystemTime; use std::time::UNIX_EPOCH; -use deno_core::error::custom_error; -use deno_core::error::not_supported; -use deno_core::error::resource_unavailable; -use deno_core::error::AnyError; use deno_core::BufMutView; use deno_core::BufView; use deno_core::OpState; @@ -59,15 +55,16 @@ impl From for FsError { } } -impl From for AnyError { +impl From for deno_core::error::AnyError { fn from(err: FsError) -> Self { match err { - FsError::Io(err) => AnyError::from(err), - FsError::FileBusy => resource_unavailable(), - FsError::NotSupported => not_supported(), - FsError::NotCapable(err) => { - custom_error("NotCapable", format!("permission denied: {err}")) - } + FsError::Io(err) => err.into(), + FsError::FileBusy => deno_core::error::resource_unavailable(), + FsError::NotSupported => deno_core::error::not_supported(), + FsError::NotCapable(err) => deno_core::error::custom_error( + "NotCapable", + format!("permission denied: {err}"), + ), } } } @@ -266,9 +263,9 @@ impl FileResource { state: &OpState, rid: ResourceId, f: F, - ) -> Result + ) -> Result where - F: FnOnce(Rc) -> Result, + F: FnOnce(Rc) -> Result, { let resource = state.resource_table.get::(rid)?; f(resource) @@ -277,7 +274,7 @@ impl FileResource { pub fn get_file( state: &OpState, rid: ResourceId, - ) -> Result, AnyError> { + ) -> Result, deno_core::error::AnyError> { let resource = state.resource_table.get::(rid)?; Ok(resource.file()) } @@ -286,9 +283,9 @@ impl FileResource { state: &OpState, rid: ResourceId, f: F, - ) -> Result + ) -> Result where - F: FnOnce(Rc) -> Result, + F: FnOnce(Rc) -> Result, { Self::with_resource(state, rid, |r| f(r.file.clone())) } @@ -303,10 +300,7 @@ impl deno_core::Resource for FileResource { Cow::Borrowed(&self.name) } - fn read( - self: Rc, - limit: usize, - ) -> deno_core::AsyncResult { + fn read(self: Rc, limit: usize) -> deno_core::AsyncResult { Box::pin(async move { self .file @@ -319,8 +313,8 @@ impl deno_core::Resource for FileResource { fn read_byob( self: Rc, - buf: deno_core::BufMutView, - ) -> deno_core::AsyncResult<(usize, deno_core::BufMutView)> { + buf: BufMutView, + ) -> deno_core::AsyncResult<(usize, BufMutView)> { Box::pin(async move { self .file @@ -333,17 +327,14 @@ impl deno_core::Resource for FileResource { fn write( self: Rc, - buf: deno_core::BufView, + buf: BufView, ) -> deno_core::AsyncResult { Box::pin(async move { self.file.clone().write(buf).await.map_err(|err| err.into()) }) } - fn write_all( - self: Rc, - buf: deno_core::BufView, - ) -> deno_core::AsyncResult<()> { + fn write_all(self: Rc, buf: BufView) -> deno_core::AsyncResult<()> { Box::pin(async move { self .file -- cgit v1.2.3 From c71e020668b40666aecfdffb1dbf979abcb41958 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Thu, 24 Oct 2024 10:45:17 -0700 Subject: refactor(ext/node): use concrete error types (#26419) --- ext/io/fs.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'ext/io/fs.rs') diff --git a/ext/io/fs.rs b/ext/io/fs.rs index 06fc3da09..885426520 100644 --- a/ext/io/fs.rs +++ b/ext/io/fs.rs @@ -1,6 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. use std::borrow::Cow; +use std::fmt::Formatter; use std::io; use std::rc::Rc; use std::time::SystemTime; @@ -21,6 +22,21 @@ pub enum FsError { NotCapable(&'static str), } +impl std::fmt::Display for FsError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + FsError::Io(err) => std::fmt::Display::fmt(err, f), + FsError::FileBusy => f.write_str("file busy"), + FsError::NotSupported => f.write_str("not supported"), + FsError::NotCapable(err) => { + f.write_str(&format!("requires {err} access")) + } + } + } +} + +impl std::error::Error for FsError {} + impl FsError { pub fn kind(&self) -> io::ErrorKind { match self { @@ -55,20 +71,6 @@ impl From for FsError { } } -impl From for deno_core::error::AnyError { - fn from(err: FsError) -> Self { - match err { - FsError::Io(err) => err.into(), - FsError::FileBusy => deno_core::error::resource_unavailable(), - FsError::NotSupported => deno_core::error::not_supported(), - FsError::NotCapable(err) => deno_core::error::custom_error( - "NotCapable", - format!("permission denied: {err}"), - ), - } - } -} - impl From for FsError { fn from(err: JoinError) -> Self { if err.is_cancelled() { -- cgit v1.2.3 From 7becd83a3828b35331d0fcb82c64146e520f154b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Czerniawski?= <33061335+lczerniawski@users.noreply.github.com> Date: Wed, 13 Nov 2024 05:35:04 +0100 Subject: feat(ext/fs): add ctime to Deno.stats and use it in node compat layer (#24801) This PR fixes #24453, by introducing a ctime (using ctime for UNIX and ChangeTime for Windows) to Deno.stats. Co-authored-by: Yoshiya Hinosawa --- ext/io/fs.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'ext/io/fs.rs') diff --git a/ext/io/fs.rs b/ext/io/fs.rs index 885426520..7ef02315b 100644 --- a/ext/io/fs.rs +++ b/ext/io/fs.rs @@ -94,6 +94,7 @@ pub struct FsStat { pub mtime: Option, pub atime: Option, pub birthtime: Option, + pub ctime: Option, pub dev: u64, pub ino: u64, @@ -153,6 +154,16 @@ impl FsStat { } } + #[inline(always)] + fn get_ctime(ctime_or_0: i64) -> Option { + if ctime_or_0 > 0 { + // ctime return seconds since epoch, but we need milliseconds + return Some(ctime_or_0 as u64 * 1000); + } + + None + } + Self { is_file: metadata.is_file(), is_directory: metadata.is_dir(), @@ -162,6 +173,7 @@ impl FsStat { mtime: to_msec(metadata.modified()), atime: to_msec(metadata.accessed()), birthtime: to_msec(metadata.created()), + ctime: get_ctime(unix_or_zero!(ctime)), dev: unix_or_zero!(dev), ino: unix_or_zero!(ino), -- cgit v1.2.3