Skip to content

Commit 557c00e

Browse files
committed
chore: use FsResult<T = ()> instead of Result
1 parent a0e1f31 commit 557c00e

File tree

13 files changed

+53
-51
lines changed

13 files changed

+53
-51
lines changed

src/0x06/pkg/kernel/src/drivers/ata/bus.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl AtaBus {
131131
/// Writes the given command
132132
///
133133
/// reference: https://wiki.osdev.org/ATA_PIO_Mode#28_bit_PIO
134-
fn write_command(&mut self, drive: u8, block: u32, cmd: AtaCommand) -> storage::Result<()> {
134+
fn write_command(&mut self, drive: u8, block: u32, cmd: AtaCommand) -> storage::FsResult {
135135
let bytes = block.to_le_bytes(); // a trick to convert u32 to [u8; 4]
136136
unsafe {
137137
// just 1 sector for current implementation
@@ -164,7 +164,7 @@ impl AtaBus {
164164
/// Identifies the drive at the given `drive` number (0 or 1).
165165
///
166166
/// reference: https://wiki.osdev.org/ATA_PIO_Mode#IDENTIFY_command
167-
pub(super) fn identify_drive(&mut self, drive: u8) -> storage::Result<AtaDeviceType> {
167+
pub(super) fn identify_drive(&mut self, drive: u8) -> storage::FsResult<AtaDeviceType> {
168168
info!("Identifying drive {}", drive);
169169

170170
// FIXME: use `AtaCommand::IdentifyDevice` to identify the drive
@@ -194,7 +194,7 @@ impl AtaBus {
194194
drive: u8,
195195
block: u32,
196196
buf: &mut [u8],
197-
) -> storage::Result<()> {
197+
) -> storage::FsResult {
198198
self.write_command(drive, block, AtaCommand::ReadPio)?;
199199

200200
// FIXME: read the data from the data port into the buffer
@@ -215,7 +215,7 @@ impl AtaBus {
215215
///
216216
/// reference: https://wiki.osdev.org/ATA_PIO_Mode#28_bit_PIO
217217
/// reference: https://wiki.osdev.org/IDE#Read.2FWrite_From_ATA_Drive
218-
pub(super) fn write_pio(&mut self, drive: u8, block: u32, buf: &[u8]) -> storage::Result<()> {
218+
pub(super) fn write_pio(&mut self, drive: u8, block: u32, buf: &[u8]) -> storage::FsResult {
219219
self.write_command(drive, block, AtaCommand::WritePio)?;
220220

221221
// FIXME: write the data from the buffer into the data port

src/0x06/pkg/kernel/src/drivers/ata/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ impl core::fmt::Display for AtaDrive {
7878
use storage::{Block512, BlockDevice};
7979

8080
impl BlockDevice<Block512> for AtaDrive {
81-
fn block_count(&self) -> storage::Result<usize> {
81+
fn block_count(&self) -> storage::FsResult<usize> {
8282
// FIXME: return the block count
8383
todo!()
8484
}
8585

86-
fn read_block(&self, offset: usize, block: &mut Block512) -> storage::Result<()> {
86+
fn read_block(&self, offset: usize, block: &mut Block512) -> storage::FsResult {
8787
// FIXME: read the block
8888
// - use `BUSES` and `self` to get bus
8989
// - use `read_pio` to get data
9090
todo!()
9191
}
9292

93-
fn write_block(&self, offset: usize, block: &Block512) -> storage::Result<()> {
93+
fn write_block(&self, offset: usize, block: &Block512) -> storage::FsResult {
9494
// FIXME: write the block
9595
// - use `BUSES` and `self` to get bus
9696
// - use `write_pio` to write data

src/0x06/pkg/storage/src/common/device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ where
66
B: BlockTrait,
77
{
88
/// Returns the number of blocks in the device
9-
fn block_count(&self) -> Result<usize>;
9+
fn block_count(&self) -> FsResult<usize>;
1010

1111
/// Reads a block from the device into the provided buffer
12-
fn read_block(&self, offset: usize, block: &mut B) -> Result<()>;
12+
fn read_block(&self, offset: usize, block: &mut B) -> FsResult;
1313

1414
/// Writes a block to the device from the provided buffer
15-
fn write_block(&self, offset: usize, block: &B) -> Result<()>;
15+
fn write_block(&self, offset: usize, block: &B) -> FsResult;
1616

1717
/// Returns the block size of the device
1818
fn block_size(&self) -> usize {

src/0x06/pkg/storage/src/common/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::*;
22

3-
pub type Result<T> = core::result::Result<T, FsError>;
3+
pub type FsResult<T = ()> = core::result::Result<T, FsError>;
44

55
#[derive(Debug, Clone, Eq, PartialEq)]
66
pub enum FsError {

src/0x06/pkg/storage/src/common/filesystem.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,53 @@ use core::fmt::Debug;
66
/// File system trait
77
pub trait FileSystem: Debug + Sync + Send {
88
/// Iterates over all direct children of this directory path
9-
fn read_dir(&self, path: &str) -> Result<Box<dyn Iterator<Item = Metadata> + Send>>;
9+
fn read_dir(&self, path: &str) -> FsResult<Box<dyn Iterator<Item = Metadata> + Send>>;
1010

1111
/// Opens the file at this path for reading
12-
fn open_file(&self, path: &str) -> Result<FileHandle>;
12+
fn open_file(&self, path: &str) -> FsResult<FileHandle>;
1313

1414
/// Returns the file metadata for the file at this path
15-
fn metadata(&self, path: &str) -> Result<Metadata>;
15+
fn metadata(&self, path: &str) -> FsResult<Metadata>;
1616

1717
/// Returns true if a file or directory at path exists, false otherwise
18-
fn exists(&self, path: &str) -> Result<bool>;
18+
fn exists(&self, path: &str) -> FsResult<bool>;
1919

2020
// ----------------------------------------------------
2121
// NOTE: following functions are not implemented (optional)
2222
// ----------------------------------------------------
2323

2424
/// Creates a file at this path for writing
25-
fn create_file(&self, _path: &str) -> Result<FileHandle> {
25+
fn create_file(&self, _path: &str) -> FsResult<FileHandle> {
2626
Err(FsError::NotSupported)
2727
}
2828

2929
/// Opens the file at this path for appending
30-
fn append_file(&self, _path: &str) -> Result<FileHandle> {
30+
fn append_file(&self, _path: &str) -> FsResult<FileHandle> {
3131
Err(FsError::NotSupported)
3232
}
3333

3434
/// Removes the file at this path
35-
fn remove_file(&self, _path: &str) -> Result<FileHandle> {
35+
fn remove_file(&self, _path: &str) -> FsResult<FileHandle> {
3636
Err(FsError::NotSupported)
3737
}
3838

3939
/// Removes the directory at this path
40-
fn remove_dir(&self, _path: &str) -> Result<FileHandle> {
40+
fn remove_dir(&self, _path: &str) -> FsResult<FileHandle> {
4141
Err(FsError::NotSupported)
4242
}
4343

4444
/// Copies the src path to the destination path within the same filesystem
45-
fn copy_file(&self, _src: &str, _dst: &str) -> Result<()> {
45+
fn copy_file(&self, _src: &str, _dst: &str) -> FsResult {
4646
Err(FsError::NotSupported)
4747
}
4848

4949
/// Moves the src path to the destination path within the same filesystem
50-
fn move_file(&self, _src: &str, _dst: &str) -> Result<()> {
50+
fn move_file(&self, _src: &str, _dst: &str) -> FsResult {
5151
Err(FsError::NotSupported)
5252
}
5353

5454
/// Moves the src directory to the destination path within the same filesystem
55-
fn move_dir(&self, _src: &str, _dst: &str) -> Result<()> {
55+
fn move_dir(&self, _src: &str, _dst: &str) -> FsResult {
5656
Err(FsError::NotSupported)
5757
}
5858
}

src/0x06/pkg/storage/src/common/io.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::*;
44
pub trait Read {
55
/// Pull some bytes from this source into the specified buffer, returning
66
/// how many bytes were read.
7-
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
7+
fn read(&mut self, buf: &mut [u8]) -> FsResult<usize>;
88

99
/// Read all bytes until EOF in this source, placing them into `buf`.
10-
fn read_all(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
10+
fn read_all(&mut self, buf: &mut Vec<u8>) -> FsResult<usize> {
1111
let mut start_len = buf.len();
1212
loop {
1313
// FIXME: read data into the buffer
@@ -23,14 +23,14 @@ pub trait Read {
2323
/// NOTE: Leave here to ensure flexibility for the optional lab task.
2424
pub trait Write {
2525
/// Write a buffer into this writer, returning how many bytes were written.
26-
fn write(&mut self, buf: &[u8]) -> Result<usize>;
26+
fn write(&mut self, buf: &[u8]) -> FsResult<usize>;
2727

2828
/// Flush this output stream, ensuring that all intermediately buffered
2929
/// contents reach their destination.
30-
fn flush(&mut self) -> Result<()>;
30+
fn flush(&mut self) -> FsResult;
3131

3232
/// Attempts to write an entire buffer into this writer.
33-
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
33+
fn write_all(&mut self, mut buf: &[u8]) -> FsResult {
3434
// not required for lab
3535
todo!()
3636
}
@@ -52,7 +52,7 @@ pub enum SeekFrom {
5252
/// The `Seek` trait provides a cursor within byte stream.
5353
pub trait Seek {
5454
/// Seek to an offset, in bytes, in a stream.
55-
fn seek(&mut self, pos: SeekFrom) -> Result<usize>;
55+
fn seek(&mut self, pos: SeekFrom) -> FsResult<usize>;
5656
}
5757

5858
pub trait FileIO: Read + Write + Seek {}

src/0x06/pkg/storage/src/common/mount.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ impl Mount {
2222

2323
impl FileSystem for Mount {
2424
#[inline]
25-
fn read_dir(&self, path: &str) -> Result<Box<dyn Iterator<Item = Metadata> + Send>> {
25+
fn read_dir(&self, path: &str) -> FsResult<Box<dyn Iterator<Item = Metadata> + Send>> {
2626
self.fs.read_dir(self.trim_mount_point(path))
2727
}
2828

2929
#[inline]
30-
fn open_file(&self, path: &str) -> Result<FileHandle> {
30+
fn open_file(&self, path: &str) -> FsResult<FileHandle> {
3131
self.fs.open_file(self.trim_mount_point(path))
3232
}
3333

3434
#[inline]
35-
fn metadata(&self, path: &str) -> Result<Metadata> {
35+
fn metadata(&self, path: &str) -> FsResult<Metadata> {
3636
self.fs.metadata(self.trim_mount_point(path))
3737
}
3838

3939
#[inline]
40-
fn exists(&self, path: &str) -> Result<bool> {
40+
fn exists(&self, path: &str) -> FsResult<bool> {
4141
self.fs.exists(self.trim_mount_point(path))
4242
}
4343
}

src/0x06/pkg/storage/src/fs/fat16/bpb.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! - <https://en.wikipedia.org/wiki/BIOS_parameter_block>
55
//! - <https://wiki.osdev.org/FAT#Boot_Record>
66
7+
use crate::*;
8+
79
/// Represents a Boot Parameter Block.
810
///
911
/// This is the first sector of a FAT 16 formatted partition,
@@ -14,12 +16,12 @@ pub struct Fat16Bpb {
1416

1517
impl Fat16Bpb {
1618
/// Attempt to parse a Boot Parameter Block from a 512 byte sector.
17-
pub fn new(data: &[u8]) -> Result<Fat16Bpb, &'static str> {
19+
pub fn new(data: &[u8]) -> FsResult<Fat16Bpb> {
1820
let data = data.try_into().unwrap();
1921
let bpb = Fat16Bpb { data };
2022

2123
if bpb.data.len() != 512 || bpb.trail() != 0xAA55 {
22-
return Err("Bad BPB format");
24+
return Err(FsError::InvalidOperation);
2325
}
2426

2527
Ok(bpb)

src/0x06/pkg/storage/src/fs/fat16/direntry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl DirEntry {
5252
/// For Standard 8.3 format
5353
///
5454
/// reference: https://osdev.org/FAT#Standard_8.3_format
55-
pub fn parse(data: &[u8]) -> Result<DirEntry> {
55+
pub fn parse(data: &[u8]) -> FsResult<DirEntry> {
5656
let filename = ShortFileName::new(&data[..11]);
5757

5858
// FIXME: parse the rest of the fields
@@ -120,7 +120,7 @@ impl ShortFileName {
120120
}
121121

122122
/// Parse a short file name from a string
123-
pub fn parse(name: &str) -> Result<ShortFileName> {
123+
pub fn parse(name: &str) -> FsResult<ShortFileName> {
124124
// FIXME: implement the parse function
125125
// use `FilenameError` and into `FsError`
126126
// use different error types for following conditions:

src/0x06/pkg/storage/src/fs/fat16/file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl File {
3232
}
3333

3434
impl Read for File {
35-
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
35+
fn read(&mut self, buf: &mut [u8]) -> FsResult<usize> {
3636
// FIXME: read file content from disk
3737
// CAUTION: file length / buffer size / offset
3838
//
@@ -47,18 +47,18 @@ impl Read for File {
4747

4848
// NOTE: `Seek` trait is not required for this lab
4949
impl Seek for File {
50-
fn seek(&mut self, pos: SeekFrom) -> Result<usize> {
50+
fn seek(&mut self, pos: SeekFrom) -> FsResult<usize> {
5151
unimplemented!()
5252
}
5353
}
5454

5555
// NOTE: `Write` trait is not required for this lab
5656
impl Write for File {
57-
fn write(&mut self, _buf: &[u8]) -> Result<usize> {
57+
fn write(&mut self, _buf: &[u8]) -> FsResult<usize> {
5858
unimplemented!()
5959
}
6060

61-
fn flush(&mut self) -> Result<()> {
61+
fn flush(&mut self) -> FsResult {
6262
unimplemented!()
6363
}
6464
}

0 commit comments

Comments
 (0)