Skip to content

Commit e9ec423

Browse files
committed
Return an error on lock failure.
1 parent be37965 commit e9ec423

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
@@ -136,7 +136,7 @@ where
136136
const PARTITION_INFO_LBA_START_INDEX: usize = 8;
137137
const PARTITION_INFO_NUM_BLOCKS_INDEX: usize = 12;
138138

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

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

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

225225
let directory_id = RawDirectory(data.id_generator.generate());
226226
let dir_info = DirectoryInfo {
@@ -249,7 +249,7 @@ where
249249
where
250250
N: ToShortFileName,
251251
{
252-
let mut data = self.data.borrow_mut();
252+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
253253

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

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

332332
for f in data.open_files.iter() {
333333
if f.raw_volume == volume {
@@ -395,7 +395,7 @@ where
395395
where
396396
N: ToShortFileName,
397397
{
398-
let mut data = self.data.borrow_mut();
398+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
399399

400400
// This check is load-bearing - we do an unchecked push later.
401401
if data.open_files.is_full() {
@@ -627,7 +627,7 @@ where
627627

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

632632
let file_idx = data.get_file_by_id(file)?;
633633
let volume_idx = data.get_volume_by_id(data.open_files[file_idx].raw_volume)?;
@@ -676,7 +676,7 @@ where
676676
#[cfg(feature = "log")]
677677
debug!("write(file={:?}, buffer={:x?}", file, buffer);
678678

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

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

817817
let file_id = data.get_file_by_id(file)?;
@@ -854,7 +854,7 @@ where
854854

855855
/// Seek a file with an offset from the start of the file.
856856
pub fn file_seek_from_start(&self, file: RawFile, offset: u32) -> Result<(), Error<D::Error>> {
857-
let mut data = self.data.borrow_mut();
857+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
858858
let file_idx = data.get_file_by_id(file)?;
859859
data.open_files[file_idx]
860860
.seek_from_start(offset)
@@ -868,7 +868,7 @@ where
868868
file: RawFile,
869869
offset: i32,
870870
) -> Result<(), Error<D::Error>> {
871-
let mut data = self.data.borrow_mut();
871+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
872872
let file_idx = data.get_file_by_id(file)?;
873873
data.open_files[file_idx]
874874
.seek_from_current(offset)
@@ -878,7 +878,7 @@ where
878878

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

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

0 commit comments

Comments
 (0)