wasmer_wasix/syscalls/wasi/
path_symlink.rs1use super::*;
2use crate::syscalls::*;
3
4#[instrument(level = "trace", skip_all, fields(%fd, old_path = field::Empty, new_path = field::Empty), ret)]
18pub fn path_symlink<M: MemorySize>(
19 mut ctx: FunctionEnvMut<'_, WasiEnv>,
20 old_path: WasmPtr<u8, M>,
21 old_path_len: M::Offset,
22 fd: WasiFd,
23 new_path: WasmPtr<u8, M>,
24 new_path_len: M::Offset,
25) -> Result<Errno, WasiError> {
26 WasiEnv::do_pending_operations(&mut ctx)?;
27
28 let env = ctx.data();
29 let (memory, mut state, inodes) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };
30 let old_path_str = unsafe { get_input_str_ok!(&memory, old_path, old_path_len) };
31 Span::current().record("old_path", old_path_str.as_str());
32 let new_path_str = unsafe { get_input_str_ok!(&memory, new_path, new_path_len) };
33 Span::current().record("new_path", new_path_str.as_str());
34
35 wasi_try_ok!(path_symlink_internal(
36 &mut ctx,
37 &old_path_str,
38 fd,
39 &new_path_str
40 ));
41 let env = ctx.data();
42
43 #[cfg(feature = "journal")]
44 if env.enable_journal {
45 JournalEffector::save_path_symlink(&mut ctx, old_path_str, fd, new_path_str).map_err(
46 |err| {
47 tracing::error!("failed to save path symbolic link event - {}", err);
48 WasiError::Exit(ExitCode::from(Errno::Fault))
49 },
50 )?;
51 }
52
53 Ok(Errno::Success)
54}
55
56pub fn path_symlink_internal(
57 ctx: &mut FunctionEnvMut<'_, WasiEnv>,
58 old_path: &str,
59 fd: WasiFd,
60 new_path: &str,
61) -> Result<(), Errno> {
62 let env = ctx.data();
63 let (memory, mut state, inodes) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };
64
65 let base_fd = state.fs.get_fd(fd)?;
66 if !base_fd.inner.rights.contains(Rights::PATH_SYMLINK) {
67 return Err(Errno::Access);
68 }
69
70 let old_path_path = std::path::Path::new(old_path);
72 let (source_inode, _) = state
73 .fs
74 .get_parent_inode_at_path(inodes, fd, old_path_path, true)?;
75 let depth = state.fs.path_depth_from_fd(fd, source_inode);
76
77 let depth = match depth {
79 Ok(depth) => depth as i32 - 1,
80 Err(_) => -1,
81 };
82
83 let new_path_path = std::path::Path::new(new_path);
84 let (target_parent_inode, entry_name) =
85 state
86 .fs
87 .get_parent_inode_at_path(inodes, fd, new_path_path, true)?;
88
89 {
91 let guard = target_parent_inode.read();
92 match guard.deref() {
93 Kind::Dir { entries, .. } => {
94 if entries.contains_key(&entry_name) {
95 return Err(Errno::Exist);
96 }
97 }
98 Kind::Root { .. } => return Err(Errno::Notcapable),
99 Kind::Socket { .. }
100 | Kind::PipeRx { .. }
101 | Kind::PipeTx { .. }
102 | Kind::DuplexPipe { .. }
103 | Kind::EventNotifications { .. }
104 | Kind::Epoll { .. } => return Err(Errno::Inval),
105 Kind::File { .. } | Kind::Symlink { .. } | Kind::Buffer { .. } => {
106 unreachable!("get_parent_inode_at_path returned something other than a Dir or Root")
107 }
108 }
109 }
110
111 let mut source_path = std::path::Path::new(old_path);
112 let mut relative_path = std::path::PathBuf::new();
113 for _ in 0..depth {
114 relative_path.push("..");
115 }
116 relative_path.push(source_path);
117
118 let kind = Kind::Symlink {
119 base_po_dir: fd,
120 path_to_symlink: std::path::PathBuf::from(new_path),
121 relative_path,
122 };
123 let new_inode =
124 state
125 .fs
126 .create_inode_with_default_stat(inodes, kind, false, entry_name.clone().into());
127
128 {
129 let mut guard = target_parent_inode.write();
130 if let Kind::Dir { entries, .. } = guard.deref_mut() {
131 entries.insert(entry_name, new_inode);
132 }
133 }
134
135 Ok(())
136}