file_system/operations/
mount.rs

1use core::ops::{Deref, DerefMut};
2
3use alloc::boxed::Box;
4use synchronization::{
5    blocking_mutex::raw::RawMutex,
6    mutex::{Mutex, MutexGuard},
7};
8
9use crate::{Error, Result};
10
11pub trait MountOperations {
12    fn mount(&self) -> Result<()> {
13        Ok(())
14    }
15
16    fn unmount(&self) -> Result<()> {
17        Err(Error::UnsupportedOperation)
18    }
19}
20pub struct MountWrapper<T>(Option<Box<T>>);
21
22impl<T> Default for MountWrapper<T> {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl<T> MountWrapper<T> {
29    pub const fn new() -> Self {
30        Self(None)
31    }
32
33    pub fn mount(&mut self, inner: T) -> Result<()> {
34        if self.is_mounted() {
35            return Err(Error::AlreadyMounted);
36        }
37        self.0 = Some(Box::new(inner));
38
39        Ok(())
40    }
41
42    pub fn unmount(&mut self) -> Result<()> {
43        if !self.is_mounted() {
44            return Err(Error::NotMounted);
45        }
46
47        self.0 = None;
48        Ok(())
49    }
50
51    pub const fn is_mounted(&self) -> bool {
52        self.0.is_some()
53    }
54
55    pub fn try_get_mutable(&mut self) -> Result<&mut T> {
56        match &mut self.0 {
57            Some(inner) => Ok(inner.as_mut()),
58            None => Err(Error::NotMounted),
59        }
60    }
61
62    pub fn try_get(&self) -> Result<&T> {
63        match &self.0 {
64            Some(inner) => Ok(inner.as_ref()),
65            None => Err(Error::NotMounted),
66        }
67    }
68}
69
70pub struct MutexMountWrapperGuard<'a, M: RawMutex, T> {
71    _guard: MutexGuard<'a, M, MountWrapper<T>>,
72    inner: &'a mut T,
73}
74
75impl<M: RawMutex, T> Deref for MutexMountWrapperGuard<'_, M, T> {
76    type Target = T;
77
78    fn deref(&self) -> &Self::Target {
79        self.inner
80    }
81}
82
83impl<M: RawMutex, T> DerefMut for MutexMountWrapperGuard<'_, M, T> {
84    fn deref_mut(&mut self) -> &mut Self::Target {
85        self.inner
86    }
87}
88
89pub struct MutexMountWrapper<M: RawMutex, T>(Mutex<M, MountWrapper<T>>);
90
91impl<M: RawMutex, T> Default for MutexMountWrapper<M, T> {
92    fn default() -> Self {
93        Self::new()
94    }
95}
96
97impl<M: RawMutex, T> MutexMountWrapper<M, T> {
98    pub const fn new() -> Self {
99        Self(Mutex::new(MountWrapper::new()))
100    }
101
102    pub fn new_mounted(inner: T) -> Self {
103        Self(Mutex::new({
104            let mut wrapper = MountWrapper::new();
105            wrapper.mount(inner).unwrap();
106            wrapper
107        }))
108    }
109
110    pub fn mount(&self, inner: T) -> Result<()> {
111        let mut guard = self.0.try_lock().map_err(|_| Error::RessourceBusy)?;
112        guard.mount(inner)
113    }
114
115    pub fn unmount(&self) -> Result<()> {
116        let mut guard = self.0.try_lock().map_err(|_| Error::RessourceBusy)?;
117        guard.unmount()
118    }
119
120    pub fn is_mounted(&self) -> Result<bool> {
121        let guard = self.0.try_lock().map_err(|_| Error::RessourceBusy)?;
122        Ok(guard.is_mounted())
123    }
124
125    pub fn try_get(&self) -> Result<MutexMountWrapperGuard<'_, M, T>> {
126        let mut guard = self.0.try_lock().map_err(|_| Error::RessourceBusy)?;
127
128        // Verify mount before creating the guard
129        if !guard.is_mounted() {
130            return Err(Error::NotMounted);
131        }
132
133        let inner_ptr = guard.try_get_mutable()? as *mut T;
134
135        Ok(MutexMountWrapperGuard {
136            _guard: guard,
137            inner: unsafe { &mut *inner_ptr },
138        })
139    }
140}