Skip to content

Commit 22fc45b

Browse files
committed
Return an error on lock failure.
1 parent 94f2246 commit 22fc45b

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.get());
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.directory_id {
@@ -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.volume_id == 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() {
@@ -600,7 +600,7 @@ where
600600

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

605605
let file_idx = data.get_file_by_id(file)?;
606606
let volume_idx = data.get_volume_by_id(data.open_files[file_idx].volume_id)?;
@@ -647,7 +647,7 @@ where
647647
#[cfg(feature = "log")]
648648
debug!("write(file={:?}, buffer={:x?}", file, buffer);
649649

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

652652
// Clone this so we can touch our other structures. Need to ensure we
653653
// write it back at the end.
@@ -773,7 +773,7 @@ where
773773
/// Close a file with the given raw file handle.
774774
pub fn close_file(&self, file: RawFile) -> Result<(), Error<D::Error>> {
775775
let flush_result = self.flush_file(file);
776-
let mut data = self.data.borrow_mut();
776+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
777777
let file_idx = data.get_file_by_id(file)?;
778778
data.open_files.swap_remove(file_idx);
779779
flush_result
@@ -782,7 +782,7 @@ where
782782
/// Flush (update the entry) for a file with the given raw file handle.
783783
pub fn flush_file(&self, file: RawFile) -> Result<(), Error<D::Error>> {
784784
use core::ops::DerefMut;
785-
let mut data = self.data.borrow_mut();
785+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
786786
let data = data.deref_mut();
787787

788788
let file_id = data.get_file_by_id(file)?;
@@ -825,7 +825,7 @@ where
825825

826826
/// Seek a file with an offset from the start of the file.
827827
pub fn file_seek_from_start(&self, file: RawFile, offset: u32) -> Result<(), Error<D::Error>> {
828-
let mut data = self.data.borrow_mut();
828+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
829829
let file_idx = data.get_file_by_id(file)?;
830830
data.open_files[file_idx]
831831
.seek_from_start(offset)
@@ -839,7 +839,7 @@ where
839839
file: RawFile,
840840
offset: i32,
841841
) -> Result<(), Error<D::Error>> {
842-
let mut data = self.data.borrow_mut();
842+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
843843
let file_idx = data.get_file_by_id(file)?;
844844
data.open_files[file_idx]
845845
.seek_from_current(offset)
@@ -849,7 +849,7 @@ where
849849

850850
/// Seek a file with an offset back from the end of the file.
851851
pub fn file_seek_from_end(&self, file: RawFile, offset: u32) -> Result<(), Error<D::Error>> {
852-
let mut data = self.data.borrow_mut();
852+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
853853
let file_idx = data.get_file_by_id(file)?;
854854
data.open_files[file_idx]
855855
.seek_from_end(offset)
@@ -881,7 +881,7 @@ where
881881
N: ToShortFileName,
882882
{
883883
use core::ops::DerefMut;
884-
let mut data = self.data.borrow_mut();
884+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
885885
let data = data.deref_mut();
886886

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

0 commit comments

Comments
 (0)