implement datasync

This commit is contained in:
Mark McCaskey 2019-04-02 15:29:32 -07:00
parent e61c03a176
commit 2de5a5da2b
2 changed files with 33 additions and 1 deletions

View File

@ -7,6 +7,7 @@ use generational_arena::{Arena, Index as Inode};
use hashbrown::hash_map::{Entry, HashMap};
use std::{
cell::{Cell, RefCell},
io::{self, Write},
ops::{Index, IndexMut},
path::PathBuf,
rc::Rc,
@ -212,6 +213,31 @@ impl WasiFs {
Err(__WASI_EBADF)
}
}
pub fn flush(&mut self, fd: __wasi_fd_t) -> Result<(), __wasi_errno_t> {
match fd {
0 => (),
1 => io::stdout().flush().map_err(|_| __WASI_EIO)?,
2 => io::stderr().flush().map_err(|_| __WASI_EIO)?,
_ => {
let fd = self.fd_map.get(&fd).ok_or(__WASI_EBADF)?;
if fd.rights & __WASI_RIGHT_FD_DATASYNC == 0 {
return Err(__WASI_EACCES);
}
let inode = &mut self.inodes[fd.inode];
match &mut inode.kind {
Kind::File { handle } => handle.flush().map_err(|_| __WASI_EIO)?,
// TODO: verify this behavior
Kind::Dir { .. } => return Err(__WASI_EISDIR),
Kind::Symlink { .. } => unimplemented!(),
Kind::Buffer { .. } => (),
}
}
}
Ok(())
}
}
pub struct WasiState<'a> {

View File

@ -256,7 +256,13 @@ pub fn fd_close(ctx: &mut Ctx, fd: __wasi_fd_t) -> __wasi_errno_t {
/// The file descriptor to sync
pub fn fd_datasync(ctx: &mut Ctx, fd: __wasi_fd_t) -> __wasi_errno_t {
debug!("wasi::fd_datasync");
unimplemented!()
let state = get_wasi_state(ctx);
if let Err(e) = state.fs.flush(fd) {
e
} else {
__WASI_ESUCCESS
}
}
/// ### `fd_fdstat_get()`