use super::*;
use crate::syscalls::*;
use crate::types::wasi::Snapshot0Filestat;
#[instrument(level = "trace", skip_all, fields(%fd, size = field::Empty, mtime = field::Empty), ret)]
pub fn fd_filestat_get<M: MemorySize>(
mut ctx: FunctionEnvMut<'_, WasiEnv>,
fd: WasiFd,
buf: WasmPtr<Filestat, M>,
) -> Errno {
let stat = wasi_try!(fd_filestat_get_internal(&mut ctx, fd));
Span::current().record("size", stat.st_size);
Span::current().record("mtime", stat.st_mtim);
let env = ctx.data();
let (memory, _) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };
let buf = buf.deref(&memory);
wasi_try_mem!(buf.write(stat));
Errno::Success
}
pub(crate) fn fd_filestat_get_internal(
ctx: &mut FunctionEnvMut<'_, WasiEnv>,
fd: WasiFd,
) -> Result<Filestat, Errno> {
let env = ctx.data();
let (_, state, inodes) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };
let fd_entry = state.fs.get_fd(fd)?;
if !fd_entry.rights.contains(Rights::FD_FILESTAT_GET) {
return Err(Errno::Access);
}
state.fs.filestat_fd(fd)
}
#[instrument(level = "trace", skip_all, fields(%fd), ret)]
pub fn fd_filestat_get_old<M: MemorySize>(
mut ctx: FunctionEnvMut<'_, WasiEnv>,
fd: WasiFd,
buf: WasmPtr<Snapshot0Filestat, M>,
) -> Errno {
let stat = wasi_try!(fd_filestat_get_internal(&mut ctx, fd));
let env = ctx.data();
let (memory, _) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };
let old_stat = Snapshot0Filestat {
st_dev: stat.st_dev,
st_ino: stat.st_ino,
st_filetype: stat.st_filetype,
st_nlink: stat.st_nlink as u32,
st_size: stat.st_size,
st_atim: stat.st_atim,
st_mtim: stat.st_mtim,
st_ctim: stat.st_ctim,
};
let buf = buf.deref(&memory);
wasi_try_mem!(buf.write(old_stat));
Errno::Success
}