dir.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. use super::Ext4;
  2. use crate::constants::*;
  3. use crate::ext4_defs::*;
  4. use crate::prelude::*;
  5. impl Ext4 {
  6. /// Find a directory entry that matches a given name under a parent directory
  7. pub(super) fn dir_find_entry(&self, parent: &InodeRef, name: &str) -> Result<DirEntry> {
  8. info!("Dir find entry {} under parent {}", name, parent.id);
  9. let inode_size: u32 = parent.inode.size;
  10. let total_blocks: u32 = inode_size / BLOCK_SIZE as u32;
  11. let mut iblock: LBlockId = 0;
  12. while iblock < total_blocks {
  13. // Get the fs block id
  14. let fblock = self.extent_get_pblock(parent, iblock)?;
  15. // Load block from disk
  16. let block = self.read_block(fblock);
  17. // Find the entry in block
  18. let res = Self::find_entry_in_block(&block, name);
  19. if let Ok(r) = res {
  20. return Ok(r);
  21. }
  22. iblock += 1;
  23. }
  24. Err(Ext4Error::new(ErrCode::ENOENT))
  25. }
  26. /// Add an entry to a directory
  27. pub(super) fn dir_add_entry(
  28. &mut self,
  29. parent: &mut InodeRef,
  30. child: &InodeRef,
  31. name: &str,
  32. ) -> Result<()> {
  33. info!(
  34. "Dir add entry: parent {}, child {}, path {}",
  35. parent.id, child.id, name
  36. );
  37. let inode_size = parent.inode.size();
  38. let total_blocks = inode_size as u32 / BLOCK_SIZE as u32;
  39. // Try finding a block with enough space
  40. let mut iblock: LBlockId = 0;
  41. while iblock < total_blocks {
  42. // Get the parent physical block id
  43. let fblock = self.extent_get_pblock(parent, iblock)?;
  44. // Load the parent block from disk
  45. let mut block = self.read_block(fblock);
  46. // Try inserting the entry to parent block
  47. if self.insert_entry_to_old_block(&mut block, child, name) {
  48. return Ok(());
  49. }
  50. // Current block has no enough space
  51. iblock += 1;
  52. }
  53. // No free block found - needed to allocate a new data block
  54. // Append a new data block
  55. let (_, fblock) = self.inode_append_block(parent)?;
  56. // Load new block
  57. let mut new_block = self.read_block(fblock);
  58. // Write the entry to block
  59. self.insert_entry_to_new_block(&mut new_block, child, name);
  60. Ok(())
  61. }
  62. /// Remove a entry from a directory
  63. pub(super) fn dir_remove_entry(&mut self, parent: &mut InodeRef, name: &str) -> Result<()> {
  64. info!("Dir remove entry: parent {}, path {}", parent.id, name);
  65. let inode_size = parent.inode.size();
  66. let total_blocks = inode_size as u32 / BLOCK_SIZE as u32;
  67. // Check each block
  68. let mut iblock: LBlockId = 0;
  69. while iblock < total_blocks {
  70. // Get the parent physical block id
  71. let fblock = self.extent_get_pblock(parent, iblock)?;
  72. // Load the block from disk
  73. let mut block = self.read_block(fblock);
  74. // Try removing the entry
  75. if let Ok(()) = Self::remove_entry_from_block(&mut block, name) {
  76. self.write_block(&block);
  77. return Ok(());
  78. }
  79. // Current block has no enough space
  80. iblock += 1;
  81. }
  82. // Not found the target entry
  83. Err(Ext4Error::new(ErrCode::ENOENT))
  84. }
  85. /// Find a directory entry that matches a given name in a given block
  86. fn find_entry_in_block(block: &Block, name: &str) -> Result<DirEntry> {
  87. info!("Dir find entry {} in block {}", name, block.block_id);
  88. let mut offset = 0;
  89. while offset < BLOCK_SIZE {
  90. let de: DirEntry = block.read_offset_as(offset);
  91. debug!("Dir entry: {} {:?}", de.rec_len(), de.name());
  92. if !de.unused() && de.compare_name(name) {
  93. return Ok(de);
  94. }
  95. offset += de.rec_len() as usize;
  96. }
  97. Err(Ext4Error::new(ErrCode::ENOENT))
  98. }
  99. /// Remove a directory entry that matches a given name from a given block
  100. fn remove_entry_from_block(block: &mut Block, name: &str) -> Result<()> {
  101. info!("Dir remove entry {} from block {}", name, block.block_id);
  102. let mut offset = 0;
  103. while offset < BLOCK_SIZE {
  104. let mut de: DirEntry = block.read_offset_as(offset);
  105. if !de.unused() && de.compare_name(name) {
  106. // Mark the target entry as unused
  107. de.set_unused();
  108. block.write_offset_as(offset, &de);
  109. return Ok(());
  110. }
  111. offset += de.rec_len() as usize;
  112. }
  113. Err(Ext4Error::new(ErrCode::ENOENT))
  114. }
  115. /// Insert a directory entry of a child inode into a new parent block.
  116. /// A new block must have enough space
  117. fn insert_entry_to_new_block(&self, dst_blk: &mut Block, child: &InodeRef, name: &str) {
  118. // Set the entry
  119. let rec_len = BLOCK_SIZE - size_of::<DirEntryTail>();
  120. let new_entry = DirEntry::new(
  121. child.id,
  122. rec_len as u16,
  123. name,
  124. inode_mode2file_type(child.inode.mode()),
  125. );
  126. // Write entry to block
  127. dst_blk.write_offset_as(0, &new_entry);
  128. // Set tail
  129. let mut tail = DirEntryTail::default();
  130. tail.rec_len = size_of::<DirEntryTail>() as u16;
  131. tail.reserved_ft = 0xDE;
  132. tail.set_csum(&self.super_block, &new_entry, &dst_blk.data[..]);
  133. // Copy tail to block
  134. let tail_offset = BLOCK_SIZE - size_of::<DirEntryTail>();
  135. dst_blk.write_offset_as(tail_offset, &tail);
  136. // Sync block to disk
  137. self.write_block(&dst_blk);
  138. }
  139. /// Try insert a directory entry of child inode into a parent block.
  140. /// Return true if the entry is successfully inserted.
  141. fn insert_entry_to_old_block(&self, dst_blk: &mut Block, child: &InodeRef, name: &str) -> bool {
  142. let required_size = DirEntry::required_size(name.len());
  143. let mut offset = 0;
  144. while offset < dst_blk.data.len() {
  145. let mut de: DirEntry = dst_blk.read_offset_as(offset);
  146. let rec_len = de.rec_len() as usize;
  147. // Try splitting dir entry
  148. // The size that `de` actually uses
  149. let used_size = de.used_size();
  150. // The rest size
  151. let free_size = rec_len - used_size;
  152. // Compare size
  153. if free_size < required_size {
  154. // No enough space, try next dir ent
  155. offset = offset + rec_len;
  156. continue;
  157. }
  158. // Has enough space
  159. // Update the old entry
  160. de.set_rec_len(used_size as u16);
  161. dst_blk.write_offset_as(offset, &de);
  162. // Insert the new entry
  163. let new_entry = DirEntry::new(
  164. child.id,
  165. free_size as u16,
  166. name,
  167. inode_mode2file_type(child.inode.mode()),
  168. );
  169. dst_blk.write_offset_as(offset + used_size, &new_entry);
  170. // Set tail csum
  171. let tail_offset = BLOCK_SIZE - size_of::<DirEntryTail>();
  172. let mut tail = dst_blk.read_offset_as::<DirEntryTail>(tail_offset);
  173. tail.set_csum(&self.super_block, &de, &dst_blk.data[offset..]);
  174. // Write tail to blk_data
  175. dst_blk.write_offset_as(tail_offset, &tail);
  176. // Sync to disk
  177. self.write_block(&dst_blk);
  178. return true;
  179. }
  180. false
  181. }
  182. /// Create a new directory. `path` is the absolute path of the new directory.
  183. pub fn mkdir(&mut self, path: &str) -> Result<()> {
  184. let open_flags = OpenFlags::from_str("w").unwrap();
  185. self.generic_open(EXT4_ROOT_INO, path, FileType::Directory, open_flags)
  186. .map(|_| {
  187. info!("ext4_dir_mk: {} ok", path);
  188. })
  189. }
  190. }