pub fn open_scratch_directory(path: &str) -> Result<wasi::Fd, String> {
    unsafe {
        for i in 3.. {
            let stat = match wasi::fd_prestat_get(i) {
                Ok(s) => s,
                Err(_) => break,
            };
            if stat.pr_type != wasi::PREOPENTYPE_DIR {
                continue;
            }
            let mut dst = Vec::with_capacity(stat.u.dir.pr_name_len);
            if wasi::fd_prestat_dir_name(i, dst.as_mut_ptr(), dst.capacity()).is_err() {
                continue;
            }
            dst.set_len(stat.u.dir.pr_name_len);
            if dst == path.as_bytes() {
                return Ok(wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
                    .expect("failed to open dir"));
            }
        }

        Err(format!("failed to find scratch dir"))
    }
}
/// Opens a fresh file descriptor for `path` where `path` should be a preopened
/// directory.
Opens a fresh file descriptor for `path` where `path` should be a preopened
directory.
Opens a fresh file descriptor for ` path ` where ` path ` should be a preopened directory .
pub fn open_scratch_directory(path: &str) -> Result<wasi::Fd, String> {
    unsafe {
        for i in 3.. {
            let stat = match wasi::fd_prestat_get(i) {
                Ok(s) => s,
                Err(_) => break,
            };
            if stat.pr_type != wasi::PREOPENTYPE_DIR {
                continue;
            }
            let mut dst = Vec::with_capacity(stat.u.dir.pr_name_len);
            if wasi::fd_prestat_dir_name(i, dst.as_mut_ptr(), dst.capacity()).is_err() {
                continue;
            }
            dst.set_len(stat.u.dir.pr_name_len);
            if dst == path.as_bytes() {
                return Ok(wasi::path_open(i, 0, ".", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
                    .expect("failed to open dir"));
            }
        }
        Err(format!("failed to find scratch dir"))
    }
}
pub fn open_scratch_directory ( path : & str ) -> Result < wasi :: Fd , String > { unsafe { for i in 3 .. { let stat = match wasi :: fd_prestat_get ( i ) { Ok ( s ) => s , Err ( _ ) => break , } ; if stat . pr_type != wasi :: PREOPENTYPE_DIR { continue ; } let mut dst = Vec :: with_capacity ( stat . u . dir . pr_name_len ) ; if wasi :: fd_prestat_dir_name ( i , dst . as_mut_ptr ( ) , dst . capacity ( ) ) . is_err ( ) { continue ; } dst . set_len ( stat . u . dir . pr_name_len ) ; if dst == path . as_bytes ( ) { return Ok ( wasi :: path_open ( i , 0 , " " , wasi :: OFLAGS_DIRECTORY , 0 , 0 , 0 ) . expect ( " " ) ) ; } } Err ( format ! ( " " ) ) } }
Opens a fresh file descriptor for `path` where `path` should be a preopened
directory.
Opens a fresh file descriptor for ` path ` where ` path ` should be a preopened directory .
path
&str