fixes to system-interface, use fs-set-times

This commit is contained in:
Pat Hickey
2020-12-10 12:06:21 -08:00
parent 229474d6e0
commit 20eb66beab
4 changed files with 44 additions and 51 deletions

View File

@@ -19,8 +19,9 @@ libc = "0.2"
getrandom = { version = "0.2.0", features = ["std"] }
wiggle = { path = "../wiggle", default-features = false, version = "0.21.0" }
tracing = "0.1.19"
system-interface = { path = "../../../system-interface" }
cap-std = "*"
system-interface = "0.2"
cap-std = "0.6"
fs-set-times = "0.2.1"
[badges]
maintenance = { status = "actively-developed" }

View File

@@ -1,8 +1,9 @@
use crate::Error;
use fs_set_times::SetTimes;
use std::ops::Deref;
use system_interface::fs::FileIoExt;
pub trait WasiFile: FileIoExt {
pub trait WasiFile: FileIoExt + SetTimes {
fn datasync(&self) -> Result<(), Error>;
fn sync(&self) -> Result<(), Error>;
fn get_filetype(&self) -> Result<Filetype, Error>;
@@ -10,13 +11,7 @@ pub trait WasiFile: FileIoExt {
fn get_oflags(&self) -> Result<OFlags, Error>;
fn set_oflags(&self, _flags: OFlags) -> Result<(), Error>;
fn get_filestat(&self) -> Result<Filestat, Error>;
fn set_filestat_times(
&self,
_atim: Option<FilestatSetTime>,
_mtim: Option<FilestatSetTime>,
) -> Result<(), Error>;
fn set_filestat_size(&self, _size: u64) -> Result<(), Error>;
fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error>;
}
#[derive(Debug, Copy, Clone)]
@@ -82,12 +77,6 @@ pub struct Filestat {
ctim: std::time::SystemTime,
}
#[derive(Debug, Copy, Clone)]
pub enum FilestatSetTime {
Now,
Absolute(std::time::SystemTime),
}
pub(crate) struct FileEntry {
pub(crate) base_caps: FileCaps,
pub(crate) inheriting_caps: FileCaps,
@@ -218,29 +207,8 @@ impl WasiFile for cap_std::fs::File {
ctim: meta.created()?.into_std(),
})
}
fn set_filestat_times(
&self,
_atim: Option<FilestatSetTime>,
_mtim: Option<FilestatSetTime>,
) -> Result<(), Error> {
// XXX cap-std does not expose a way to set accessed time or modified time
todo!()
}
fn set_filestat_size(&self, size: u64) -> Result<(), Error> {
self.set_len(size)?;
Ok(())
}
fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> {
let metadata = self.metadata()?;
let current_size = metadata.len();
let wanted_size = offset.checked_add(len).ok_or(Error::TooBig)?;
// This check will be unnecessary when rust-lang/rust#63326 is fixed
if wanted_size > i64::max_value() as u64 {
return Err(Error::TooBig);
}
if wanted_size > current_size {
self.set_len(wanted_size)?;
}
Ok(())
}
}

View File

@@ -1,9 +1,8 @@
#![allow(unused_variables)]
use crate::dir::{DirCaps, DirEntry, TableDirExt};
use crate::file::{
FdFlags, FdStat, FileCaps, FileEntry, Filestat, FilestatSetTime, Filetype, OFlags,
};
use crate::file::{FdFlags, FdStat, FileCaps, FileEntry, Filestat, Filetype, OFlags};
use crate::{Error, WasiCtx};
use fs_set_times::SystemTimeSpec;
use std::cell::RefMut;
use std::convert::TryFrom;
use std::io::{IoSlice, IoSliceMut};
@@ -265,25 +264,25 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
return Err(Error::Inval);
}
let atim = if set_atim {
Some(FilestatSetTime::Absolute(
Some(SystemTimeSpec::Absolute(
UNIX_EPOCH + Duration::from_nanos(atim),
))
} else if set_atim_now {
Some(FilestatSetTime::Now)
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
let mtim = if set_mtim {
Some(FilestatSetTime::Absolute(
Some(SystemTimeSpec::Absolute(
UNIX_EPOCH + Duration::from_nanos(mtim),
))
} else if set_mtim_now {
Some(FilestatSetTime::Now)
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
f.set_filestat_times(atim, mtim)?;
f.set_times(atim, mtim)?;
Ok(())
}