1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::LibUserResult;
use sunrise_libuser::error::{Error, FileSystemError};
use storage_device::{BlockDevice, StorageDevice};
use storage_device::storage_device::StorageBlockDevice;
use crate::interface::filesystem::FileOperations;
use alloc::sync::Arc;
use alloc::boxed::Box;
use spin::Mutex;
use core::fmt::Debug;
pub trait IStorage : StorageDevice + Debug + Sync + Send {
fn set_size(&mut self, new_size: u64) -> LibUserResult<()>;
}
#[derive(Debug)]
pub struct PartitionStorage {
inner: Arc<Mutex<Box<dyn IStorage<Error = Error>>>>,
partition_start: u64,
partition_len: u64
}
impl PartitionStorage {
pub fn new(inner: Arc<Mutex<Box<dyn IStorage<Error = Error>>>>, partition_start: u64, partition_len: u64) -> Self {
PartitionStorage { inner, partition_start, partition_len}
}
}
impl StorageDevice for PartitionStorage {
type Error = Error;
fn read(&mut self, offset: u64, buf: &mut [u8]) -> Result<(), Error> {
if offset + buf.len() as u64 > self.len()? {
return Err(FileSystemError::OutOfRange.into())
}
self.inner.lock().read(self.partition_start + offset, buf)
}
fn write(&mut self, offset: u64, buf: &[u8]) -> Result<(), Error> {
if offset + buf.len() as u64 > self.len()? {
return Err(FileSystemError::OutOfRange.into())
}
self.inner.lock().write(self.partition_start + offset, buf)
}
fn flush(&mut self) -> LibUserResult<()> {
self.inner.lock().flush()
}
fn len(&mut self) -> Result<u64, Error> {
Ok(self.partition_len)
}
}
impl IStorage for PartitionStorage {
fn set_size(&mut self, _new_size: u64) -> LibUserResult<()> {
Err(FileSystemError::UnsupportedOperation.into())
}
}
#[repr(transparent)]
#[derive(Debug)]
pub struct FileStorage<F: FileOperations>(F);
impl<F: FileOperations> FileStorage<F> {
pub fn new(f: F) -> FileStorage<F> {
FileStorage(f)
}
}
impl<F: FileOperations> StorageDevice for FileStorage<F> {
type Error = Error;
fn read(&mut self, offset: u64, mut buf: &mut [u8]) -> LibUserResult<()> {
while !buf.is_empty() {
let data_read = FileOperations::read(&mut self.0, offset, buf)?;
if data_read == 0 {
return Err(FileSystemError::OutOfRange.into());
}
buf = &mut buf[data_read as usize..];
}
Ok(())
}
fn write(&mut self, offset: u64, buf: &[u8]) -> LibUserResult<()> {
FileOperations::write(&mut self.0, offset, buf)
}
fn flush(&mut self) -> LibUserResult<()> {
FileOperations::flush(&mut self.0)
}
fn len(&mut self) -> Result<u64, Error> {
FileOperations::get_len(&mut self.0)
}
}
impl<F: FileOperations> IStorage for FileStorage<F> {
fn set_size(&mut self, new_size: u64) -> LibUserResult<()> {
FileOperations::set_len(&mut self.0, new_size)
}
}
impl<B> IStorage for StorageBlockDevice<B>
where
B: BlockDevice<Error = Error> + Send + Sync,
B::Block: Send + Sync
{
fn set_size(&mut self, _new_size: u64) -> Result<(), B::Error> {
Err(FileSystemError::ReadOnlyFileSystem.into())
}
}