Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Experimental Filesystem version using closures.

use core::ffi::{c_int, c_void};
use core::marker::PhantomData;
use core::ptr::addr_of;
use core::ptr::addr_of_mut;
use core::{
Expand Down Expand Up @@ -72,6 +73,19 @@ pub struct Allocation<Storage: driver::Storage> {
state: ll::lfs_t,
}

/// # Safety
///
/// All operations are done on `&mut Allocation, and the reference is held
/// during the entire lifetime of the filesystem, so once the reference is
/// available again, the filesystem is closed
unsafe impl<Storage: driver::Storage> Sync for Allocation<Storage> {}
/// # Safety
///
/// All operations are done on `&mut Allocation, and the reference is held
/// during the entire lifetime of the filesystem, so once the reference is
/// available again, the filesystem is closed
unsafe impl<Storage: driver::Storage> Send for Allocation<Storage> {}

// pub fn check_storage_requirements(

impl<Storage: driver::Storage> Default for Allocation<Storage> {
Expand Down Expand Up @@ -613,6 +627,7 @@ pub struct File<'a, 'b, S: driver::Storage> {
// We must store a raw pointer here since the FFI retains a copy of a pointer
// to the field alloc.state, so we cannot assert unique mutable access.
alloc: RefCell<*mut FileAllocation<S>>,
phantom: PhantomData<RefCell<&'b mut FileAllocation<S>>>,
fs: &'b Filesystem<'a, S>,
}

Expand Down Expand Up @@ -803,7 +818,7 @@ impl OpenOptions {
pub unsafe fn open<'a, 'b, S: driver::Storage>(
&self,
fs: &'b Filesystem<'a, S>,
alloc: &mut FileAllocation<S>,
alloc: &'b mut FileAllocation<S>,
path: &Path,
) -> Result<File<'a, 'b, S>> {
alloc.config.buffer = alloc.cache.get() as *mut _;
Expand All @@ -820,6 +835,7 @@ impl OpenOptions {

let file = File {
alloc: RefCell::new(alloc),
phantom: PhantomData,
fs,
};

Expand Down Expand Up @@ -1544,4 +1560,21 @@ mod tests {
})
.unwrap();
}

#[allow(unreachable_code)]
fn _filesystem_is_sync() {
fn assert_is_sync<T: Sync, R>(_: &T) -> R {
todo!()
}
fn assert_is_send<T: Send, R>(_: &T) -> R {
todo!()
}

assert_is_sync::<Allocation<TestStorage>, ()>(todo!());
assert_is_send::<&mut Allocation<TestStorage>, ()>(todo!());
assert_is_send::<RefCell<&mut Allocation<TestStorage>>, ()>(todo!());

let mut test_storage = TestStorage::new();
Filesystem::mount_and_then(&mut test_storage, |fs| assert_is_send(fs)).unwrap()
}
}