1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use super::*;

impl JournalEffector {
    pub fn save_fd_duplicate(
        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
        original_fd: Fd,
        copied_fd: Fd,
    ) -> anyhow::Result<()> {
        Self::save_event(
            ctx,
            JournalEntry::DuplicateFileDescriptorV1 {
                original_fd,
                copied_fd,
            },
        )
    }

    pub fn apply_fd_duplicate(
        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
        original_fd: Fd,
        copied_fd: Fd,
    ) -> anyhow::Result<()> {
        let ret_fd = crate::syscalls::fd_dup_internal(ctx, original_fd)
            .map_err(|err| {
                anyhow::format_err!(
                    "journal restore error: failed to duplicate file descriptor (original={}, copied={}) - {}",
                    original_fd,
                    copied_fd,
                    err
                )
            })?;

        let ret = crate::syscalls::fd_renumber_internal(ctx, ret_fd, copied_fd);
        if ret != Errno::Success {
            bail!(
                "journal restore error: failed renumber file descriptor after duplicate (from={}, to={}) - {}",
                ret_fd,
                copied_fd,
                ret
            );
        }
        Ok(())
    }
}