Skip to content

Commit cc602d5

Browse files
committed
Return an error on lock failure.
1 parent 2655b00 commit cc602d5

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ where
202202
DiskFull,
203203
/// A directory with that name already exists
204204
DirAlreadyExists,
205+
/// The filesystem tried to gain a lock whilst already locked.
206+
///
207+
/// This is a bug in the filesystem. Please open an issue.
208+
LockError,
205209
}
206210

207211
impl<E: Debug> embedded_io::Error for Error<E> {
@@ -216,7 +220,8 @@ impl<E: Debug> embedded_io::Error for Error<E> {
216220
| Error::EndOfFile
217221
| Error::DiskFull
218222
| Error::NotEnoughSpace
219-
| Error::AllocationError => ErrorKind::Other,
223+
| Error::AllocationError
224+
| Error::LockError => ErrorKind::Other,
220225
Error::NoSuchVolume
221226
| Error::FilenameError(_)
222227
| Error::BadHandle

src/volume_mgr.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ where
135135
const PARTITION_INFO_LBA_START_INDEX: usize = 8;
136136
const PARTITION_INFO_NUM_BLOCKS_INDEX: usize = 12;
137137

138-
let mut data = self.data.borrow_mut();
138+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
139139

140140
if data.open_volumes.is_full() {
141141
return Err(Error::TooManyOpenVolumes);
@@ -218,7 +218,7 @@ where
218218
pub fn open_root_dir(&self, volume: RawVolume) -> Result<RawDirectory, Error<D::Error>> {
219219
// Opening a root directory twice is OK
220220

221-
let mut data = self.data.borrow_mut();
221+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
222222

223223
let directory_id = RawDirectory(data.id_generator.generate());
224224
let dir_info = DirectoryInfo {
@@ -247,7 +247,7 @@ where
247247
where
248248
N: ToShortFileName,
249249
{
250-
let mut data = self.data.borrow_mut();
250+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
251251

252252
if data.open_dirs.is_full() {
253253
return Err(Error::TooManyOpenDirs);
@@ -310,7 +310,7 @@ where
310310
/// Close a directory. You cannot perform operations on an open directory
311311
/// and so must close it if you want to do something with it.
312312
pub fn close_dir(&self, directory: RawDirectory) -> Result<(), Error<D::Error>> {
313-
let mut data = self.data.borrow_mut();
313+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
314314

315315
for (idx, info) in data.open_dirs.iter().enumerate() {
316316
if directory == info.raw_directory {
@@ -325,7 +325,7 @@ where
325325
///
326326
/// You can't close it if there are any files or directories open on it.
327327
pub fn close_volume(&self, volume: RawVolume) -> Result<(), Error<D::Error>> {
328-
let mut data = self.data.borrow_mut();
328+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
329329

330330
for f in data.open_files.iter() {
331331
if f.raw_volume == volume {
@@ -393,7 +393,7 @@ where
393393
where
394394
N: ToShortFileName,
395395
{
396-
let mut data = self.data.borrow_mut();
396+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
397397

398398
// This check is load-bearing - we do an unchecked push later.
399399
if data.open_files.is_full() {
@@ -625,7 +625,7 @@ where
625625

626626
/// Read from an open file.
627627
pub fn read(&self, file: RawFile, buffer: &mut [u8]) -> Result<usize, Error<D::Error>> {
628-
let mut data = self.data.borrow_mut();
628+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
629629

630630
let file_idx = data.get_file_by_id(file)?;
631631
let volume_idx = data.get_volume_by_id(data.open_files[file_idx].raw_volume)?;
@@ -673,7 +673,7 @@ where
673673
#[cfg(feature = "log")]
674674
debug!("write(file={:?}, buffer={:x?}", file, buffer);
675675

676-
let mut data = self.data.borrow_mut();
676+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
677677

678678
// Clone this so we can touch our other structures. Need to ensure we
679679
// write it back at the end.
@@ -799,7 +799,7 @@ where
799799
/// Close a file with the given raw file handle.
800800
pub fn close_file(&self, file: RawFile) -> Result<(), Error<D::Error>> {
801801
let flush_result = self.flush_file(file);
802-
let mut data = self.data.borrow_mut();
802+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
803803
let file_idx = data.get_file_by_id(file)?;
804804
data.open_files.swap_remove(file_idx);
805805
flush_result
@@ -808,7 +808,7 @@ where
808808
/// Flush (update the entry) for a file with the given raw file handle.
809809
pub fn flush_file(&self, file: RawFile) -> Result<(), Error<D::Error>> {
810810
use core::ops::DerefMut;
811-
let mut data = self.data.borrow_mut();
811+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
812812
let data = data.deref_mut();
813813

814814
let file_id = data.get_file_by_id(file)?;
@@ -851,7 +851,7 @@ where
851851

852852
/// Seek a file with an offset from the start of the file.
853853
pub fn file_seek_from_start(&self, file: RawFile, offset: u32) -> Result<(), Error<D::Error>> {
854-
let mut data = self.data.borrow_mut();
854+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
855855
let file_idx = data.get_file_by_id(file)?;
856856
data.open_files[file_idx]
857857
.seek_from_start(offset)
@@ -865,7 +865,7 @@ where
865865
file: RawFile,
866866
offset: i32,
867867
) -> Result<(), Error<D::Error>> {
868-
let mut data = self.data.borrow_mut();
868+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
869869
let file_idx = data.get_file_by_id(file)?;
870870
data.open_files[file_idx]
871871
.seek_from_current(offset)
@@ -875,7 +875,7 @@ where
875875

876876
/// Seek a file with an offset back from the end of the file.
877877
pub fn file_seek_from_end(&self, file: RawFile, offset: u32) -> Result<(), Error<D::Error>> {
878-
let mut data = self.data.borrow_mut();
878+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
879879
let file_idx = data.get_file_by_id(file)?;
880880
data.open_files[file_idx]
881881
.seek_from_end(offset)
@@ -907,7 +907,7 @@ where
907907
N: ToShortFileName,
908908
{
909909
use core::ops::DerefMut;
910-
let mut data = self.data.borrow_mut();
910+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
911911
let data = data.deref_mut();
912912

913913
// This check is load-bearing - we do an unchecked push later.

0 commit comments

Comments
 (0)