wasmer_wasix/journal/effector/syscalls/
path_rename.rs

1use crate::{VIRTUAL_ROOT_FD, syscalls::__asyncify_light};
2
3use super::*;
4
5impl JournalEffector {
6    pub fn save_path_rename(
7        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
8        old_fd: Fd,
9        old_path: String,
10        new_fd: Fd,
11        new_path: String,
12    ) -> anyhow::Result<()> {
13        Self::save_event(
14            ctx,
15            JournalEntry::PathRenameV1 {
16                old_fd,
17                old_path: Cow::Owned(old_path),
18                new_fd,
19                new_path: Cow::Owned(new_path),
20            },
21        )
22    }
23
24    pub fn apply_path_rename(
25        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
26        old_fd: Fd,
27        old_path: &str,
28        new_fd: Fd,
29        new_path: &str,
30    ) -> anyhow::Result<()> {
31        // see `VIRTUAL_ROOT_FD` for details as to why this exists
32        if old_fd == VIRTUAL_ROOT_FD && new_fd == VIRTUAL_ROOT_FD {
33            let state = ctx.data().state.clone();
34            let old_path = old_path.to_string();
35            let new_path = new_path.to_string();
36            __asyncify_light(ctx.data(), None, async move {
37                state.fs_rename(old_path, new_path).await
38            })??;
39        } else {
40            let ret =
41                crate::syscalls::path_rename_internal(ctx, old_fd, old_path, new_fd, new_path)?;
42            if ret != Errno::Success {
43                bail!(
44                    "journal restore error: failed to rename path (old_fd={old_fd}, old_path={old_path}, new_fd={new_fd}, new_path={new_path}) - {ret}"
45                );
46            }
47        }
48        Ok(())
49    }
50}