Skip to content

Commit

Permalink
feat(error): create new InternalFSError enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Oakchris1955 committed Aug 1, 2024
1 parent 6460079 commit 88a99a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
21 changes: 14 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,27 @@ impl IOErrorKind for std::io::ErrorKind {
}
}

/// An error indicating that a filesystem-related operation has failed
/// An error type that denotes that there is something wrong
/// with the filesystem's structure itself (perhaps the FS itself is malformed/corrupted)
#[derive(Debug, displaydoc::Display)]
pub enum FSError<I>
where
I: IOError,
{
pub enum InternalFSError {
/// The storage medium isn't large enough to accompany a FAT filesystem
StorageTooSmall,
/// Invalid boot sector signature. Perharps this isn't a FAT filesystem?
InvalidBPBSig,
/// Encountered a malformed cluster chain
MalformedClusterChain,
}

/// An error indicating that a filesystem-related operation has failed
#[derive(Debug, displaydoc::Display)]
pub enum FSError<I>
where
I: IOError,
{
/// An internal FS error occured
#[displaydoc("An internal FS error occured: {0}")]
InternalFSError(InternalFSError),
/**
The [PathBuf](`crate::path::PathBuf`) provided is malformed.
Expand All @@ -99,8 +108,6 @@ where
IsADirectory,
/// A file or directory wasn't found
NotFound,
/// Found an unexpected EOF while reading a file
UnexpectedEOF,
/// An IO error occured
#[displaydoc("An IO error occured: {0}")]
IOError(I),
Expand Down
14 changes: 10 additions & 4 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ where
let bytes_read = storage.read(&mut buffer)?;

if bytes_read < mem::size_of::<BootRecord>() {
return Err(FSError::StorageTooSmall);
return Err(FSError::InternalFSError(InternalFSError::StorageTooSmall));
}

let boot_record: BootRecord = unsafe { mem::transmute_copy(&buffer) };
Expand All @@ -876,7 +876,7 @@ where
match fat_type {
FATType::FAT12 | FATType::FAT16 | FATType::FAT32 => {
if unsafe { boot_record.fat.verify_signature() } {
return Err(FSError::InvalidBPBSig);
return Err(FSError::InternalFSError(InternalFSError::InvalidBPBSig));
}
}
FATType::ExFAT => todo!("ExFAT not yet implemented"),
Expand Down Expand Up @@ -1007,7 +1007,9 @@ where
if file.cluster_chain_is_healthy()? {
Ok(file)
} else {
Err(FSError::MalformedClusterChain)
Err(FSError::InternalFSError(
InternalFSError::MalformedClusterChain,
))
}
}
None => Err(FSError::NotFound),
Expand Down Expand Up @@ -1166,7 +1168,11 @@ where
// this cluster chain goes on, follow it
FATEntry::Allocated(next_cluster) => data_cluster = next_cluster,
// any other case (whether a bad, reserved or free cluster) is invalid, consider this cluster chain malformed
_ => return Err(FSError::MalformedClusterChain),
_ => {
return Err(FSError::InternalFSError(
InternalFSError::MalformedClusterChain,
))
}
}
}

Expand Down

0 comments on commit 88a99a3

Please sign in to comment.