Made DirEntryReader return an Option when reading from bytes, instead of the caller checking for a specific error.

Improved documentation for `drv`.
This commit is contained in:
Filipe Rodrigues 2021-01-25 04:41:33 +00:00
parent 1d6be27feb
commit 21480ebb0e
5 changed files with 44 additions and 11 deletions

View File

@ -2,7 +2,11 @@
# Layout
The `.DRV` filesystem begins with the root directory.
From there all other directories and files are situated, always sector (2048) aligned.
The `.DRV` filesystem splits the file into sectors of `0x800` bytes, dedicating each one
to either a file or directory.
Both files and directories may only begin at the start of each sector, but may span various
sectors.
See the [`dir`] documentation for more information on the directory structure.
The filesystem proper begins with a directory sector, called the 'root' directory.
See the [`dir`] documentation for more information on how each directory works.

12
dcb-io/src/drv/dir.md Normal file
View File

@ -0,0 +1,12 @@
# Directory
# Layout
Each directory is a contiguous array of [`entry`]'s.
See it's module for details on each entry
# Limitations
Currently it is not known if directories must appear before files, but the original
files all do this.
The current implementations does _not_ ensure any order.

View File

@ -1,4 +1,4 @@
//! Directory
#![doc(include = "dir.md")]
// Modules
pub mod entry;
@ -58,10 +58,7 @@ impl DirReader {
}
// And parse it
match DirEntryReader::from_bytes(&entry_bytes) {
Err(entry::FromBytesError::InvalidKind(0)) => None,
res => Some(res.map_err(ReadEntryError::ParseEntry)),
}
DirEntryReader::from_bytes(&entry_bytes).map_err(ReadEntryError::ParseEntry).transpose()
});
Ok(iter)

View File

@ -0,0 +1,19 @@
# Directory entry
# Layout
Each directory entry has the following layout:
| Offset | Size | Type | Name |
| ------ | ---- | ------------ | ---------------------------------------------------------------------- |
| 0x0 | 0x1 | `Kind` | Entry kind, `0x0` for none, `0x1` for files and `0x80` for directories |
| 0x1 | 0x3 | `[u8; 3]` | (Files only) File extension |
| 0x4 | 0x4 | `u32` | Entry sector position |
| 0x8 | 0x4 | `u32` | (Files only) File size in bytes |
| 0xc | 0x4 | `u32` | Entry date (seconds since epoch) |
| 0x10 | 0x10 | `[u8; 0x10]` | Entry name |
# Limitations
The max size for a file is `u32::MAX` bytes, but the filesystem itself can hold `2^43` bytes in total, as each
directory and file position is done by sector.

View File

@ -1,4 +1,4 @@
//! Directory entry
#![doc(include = "entry.md")]
// Modules
pub mod error;
@ -39,7 +39,7 @@ pub struct DirEntryReader {
impl DirEntryReader {
/// Reads a directory entry reader from bytes
pub fn from_bytes(bytes: &[u8; 0x20]) -> Result<Self, FromBytesError> {
pub fn from_bytes(bytes: &[u8; 0x20]) -> Result<Option<Self>, FromBytesError> {
let bytes = array_split!(bytes,
kind : 0x1,
extension : [0x3],
@ -53,6 +53,7 @@ impl DirEntryReader {
// Check kind
let kind = match bytes.kind {
0x0 => return Ok(None),
0x1 => {
let mut extension = AsciiStrArr::from_bytes(bytes.extension).map_err(FromBytesError::Extension)?;
extension.trim_end(AsciiChar::Null);
@ -79,7 +80,7 @@ impl DirEntryReader {
name.trim_end(AsciiChar::Null);
let date = NaiveDateTime::from_timestamp(i64::from(LittleEndian::read_u32(bytes.data)), 0);
Ok(Self { name, date, kind })
Ok(Some(Self { name, date, kind }))
}
/// Returns this entry's name