MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / open_scratch_directory

Function open_scratch_directory

crates/test-programs/src/preview1.rs:16–41  ·  view source on GitHub ↗

Opens a fresh file descriptor for `path` where `path` should be a preopened directory.

(path: &str)

Source from the content-addressed store, hash-verified

14/// Opens a fresh file descriptor for `path` where `path` should be a preopened
15/// directory.
16pub 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
43pub unsafe fn create_file(dir_fd: wasip1::Fd, filename: &str) {
44 unsafe {

Callers 15

mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
big_stringFunction · 0.85
big_iovecsFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85

Calls 9

fd_prestat_getFunction · 0.85
fd_prestat_dir_nameFunction · 0.85
OkFunction · 0.85
path_openFunction · 0.85
as_bytesMethod · 0.80
rawMethod · 0.45
as_mut_ptrMethod · 0.45
capacityMethod · 0.45
expectMethod · 0.45

Tested by

no test coverage detected