kerla icon indicating copy to clipboard operation
kerla copied to clipboard

Implement ext2 reader/writer library

Open dust1 opened this issue 3 years ago • 6 comments

Summary

https://github.com/nuta/kerla/issues/48#issuecomment-962287339

dust1 avatar Nov 10 '21 08:11 dust1

@nuta hello, I try to implement it, does it mean I need to read and write to the disk directly?

dust1 avatar Nov 11 '21 03:11 dust1

The first step will be an ext2 reader. You should use a trait to access disks to make it portable and testable as a simple Rust application.

For example , if I were to implement it, I would:

#![cfg_attr(not(test), no_std)]

trait Disk {
    fn read_block(&mut self, lba: usize, buf: &mut [u8]) -> Result<usize /* # of bytes actually read */, DiskError>;
    fn write_block(&mut self, lba: usize, buf: &[u8]) -> Result<usize /* # of bytes actually written */, DiskError>;
}

struct Ext2Fs<D: Disk> {
    disk: D,
}

impl<D: Disk> Ext2Fs<D> {
  pub fn new(disk: D) {
    Ext2Fs {
      disk,
    }
  }
}

#[cfg(test)]
mod tests {
    struct PseudoDisk;
    impl Disk for PseudoDIsk {
        fn read_block(...) {
            let f = std::fs::File::open("testdata/ext2-test.img");
            ...
        }
    }

    #[test]
    fn test() {
        let mut fs = Ext2Fs(PseudoDisk);
    }
}

nuta avatar Nov 11 '21 10:11 nuta

that means i should implement the way to reader disk in no std, not just defined trait?

dust1 avatar Nov 11 '21 10:11 dust1

You don't need to provide the implementation of a trait in no_std until you port the library into Kerla. In the library development, as shown in the code above, you can use std features to emulate disks.

nuta avatar Nov 11 '21 11:11 nuta

IIRC, #![cfg_attr(not(test), no_std)] does not enableno_std mode in testing (cargo test).

nuta avatar Nov 11 '21 11:11 nuta

i get it now. thank you

dust1 avatar Nov 11 '21 11:11 dust1