Move extracting fds from context into hostcalls

This commit is contained in:
Jakub Konka
2019-07-20 10:38:21 +02:00
committed by Dan Gohman
parent 14391bab56
commit 0d571a4e6d
6 changed files with 301 additions and 361 deletions

View File

@@ -14,6 +14,13 @@ pub enum Descriptor {
} }
impl Descriptor { impl Descriptor {
pub fn as_file(&self) -> Result<&fs::File> {
match self {
Descriptor::File(f) => Ok(f),
_ => Err(host::__WASI_EBADF),
}
}
pub fn is_file(&self) -> bool { pub fn is_file(&self) -> bool {
match self { match self {
Descriptor::File(_) => true, Descriptor::File(_) => true,

View File

@@ -36,17 +36,15 @@ pub fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wa
pub fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wasi_errno_t { pub fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wasi_errno_t {
trace!("fd_datasync(fd={:?})", fd); trace!("fd_datasync(fd={:?})", fd);
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let rights = host::__WASI_RIGHT_FD_DATASYNC; let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_DATASYNC, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let file = match &*fe.fd_object.descriptor { let ret = match fd.sync_data() {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let ret = match file.sync_data() {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(err) => err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host), Err(err) => err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host),
}; };
@@ -78,15 +76,13 @@ pub fn fd_pread(
Ok(iovs) => iovs, Ok(iovs) => iovs,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let rights = host::__WASI_RIGHT_FD_READ; let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_READ, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let file = match &*fe.fd_object.descriptor {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let offset = dec_filesize(offset); let offset = dec_filesize(offset);
if offset > i64::max_value() as u64 { if offset > i64::max_value() as u64 {
@@ -94,7 +90,7 @@ pub fn fd_pread(
} }
let buf_size = iovs.iter().map(|v| v.buf_len).sum(); let buf_size = iovs.iter().map(|v| v.buf_len).sum();
let mut buf = vec![0; buf_size]; let mut buf = vec![0; buf_size];
let host_nread = match hostcalls_impl::fd_pread(file, &mut buf, offset) { let host_nread = match hostcalls_impl::fd_pread(fd, &mut buf, offset) {
Ok(host_nread) => host_nread, Ok(host_nread) => host_nread,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -144,15 +140,13 @@ pub fn fd_pwrite(
Ok(iovs) => iovs, Ok(iovs) => iovs,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let rights = host::__WASI_RIGHT_FD_READ; let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_READ, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let file = match &*fe.fd_object.descriptor {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let offset = dec_filesize(offset); let offset = dec_filesize(offset);
if offset > i64::max_value() as u64 { if offset > i64::max_value() as u64 {
@@ -165,7 +159,7 @@ pub fn fd_pwrite(
std::slice::from_raw_parts(iov.buf as *const u8, iov.buf_len) std::slice::from_raw_parts(iov.buf as *const u8, iov.buf_len)
}); });
} }
let host_nwritten = match hostcalls_impl::fd_pwrite(file, &buf, offset) { let host_nwritten = match hostcalls_impl::fd_pwrite(fd, &buf, offset) {
Ok(host_nwritten) => host_nwritten, Ok(host_nwritten) => host_nwritten,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -263,13 +257,14 @@ pub fn fd_renumber(
return return_enc_errno(host::__WASI_EBADF); return return_enc_errno(host::__WASI_EBADF);
} }
let fe_from_dup = if let Descriptor::File(f) = &*wasi_ctx.fds[&from].fd_object.descriptor { let fe_from_dup = match wasi_ctx.fds[&from]
match FdEntry::duplicate(f) { .fd_object
Ok(fe) => fe, .descriptor
Err(e) => return return_enc_errno(e), .as_file()
} .and_then(FdEntry::duplicate)
} else { {
return return_enc_errno(host::__WASI_EBADF); Ok(fe) => fe,
Err(e) => return return_enc_errno(e),
}; };
wasi_ctx.fds.insert(to, fe_from_dup); wasi_ctx.fds.insert(to, fe_from_dup);
@@ -304,16 +299,15 @@ pub fn fd_seek(
} else { } else {
host::__WASI_RIGHT_FD_SEEK | host::__WASI_RIGHT_FD_TELL host::__WASI_RIGHT_FD_SEEK | host::__WASI_RIGHT_FD_TELL
}; };
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) { let fd = match wasi_ctx
Ok(fe) => fe, .get_fd_entry(fd, rights, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let file = match &*fe.fd_object.descriptor {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let host_newoffset = match hostcalls_impl::fd_seek(file, offset, whence) { let host_newoffset = match hostcalls_impl::fd_seek(fd, offset, whence) {
Ok(host_newoffset) => host_newoffset, Ok(host_newoffset) => host_newoffset,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -337,18 +331,14 @@ pub fn fd_tell(
trace!("fd_tell(fd={:?}, newoffset={:#x?})", fd, newoffset); trace!("fd_tell(fd={:?}, newoffset={:#x?})", fd, newoffset);
let fd = dec_fd(fd); let fd = dec_fd(fd);
let rights = host::__WASI_RIGHT_FD_TELL; let fd = match wasi_ctx
.get_fd_entry(fd, host::__WASI_RIGHT_FD_TELL, 0)
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) { .and_then(|fe| fe.fd_object.descriptor.as_file())
Ok(fe) => fe, {
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let file = match &*fe.fd_object.descriptor { let host_offset = match hostcalls_impl::fd_tell(fd) {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let host_offset = match hostcalls_impl::fd_tell(file) {
Ok(host_offset) => host_offset, Ok(host_offset) => host_offset,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -371,30 +361,33 @@ pub fn fd_fdstat_get(
) -> wasm32::__wasi_errno_t { ) -> wasm32::__wasi_errno_t {
trace!("fd_fdstat_get(fd={:?}, fdstat_ptr={:#x?})", fd, fdstat_ptr); trace!("fd_fdstat_get(fd={:?}, fdstat_ptr={:#x?})", fd, fdstat_ptr);
let host_fd = dec_fd(fd); let mut fdstat = match dec_fdstat_byref(memory, fdstat_ptr) {
let mut host_fdstat = match dec_fdstat_byref(memory, fdstat_ptr) { Ok(fdstat) => fdstat,
Ok(host_fdstat) => host_fdstat, Err(e) => return return_enc_errno(e),
};
let fd = dec_fd(fd);
let fe = match wasi_ctx.get_fd_entry(fd, 0, 0) {
Ok(fe) => fe,
Err(e) => return return_enc_errno(e),
};
let fd = match fe.fd_object.descriptor.as_file() {
Ok(fd) => fd,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let fe = match wasi_ctx.fds.get(&host_fd) { let fs_flags = match hostcalls_impl::fd_fdstat_get(fd) {
Some(fe) => fe,
None => return return_enc_errno(host::__WASI_EBADF),
};
let fs_flags = match hostcalls_impl::fd_fdstat_get(fe) {
Ok(flags) => flags, Ok(flags) => flags,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
host_fdstat.fs_filetype = fe.fd_object.file_type; fdstat.fs_filetype = fe.fd_object.file_type;
host_fdstat.fs_rights_base = fe.rights_base; fdstat.fs_rights_base = fe.rights_base;
host_fdstat.fs_rights_inheriting = fe.rights_inheriting; fdstat.fs_rights_inheriting = fe.rights_inheriting;
host_fdstat.fs_flags = fs_flags; fdstat.fs_flags = fs_flags;
trace!(" | *buf={:?}", host_fdstat); trace!(" | *buf={:?}", fdstat);
let ret = enc_fdstat_byref(memory, fdstat_ptr, host_fdstat) let ret = enc_fdstat_byref(memory, fdstat_ptr, fdstat)
.map(|_| host::__WASI_ESUCCESS) .map(|_| host::__WASI_ESUCCESS)
.unwrap_or_else(identity); .unwrap_or_else(identity);
@@ -409,16 +402,20 @@ pub fn fd_fdstat_set_flags(
) -> wasm32::__wasi_errno_t { ) -> wasm32::__wasi_errno_t {
trace!("fd_fdstat_set_flags(fd={:?}, fdflags={:#x?})", fd, fdflags); trace!("fd_fdstat_set_flags(fd={:?}, fdflags={:#x?})", fd, fdflags);
let host_fd = dec_fd(fd); let fdflags = dec_fdflags(fdflags);
let host_fdflags = dec_fdflags(fdflags); let fd = dec_fd(fd);
let ret = match wasi_ctx.fds.get(&host_fd) { let fd = match wasi_ctx
Some(fe) => match hostcalls_impl::fd_fdstat_set_flags(fe, host_fdflags) { .get_fd_entry(fd, 0, 0)
Ok(()) => host::__WASI_ESUCCESS, .and_then(|fe| fe.fd_object.descriptor.as_file())
Err(e) => e, {
}, Ok(f) => f,
None => host::__WASI_EBADF, Err(e) => return return_enc_errno(e),
}; };
let ret = hostcalls_impl::fd_fdstat_set_flags(fd, fdflags)
.map(|_| host::__WASI_ESUCCESS)
.unwrap_or_else(identity);
return_enc_errno(ret) return_enc_errno(ret)
} }
@@ -436,8 +433,8 @@ pub fn fd_fdstat_set_rights(
fs_rights_inheriting fs_rights_inheriting
); );
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let fe = match wasi_ctx.fds.get_mut(&host_fd) { let fe = match wasi_ctx.fds.get_mut(&fd) {
Some(fe) => fe, Some(fe) => fe,
None => return return_enc_errno(host::__WASI_EBADF), None => return return_enc_errno(host::__WASI_EBADF),
}; };
@@ -456,17 +453,15 @@ pub fn fd_fdstat_set_rights(
pub fn fd_sync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wasi_errno_t { pub fn fd_sync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wasi_errno_t {
trace!("fd_sync(fd={:?})", fd); trace!("fd_sync(fd={:?})", fd);
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let rights = host::__WASI_RIGHT_FD_SYNC; let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_SYNC, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let file = match &*fe.fd_object.descriptor { let ret = match fd.sync_all() {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let ret = match file.sync_all() {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(err) => err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host), Err(err) => err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host),
}; };
@@ -545,22 +540,19 @@ pub fn fd_advise(
advice advice
); );
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let advice = dec_advice(advice); let advice = dec_advice(advice);
let offset = dec_filesize(offset); let offset = dec_filesize(offset);
let len = dec_filesize(len); let len = dec_filesize(len);
let rights = host::__WASI_RIGHT_FD_ADVISE; let fd = match wasi_ctx
.get_fd_entry(fd, host::__WASI_RIGHT_FD_ADVISE, 0)
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) { .and_then(|fe| fe.fd_object.descriptor.as_file())
Ok(fe) => fe, {
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let file = match &*fe.fd_object.descriptor {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let ret = match hostcalls_impl::fd_advise(file, advice, offset, len) { let ret = match hostcalls_impl::fd_advise(fd, advice, offset, len) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -577,21 +569,18 @@ pub fn fd_allocate(
) -> wasm32::__wasi_errno_t { ) -> wasm32::__wasi_errno_t {
trace!("fd_allocate(fd={:?}, offset={}, len={})", fd, offset, len); trace!("fd_allocate(fd={:?}, offset={}, len={})", fd, offset, len);
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let rights = host::__WASI_RIGHT_FD_ALLOCATE;
let offset = dec_filesize(offset); let offset = dec_filesize(offset);
let len = dec_filesize(len); let len = dec_filesize(len);
let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_ALLOCATE, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let f = match &*fe.fd_object.descriptor {
Descriptor::File(f) => f,
_ => return return_enc_errno(host::__WASI_EBADF),
};
let metadata = match f let metadata = match fd
.metadata() .metadata()
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
{ {
@@ -608,7 +597,7 @@ pub fn fd_allocate(
} }
if wanted_size > current_size { if wanted_size > current_size {
if let Err(e) = f if let Err(e) = fd
.set_len(wanted_size) .set_len(wanted_size)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
{ {
@@ -643,7 +632,16 @@ pub fn path_create_directory(
trace!(" | (path_ptr,path_len)='{}'", path); trace!(" | (path_ptr,path_len)='{}'", path);
let ret = match hostcalls_impl::path_create_directory(wasi_ctx, dirfd, path) { let rights = host::__WASI_RIGHT_PATH_OPEN | host::__WASI_RIGHT_PATH_CREATE_DIRECTORY;
let dirfd = match wasi_ctx
.get_fd_entry(dirfd, rights, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let ret = match hostcalls_impl::path_create_directory(dirfd, path) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -692,15 +690,22 @@ pub fn path_link(
trace!(" | (old_path_ptr,old_path_len)='{}'", old_path); trace!(" | (old_path_ptr,old_path_len)='{}'", old_path);
trace!(" | (new_path_ptr,new_path_len)='{}'", new_path); trace!(" | (new_path_ptr,new_path_len)='{}'", new_path);
let ret = match hostcalls_impl::path_link( let old_dirfd = match wasi_ctx
wasi_ctx, .get_fd_entry(old_dirfd, host::__WASI_RIGHT_PATH_LINK_SOURCE, 0)
old_dirfd, .and_then(|fe| fe.fd_object.descriptor.as_file())
new_dirfd, {
old_path, Ok(f) => f,
new_path, Err(e) => return return_enc_errno(e),
host::__WASI_RIGHT_PATH_LINK_SOURCE, };
host::__WASI_RIGHT_PATH_LINK_TARGET, let new_dirfd = match wasi_ctx
) { .get_fd_entry(new_dirfd, host::__WASI_RIGHT_PATH_LINK_TARGET, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let ret = match hostcalls_impl::path_link(old_dirfd, new_dirfd, old_path, new_path) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -823,9 +828,11 @@ pub fn fd_readdir(
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let fd = dec_fd(fd); let fd = dec_fd(fd);
let rights = host::__WASI_RIGHT_FD_READDIR; let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_READDIR, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let host_buf = match dec_slice_of_mut::<u8>(memory, buf, buf_len) { let host_buf = match dec_slice_of_mut::<u8>(memory, buf, buf_len) {
@@ -837,7 +844,7 @@ pub fn fd_readdir(
let cookie = dec_dircookie(cookie); let cookie = dec_dircookie(cookie);
let host_bufused = match hostcalls_impl::fd_readdir(fe, host_buf, cookie) { let host_bufused = match hostcalls_impl::fd_readdir(fd, host_buf, cookie) {
Ok(host_bufused) => host_bufused, Ok(host_bufused) => host_bufused,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -884,17 +891,19 @@ pub fn path_readlink(
trace!(" | (path_ptr,path_len)='{}'", &path); trace!(" | (path_ptr,path_len)='{}'", &path);
let dirfd = match wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_READLINK, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let mut buf = match dec_slice_of_mut::<u8>(memory, buf_ptr, buf_len) { let mut buf = match dec_slice_of_mut::<u8>(memory, buf_ptr, buf_len) {
Ok(slice) => slice, Ok(slice) => slice,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let host_bufused = match hostcalls_impl::path_readlink( let host_bufused = match hostcalls_impl::path_readlink(dirfd, &path, &mut buf) {
wasi_ctx,
dirfd,
&path,
host::__WASI_RIGHT_PATH_READLINK,
&mut buf,
) {
Ok(host_bufused) => host_bufused, Ok(host_bufused) => host_bufused,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -948,12 +957,22 @@ pub fn path_rename(
trace!(" | (old_path_ptr,old_path_len)='{}'", old_path); trace!(" | (old_path_ptr,old_path_len)='{}'", old_path);
trace!(" | (new_path_ptr,new_path_len)='{}'", new_path); trace!(" | (new_path_ptr,new_path_len)='{}'", new_path);
let old_rights = host::__WASI_RIGHT_PATH_RENAME_SOURCE; let old_dirfd = match wasi_ctx
let new_rights = host::__WASI_RIGHT_PATH_RENAME_TARGET; .get_fd_entry(old_dirfd, host::__WASI_RIGHT_PATH_RENAME_SOURCE, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let new_dirfd = match wasi_ctx
.get_fd_entry(new_dirfd, host::__WASI_RIGHT_PATH_RENAME_TARGET, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let ret = match hostcalls_impl::path_rename( let ret = match hostcalls_impl::path_rename(old_dirfd, old_path, new_dirfd, new_path) {
wasi_ctx, old_dirfd, old_path, old_rights, new_dirfd, new_path, new_rights,
) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -974,13 +993,16 @@ pub fn fd_filestat_get(
filestat_ptr filestat_ptr
); );
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let fe = match wasi_ctx.fds.get(&host_fd) { let fd = match wasi_ctx
Some(fe) => fe, .get_fd_entry(fd, 0, 0)
None => return return_enc_errno(host::__WASI_EBADF), .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
}; };
let host_filestat = match hostcalls_impl::fd_filestat_get(fe) { let host_filestat = match hostcalls_impl::fd_filestat_get(fd) {
Ok(fstat) => fstat, Ok(fstat) => fstat,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -1011,17 +1033,19 @@ pub fn fd_filestat_set_times(
fst_flags fst_flags
); );
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let rights = host::__WASI_RIGHT_FD_FILESTAT_SET_TIMES; let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_FILESTAT_SET_TIMES, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let st_atim = dec_timestamp(st_atim); let st_atim = dec_timestamp(st_atim);
let st_mtim = dec_timestamp(st_mtim); let st_mtim = dec_timestamp(st_mtim);
let fst_flags = dec_fstflags(fst_flags); let fst_flags = dec_fstflags(fst_flags);
let ret = match hostcalls_impl::fd_filestat_set_times(fe, st_atim, st_mtim, fst_flags) { let ret = match hostcalls_impl::fd_filestat_set_times(fd, st_atim, st_mtim, fst_flags) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -1037,10 +1061,12 @@ pub fn fd_filestat_set_size(
) -> wasm32::__wasi_errno_t { ) -> wasm32::__wasi_errno_t {
trace!("fd_filestat_set_size(fd={:?}, st_size={})", fd, st_size); trace!("fd_filestat_set_size(fd={:?}, st_size={})", fd, st_size);
let host_fd = dec_fd(fd); let fd = dec_fd(fd);
let rights = host::__WASI_RIGHT_FD_FILESTAT_SET_SIZE; let fd = match wasi_ctx
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) { .get_fd_entry(fd, host::__WASI_RIGHT_FD_FILESTAT_SET_SIZE, 0)
Ok(fe) => fe, .and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
let st_size = dec_filesize(st_size); let st_size = dec_filesize(st_size);
@@ -1048,7 +1074,7 @@ pub fn fd_filestat_set_size(
return return_enc_errno(host::__WASI_E2BIG); return return_enc_errno(host::__WASI_E2BIG);
} }
let ret = match hostcalls_impl::fd_filestat_set_size(fe, st_size) { let ret = match hostcalls_impl::fd_filestat_set_size(fd, st_size) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -1085,7 +1111,15 @@ pub fn path_filestat_get(
trace!(" | (path_ptr,path_len)='{}'", path); trace!(" | (path_ptr,path_len)='{}'", path);
let host_filestat = match hostcalls_impl::path_filestat_get(wasi_ctx, dirfd, dirflags, path) { let dirfd = match wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_FILESTAT_GET, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let host_filestat = match hostcalls_impl::path_filestat_get(dirfd, dirflags, path) {
Ok(host_filestat) => host_filestat, Ok(host_filestat) => host_filestat,
Err(e) => return return_enc_errno(e), Err(e) => return return_enc_errno(e),
}; };
@@ -1132,13 +1166,20 @@ pub fn path_filestat_set_times(
trace!(" | (path_ptr,path_len)='{}'", path); trace!(" | (path_ptr,path_len)='{}'", path);
let rights = host::__WASI_RIGHT_PATH_FILESTAT_SET_TIMES;
let st_atim = dec_timestamp(st_atim); let st_atim = dec_timestamp(st_atim);
let st_mtim = dec_timestamp(st_mtim); let st_mtim = dec_timestamp(st_mtim);
let fst_flags = dec_fstflags(fst_flags); let fst_flags = dec_fstflags(fst_flags);
let dirfd = match wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_FILESTAT_SET_TIMES, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let ret = match hostcalls_impl::path_filestat_set_times( let ret = match hostcalls_impl::path_filestat_set_times(
wasi_ctx, dirfd, dirflags, path, rights, st_atim, st_mtim, fst_flags, dirfd, dirflags, path, st_atim, st_mtim, fst_flags,
) { ) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
@@ -1183,9 +1224,15 @@ pub fn path_symlink(
trace!(" | (old_path_ptr,old_path_len)='{}'", old_path); trace!(" | (old_path_ptr,old_path_len)='{}'", old_path);
trace!(" | (new_path_ptr,new_path_len)='{}'", new_path); trace!(" | (new_path_ptr,new_path_len)='{}'", new_path);
let rights = host::__WASI_RIGHT_PATH_SYMLINK; let dirfd = match wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_SYMLINK, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let ret = match hostcalls_impl::path_symlink(wasi_ctx, dirfd, rights, old_path, new_path) { let ret = match hostcalls_impl::path_symlink(dirfd, old_path, new_path) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -1217,12 +1264,15 @@ pub fn path_unlink_file(
trace!(" | (path_ptr,path_len)='{}'", path); trace!(" | (path_ptr,path_len)='{}'", path);
let ret = match hostcalls_impl::path_unlink_file( let dirfd = match wasi_ctx
wasi_ctx, .get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_UNLINK_FILE, 0)
dirfd, .and_then(|fe| fe.fd_object.descriptor.as_file())
path, {
host::__WASI_RIGHT_PATH_UNLINK_FILE, Ok(f) => f,
) { Err(e) => return return_enc_errno(e),
};
let ret = match hostcalls_impl::path_unlink_file(dirfd, path) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };
@@ -1254,9 +1304,15 @@ pub fn path_remove_directory(
trace!(" | (path_ptr,path_len)='{}'", path); trace!(" | (path_ptr,path_len)='{}'", path);
let rights = host::__WASI_RIGHT_PATH_REMOVE_DIRECTORY; let dirfd = match wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_REMOVE_DIRECTORY, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())
{
Ok(f) => f,
Err(e) => return return_enc_errno(e),
};
let ret = match hostcalls_impl::path_remove_directory(wasi_ctx, dirfd, path, rights) { let ret = match hostcalls_impl::path_remove_directory(dirfd, path) {
Ok(()) => host::__WASI_ESUCCESS, Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e, Err(e) => e,
}; };

View File

@@ -2,7 +2,7 @@
#![allow(unused_unsafe)] #![allow(unused_unsafe)]
use super::fs_helpers::*; use super::fs_helpers::*;
use crate::ctx::WasiCtx; use crate::ctx::WasiCtx;
use crate::fdentry::FdEntry; use crate::fdentry::{Descriptor, FdEntry};
use crate::sys::errno_from_host; use crate::sys::errno_from_host;
use crate::sys::fdentry_impl::determine_type_rights; use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::host_impl; use crate::sys::host_impl;
@@ -54,22 +54,17 @@ pub(crate) fn fd_tell(file: &File) -> Result<u64> {
} }
} }
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> { pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
use nix::fcntl::{fcntl, OFlag, F_GETFL}; use nix::fcntl::{fcntl, OFlag, F_GETFL};
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd(); match fcntl(fd.as_raw_fd(), F_GETFL).map(OFlag::from_bits_truncate) {
match fcntl(rawfd, F_GETFL).map(OFlag::from_bits_truncate) {
Ok(flags) => Ok(host_impl::fdflags_from_nix(flags)), Ok(flags) => Ok(host_impl::fdflags_from_nix(flags)),
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())), Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
} }
} }
pub(crate) fn fd_fdstat_set_flags( pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: host::__wasi_fdflags_t) -> Result<()> {
fd_entry: &FdEntry,
fdflags: host::__wasi_fdflags_t,
) -> Result<()> {
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
let nix_flags = host_impl::nix_from_fdflags(fdflags); let nix_flags = host_impl::nix_from_fdflags(fdflags);
match nix::fcntl::fcntl(rawfd, nix::fcntl::F_SETFL(nix_flags)) { match nix::fcntl::fcntl(fd.as_raw_fd(), nix::fcntl::F_SETFL(nix_flags)) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())), Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
} }
@@ -117,22 +112,10 @@ pub(crate) fn fd_advise(
Ok(()) Ok(())
} }
pub(crate) fn path_create_directory( pub(crate) fn path_create_directory(dirfd: &File, path: &str) -> Result<()> {
ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
) -> Result<()> {
use nix::libc::mkdirat; use nix::libc::mkdirat;
let (dir, path) = match path_get( let (dir, path) = match path_get(dirfd, 0, path, false) {
ctx,
dirfd,
0,
path,
host::__WASI_RIGHT_PATH_OPEN | host::__WASI_RIGHT_PATH_CREATE_DIRECTORY,
0,
false,
) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -146,20 +129,17 @@ pub(crate) fn path_create_directory(
} }
pub(crate) fn path_link( pub(crate) fn path_link(
ctx: &WasiCtx, old_dirfd: &File,
old_dirfd: host::__wasi_fd_t, new_dirfd: &File,
new_dirfd: host::__wasi_fd_t,
old_path: &str, old_path: &str,
new_path: &str, new_path: &str,
source_rights: host::__wasi_rights_t,
target_rights: host::__wasi_rights_t,
) -> Result<()> { ) -> Result<()> {
use nix::libc::linkat; use nix::libc::linkat;
let (old_dir, old_path) = match path_get(ctx, old_dirfd, 0, old_path, source_rights, 0, false) { let (old_dir, old_path) = match path_get(old_dirfd, 0, old_path, false) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
let (new_dir, new_path) = match path_get(ctx, new_dirfd, 0, new_path, target_rights, 0, false) { let (new_dir, new_path) = match path_get(new_dirfd, 0, new_path, false) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -230,15 +210,13 @@ pub(crate) fn path_open(
needed_inheriting |= host::__WASI_RIGHT_FD_SYNC; needed_inheriting |= host::__WASI_RIGHT_FD_SYNC;
} }
let (dir, path) = match path_get( let dirfe = ctx.get_fd_entry(dirfd, needed_base, needed_inheriting)?;
ctx, let dirfd = match &*dirfe.fd_object.descriptor {
dirfd, Descriptor::File(f) => f,
dirflags, _ => return Err(host::__WASI_EBADF),
path, };
needed_base,
needed_inheriting, let (dir, path) = match path_get(dirfd, dirflags, path, nix_oflags.contains(OFlag::O_CREAT)) {
nix_oflags.contains(OFlag::O_CREAT),
) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -308,16 +286,15 @@ pub(crate) fn path_open(
} }
pub(crate) fn fd_readdir( pub(crate) fn fd_readdir(
fd_entry: &FdEntry, fd: &File,
host_buf: &mut [u8], host_buf: &mut [u8],
cookie: host::__wasi_dircookie_t, cookie: host::__wasi_dircookie_t,
) -> Result<usize> { ) -> Result<usize> {
use libc::{dirent, fdopendir, memcpy, readdir_r, seekdir}; use libc::{dirent, fdopendir, memcpy, readdir_r, seekdir};
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
let host_buf_ptr = host_buf.as_mut_ptr(); let host_buf_ptr = host_buf.as_mut_ptr();
let host_buf_len = host_buf.len(); let host_buf_len = host_buf.len();
let dir = unsafe { fdopendir(rawfd) }; let dir = unsafe { fdopendir(fd.as_raw_fd()) };
if dir.is_null() { if dir.is_null() {
return Err(host_impl::errno_from_nix(nix::errno::Errno::last())); return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
} }
@@ -366,16 +343,10 @@ pub(crate) fn fd_readdir(
Ok(host_buf_len - left) Ok(host_buf_len - left)
} }
pub(crate) fn path_readlink( pub(crate) fn path_readlink(dirfd: &File, path: &str, buf: &mut [u8]) -> Result<usize> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
buf: &mut [u8],
) -> Result<usize> {
use nix::errno::Errno; use nix::errno::Errno;
let (dir, path) = match path_get(wasi_ctx, dirfd, 0, path, rights, 0, false) { let (dir, path) = match path_get(dirfd, 0, path, false) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -409,23 +380,18 @@ pub(crate) fn path_readlink(
} }
pub(crate) fn path_rename( pub(crate) fn path_rename(
wasi_ctx: &WasiCtx, old_dirfd: &File,
old_dirfd: host::__wasi_fd_t,
old_path: &str, old_path: &str,
old_rights: host::__wasi_rights_t, new_dirfd: &File,
new_dirfd: host::__wasi_fd_t,
new_path: &str, new_path: &str,
new_rights: host::__wasi_rights_t,
) -> Result<()> { ) -> Result<()> {
use nix::libc::renameat; use nix::libc::renameat;
let (old_dir, old_path) = match path_get(wasi_ctx, old_dirfd, 0, old_path, old_rights, 0, false) let (old_dir, old_path) = match path_get(old_dirfd, 0, old_path, false) {
{
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
let (new_dir, new_path) = match path_get(wasi_ctx, new_dirfd, 0, new_path, new_rights, 0, false) let (new_dir, new_path) = match path_get(new_dirfd, 0, new_path, false) {
{
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -447,18 +413,16 @@ pub(crate) fn path_rename(
} }
} }
pub(crate) fn fd_filestat_get(fd_entry: &FdEntry) -> Result<host::__wasi_filestat_t> { pub(crate) fn fd_filestat_get(fd: &File) -> Result<host::__wasi_filestat_t> {
use nix::sys::stat::fstat; use nix::sys::stat::fstat;
match fstat(fd.as_raw_fd()) {
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
match fstat(rawfd) {
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())), Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
Ok(filestat) => Ok(host_impl::filestat_from_nix(filestat)?), Ok(filestat) => Ok(host_impl::filestat_from_nix(filestat)?),
} }
} }
pub(crate) fn fd_filestat_set_times( pub(crate) fn fd_filestat_set_times(
fd_entry: &FdEntry, fd: &File,
st_atim: host::__wasi_timestamp_t, st_atim: host::__wasi_timestamp_t,
mut st_mtim: host::__wasi_timestamp_t, mut st_mtim: host::__wasi_timestamp_t,
fst_flags: host::__wasi_fstflags_t, fst_flags: host::__wasi_fstflags_t,
@@ -496,8 +460,7 @@ pub(crate) fn fd_filestat_set_times(
}; };
let ts_mtime = *TimeSpec::nanoseconds(st_mtim as i64).as_ref(); let ts_mtime = *TimeSpec::nanoseconds(st_mtim as i64).as_ref();
let times = [ts_atime, ts_mtime]; let times = [ts_atime, ts_mtime];
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd(); let res = unsafe { libc::futimens(fd.as_raw_fd(), times.as_ptr()) };
let res = unsafe { libc::futimens(rawfd, times.as_ptr()) };
if res != 0 { if res != 0 {
Err(host_impl::errno_from_nix(nix::errno::Errno::last())) Err(host_impl::errno_from_nix(nix::errno::Errno::last()))
} else { } else {
@@ -505,34 +468,21 @@ pub(crate) fn fd_filestat_set_times(
} }
} }
pub(crate) fn fd_filestat_set_size( pub(crate) fn fd_filestat_set_size(fd: &File, st_size: host::__wasi_filesize_t) -> Result<()> {
fd_entry: &FdEntry,
st_size: host::__wasi_filesize_t,
) -> Result<()> {
use nix::unistd::ftruncate; use nix::unistd::ftruncate;
ftruncate(fd.as_raw_fd(), st_size as off_t)
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd(); .map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
ftruncate(rawfd, st_size as off_t).map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
} }
pub(crate) fn path_filestat_get( pub(crate) fn path_filestat_get(
wasi_ctx: &WasiCtx, dirfd: &File,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t, dirflags: host::__wasi_lookupflags_t,
path: &str, path: &str,
) -> Result<host::__wasi_filestat_t> { ) -> Result<host::__wasi_filestat_t> {
use nix::fcntl::AtFlags; use nix::fcntl::AtFlags;
use nix::sys::stat::fstatat; use nix::sys::stat::fstatat;
let (dir, path) = match path_get( let (dir, path) = match path_get(dirfd, dirflags, path, false) {
wasi_ctx,
dirfd,
dirflags,
path,
host::__WASI_RIGHT_PATH_FILESTAT_GET,
0,
false,
) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -548,18 +498,16 @@ pub(crate) fn path_filestat_get(
} }
pub(crate) fn path_filestat_set_times( pub(crate) fn path_filestat_set_times(
wasi_ctx: &WasiCtx, dirfd: &File,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t, dirflags: host::__wasi_lookupflags_t,
path: &str, path: &str,
rights: host::__wasi_rights_t,
st_atim: host::__wasi_timestamp_t, st_atim: host::__wasi_timestamp_t,
mut st_mtim: host::__wasi_timestamp_t, mut st_mtim: host::__wasi_timestamp_t,
fst_flags: host::__wasi_fstflags_t, fst_flags: host::__wasi_fstflags_t,
) -> Result<()> { ) -> Result<()> {
use nix::sys::time::{TimeSpec, TimeValLike}; use nix::sys::time::{TimeSpec, TimeValLike};
let (dir, path) = match path_get(wasi_ctx, dirfd, dirflags, &path, rights, 0, false) { let (dir, path) = match path_get(dirfd, dirflags, &path, false) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -610,16 +558,10 @@ pub(crate) fn path_filestat_set_times(
} }
} }
pub(crate) fn path_symlink( pub(crate) fn path_symlink(dirfd: &File, old_path: &str, new_path: &str) -> Result<()> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
rights: host::__wasi_rights_t,
old_path: &str,
new_path: &str,
) -> Result<()> {
use nix::libc::symlinkat; use nix::libc::symlinkat;
let (dir, new_path) = match path_get(wasi_ctx, dirfd, 0, new_path, rights, 0, false) { let (dir, new_path) = match path_get(dirfd, 0, new_path, false) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -640,16 +582,11 @@ pub(crate) fn path_symlink(
} }
} }
pub(crate) fn path_unlink_file( pub(crate) fn path_unlink_file(dirfd: &File, path: &str) -> Result<()> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
) -> Result<()> {
use nix::errno; use nix::errno;
use nix::libc::unlinkat; use nix::libc::unlinkat;
let (dir, path) = match path_get(wasi_ctx, dirfd, 0, path, rights, 0, false) { let (dir, path) = match path_get(dirfd, 0, path, false) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
@@ -691,16 +628,11 @@ pub(crate) fn path_unlink_file(
} }
} }
pub(crate) fn path_remove_directory( pub(crate) fn path_remove_directory(dirfd: &File, path: &str) -> Result<()> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
) -> Result<()> {
use nix::errno; use nix::errno;
use nix::libc::{unlinkat, AT_REMOVEDIR}; use nix::libc::{unlinkat, AT_REMOVEDIR};
let (dir, path) = match path_get(wasi_ctx, dirfd, 0, path, rights, 0, false) { let (dir, path) = match path_get(dirfd, 0, path, false) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
Err(e) => return Err(e), Err(e) => return Err(e),
}; };

View File

@@ -1,8 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(unused_unsafe)] #![allow(unused_unsafe)]
use crate::ctx::WasiCtx;
use crate::fdentry::Descriptor;
use crate::sys::errno_from_host; use crate::sys::errno_from_host;
use crate::sys::host_impl; use crate::sys::host_impl;
use crate::{host, Result}; use crate::{host, Result};
@@ -14,12 +12,9 @@ use std::path::{Component, Path};
/// ///
/// This is a workaround for not having Capsicum support in the OS. /// This is a workaround for not having Capsicum support in the OS.
pub(crate) fn path_get( pub(crate) fn path_get(
wasi_ctx: &WasiCtx, dirfd: &File,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t, dirflags: host::__wasi_lookupflags_t,
path: &str, path: &str,
needed_base: host::__wasi_rights_t,
needed_inheriting: host::__wasi_rights_t,
needs_final_component: bool, needs_final_component: bool,
) -> Result<(File, String)> { ) -> Result<(File, String)> {
const MAX_SYMLINK_EXPANSIONS: usize = 128; const MAX_SYMLINK_EXPANSIONS: usize = 128;
@@ -29,14 +24,10 @@ pub(crate) fn path_get(
return Err(host::__WASI_EILSEQ); return Err(host::__WASI_EILSEQ);
} }
let dirfe = wasi_ctx.get_fd_entry(dirfd, needed_base, needed_inheriting)?; let dirfd = dirfd.try_clone().map_err(|err| {
let dirfd = match &*dirfe.fd_object.descriptor { err.raw_os_error()
Descriptor::File(f) => f.try_clone().map_err(|err| { .map_or(host::__WASI_EBADF, errno_from_host)
err.raw_os_error() })?;
.map_or(host::__WASI_EBADF, errno_from_host)
})?,
_ => return Err(host::__WASI_EBADF),
};
// Stack of directory file descriptors. Index 0 always corresponds with the directory provided // Stack of directory file descriptors. Index 0 always corresponds with the directory provided
// to this function. Entering a directory causes a file descriptor to be pushed, while handling // to this function. Entering a directory causes a file descriptor to be pushed, while handling

View File

@@ -2,7 +2,7 @@
#![allow(unused)] #![allow(unused)]
use super::fs_helpers::*; use super::fs_helpers::*;
use crate::ctx::WasiCtx; use crate::ctx::WasiCtx;
use crate::fdentry::FdEntry; use crate::fdentry::{Descriptor, FdEntry};
use crate::sys::errno_from_host; use crate::sys::errno_from_host;
use crate::sys::fdentry_impl::determine_type_rights; use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::host_impl; use crate::sys::host_impl;
@@ -58,19 +58,17 @@ pub(crate) fn fd_tell(file: &File) -> Result<u64> {
unimplemented!("fd_tell") unimplemented!("fd_tell")
} }
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> { pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
use winx::file::AccessRight; use winx::file::AccessRight;
let raw_handle = fd_entry.fd_object.descriptor.as_raw_handle(); match winx::file::get_file_access_rights(fd.as_raw_handle())
match winx::file::get_file_access_rights(raw_handle).map(AccessRight::from_bits_truncate) { .map(AccessRight::from_bits_truncate)
{
Ok(rights) => Ok(host_impl::fdflags_from_win(rights)), Ok(rights) => Ok(host_impl::fdflags_from_win(rights)),
Err(e) => Err(host_impl::errno_from_win(e)), Err(e) => Err(host_impl::errno_from_win(e)),
} }
} }
pub(crate) fn fd_fdstat_set_flags( pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: host::__wasi_fdflags_t) -> Result<()> {
fd_entry: &FdEntry,
fdflags: host::__wasi_fdflags_t,
) -> Result<()> {
unimplemented!("fd_fdstat_set_flags") unimplemented!("fd_fdstat_set_flags")
} }
@@ -83,22 +81,15 @@ pub(crate) fn fd_advise(
unimplemented!("fd_advise") unimplemented!("fd_advise")
} }
pub(crate) fn path_create_directory( pub(crate) fn path_create_directory(dirfd: &File, path: &str) -> Result<()> {
ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
) -> Result<()> {
unimplemented!("path_create_directory") unimplemented!("path_create_directory")
} }
pub(crate) fn path_link( pub(crate) fn path_link(
ctx: &WasiCtx, old_dirfd: &File,
old_dirfd: host::__wasi_fd_t, new_dirfd: &File,
new_dirfd: host::__wasi_fd_t,
old_path: &str, old_path: &str,
new_path: &str, new_path: &str,
source_rights: host::__wasi_rights_t,
target_rights: host::__wasi_rights_t,
) -> Result<()> { ) -> Result<()> {
unimplemented!("path_link") unimplemented!("path_link")
} }
@@ -144,13 +135,16 @@ pub(crate) fn path_open(
needed_inheriting |= host::__WASI_RIGHT_FD_SYNC; needed_inheriting |= host::__WASI_RIGHT_FD_SYNC;
} }
let dirfe = ctx.get_fd_entry(dirfd, needed_base, needed_inheriting)?;
let dirfd = match &*dirfe.fd_object.descriptor {
Descriptor::File(f) => f,
_ => return Err(host::__WASI_EBADF),
};
let (dir, path) = match path_get( let (dir, path) = match path_get(
ctx,
dirfd, dirfd,
dirflags, dirflags,
path, path,
needed_base,
needed_inheriting,
!win_flags_attrs.contains(FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS), !win_flags_attrs.contains(FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS),
) { ) {
Ok((dir, path)) => (dir, path), Ok((dir, path)) => (dir, path),
@@ -182,41 +176,32 @@ pub(crate) fn path_open(
} }
pub(crate) fn fd_readdir( pub(crate) fn fd_readdir(
fd_entry: &FdEntry, fd: &File,
host_buf: &mut [u8], host_buf: &mut [u8],
cookie: host::__wasi_dircookie_t, cookie: host::__wasi_dircookie_t,
) -> Result<usize> { ) -> Result<usize> {
unimplemented!("fd_readdir") unimplemented!("fd_readdir")
} }
pub(crate) fn path_readlink( pub(crate) fn path_readlink(dirfd: &File, path: &str, buf: &mut [u8]) -> Result<usize> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
buf: &mut [u8],
) -> Result<usize> {
unimplemented!("path_readlink") unimplemented!("path_readlink")
} }
pub(crate) fn path_rename( pub(crate) fn path_rename(
wasi_ctx: &WasiCtx, old_dirfd: &File,
old_dirfd: host::__wasi_fd_t,
old_path: &str, old_path: &str,
old_rights: host::__wasi_rights_t, new_dirfd: &File,
new_dirfd: host::__wasi_fd_t,
new_path: &str, new_path: &str,
new_rights: host::__wasi_rights_t,
) -> Result<()> { ) -> Result<()> {
unimplemented!("path_rename") unimplemented!("path_rename")
} }
pub(crate) fn fd_filestat_get(fd_entry: &FdEntry) -> Result<host::__wasi_filestat_t> { pub(crate) fn fd_filestat_get(fd: &File) -> Result<host::__wasi_filestat_t> {
unimplemented!("fd_filestat_get") unimplemented!("fd_filestat_get")
} }
pub(crate) fn fd_filestat_set_times( pub(crate) fn fd_filestat_set_times(
fd_entry: &FdEntry, fd: &File,
st_atim: host::__wasi_timestamp_t, st_atim: host::__wasi_timestamp_t,
mut st_mtim: host::__wasi_timestamp_t, mut st_mtim: host::__wasi_timestamp_t,
fst_flags: host::__wasi_fstflags_t, fst_flags: host::__wasi_fstflags_t,
@@ -224,16 +209,12 @@ pub(crate) fn fd_filestat_set_times(
unimplemented!("fd_filestat_set_times") unimplemented!("fd_filestat_set_times")
} }
pub(crate) fn fd_filestat_set_size( pub(crate) fn fd_filestat_set_size(fd: &File, st_size: host::__wasi_filesize_t) -> Result<()> {
fd_entry: &FdEntry,
st_size: host::__wasi_filesize_t,
) -> Result<()> {
unimplemented!("fd_filestat_set_size") unimplemented!("fd_filestat_set_size")
} }
pub(crate) fn path_filestat_get( pub(crate) fn path_filestat_get(
wasi_ctx: &WasiCtx, dirfd: &File,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t, dirflags: host::__wasi_lookupflags_t,
path: &str, path: &str,
) -> Result<host::__wasi_filestat_t> { ) -> Result<host::__wasi_filestat_t> {
@@ -241,11 +222,9 @@ pub(crate) fn path_filestat_get(
} }
pub(crate) fn path_filestat_set_times( pub(crate) fn path_filestat_set_times(
wasi_ctx: &WasiCtx, dirfd: &File,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t, dirflags: host::__wasi_lookupflags_t,
path: &str, path: &str,
rights: host::__wasi_rights_t,
st_atim: host::__wasi_timestamp_t, st_atim: host::__wasi_timestamp_t,
mut st_mtim: host::__wasi_timestamp_t, mut st_mtim: host::__wasi_timestamp_t,
fst_flags: host::__wasi_fstflags_t, fst_flags: host::__wasi_fstflags_t,
@@ -253,30 +232,14 @@ pub(crate) fn path_filestat_set_times(
unimplemented!("path_filestat_set_times") unimplemented!("path_filestat_set_times")
} }
pub(crate) fn path_symlink( pub(crate) fn path_symlink(dirfd: &File, old_path: &str, new_path: &str) -> Result<()> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
rights: host::__wasi_rights_t,
old_path: &str,
new_path: &str,
) -> Result<()> {
unimplemented!("path_symlink") unimplemented!("path_symlink")
} }
pub(crate) fn path_unlink_file( pub(crate) fn path_unlink_file(dirfd: &File, path: &str) -> Result<()> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
) -> Result<()> {
unimplemented!("path_unlink_file") unimplemented!("path_unlink_file")
} }
pub(crate) fn path_remove_directory( pub(crate) fn path_remove_directory(dirfd: &File, path: &str) -> Result<()> {
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
) -> Result<()> {
unimplemented!("path_remove_directory") unimplemented!("path_remove_directory")
} }

View File

@@ -1,8 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(unused_unsafe)] #![allow(unused_unsafe)]
use crate::ctx::WasiCtx;
use crate::fdentry::Descriptor;
use crate::sys::errno_from_host; use crate::sys::errno_from_host;
use crate::sys::host_impl; use crate::sys::host_impl;
use crate::{host, Result}; use crate::{host, Result};
@@ -12,12 +10,9 @@ use std::path::{Component, Path};
/// Normalizes a path to ensure that the target path is located under the directory provided. /// Normalizes a path to ensure that the target path is located under the directory provided.
pub(crate) fn path_get( pub(crate) fn path_get(
wasi_ctx: &WasiCtx, dirfd: &File,
dirfd: host::__wasi_fd_t,
_dirflags: host::__wasi_lookupflags_t, _dirflags: host::__wasi_lookupflags_t,
path: &str, path: &str,
needed_base: host::__wasi_rights_t,
needed_inheriting: host::__wasi_rights_t,
needs_final_component: bool, needs_final_component: bool,
) -> Result<(File, String)> { ) -> Result<(File, String)> {
if path.contains("\0") { if path.contains("\0") {
@@ -25,14 +20,10 @@ pub(crate) fn path_get(
return Err(host::__WASI_EILSEQ); return Err(host::__WASI_EILSEQ);
} }
let dirfe = wasi_ctx.get_fd_entry(dirfd, needed_base, needed_inheriting)?; let dirfd = dirfd.try_clone().map_err(|err| {
let dirfd = match &*dirfe.fd_object.descriptor { err.raw_os_error()
Descriptor::File(f) => f.try_clone().map_err(|err| { .map_or(host::__WASI_EBADF, errno_from_host)
err.raw_os_error() })?;
.map_or(host::__WASI_EBADF, errno_from_host)
})?,
_ => return Err(host::__WASI_EBADF),
};
// Stack of directory handles. Index 0 always corresponds with the directory provided // Stack of directory handles. Index 0 always corresponds with the directory provided
// to this function. Entering a directory causes a handle to be pushed, while handling // to this function. Entering a directory causes a handle to be pushed, while handling