/** * Write to the 'block'th block within an inode 'inode' from the buffer 'buf'. * In other words, this function writes to the actual file content. * @return the actual block number read from. */ uint32_t ext2_disk_inode_write_block(ext2_inodetable_t *inode, uint32_t no, uint32_t block, uint8_t *buf) { /* We must allocate blocks up to this point to account for unused space in the middle. */ while (block >= inode->blocks) { kprintf("[kernel/ext2] Need to allocate blocks, have %d, want to write to #%d.\n", inode->blocks, block); ext2_disk_inode_alloc_block(inode, no, inode->blocks); if (block != inode->blocks - 1) { /* Clear the block */ uint32_t real_block = ext2_get_real_block(inode, inode->blocks - 1); uint8_t empty[1024] = {0}; memset(empty, 0x00, 1024); ext2_disk_write_block(real_block, empty); } } // The real work to write to a block of an inode. uint32_t real_block = ext2_get_real_block(inode, block); ext2_disk_write_block(real_block, buf); return real_block; }
/** * Write to the 'block'th block within an inode 'inode' from the buffer 'buf'. * In other words, this function writes to the actual file content. * @return the actual block number read from. */ uint32_t ext2_disk_inode_write_block(ext2_inodetable_t *inode, uint32_t inode_no, uint32_t block, uint8_t *buf) { /* We must allocate blocks up to this point to account for unused space in the middle. */ while (block >= inode->blocks) { ext2_disk_inode_alloc_block(inode, inode_no, inode->blocks); if (block != inode->blocks - 1) { /* Clear the block */ uint32_t real_block = ext2_get_real_block(inode, inode->blocks - 1); uint8_t * empty = malloc(BLOCKSIZE); memset(empty, 0x00, BLOCKSIZE); ext2_disk_write_block(real_block, empty); free(empty); } } // The real work to write to a block of an inode. uint32_t real_block = ext2_get_real_block(inode, block); debug_print(INFO, "virtual block %d maps to real block %d", block, real_block); ext2_disk_write_block(real_block, buf); return real_block; }