wasmer_wasix/journal/effector/syscalls/
path_remove_directory.rs

1use std::path::Path;
2
3use virtual_fs::FileSystem;
4
5use crate::VIRTUAL_ROOT_FD;
6
7use super::*;
8
9impl JournalEffector {
10    pub fn save_path_remove_directory(
11        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
12        fd: Fd,
13        path: String,
14    ) -> anyhow::Result<()> {
15        Self::save_event(
16            ctx,
17            JournalEntry::RemoveDirectoryV1 {
18                fd,
19                path: Cow::Owned(path),
20            },
21        )
22    }
23
24    pub fn apply_path_remove_directory(
25        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
26        fd: Fd,
27        path: &str,
28    ) -> anyhow::Result<()> {
29        // see `VIRTUAL_ROOT_FD` for details as to why this exists
30        if fd == VIRTUAL_ROOT_FD {
31            ctx.data().state.fs.root_fs.remove_dir(Path::new(path))?;
32        } else {
33            let base_dir = ctx.data().state.fs.get_fd(fd).map_err(|err| {
34                anyhow::format_err!(
35                    "journal restore error: invalid directory descriptor (fd={fd}) - {err}"
36                )
37            })?;
38            if let Err(err) =
39                crate::syscalls::path_remove_directory_internal(ctx, fd, base_dir, path)
40            {
41                bail!("journal restore error: failed to remove directory - {err}");
42            }
43        }
44        Ok(())
45    }
46}