Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Change the filesystem timestamp to seconds+nanoseconds.
Browse files Browse the repository at this point in the history
This corresponds to WebAssembly/wasi-filesystem#76.
  • Loading branch information
sunfishcode committed Jan 9, 2023
1 parent d56b897 commit 1df7a3b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
17 changes: 10 additions & 7 deletions host/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,18 @@ impl From<wasi_common::file::FileType> for wasi_filesystem::DescriptorType {

impl From<wasi_common::file::Filestat> for wasi_filesystem::DescriptorStat {
fn from(stat: wasi_common::file::Filestat) -> Self {
fn timestamp(time: Option<std::time::SystemTime>) -> wasi_filesystem::Timestamp {
fn timestamp(time: Option<std::time::SystemTime>) -> wasi_filesystem::Datetime {
time.map(|t| {
t.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos()
.try_into()
.unwrap()
let since = t.duration_since(SystemTime::UNIX_EPOCH).unwrap();
wasi_filesystem::Datetime {
seconds: since.as_secs(),
nanoseconds: since.subsec_nanos(),
}
})
.unwrap_or(wasi_filesystem::Datetime {
seconds: 0,
nanoseconds: 0,
})
.unwrap_or(0)
}

Self {
Expand Down
36 changes: 26 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,9 @@ pub unsafe extern "C" fn fd_filestat_get(fd: Fd, buf: *mut Filestat) -> Errno {
filetype,
nlink: stat.nlink,
size: stat.size,
atim: stat.atim,
mtim: stat.mtim,
ctim: stat.ctim,
atim: datetime_to_timestamp(stat.atim),
mtim: datetime_to_timestamp(stat.mtim),
ctim: datetime_to_timestamp(stat.ctim),
};
Ok(())
})
Expand Down Expand Up @@ -592,15 +592,21 @@ pub unsafe extern "C" fn fd_filestat_set_times(
if fst_flags & (FSTFLAGS_ATIM | FSTFLAGS_ATIM_NOW) == (FSTFLAGS_ATIM | FSTFLAGS_ATIM_NOW) {
wasi_filesystem::NewTimestamp::Now
} else if fst_flags & FSTFLAGS_ATIM == FSTFLAGS_ATIM {
wasi_filesystem::NewTimestamp::Timestamp(atim)
wasi_filesystem::NewTimestamp::Timestamp(wasi_filesystem::Datetime {
seconds: atim / 1_000_000_000,
nanoseconds: (atim % 1_000_000_000) as _,
})
} else {
wasi_filesystem::NewTimestamp::NoChange
};
let mtim =
if fst_flags & (FSTFLAGS_MTIM | FSTFLAGS_MTIM_NOW) == (FSTFLAGS_MTIM | FSTFLAGS_MTIM_NOW) {
wasi_filesystem::NewTimestamp::Now
} else if fst_flags & FSTFLAGS_MTIM == FSTFLAGS_MTIM {
wasi_filesystem::NewTimestamp::Timestamp(mtim)
wasi_filesystem::NewTimestamp::Timestamp(wasi_filesystem::Datetime {
seconds: mtim / 1_000_000_000,
nanoseconds: (mtim % 1_000_000_000) as _,
})
} else {
wasi_filesystem::NewTimestamp::NoChange
};
Expand Down Expand Up @@ -1154,9 +1160,9 @@ pub unsafe extern "C" fn path_filestat_get(
filetype,
nlink: stat.nlink,
size: stat.size,
atim: stat.atim,
mtim: stat.mtim,
ctim: stat.ctim,
atim: datetime_to_timestamp(stat.atim),
mtim: datetime_to_timestamp(stat.mtim),
ctim: datetime_to_timestamp(stat.ctim),
};
Ok(())
})
Expand All @@ -1178,15 +1184,21 @@ pub unsafe extern "C" fn path_filestat_set_times(
if fst_flags & (FSTFLAGS_ATIM | FSTFLAGS_ATIM_NOW) == (FSTFLAGS_ATIM | FSTFLAGS_ATIM_NOW) {
wasi_filesystem::NewTimestamp::Now
} else if fst_flags & FSTFLAGS_ATIM == FSTFLAGS_ATIM {
wasi_filesystem::NewTimestamp::Timestamp(atim)
wasi_filesystem::NewTimestamp::Timestamp(wasi_filesystem::Datetime {
seconds: atim / 1_000_000_000,
nanoseconds: (atim % 1_000_000_000) as _,
})
} else {
wasi_filesystem::NewTimestamp::NoChange
};
let mtim =
if fst_flags & (FSTFLAGS_MTIM | FSTFLAGS_MTIM_NOW) == (FSTFLAGS_MTIM | FSTFLAGS_MTIM_NOW) {
wasi_filesystem::NewTimestamp::Now
} else if fst_flags & FSTFLAGS_MTIM == FSTFLAGS_MTIM {
wasi_filesystem::NewTimestamp::Timestamp(mtim)
wasi_filesystem::NewTimestamp::Timestamp(wasi_filesystem::Datetime {
seconds: mtim / 1_000_000_000,
nanoseconds: (mtim % 1_000_000_000) as _,
})
} else {
wasi_filesystem::NewTimestamp::NoChange
};
Expand Down Expand Up @@ -1729,6 +1741,10 @@ pub unsafe extern "C" fn sock_shutdown(fd: Fd, how: Sdflags) -> Errno {
unreachable()
}

fn datetime_to_timestamp(datetime: wasi_filesystem::Datetime) -> Timestamp {
u64::from(datetime.nanoseconds).saturating_add(datetime.seconds.saturating_mul(1_000_000_000))
}

fn at_flags_from_lookupflags(flags: Lookupflags) -> wasi_filesystem::AtFlags {
if flags & LOOKUPFLAGS_SYMLINK_FOLLOW == LOOKUPFLAGS_SYMLINK_FOLLOW {
wasi_filesystem::AtFlags::SYMLINK_FOLLOW
Expand Down
17 changes: 9 additions & 8 deletions wit/wasi.wit
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ interface wasi-filesystem {
/// Relative offset within a file.
type filedelta = s64

/// Timestamp in nanoseconds.
///
/// TODO: wasi-clocks is moving to seconds+nanoseconds.
type timestamp = u64
/// A time and date in seconds plus nanoseconds.
record datetime {
seconds: u64,
nanoseconds: u32,
}

/// The type of a filesystem object referenced by a descriptor.
///
Expand Down Expand Up @@ -306,11 +307,11 @@ interface wasi-filesystem {
/// in bytes of the pathname contained in the symbolic link.
size: filesize,
/// Last data access timestamp.
atim: timestamp,
atim: datetime,
/// Last data modification timestamp.
mtim: timestamp,
mtim: datetime,
/// Last file status change timestamp.
ctim: timestamp,
ctim: datetime,
}

/// Flags determining the method of how paths are resolved.
Expand Down Expand Up @@ -363,7 +364,7 @@ interface wasi-filesystem {
/// with the filesystem.
now,
/// Set the timestamp to the given value.
timestamp(timestamp),
timestamp(datetime),
}

/// A directory entry.
Expand Down

0 comments on commit 1df7a3b

Please sign in to comment.