Configures a "preopened directory" to be available to WebAssembly. By default WebAssembly does not have access to the filesystem because there are no preopened directories. All filesystem operations, such as opening a file, are done through a preexisting handle. This means that to provide WebAssembly access to a directory it must be configured through this API. WASI will also prevent access outs
(
&mut self,
host_path: impl AsRef<Path>,
guest_path: impl AsRef<str>,
dir_perms: DirPerms,
file_perms: FilePerms,
)
| 298 | /// # } |
| 299 | /// ``` |
| 300 | pub fn preopened_dir( |
| 301 | &mut self, |
| 302 | host_path: impl AsRef<Path>, |
| 303 | guest_path: impl AsRef<str>, |
| 304 | dir_perms: DirPerms, |
| 305 | file_perms: FilePerms, |
| 306 | ) -> Result<&mut Self> { |
| 307 | let dir = cap_std::fs::Dir::open_ambient_dir(host_path.as_ref(), ambient_authority())?; |
| 308 | let mut open_mode = OpenMode::empty(); |
| 309 | if dir_perms.contains(DirPerms::READ) { |
| 310 | open_mode |= OpenMode::READ; |
| 311 | } |
| 312 | if dir_perms.contains(DirPerms::MUTATE) { |
| 313 | open_mode |= OpenMode::WRITE; |
| 314 | } |
| 315 | self.filesystem.preopens.push(( |
| 316 | Dir::new( |
| 317 | dir, |
| 318 | dir_perms, |
| 319 | file_perms, |
| 320 | open_mode, |
| 321 | self.filesystem.allow_blocking_current_thread, |
| 322 | ), |
| 323 | guest_path.as_ref().to_owned(), |
| 324 | )); |
| 325 | Ok(self) |
| 326 | } |
| 327 | |
| 328 | /// Set the generator for the `wasi:random/random` number generator to the |
| 329 | /// custom generator specified. |