safa_api/syscalls/
fs.rs

1use safa_abi::{
2    errors::ErrorStatus,
3    raw::io::{DirEntry, OpenOptions},
4};
5
6use super::SyscallNum;
7use super::{define_syscall, err_from_u16};
8use crate::syscalls::types::{OptionalPtrMut, RequiredPtr, RequiredPtrMut, Ri, StrPtr};
9
10define_syscall!(SyscallNum::SysGetDirEntry => {
11    /// Gets the directory entry for the path `path` and puts it in `dest_direntry`
12    /// path must be valid utf-8
13    /// if `dest_direntry` is not null, it will be set to the directory entry
14    sysgetdirentry(path_ptr: StrPtr, path_len: usize, dest_direntry: OptionalPtrMut<DirEntry>)
15});
16
17#[inline]
18pub fn getdirentry(path: &str) -> Result<DirEntry, ErrorStatus> {
19    let mut dest_direntry: DirEntry = unsafe { core::mem::zeroed() };
20    err_from_u16!(
21        sysgetdirentry(path.as_ptr(), path.len(), &raw mut dest_direntry),
22        dest_direntry
23    )
24}
25
26define_syscall! {
27    SyscallNum::SysOpenAll => {
28        /// Opens the file with the path `path` and puts the resource id in `dest_fd`, with all permissions
29        ///
30        /// path must be valid utf-8
31        sysopen_all(path_ptr: StrPtr, path_len: usize, dest_fd: RequiredPtr<Ri>)
32    },
33    SyscallNum::SysOpen => {
34        /// Opens the file with the path `path` and puts the resource id in `dest_fd`, with a given mode (permissions and flags)
35        ///
36        /// path must be valid utf-8
37        sysopen(path_ptr: StrPtr, path_len: usize, options: OpenOptions, dest_fd: RequiredPtrMut<Ri>)
38    },
39    SyscallNum::SysCreate => {
40        /// Creates the file with the path `path`
41        /// path must be valid utf-8
42        syscreate_file(path_ptr: StrPtr, path_len: usize)
43    },
44    SyscallNum::SysCreateDir => {
45        /// Creates the directory with the path `path`
46        ///
47        /// path must be valid utf-8
48        syscreate_dir(path_ptr: StrPtr, path_len: usize)
49    },
50    SyscallNum::SysRemovePath => {
51        /// Deletes "removes" a given path
52        ///
53        /// path must be valid utf-8
54        sysremove_path(path_ptr: StrPtr, path_len: usize)
55    },
56}
57
58#[inline]
59/// Opens the path `path` and returns the resource id of the file descriptor, with all permissions
60///
61/// see [`sysopen_all`] for underlying syscall
62pub fn open_all(path: &str) -> Result<Ri, ErrorStatus> {
63    let mut dest_fd = 0xAAAAAAAAAAAAAAAAusize;
64    err_from_u16!(
65        sysopen_all(path.as_ptr(), path.len(), &raw mut dest_fd),
66        dest_fd
67    )
68}
69
70/// Opens the file with the path `path` with a given mode (permissions and flags), returns the resource id of the file descriptor
71///
72/// see [`sysopen`] for underlying syscall
73#[inline]
74pub fn open(path: &str, options: OpenOptions) -> Result<Ri, ErrorStatus> {
75    let mut dest_fd = 0xAAAAAAAAAAAAAAAAusize;
76    err_from_u16!(
77        sysopen(path.as_ptr(), path.len(), options, &raw mut dest_fd),
78        dest_fd
79    )
80}
81
82#[inline]
83/// Creates the file with the path `path`
84///
85/// see [`syscreate_file`] for underlying syscall
86pub fn create(path: &str) -> Result<(), ErrorStatus> {
87    err_from_u16!(syscreate_file(path.as_ptr(), path.len()))
88}
89
90#[inline]
91/// Creates the directory with the path `path`
92///
93/// see [`syscreate_dir`] for underlying syscall
94pub fn createdir(path: &str) -> Result<(), ErrorStatus> {
95    err_from_u16!(syscreate_dir(path.as_ptr(), path.len()))
96}
97
98#[inline]
99/// Removes a given path
100///
101/// see [`sysremove_path`] for underlying syscall
102pub fn remove_path(path: &str) -> Result<(), ErrorStatus> {
103    err_from_u16!(sysremove_path(path.as_ptr(), path.len()))
104}