safa_api/syscalls/
fs.rs

1use safa_abi::{
2    errors::ErrorStatus,
3    ffi::str::Str,
4    fs::{DirEntry, OpenOptions},
5};
6
7use super::SyscallNum;
8use super::{define_syscall, err_from_u16};
9use crate::syscalls::types::{OptionalPtrMut, RequiredPtr, RequiredPtrMut, Ri};
10
11define_syscall!(SyscallNum::SysFGetDirEntry => {
12    /// Gets the directory entry for the path `path` and puts it in `dest_direntry`
13    /// path must be valid utf-8
14    /// if `dest_direntry` is not null, it will be set to the directory entry
15    sysgetdirentry(path: Str, dest_direntry: OptionalPtrMut<DirEntry>)
16});
17
18#[inline]
19pub fn getdirentry(path: &str) -> Result<DirEntry, ErrorStatus> {
20    let mut dest_direntry: DirEntry = unsafe { core::mem::zeroed() };
21    let ptr = RequiredPtrMut::new(&raw mut dest_direntry).into();
22
23    err_from_u16!(sysgetdirentry(Str::from_str(path), ptr), dest_direntry)
24}
25
26define_syscall! {
27    SyscallNum::SysFSOpenAll => {
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: Str, dest_fd: RequiredPtr<Ri>)
32    },
33    SyscallNum::SysFSOpen => {
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: Str, options: OpenOptions, dest_fd: RequiredPtrMut<Ri>)
38    },
39    SyscallNum::SysFSCreate => {
40        /// Creates the file with the path `path`
41        /// path must be valid utf-8
42        syscreate_file(path: Str)
43    },
44    SyscallNum::SysFSCreateDir => {
45        /// Creates the directory with the path `path`
46        ///
47        /// path must be valid utf-8
48        syscreate_dir(path: Str)
49    },
50    SyscallNum::SysFSRemovePath => {
51        /// Deletes "removes" a given path
52        ///
53        /// path must be valid utf-8
54        sysremove_path(path: Str)
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    let ptr = unsafe { RequiredPtrMut::new_unchecked(&raw mut dest_fd) };
65    err_from_u16!(sysopen_all(Str::from_str(path), ptr), dest_fd)
66}
67
68/// Opens the file with the path `path` with a given mode (permissions and flags), returns the resource id of the file descriptor
69///
70/// see [`sysopen`] for underlying syscall
71#[inline]
72pub fn open(path: &str, options: OpenOptions) -> Result<Ri, ErrorStatus> {
73    let mut dest_fd = 0xAAAAAAAAAAAAAAAAusize;
74    let ptr = unsafe { RequiredPtrMut::new_unchecked(&raw mut dest_fd) };
75    err_from_u16!(sysopen(Str::from_str(path), options, ptr), dest_fd)
76}
77
78#[inline]
79/// Creates the file with the path `path`
80///
81/// see [`syscreate_file`] for underlying syscall
82pub fn create(path: &str) -> Result<(), ErrorStatus> {
83    err_from_u16!(syscreate_file(Str::from_str(path)))
84}
85
86#[inline]
87/// Creates the directory with the path `path`
88///
89/// see [`syscreate_dir`] for underlying syscall
90pub fn createdir(path: &str) -> Result<(), ErrorStatus> {
91    err_from_u16!(syscreate_dir(Str::from_str(path)))
92}
93
94#[inline]
95/// Removes a given path
96///
97/// see [`sysremove_path`] for underlying syscall
98pub fn remove_path(path: &str) -> Result<(), ErrorStatus> {
99    err_from_u16!(sysremove_path(Str::from_str(path)))
100}