Opens a fresh file descriptor for `path` where `path` should be a preopened directory.
(path: &str)
| 14 | /// Opens a fresh file descriptor for `path` where `path` should be a preopened |
| 15 | /// directory. |
| 16 | pub fn open_scratch_directory(path: &str) -> Result<wasip1::Fd, String> { |
| 17 | unsafe { |
| 18 | for i in 3.. { |
| 19 | let stat = match wasip1::fd_prestat_get(i) { |
| 20 | Ok(s) => s, |
| 21 | Err(_) => break, |
| 22 | }; |
| 23 | if stat.tag != wasip1::PREOPENTYPE_DIR.raw() { |
| 24 | continue; |
| 25 | } |
| 26 | let mut dst = Vec::with_capacity(stat.u.dir.pr_name_len); |
| 27 | if wasip1::fd_prestat_dir_name(i, dst.as_mut_ptr(), dst.capacity()).is_err() { |
| 28 | continue; |
| 29 | } |
| 30 | dst.set_len(stat.u.dir.pr_name_len); |
| 31 | if dst == path.as_bytes() { |
| 32 | return Ok( |
| 33 | wasip1::path_open(i, 0, ".", wasip1::OFLAGS_DIRECTORY, 0, 0, 0) |
| 34 | .expect("failed to open dir"), |
| 35 | ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | Err(format!("failed to find scratch dir")) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub unsafe fn create_file(dir_fd: wasip1::Fd, filename: &str) { |
| 44 | unsafe { |
no test coverage detected