|
| 1 | +use super::TimeSource; |
1 | 2 | use crate::{
|
2 | 3 | filesystem::{ClusterId, DirEntry, SearchId},
|
3 |
| - Error, RawVolume, VolumeManager, |
| 4 | + BlockDevice, Error, RawVolume, VolumeManager, |
4 | 5 | };
|
| 6 | +use embedded_io::{ErrorType, Read, Seek, SeekFrom, Write}; |
5 | 7 |
|
6 | 8 | /// A handle for an open file on disk.
|
7 | 9 | ///
|
@@ -165,6 +167,80 @@ where
|
165 | 167 | }
|
166 | 168 | }
|
167 | 169 |
|
| 170 | +impl< |
| 171 | + D: BlockDevice, |
| 172 | + T: TimeSource, |
| 173 | + const MAX_DIRS: usize, |
| 174 | + const MAX_FILES: usize, |
| 175 | + const MAX_VOLUMES: usize, |
| 176 | + > ErrorType for File<'_, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES> |
| 177 | +{ |
| 178 | + type Error = crate::Error<D::Error>; |
| 179 | +} |
| 180 | + |
| 181 | +impl< |
| 182 | + D: BlockDevice, |
| 183 | + T: TimeSource, |
| 184 | + const MAX_DIRS: usize, |
| 185 | + const MAX_FILES: usize, |
| 186 | + const MAX_VOLUMES: usize, |
| 187 | + > Read for File<'_, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES> |
| 188 | +{ |
| 189 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 190 | + if buf.is_empty() { |
| 191 | + Ok(0) |
| 192 | + } else { |
| 193 | + self.read(buf) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +impl< |
| 199 | + D: BlockDevice, |
| 200 | + T: TimeSource, |
| 201 | + const MAX_DIRS: usize, |
| 202 | + const MAX_FILES: usize, |
| 203 | + const MAX_VOLUMES: usize, |
| 204 | + > Write for File<'_, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES> |
| 205 | +{ |
| 206 | + fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 207 | + if buf.is_empty() { |
| 208 | + Ok(0) |
| 209 | + } else { |
| 210 | + self.write(buf)?; |
| 211 | + Ok(buf.len()) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + fn flush(&mut self) -> Result<(), Self::Error> { |
| 216 | + self.flush() |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +impl< |
| 221 | + D: BlockDevice, |
| 222 | + T: TimeSource, |
| 223 | + const MAX_DIRS: usize, |
| 224 | + const MAX_FILES: usize, |
| 225 | + const MAX_VOLUMES: usize, |
| 226 | + > Seek for File<'_, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES> |
| 227 | +{ |
| 228 | + fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error> { |
| 229 | + match pos { |
| 230 | + SeekFrom::Start(offset) => { |
| 231 | + self.seek_from_start(offset.try_into().map_err(|_| Error::InvalidOffset)?)? |
| 232 | + } |
| 233 | + SeekFrom::End(offset) => { |
| 234 | + self.seek_from_end((-offset).try_into().map_err(|_| Error::InvalidOffset)?)? |
| 235 | + } |
| 236 | + SeekFrom::Current(offset) => { |
| 237 | + self.seek_from_current(offset.try_into().map_err(|_| Error::InvalidOffset)?)? |
| 238 | + } |
| 239 | + } |
| 240 | + Ok(self.offset().into()) |
| 241 | + } |
| 242 | +} |
| 243 | + |
168 | 244 | #[cfg(feature = "defmt-log")]
|
169 | 245 | impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize>
|
170 | 246 | defmt::Format for File<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
|
|
0 commit comments