Skip to content

Commit 0abc8ef

Browse files
committed
Return an error on lock failure.
1 parent 1082705 commit 0abc8ef

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() {
@@ -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].raw_volume)?;
@@ -648,7 +648,7 @@ where
648648
#[cfg(feature = "log")]
649649
debug!("write(file={:?}, buffer={:x?}", file, buffer);
650650

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

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

789789
let file_id = data.get_file_by_id(file)?;
@@ -826,7 +826,7 @@ where
826826

827827
/// Seek a file with an offset from the start of the file.
828828
pub fn file_seek_from_start(&self, file: RawFile, offset: u32) -> Result<(), Error<D::Error>> {
829-
let mut data = self.data.borrow_mut();
829+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
830830
let file_idx = data.get_file_by_id(file)?;
831831
data.open_files[file_idx]
832832
.seek_from_start(offset)
@@ -840,7 +840,7 @@ where
840840
file: RawFile,
841841
offset: i32,
842842
) -> Result<(), Error<D::Error>> {
843-
let mut data = self.data.borrow_mut();
843+
let mut data = self.data.try_borrow_mut().map_err(|_| Error::LockError)?;
844844
let file_idx = data.get_file_by_id(file)?;
845845
data.open_files[file_idx]
846846
.seek_from_current(offset)
@@ -850,7 +850,7 @@ where
850850

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

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

0 commit comments

Comments
 (0)