Improve error handling, fix probable lifetime issue

This commit is contained in:
Mark McCaskey 2019-08-28 13:19:06 -07:00
parent a188e7f652
commit 8a2dba534a
2 changed files with 21 additions and 11 deletions

View File

@ -41,20 +41,20 @@ pub fn generate_import_object(
mapped_dirs: Vec<(String, PathBuf)>, mapped_dirs: Vec<(String, PathBuf)>,
) -> ImportObject { ) -> ImportObject {
let state_gen = move || { let state_gen = move || {
// TODO: look into removing all these unnecessary clones
fn state_destructor(data: *mut c_void) { fn state_destructor(data: *mut c_void) {
dbg!("IN DESTRUCTOR");
dbg!(&data);
unsafe { unsafe {
drop(Box::from_raw(data as *mut WasiState)); drop(Box::from_raw(data as *mut WasiState));
} }
} }
let preopened_files = preopened_files.clone();
let mapped_dirs = mapped_dirs.clone();
let state = Box::new(WasiState { let state = Box::new(WasiState {
fs: WasiFs::new(&preopened_files, &mapped_dirs).unwrap(), fs: WasiFs::new(&preopened_files, &mapped_dirs).expect("Could not create WASI FS"),
args: args.clone(), args: args.clone(),
envs: envs.clone(), envs: envs.clone(),
}); });
dbg!("IN CONSTRUCTOR");
( (
Box::into_raw(state) as *mut c_void, Box::into_raw(state) as *mut c_void,

View File

@ -168,7 +168,7 @@ impl WasiFs {
let inode = wasi_fs.create_virtual_root(); let inode = wasi_fs.create_virtual_root();
let fd = wasi_fs let fd = wasi_fs
.create_fd(root_rights, root_rights, 0, Fd::READ, inode) .create_fd(root_rights, root_rights, 0, Fd::READ, inode)
.expect("Could not create root fd"); .map_err(|e| format!("Could not create root fd: {}", e))?;
wasi_fs.preopen_fds.push(fd); wasi_fs.preopen_fds.push(fd);
inode inode
}; };
@ -179,7 +179,13 @@ impl WasiFs {
// TODO: think about this // TODO: think about this
let default_rights = 0x1FFFFFFF; // all rights let default_rights = 0x1FFFFFFF; // all rights
let cur_dir = PathBuf::from(dir); let cur_dir = PathBuf::from(dir);
let cur_dir_metadata = cur_dir.metadata().expect("Could not find directory"); let cur_dir_metadata = cur_dir.metadata().map_err(|e| {
format!(
"Could not get metadata for file {:?}: {}",
dir,
e.to_string()
)
})?;
let kind = if cur_dir_metadata.is_dir() { let kind = if cur_dir_metadata.is_dir() {
Kind::Dir { Kind::Dir {
parent: Some(root_inode), parent: Some(root_inode),
@ -209,7 +215,7 @@ impl WasiFs {
Fd::READ | Fd::WRITE, Fd::READ | Fd::WRITE,
inode, inode,
) )
.expect("Could not open fd"); .map_err(|e| format!("Could not open fd for file {:?}: {}", dir, e))?;
if let Kind::Root { entries } = &mut wasi_fs.inodes[root_inode].kind { if let Kind::Root { entries } = &mut wasi_fs.inodes[root_inode].kind {
// todo handle collisions // todo handle collisions
assert!(entries.insert(dir.to_string(), inode).is_none()) assert!(entries.insert(dir.to_string(), inode).is_none())
@ -221,9 +227,13 @@ impl WasiFs {
debug!("Attempting to open {:?} at {}", real_dir, alias); debug!("Attempting to open {:?} at {}", real_dir, alias);
// TODO: think about this // TODO: think about this
let default_rights = 0x1FFFFFFF; // all rights let default_rights = 0x1FFFFFFF; // all rights
let cur_dir_metadata = real_dir let cur_dir_metadata = real_dir.metadata().map_err(|e| {
.metadata() format!(
.expect("mapped dir not at previously verified location"); "Could not get metadata for file {:?}: {}",
&real_dir,
e.to_string()
)
})?;
let kind = if cur_dir_metadata.is_dir() { let kind = if cur_dir_metadata.is_dir() {
Kind::Dir { Kind::Dir {
parent: Some(root_inode), parent: Some(root_inode),
@ -253,7 +263,7 @@ impl WasiFs {
Fd::READ | Fd::WRITE, Fd::READ | Fd::WRITE,
inode, inode,
) )
.expect("Could not open fd"); .map_err(|e| format!("Could not open fd for file {:?}: {}", &real_dir, e))?;
if let Kind::Root { entries } = &mut wasi_fs.inodes[root_inode].kind { if let Kind::Root { entries } = &mut wasi_fs.inodes[root_inode].kind {
// todo handle collisions // todo handle collisions
assert!(entries.insert(alias.clone(), inode).is_none()); assert!(entries.insert(alias.clone(), inode).is_none());