Skip to main content

abi_definitions/file_system/
path.rs

1use core::{
2    ffi::c_char,
3    ptr::{self, copy_nonoverlapping},
4};
5use task::block_on;
6
7use crate::{XilaFileSystemStatistics, XilaTaskIdentifier, abi_unsafe_function, parse_c_str};
8
9abi_unsafe_function! {
10    /// This function is used to get statistics for a path.
11    ///
12    /// # Safety
13    ///
14    /// This function is unsafe because it dereferences raw pointers.
15    fn xila_file_system_get_statistics_from_path(
16        path: *const c_char,
17        statistics: *mut XilaFileSystemStatistics,
18    ) -> XilaFileSystemResult {
19        let path = parse_c_str(path)?;
20
21        log::information!("Getting statistics for path {path:?}");
22
23        let result = block_on(virtual_file_system::get_instance().get_statistics(&path))?;
24
25        log::information!("Got statistics for path {path:?}: {result:?}");
26
27        ptr::write(statistics, XilaFileSystemStatistics::from_statistics(result));
28        Ok(())
29    }
30}
31
32abi_unsafe_function! {
33    /// This function is used to convert a path to a resolved path (i.e. a path without symbolic links or relative paths).
34    ///
35    /// # Safety
36    ///
37    /// This function is unsafe because it dereferences raw pointers.
38    fn xila_file_system_resolve_path(
39        path: *const i8,
40        resolved_path: *mut u8,
41        resolved_path_size: usize,
42    ) -> XilaFileSystemResult {
43        // Casting *const i8 to *const c_char so it plays nicely with parse_c_str
44        let path = parse_c_str(path as *const c_char)?;
45
46        // Debug: Resolving path
47
48        // Copy path to resolved path.
49        copy_nonoverlapping(
50            path.as_ptr(),
51            resolved_path,
52            usize::min(resolved_path_size, path.len()),
53        );
54
55        Ok(())
56    }
57}
58
59abi_unsafe_function! {
60    /// This function is used to rename (move) a file.
61    ///
62    /// # Safety
63    ///
64    /// This function is unsafe because it dereferences raw pointers.
65    fn xila_file_system_rename(
66        old_path: *const c_char,
67        new_path: *const c_char,
68    ) -> XilaFileSystemResult {
69        let old_path = parse_c_str(old_path)?;
70        let new_path = parse_c_str(new_path)?;
71
72        // Debug: Renaming files
73
74        block_on(virtual_file_system::get_instance().rename(&old_path, &new_path))?;
75        Ok(())
76    }
77}
78
79abi_unsafe_function! {
80    /// This function is used to remove a file.
81    ///
82    /// # Safety
83    ///
84    /// This function is unsafe because it dereferences raw pointers.
85    fn xila_file_system_remove(
86        task: XilaTaskIdentifier,
87        path: *const c_char,
88    ) -> XilaFileSystemResult {
89        let path = parse_c_str(path)?;
90
91        block_on(virtual_file_system::get_instance().remove(task.into(), path))
92    }
93}