wasmer_wasix/syscalls/wasix/
chdir.rs

1use super::*;
2use crate::syscalls::*;
3
4/// ### `chdir()`
5/// Sets the current working directory
6#[instrument(level = "trace", skip_all, fields(name = field::Empty), ret)]
7pub fn chdir<M: MemorySize>(
8    mut ctx: FunctionEnvMut<'_, WasiEnv>,
9    path: WasmPtr<u8, M>,
10    path_len: M::Offset,
11) -> Result<Errno, WasiError> {
12    WasiEnv::do_pending_operations(&mut ctx)?;
13
14    let env = ctx.data();
15    let (memory, mut state) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };
16    let path = unsafe { get_input_str_ok!(&memory, path, path_len) };
17    Span::current().record("path", path.as_str());
18
19    wasi_try_ok!(chdir_internal(ctx.data(), &path));
20    let env = ctx.data();
21
22    #[cfg(feature = "journal")]
23    if env.enable_journal {
24        JournalEffector::save_chdir(&mut ctx, path).map_err(|err| {
25            tracing::error!("failed to chdir event - {}", err);
26            WasiError::Exit(ExitCode::from(Errno::Fault))
27        })?;
28    }
29
30    Ok(Errno::Success)
31}
32
33/// Change the WASIX current directory to the directory reached by resolving
34/// `path`.
35///
36/// `chdir` is intentionally not a textual path update. POSIX shells often keep
37/// a logical `$PWD` for display, so after `cd some_symlink` a shell builtin
38/// `pwd` may print the symlinked spelling. The process current directory,
39/// though, is the resolved directory object. A physical lookup such as
40/// `realpath(".")` observes the symlink target. WASIX stores the latter form in
41/// `state.fs.current_dir`, because this string is used by the runtime to resolve
42/// future relative paths, not merely to echo the spelling the user typed.
43///
44/// The resolver is called with `follow_symlinks = true`, matching `chdir(2)`:
45/// the final component must be followed if it is a symlink, and the resolved
46/// inode must be a directory. The stored cwd therefore comes from the resolved
47/// `Kind::Dir` path (or `/` for the virtual root), not from the original input
48/// path or its lexical absolute form. Preserving the logical input here would
49/// make later relative lookups re-enter symlink paths and would diverge from the
50/// directory object that `chdir` actually selected.
51///
52/// For resolved non-root directories, the final `read_dir` check refreshes the
53/// backing filesystem view enough to reject stale cached directories that can no
54/// longer be listed.
55pub fn chdir_internal(env: &WasiEnv, path: &str) -> Result<(), Errno> {
56    let state = &env.state;
57    if path.is_empty() {
58        return Err(Errno::Noent);
59    }
60
61    let path = state.fs.relative_path_to_absolute(path.to_string());
62    let inode = state
63        .fs
64        .get_inode_at_path(&state.inodes, crate::VIRTUAL_ROOT_FD, &path, true)?;
65
66    let resolved_path = {
67        let guard = inode.read();
68        match guard.deref() {
69            Kind::Dir { path, .. } => {
70                let resolved_path = crate::fs::PosixPath::from_path(path).as_str().to_owned();
71                if state
72                    .fs
73                    .root_fs
74                    .read_dir(Path::new(&resolved_path))
75                    .is_err()
76                {
77                    return Err(Errno::Noent);
78                }
79                resolved_path
80            }
81            Kind::Root { .. } => "/".to_string(),
82            _ => return Err(Errno::Notdir),
83        }
84    };
85
86    state.fs.set_current_dir(&resolved_path);
87    Ok(())
88}