1use core::ffi::CStr;
2
3pub use kernel_user_link::file::BlockingMode;
4pub use kernel_user_link::file::DirEntry;
5pub use kernel_user_link::file::DirFilename;
6pub use kernel_user_link::file::FileMeta;
7pub use kernel_user_link::file::FileStat;
8pub use kernel_user_link::file::FileType;
9pub use kernel_user_link::file::OpenOptions;
10pub use kernel_user_link::file::SeekFrom;
11pub use kernel_user_link::file::SeekWhence;
12pub use kernel_user_link::file::MAX_FILENAME_LEN;
13pub use kernel_user_link::FD_STDERR;
14pub use kernel_user_link::FD_STDIN;
15pub use kernel_user_link::FD_STDOUT;
16
17use kernel_user_link::call_syscall;
18use kernel_user_link::syscalls::SyscallError;
19use kernel_user_link::syscalls::SYS_CHDIR;
20use kernel_user_link::syscalls::SYS_CLOSE;
21use kernel_user_link::syscalls::SYS_CREATE_PIPE;
22use kernel_user_link::syscalls::SYS_GET_CWD;
23use kernel_user_link::syscalls::SYS_GET_FILE_META;
24use kernel_user_link::syscalls::SYS_OPEN;
25use kernel_user_link::syscalls::SYS_OPEN_DIR;
26use kernel_user_link::syscalls::SYS_READ;
27use kernel_user_link::syscalls::SYS_READ_DIR;
28use kernel_user_link::syscalls::SYS_SEEK;
29use kernel_user_link::syscalls::SYS_SET_FILE_META;
30use kernel_user_link::syscalls::SYS_STAT;
31use kernel_user_link::syscalls::SYS_WRITE;
32
33pub unsafe fn syscall_read(fd: usize, buf: &mut [u8]) -> Result<u64, SyscallError> {
37 unsafe {
38 call_syscall!(
39 SYS_READ,
40 fd, buf.as_mut_ptr() as u64, buf.len() as u64 )
44 }
45}
46
47pub unsafe fn syscall_write(fd: usize, buf: &[u8]) -> Result<u64, SyscallError> {
51 unsafe {
52 call_syscall!(
53 SYS_WRITE,
54 fd, buf.as_ptr() as u64, buf.len() as u64 )
58 }
59}
60
61pub unsafe fn syscall_open(
65 path: &CStr,
66 open_options: OpenOptions,
67 flags: usize,
68) -> Result<usize, SyscallError> {
69 unsafe {
70 call_syscall!(
71 SYS_OPEN,
72 path.as_ptr() as u64, open_options.to_u64(), flags as u64 )
76 .map(|fd| fd as usize)
77 }
78}
79
80pub unsafe fn syscall_close(fd: usize) -> Result<(), SyscallError> {
83 unsafe {
84 call_syscall!(
85 SYS_CLOSE,
86 fd, )
88 .map(|e| assert!(e == 0))
89 }
90}
91
92pub unsafe fn syscall_create_pipe() -> Result<(usize, usize), SyscallError> {
96 let mut in_fd: u64 = 0;
97 let mut out_fd: u64 = 0;
98 unsafe {
99 call_syscall!(
100 SYS_CREATE_PIPE,
101 &mut in_fd as *mut u64 as u64, &mut out_fd as *mut u64 as u64 )?
104 };
105
106 Ok((in_fd as usize, out_fd as usize))
107}
108
109#[deprecated(note = "Use `syscall_set_file_meta` instead")]
112pub unsafe fn syscall_blocking_mode(
113 fd: usize,
114 blocking_mode: BlockingMode,
115) -> Result<(), SyscallError> {
116 syscall_set_file_meta(fd, FileMeta::BlockingMode(blocking_mode))
117}
118
119pub unsafe fn syscall_stat(path: &CStr, stat: &mut FileStat) -> Result<(), SyscallError> {
123 let stat_ptr = stat as *mut FileStat as u64;
124 unsafe {
125 call_syscall!(
126 SYS_STAT,
127 path.as_ptr() as u64, stat_ptr )
130 .map(|e| assert!(e == 0))
131 }
132}
133
134pub unsafe fn syscall_open_dir(path: &CStr) -> Result<usize, SyscallError> {
137 unsafe {
138 call_syscall!(
139 SYS_OPEN_DIR,
140 path.as_ptr() as u64, )
142 .map(|fd| fd as usize)
143 }
144}
145
146pub unsafe fn syscall_read_dir(fd: usize, entries: &mut [DirEntry]) -> Result<usize, SyscallError> {
150 let entries_ptr = entries.as_mut_ptr() as u64;
151 unsafe {
152 call_syscall!(
153 SYS_READ_DIR,
154 fd, entries_ptr, entries.len() as u64 )
158 .map(|written| written as usize)
159 }
160}
161
162pub unsafe fn syscall_chdir(path: &CStr) -> Result<(), SyscallError> {
165 unsafe {
166 call_syscall!(
167 SYS_CHDIR,
168 path.as_ptr() as u64, )
170 .map(|e| assert!(e == 0))
171 }
172}
173
174pub unsafe fn syscall_get_cwd(path: &mut [u8]) -> Result<usize, SyscallError> {
178 unsafe {
179 call_syscall!(
180 SYS_GET_CWD,
181 path.as_mut_ptr() as u64, path.len() as u64 )
184 .map(|written| written as usize)
185 }
186}
187
188pub unsafe fn syscall_set_file_meta(fd: usize, meta: FileMeta) -> Result<(), SyscallError> {
191 let meta_id = meta.to_u64_meta_id();
192 let meta_data = meta.inner_u64();
193 unsafe {
194 call_syscall!(
195 SYS_SET_FILE_META,
196 fd, meta_id, meta_data )
200 .map(|e| assert!(e == 0))
201 }
202}
203
204pub unsafe fn syscall_get_file_meta(fd: usize, meta: &mut FileMeta) -> Result<(), SyscallError> {
209 let meta_id = meta.to_u64_meta_id();
210 let mut meta_data = 0;
211 unsafe {
212 call_syscall!(
213 SYS_GET_FILE_META,
214 fd, meta_id, &mut meta_data as *mut u64 as u64 )
218 .map(|e| assert!(e == 0))
219 }?;
220 *meta = FileMeta::try_from((meta_id, meta_data)).unwrap();
223
224 Ok(())
225}
226
227pub unsafe fn syscall_seek(fd: usize, seek: SeekFrom) -> Result<u64, SyscallError> {
230 unsafe {
231 call_syscall!(
232 SYS_SEEK,
233 fd, seek.whence as u64, seek.offset as u64, )
237 }
238}