safa_api/syscalls/
io.rs

1use safa_abi::{
2    errors::ErrorStatus,
3    raw::io::{DirEntry, FileAttr},
4};
5
6use crate::syscalls::types::{OptionalPtrMut, RequiredPtr, RequiredPtrMut, Ri};
7
8use super::{define_syscall, err_from_u16, SyscallNum};
9
10#[cfg(not(feature = "rustc-dep-of-std"))]
11extern crate alloc;
12
13// Directory Iterator related syscalls
14define_syscall! {
15    SyscallNum::SysDirIterOpen =>
16    {
17        /// Opens a directory iterator for the directory with the resource id `dir_ri`
18        sysdiriter_open(dir_ri: Ri, dest_ri: RequiredPtrMut<Ri>)
19    },
20    SyscallNum::SysDirIterNext => {
21        /// Gets the next directory entry from a directory iterator,
22        ///
23        /// puts the results in `dest_direntry`,
24        ///
25        /// puts zeroed DirEntry in `dest_direntry` if there are no more entries
26        ///
27        /// returns [`ErrorStatus::Generic`] (1) if there are no more entries
28        sysdiriter_next(dir_ri: Ri, dest_direntry: OptionalPtrMut<DirEntry>)
29    }
30}
31
32#[inline]
33/// Opens a directory iterator for the directory with the resource id `dir_ri`,
34/// returns the resource id of the directory iterator
35///
36/// see [`sysdiriter_open`] for underlying syscall
37pub fn diriter_open(dir_ri: Ri) -> Result<Ri, ErrorStatus> {
38    let mut dest_fd: usize = 0xAAAAAAAAAAAAAAAAusize;
39    err_from_u16!(sysdiriter_open(dir_ri, &raw mut dest_fd), dest_fd)
40}
41
42#[inline]
43/// Gets the next directory entry from a directory iterator,
44///
45/// see [`sysdiriter_next`] for underlying syscall
46pub fn diriter_next(dir_ri: Ri) -> Result<DirEntry, ErrorStatus> {
47    let mut dest_direntry: DirEntry = unsafe { core::mem::zeroed() };
48    err_from_u16!(
49        sysdiriter_next(dir_ri, &raw mut dest_direntry),
50        dest_direntry
51    )
52}
53
54// File related syscalls
55define_syscall! {
56    SyscallNum::SysWrite => {
57        /// Writes `len` bytes from `buf` to the file with the resource id `fd` at offset `offset`
58        ///
59        /// if `dest_wrote` is not null, it will be set to the number of bytes written
60        syswrite(fd: Ri, offset: isize, buf: RequiredPtr<u8>, len: usize, dest_wrote: OptionalPtrMut<usize>)
61    },
62    SyscallNum::SysTruncate => {
63        /// Truncates the file with the resource id `fd` to `len` bytes
64        systruncate(fd: Ri, len: usize)
65    },
66    SyscallNum::SysFSize => {
67        /// Gets the size of the file with the resource id `fd` and puts it in `dest_size`
68        sysfsize(fd: Ri, dest_size: OptionalPtrMut<usize>)
69    },
70    SyscallNum::SysFAttrs => {
71        /// Gets the file attributes of the file with the resource id `fd` and puts them in `dest_attrs`
72        sysfattrs(fd: Ri, dest_attrs: OptionalPtrMut<FileAttr>)
73    },
74    SyscallNum::SysRead => {
75        /// Reads `len` bytes from the file with the resource id `fd` at offset `offset` into `buf`
76        ///
77        /// if `dest_read` is not null, it will be set to the number of bytes read
78        sysread(fd: Ri, offset: isize, buf: RequiredPtrMut<u8>, len: usize, dest_read: OptionalPtrMut<usize>)
79    },
80    SyscallNum::SysSync => {
81        /// Syncs the resource with the resource id `fd`
82        syssync(ri: Ri)
83    },
84}
85
86#[inline]
87/// Writes `buf.len()` bytes from `buf` to the file with the resource id `fd` at offset `offset`
88/// and returns the number of bytes written
89pub fn write(fd: Ri, offset: isize, buf: &[u8]) -> Result<usize, ErrorStatus> {
90    let mut dest_wrote = 0;
91    err_from_u16!(
92        syswrite(fd, offset, buf.as_ptr(), buf.len(), &mut dest_wrote),
93        dest_wrote
94    )
95}
96
97#[inline]
98/// Truncates the file with the resource id `fd` to `len` bytes
99pub fn truncate(fd: Ri, len: usize) -> Result<(), ErrorStatus> {
100    err_from_u16!(systruncate(fd, len))
101}
102
103#[inline]
104/// Gets the size of the file with the resource id `fd`
105pub fn fsize(fd: Ri) -> Result<usize, ErrorStatus> {
106    let mut dest_size = 0;
107    err_from_u16!(sysfsize(fd, &raw mut dest_size), dest_size)
108}
109
110#[inline]
111/// Gets the file attributes of the file with the resource id `fd`
112pub fn fattrs(fd: Ri) -> Result<FileAttr, ErrorStatus> {
113    let mut attrs: FileAttr = unsafe { core::mem::zeroed() };
114    err_from_u16!(sysfattrs(fd, &raw mut attrs), attrs)
115}
116
117#[inline]
118/// Reads `buf.len()` bytes from the file with the resource id `fd` at offset `offset` into `buf`
119pub fn read(fd: Ri, offset: isize, buf: &mut [u8]) -> Result<Ri, ErrorStatus> {
120    let mut dest_read = 0;
121    err_from_u16!(
122        sysread(fd, offset, buf.as_mut_ptr(), buf.len(), &mut dest_read),
123        dest_read
124    )
125}
126
127#[inline]
128/// Syncs the resource with the resource id `ri`
129pub fn sync(ri: Ri) -> Result<(), ErrorStatus> {
130    err_from_u16!(syssync(ri))
131}