Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propagate timestamps to virtual file handles #4618

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/virtual-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ pub trait VirtualFile:
/// the time at which the file was created in nanoseconds as a UNIX timestamp
fn created_time(&self) -> u64;

/// sets accessed time
fn set_accessed(&mut self, _atime: u64) {}

/// sets modification time
fn set_modified(&mut self, _mtime: u64) {}

/// the size of the file in bytes
fn size(&self) -> u64;

Expand Down
24 changes: 24 additions & 0 deletions lib/virtual-fs/src/mem_fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ impl VirtualFile for FileHandle {
node.metadata().created
}

fn set_accessed(&mut self, atime: u64) {
let mut fs = match self.filesystem.inner.write() {
Ok(fs) => fs,
_ => return,
};

let inode = fs.storage.get_mut(self.inode);
if let Some(node) = inode {
node.metadata_mut().accessed = atime;
}
}

fn set_modified(&mut self, mtime: u64) {
let mut fs = match self.filesystem.inner.write() {
Ok(fs) => fs,
_ => return,
};

let inode = fs.storage.get_mut(self.inode);
if let Some(node) = inode {
node.metadata_mut().modified = mtime;
}
}

maminrayej marked this conversation as resolved.
Show resolved Hide resolved
fn size(&self) -> u64 {
let fs = match self.filesystem.inner.read() {
Ok(fs) => fs,
Expand Down
23 changes: 23 additions & 0 deletions lib/wasix/src/syscalls/wasi/fd_filestat_set_times.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::BorrowMut;

use super::*;
use crate::syscalls::*;

Expand Down Expand Up @@ -59,13 +61,17 @@ pub(crate) fn fd_filestat_set_times_internal(

let inode = fd_entry.inode;

let mut atime = None;
let mut mtime = None;

if fst_flags.contains(Fstflags::SET_ATIM) || fst_flags.contains(Fstflags::SET_ATIM_NOW) {
let time_to_set = if fst_flags.contains(Fstflags::SET_ATIM) {
st_atim
} else {
get_current_time_in_nanos()?
};
inode.stat.write().unwrap().st_atim = time_to_set;
atime = Some(time_to_set);
}

if fst_flags.contains(Fstflags::SET_MTIM) || fst_flags.contains(Fstflags::SET_MTIM_NOW) {
Expand All @@ -75,6 +81,23 @@ pub(crate) fn fd_filestat_set_times_internal(
get_current_time_in_nanos()?
};
inode.stat.write().unwrap().st_mtim = time_to_set;
mtime = Some(time_to_set);
}

if let Kind::File {
handle: Some(handle),
..
} = inode.kind.write().unwrap().deref()
{
let mut handle = handle.write().unwrap();

if let Some(time) = atime {
handle.set_accessed(time);
}

if let Some(time) = mtime {
handle.set_modified(time);
}
}

Ok(())
Expand Down
Loading