Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ext2 reader/writer library #70

Open
dust1 opened this issue Nov 10, 2021 · 6 comments
Open

Implement ext2 reader/writer library #70

dust1 opened this issue Nov 10, 2021 · 6 comments

Comments

@dust1
Copy link
Contributor

dust1 commented Nov 10, 2021

Summary

#48 (comment)

@dust1
Copy link
Contributor Author

dust1 commented Nov 11, 2021

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

@nuta
Copy link
Owner

nuta commented Nov 11, 2021

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);
    }
}

@dust1
Copy link
Contributor Author

dust1 commented Nov 11, 2021

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

@nuta
Copy link
Owner

nuta commented Nov 11, 2021

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
Copy link
Owner

nuta commented Nov 11, 2021

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

@dust1
Copy link
Contributor Author

dust1 commented Nov 11, 2021

i get it now. thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants