|
| 1 | +use alloc::vec; |
| 2 | +use alloc::vec::Vec; |
| 3 | + |
| 4 | +use filesystem::BlockDevice; |
| 5 | + |
| 6 | +use crate::{BlockAddress, Error, Ext2Fs, Inode, RegularFile}; |
| 7 | + |
| 8 | +const SZ: usize = size_of::<BlockAddress>(); |
| 9 | + |
| 10 | +impl<T> Ext2Fs<T> |
| 11 | +where |
| 12 | + T: BlockDevice, |
| 13 | +{ |
| 14 | + pub fn read_from_file( |
| 15 | + &self, |
| 16 | + file: &RegularFile, |
| 17 | + offset: usize, |
| 18 | + buf: &mut [u8], |
| 19 | + ) -> Result<usize, Error> { |
| 20 | + let file_size = file.len(); |
| 21 | + if offset >= file_size { |
| 22 | + return Ok(0); |
| 23 | + } |
| 24 | + |
| 25 | + let block_size = self.superblock.block_size(); |
| 26 | + let offset = offset as u32; |
| 27 | + |
| 28 | + let start_block = offset / block_size; |
| 29 | + let end_block = (offset + buf.len() as u32 - 1) / block_size; |
| 30 | + let relative_offset = (offset % block_size) as usize; |
| 31 | + let block_count = (end_block - start_block + 1) as usize; |
| 32 | + |
| 33 | + // read blocks |
| 34 | + let mut data: Vec<u8> = vec![0_u8; block_count * block_size as usize]; // TODO: avoid allocation - maybe try to only allocate the first and last block if the read is not aligned, but read the rest directly into the buffer |
| 35 | + let res = self.read_blocks_from_inode(file, start_block as usize, end_block as usize, &mut data)?; |
| 36 | + // copy the data into buf, but only the requested part and only up to the file size |
| 37 | + let total_read = res.min(file_size - offset as usize).min(buf.len()); |
| 38 | + buf[..total_read].copy_from_slice(&data[relative_offset..relative_offset + total_read]); |
| 39 | + |
| 40 | + |
| 41 | + Ok(total_read) |
| 42 | + } |
| 43 | + |
| 44 | + fn read_blocks_from_inode(&self, inode: &Inode, start_block: usize, end_block: usize, buf: &mut [u8]) -> Result<usize, Error> { |
| 45 | + let block_size = self.superblock.block_size() as usize; |
| 46 | + assert_eq!(buf.len(), (end_block - start_block + 1) * block_size, "buf.len() must be equal to the number of blocks you want to read"); |
| 47 | + |
| 48 | + let (direct_limit, indirect_limit, double_indirect_limit) = self.indirect_pointer_limits(); |
| 49 | + |
| 50 | + let mut total_read = 0; |
| 51 | + |
| 52 | + for (i, block) in (start_block..=end_block).enumerate() { |
| 53 | + let block_data = &mut buf[i * block_size..(i + 1) * block_size]; |
| 54 | + let block_pointer = if block < direct_limit as usize { |
| 55 | + inode.direct_ptr(block) |
| 56 | + } else if block < indirect_limit as usize { |
| 57 | + self.resolve_indirect_ptr(inode.single_indirect_ptr(), block as u32 - direct_limit)? |
| 58 | + } else if block < double_indirect_limit as usize { |
| 59 | + self.resolve_double_indirect_ptr(inode.double_indirect_ptr(), block as u32 - indirect_limit)? |
| 60 | + } else { |
| 61 | + self.resolve_triple_indirect_ptr(inode.triple_indirect_ptr(), block as u32 - double_indirect_limit)? |
| 62 | + }; |
| 63 | + if let Some(block_pointer) = block_pointer { |
| 64 | + total_read += self.read_block(block_pointer, block_data)?; |
| 65 | + } else { |
| 66 | + block_data.fill(0); |
| 67 | + total_read += block_size; // FIXME: what if the last block is sparse? |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Ok(total_read) |
| 72 | + } |
| 73 | + |
| 74 | + pub fn indirect_pointer_limits(&self) -> (u32, u32, u32) { |
| 75 | + let direct_limit = 12; |
| 76 | + let indirect_limit = direct_limit + self.superblock.block_size() / 4; |
| 77 | + let double_indirect_limit = indirect_limit + indirect_limit * indirect_limit; |
| 78 | + (direct_limit, indirect_limit, double_indirect_limit) |
| 79 | + } |
| 80 | + |
| 81 | + pub fn is_block_allocated(&self, inode: &Inode, block_index: u32) -> Result<bool, Error> { |
| 82 | + let (direct_limit, indirect_limit, double_indirect_limit) = self.indirect_pointer_limits(); |
| 83 | + |
| 84 | + Ok( |
| 85 | + if block_index < direct_limit { |
| 86 | + inode.direct_ptr(block_index as usize) |
| 87 | + } else if block_index < indirect_limit { |
| 88 | + self.resolve_indirect_ptr(inode.single_indirect_ptr(), block_index - direct_limit)? |
| 89 | + } else if block_index < double_indirect_limit { |
| 90 | + self.resolve_double_indirect_ptr(inode.double_indirect_ptr(), block_index - indirect_limit)? |
| 91 | + } else { |
| 92 | + self.resolve_triple_indirect_ptr(inode.triple_indirect_ptr(), block_index - double_indirect_limit)? |
| 93 | + } |
| 94 | + .is_some() |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + pub fn resolve_indirect_ptr(&self, indirect_ptr: Option<BlockAddress>, block_index: u32) -> Result<Option<BlockAddress>, Error> { |
| 99 | + if indirect_ptr.is_none() { |
| 100 | + return Ok(None); |
| 101 | + } |
| 102 | + let indirect_ptr = indirect_ptr.unwrap(); |
| 103 | + |
| 104 | + let mut indirect_block_data = vec![0_u8; self.superblock.block_size() as usize]; |
| 105 | + self.read_block(indirect_ptr, &mut indirect_block_data)?; |
| 106 | + Ok( |
| 107 | + indirect_block_data |
| 108 | + .iter() |
| 109 | + .copied() |
| 110 | + .array_chunks::<SZ>() |
| 111 | + .map(u32::from_le_bytes) |
| 112 | + .map(BlockAddress::new) |
| 113 | + .nth(block_index as usize) |
| 114 | + .unwrap() // the amount of pointers is fixed, so this is fine |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + pub fn resolve_double_indirect_ptr(&self, double_indirect_block: Option<BlockAddress>, block_index: u32) -> Result<Option<BlockAddress>, Error> { |
| 119 | + let block_size = self.superblock.block_size(); |
| 120 | + |
| 121 | + let single_indirect_block_size = block_size / 4; |
| 122 | + let single_indirect_index = block_index / single_indirect_block_size; |
| 123 | + |
| 124 | + self.resolve_indirect_ptr(double_indirect_block, single_indirect_index) |
| 125 | + .and_then(|single_indirect_block_ptr| self.resolve_indirect_ptr(single_indirect_block_ptr, block_index % single_indirect_block_size)) |
| 126 | + } |
| 127 | + |
| 128 | + pub fn resolve_triple_indirect_ptr(&self, triple_indirect_block: Option<BlockAddress>, block_index: u32) -> Result<Option<BlockAddress>, Error> { |
| 129 | + let block_size = self.superblock.block_size(); |
| 130 | + |
| 131 | + let double_indirect_block_size = block_size / 4; |
| 132 | + let double_indirect_index = block_index / double_indirect_block_size; |
| 133 | + |
| 134 | + self.resolve_indirect_ptr(triple_indirect_block, double_indirect_index) |
| 135 | + .and_then(|double_indirect_block_ptr| self.resolve_double_indirect_ptr(double_indirect_block_ptr, block_index % double_indirect_block_size)) |
| 136 | + } |
| 137 | +} |
0 commit comments