use core::ffi::CStr;
pub use kernel_user_link::file::BlockingMode;
pub use kernel_user_link::file::DirEntry;
pub use kernel_user_link::file::DirFilename;
pub use kernel_user_link::file::FileMeta;
pub use kernel_user_link::file::FileStat;
pub use kernel_user_link::file::FileType;
pub use kernel_user_link::file::OpenOptions;
pub use kernel_user_link::file::SeekFrom;
pub use kernel_user_link::file::SeekWhence;
pub use kernel_user_link::file::MAX_FILENAME_LEN;
pub use kernel_user_link::FD_STDERR;
pub use kernel_user_link::FD_STDIN;
pub use kernel_user_link::FD_STDOUT;
use kernel_user_link::call_syscall;
use kernel_user_link::syscalls::SyscallError;
use kernel_user_link::syscalls::SYS_CHDIR;
use kernel_user_link::syscalls::SYS_CLOSE;
use kernel_user_link::syscalls::SYS_CREATE_PIPE;
use kernel_user_link::syscalls::SYS_GET_CWD;
use kernel_user_link::syscalls::SYS_GET_FILE_META;
use kernel_user_link::syscalls::SYS_OPEN;
use kernel_user_link::syscalls::SYS_OPEN_DIR;
use kernel_user_link::syscalls::SYS_READ;
use kernel_user_link::syscalls::SYS_READ_DIR;
use kernel_user_link::syscalls::SYS_SEEK;
use kernel_user_link::syscalls::SYS_SET_FILE_META;
use kernel_user_link::syscalls::SYS_STAT;
use kernel_user_link::syscalls::SYS_WRITE;
pub unsafe fn syscall_read(fd: usize, buf: &mut [u8]) -> Result<u64, SyscallError> {
unsafe {
call_syscall!(
SYS_READ,
fd, buf.as_mut_ptr() as u64, buf.len() as u64 )
}
}
pub unsafe fn syscall_write(fd: usize, buf: &[u8]) -> Result<u64, SyscallError> {
unsafe {
call_syscall!(
SYS_WRITE,
fd, buf.as_ptr() as u64, buf.len() as u64 )
}
}
pub unsafe fn syscall_open(
path: &CStr,
open_options: OpenOptions,
flags: usize,
) -> Result<usize, SyscallError> {
unsafe {
call_syscall!(
SYS_OPEN,
path.as_ptr() as u64, open_options.to_u64(), flags as u64 )
.map(|fd| fd as usize)
}
}
pub unsafe fn syscall_close(fd: usize) -> Result<(), SyscallError> {
unsafe {
call_syscall!(
SYS_CLOSE,
fd, )
.map(|e| assert!(e == 0))
}
}
pub unsafe fn syscall_create_pipe() -> Result<(usize, usize), SyscallError> {
let mut in_fd: u64 = 0;
let mut out_fd: u64 = 0;
unsafe {
call_syscall!(
SYS_CREATE_PIPE,
&mut in_fd as *mut u64 as u64, &mut out_fd as *mut u64 as u64 )?
};
Ok((in_fd as usize, out_fd as usize))
}
#[deprecated(note = "Use `syscall_set_file_meta` instead")]
pub unsafe fn syscall_blocking_mode(
fd: usize,
blocking_mode: BlockingMode,
) -> Result<(), SyscallError> {
syscall_set_file_meta(fd, FileMeta::BlockingMode(blocking_mode))
}
pub unsafe fn syscall_stat(path: &CStr, stat: &mut FileStat) -> Result<(), SyscallError> {
let stat_ptr = stat as *mut FileStat as u64;
unsafe {
call_syscall!(
SYS_STAT,
path.as_ptr() as u64, stat_ptr )
.map(|e| assert!(e == 0))
}
}
pub unsafe fn syscall_open_dir(path: &CStr) -> Result<usize, SyscallError> {
unsafe {
call_syscall!(
SYS_OPEN_DIR,
path.as_ptr() as u64, )
.map(|fd| fd as usize)
}
}
pub unsafe fn syscall_read_dir(fd: usize, entries: &mut [DirEntry]) -> Result<usize, SyscallError> {
let entries_ptr = entries.as_mut_ptr() as u64;
unsafe {
call_syscall!(
SYS_READ_DIR,
fd, entries_ptr, entries.len() as u64 )
.map(|written| written as usize)
}
}
pub unsafe fn syscall_chdir(path: &CStr) -> Result<(), SyscallError> {
unsafe {
call_syscall!(
SYS_CHDIR,
path.as_ptr() as u64, )
.map(|e| assert!(e == 0))
}
}
pub unsafe fn syscall_get_cwd(path: &mut [u8]) -> Result<usize, SyscallError> {
unsafe {
call_syscall!(
SYS_GET_CWD,
path.as_mut_ptr() as u64, path.len() as u64 )
.map(|written| written as usize)
}
}
pub unsafe fn syscall_set_file_meta(fd: usize, meta: FileMeta) -> Result<(), SyscallError> {
let meta_id = meta.to_u64_meta_id();
let meta_data = meta.inner_u64();
unsafe {
call_syscall!(
SYS_SET_FILE_META,
fd, meta_id, meta_data )
.map(|e| assert!(e == 0))
}
}
pub unsafe fn syscall_get_file_meta(fd: usize, meta: &mut FileMeta) -> Result<(), SyscallError> {
let meta_id = meta.to_u64_meta_id();
let mut meta_data = 0;
unsafe {
call_syscall!(
SYS_GET_FILE_META,
fd, meta_id, &mut meta_data as *mut u64 as u64 )
.map(|e| assert!(e == 0))
}?;
*meta = FileMeta::try_from((meta_id, meta_data)).unwrap();
Ok(())
}
pub unsafe fn syscall_seek(fd: usize, seek: SeekFrom) -> Result<u64, SyscallError> {
unsafe {
call_syscall!(
SYS_SEEK,
fd, seek.whence as u64, seek.offset as u64, )
}
}