| 283 | } |
| 284 | |
| 285 | void Ext2Volume::SetInodeBlock(uint32_t index, ext2_inode_t& ino, uint32_t block){ |
| 286 | uint32_t blocksPerPointer = blocksize / sizeof(uint32_t); // Amount of blocks in a indirect block table |
| 287 | uint32_t singlyIndirectStart = EXT2_DIRECT_BLOCK_COUNT; |
| 288 | uint32_t doublyIndirectStart = singlyIndirectStart + blocksPerPointer; |
| 289 | uint32_t triplyIndirectStart = doublyIndirectStart + blocksPerPointer * blocksPerPointer; |
| 290 | |
| 291 | assert(index < triplyIndirectStart + blocksPerPointer * blocksPerPointer * blocksPerPointer); // Make sure that an invalid index was not passed |
| 292 | |
| 293 | if(index < EXT2_DIRECT_BLOCK_COUNT){ |
| 294 | // Index lies within the direct blocklist |
| 295 | ino.blocks[index] = block; |
| 296 | } else if(index < doublyIndirectStart){ |
| 297 | // Index lies within the singly indirect blocklist |
| 298 | uint32_t buffer[blocksize / sizeof(uint32_t)]; |
| 299 | |
| 300 | if(ino.blocks[EXT2_SINGLY_INDIRECT_INDEX] == 0){ |
| 301 | ino.blocks[EXT2_SINGLY_INDIRECT_INDEX] = AllocateBlock(); |
| 302 | } |
| 303 | |
| 304 | if(int e = ReadBlockCached(ino.blocks[EXT2_SINGLY_INDIRECT_INDEX], buffer)){ |
| 305 | (void)e; |
| 306 | error = DiskReadError; |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | buffer[index - singlyIndirectStart] = block; |
| 311 | |
| 312 | if(int e = WriteBlockCached(ino.blocks[EXT2_SINGLY_INDIRECT_INDEX], buffer)){ |
| 313 | (void)e; |
| 314 | error = DiskWriteError; |
| 315 | return; |
| 316 | } |
| 317 | return; |
| 318 | } else if(index < triplyIndirectStart){ |
| 319 | // Index lies within the doubly indirect blocklist |
| 320 | uint32_t blockPointers[blocksize / sizeof(uint32_t)]; |
| 321 | uint32_t buffer[blocksize / sizeof(uint32_t)]; |
| 322 | |
| 323 | if(int e = ReadBlockCached(ino.blocks[EXT2_DOUBLY_INDIRECT_INDEX], blockPointers)){ // Read indirect block pointer list |
| 324 | (void)e; |
| 325 | error = DiskReadError; |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | uint32_t blockPointer = blockPointers[(index - doublyIndirectStart) / blocksPerPointer]; |
| 330 | |
| 331 | if(int e = ReadBlockCached(blockPointer, buffer)){ // Read blocklist |
| 332 | (void)e; |
| 333 | error = DiskReadError; |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | buffer[(index - doublyIndirectStart) % blocksPerPointer] = block; // Update the index |
| 338 | |
| 339 | if(int e = WriteBlockCached(blockPointer, buffer)){ // Write our updated blocklist |
| 340 | (void)e; |
| 341 | error = DiskWriteError; |
| 342 | return; |
nothing calls this directly
no outgoing calls
no test coverage detected