mod.rs 1017 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. use crate::constants::*;
  2. use crate::ext4_defs::*;
  3. use crate::prelude::*;
  4. use crate::return_error;
  5. mod alloc;
  6. mod dir;
  7. mod extent;
  8. mod high_level;
  9. mod journal;
  10. mod link;
  11. mod low_level;
  12. mod rw;
  13. #[derive(Debug)]
  14. pub struct Ext4 {
  15. block_device: Arc<dyn BlockDevice>,
  16. }
  17. impl Ext4 {
  18. /// Opens and loads an Ext4 from the `block_device`.
  19. pub fn load(block_device: Arc<dyn BlockDevice>) -> Result<Self> {
  20. // Load the superblock
  21. // TODO: if the main superblock is corrupted, should we load the backup?
  22. let block = block_device.read_block(0);
  23. let sb = block.read_offset_as::<SuperBlock>(BASE_OFFSET);
  24. // Check magic number
  25. if !sb.check_magic() {
  26. return_error!(ErrCode::EINVAL, "invalid magic number");
  27. }
  28. // Create Ext4 instance
  29. Ok(Self { block_device })
  30. }
  31. /// Initializes the root directory.
  32. pub fn init(&mut self) -> Result<()> {
  33. // Create root directory
  34. self.create_root_inode().map(|_| ())
  35. }
  36. }