Skip to content

Commit 177fee2

Browse files
add mtime and unsupported error (#4)
1 parent 95c3163 commit 177fee2

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

async-vfs-os/src/os.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::fs_shims::{fs, read_dir, Path, PathBuf};
22
use async_vfs::{async_trait, OpenOptions, VFile, VMetadata, Vfs, VfsError, VfsResult};
33
use futures_lite::StreamExt;
4-
use std::pin::Pin;
4+
use std::{pin::Pin, time::SystemTime};
55

66
pub struct OsFs {
77
root: PathBuf,
@@ -11,6 +11,7 @@ struct VOsMetadata {
1111
path: String,
1212
is_file: bool,
1313
len: u64,
14+
mtime: u64,
1415
}
1516

1617
impl OsFs {
@@ -96,6 +97,10 @@ impl VMetadata for VOsMetadata {
9697
fn len(&self) -> u64 {
9798
self.len
9899
}
100+
101+
fn mtime(&self) -> u64 {
102+
self.mtime
103+
}
99104
}
100105

101106
#[async_trait]
@@ -112,18 +117,24 @@ impl Vfs for OsFs {
112117
let entry = entry?;
113118
let metadata = entry.metadata().await?;
114119
let path = entry.path();
120+
let mtime = match metadata.modified() {
121+
Ok(time) => to_timestamp(&time),
122+
_ => 0,
123+
};
115124

116125
let vmetadata = if metadata.is_dir() {
117126
VOsMetadata {
118127
is_file: false,
119128
len: 0,
120129
path: self.get_vfs_path(&path)?,
130+
mtime,
121131
}
122132
} else {
123133
VOsMetadata {
124134
is_file: true,
125135
len: metadata.len(),
126136
path: self.get_vfs_path(&path)?,
137+
mtime,
127138
}
128139
};
129140

@@ -141,18 +152,24 @@ impl Vfs for OsFs {
141152
let path = self.get_real_path(path)?;
142153

143154
let metadata = fs::metadata(&path).await?;
155+
let mtime = match metadata.modified() {
156+
Ok(time) => to_timestamp(&time),
157+
_ => 0,
158+
};
144159

145160
let vmetadata = if metadata.is_dir() {
146161
VOsMetadata {
147162
is_file: false,
148163
len: 0,
149164
path: self.get_vfs_path(&path)?,
165+
mtime,
150166
}
151167
} else {
152168
VOsMetadata {
153169
is_file: true,
154170
len: metadata.len(),
155171
path: self.get_vfs_path(&path)?,
172+
mtime,
156173
}
157174
};
158175
Ok(Box::new(vmetadata))
@@ -200,3 +217,9 @@ impl Vfs for OsFs {
200217
}
201218
}
202219
}
220+
221+
fn to_timestamp(time: &SystemTime) -> u64 {
222+
time.duration_since(SystemTime::UNIX_EPOCH)
223+
.unwrap()
224+
.as_millis() as u64
225+
}

async-vfs/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub enum VfsError {
1212
#[error("Invalid file: {path}")]
1313
InvalidFile { path: String },
1414

15+
#[error("Not supported")]
16+
NotSupported,
17+
1518
/// Generic error context, used for adding context to an error (like a path)
1619
#[error("{context}, cause: {cause}")]
1720
WithContext {

async-vfs/src/vfs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub trait VMetadata: Sync + Send {
77
fn is_dir(&self) -> bool;
88
fn is_file(&self) -> bool;
99
fn len(&self) -> u64;
10+
fn mtime(&self) -> u64;
1011
}
1112

1213
pub trait VFile: AsyncRead + AsyncWrite + AsyncSeek {}

0 commit comments

Comments
 (0)