diff options
author | haturau <135221985+haturatu@users.noreply.github.com> | 2024-11-20 01:20:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-20 01:20:47 +0900 |
commit | 85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch) | |
tree | face0aecaac53e93ce2f23b53c48859bcf1a36ec /ext/io/fs.rs | |
parent | 67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff) | |
parent | 186b52731c6bb326c4d32905c5e732d082e83465 (diff) |
Merge branch 'denoland:main' into main
Diffstat (limited to 'ext/io/fs.rs')
-rw-r--r-- | ext/io/fs.rs | 71 |
1 files changed, 38 insertions, 33 deletions
diff --git a/ext/io/fs.rs b/ext/io/fs.rs index 3798c1429..7ef02315b 100644 --- a/ext/io/fs.rs +++ b/ext/io/fs.rs @@ -1,15 +1,12 @@ // 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; 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; @@ -25,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 { @@ -59,19 +71,6 @@ impl From<io::ErrorKind> for FsError { } } -impl From<FsError> for 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}")) - } - } - } -} - impl From<JoinError> for FsError { fn from(err: JoinError) -> Self { if err.is_cancelled() { @@ -95,6 +94,7 @@ pub struct FsStat { pub mtime: Option<u64>, pub atime: Option<u64>, pub birthtime: Option<u64>, + pub ctime: Option<u64>, pub dev: u64, pub ino: u64, @@ -154,6 +154,16 @@ impl FsStat { } } + #[inline(always)] + fn get_ctime(ctime_or_0: i64) -> Option<u64> { + 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(), @@ -163,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), @@ -266,9 +277,9 @@ impl FileResource { state: &OpState, rid: ResourceId, f: F, - ) -> Result<R, AnyError> + ) -> Result<R, deno_core::error::AnyError> where - F: FnOnce(Rc<FileResource>) -> Result<R, AnyError>, + F: FnOnce(Rc<FileResource>) -> Result<R, deno_core::error::AnyError>, { let resource = state.resource_table.get::<FileResource>(rid)?; f(resource) @@ -277,7 +288,7 @@ impl FileResource { pub fn get_file( state: &OpState, rid: ResourceId, - ) -> Result<Rc<dyn File>, AnyError> { + ) -> Result<Rc<dyn File>, deno_core::error::AnyError> { let resource = state.resource_table.get::<FileResource>(rid)?; Ok(resource.file()) } @@ -286,9 +297,9 @@ impl FileResource { state: &OpState, rid: ResourceId, f: F, - ) -> Result<R, AnyError> + ) -> Result<R, deno_core::error::AnyError> where - F: FnOnce(Rc<dyn File>) -> Result<R, AnyError>, + F: FnOnce(Rc<dyn File>) -> Result<R, deno_core::error::AnyError>, { Self::with_resource(state, rid, |r| f(r.file.clone())) } @@ -303,10 +314,7 @@ impl deno_core::Resource for FileResource { Cow::Borrowed(&self.name) } - fn read( - self: Rc<Self>, - limit: usize, - ) -> deno_core::AsyncResult<deno_core::BufView> { + fn read(self: Rc<Self>, limit: usize) -> deno_core::AsyncResult<BufView> { Box::pin(async move { self .file @@ -319,8 +327,8 @@ impl deno_core::Resource for FileResource { fn read_byob( self: Rc<Self>, - 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 +341,14 @@ impl deno_core::Resource for FileResource { fn write( self: Rc<Self>, - buf: deno_core::BufView, + buf: BufView, ) -> deno_core::AsyncResult<deno_core::WriteOutcome> { Box::pin(async move { self.file.clone().write(buf).await.map_err(|err| err.into()) }) } - fn write_all( - self: Rc<Self>, - buf: deno_core::BufView, - ) -> deno_core::AsyncResult<()> { + fn write_all(self: Rc<Self>, buf: BufView) -> deno_core::AsyncResult<()> { Box::pin(async move { self .file |