errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags) { char *ptr; int c, size; char zero_buf[1024]; ssize_t actual; errcode_t retval; if (flags & IMAGER_FLAG_INODEMAP) { if (!fs->inode_map) { retval = ext2fs_read_inode_bitmap(fs); if (retval) return retval; } ptr = fs->inode_map->bitmap; size = (EXT2_INODES_PER_GROUP(fs->super) / 8); } else { if (!fs->block_map) { retval = ext2fs_read_block_bitmap(fs); if (retval) return retval; } ptr = fs->block_map->bitmap; size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; } size = size * fs->group_desc_count; actual = write(fd, ptr, size); if (actual == -1) { retval = errno; goto errout; } if (actual != size) { retval = EXT2_ET_SHORT_WRITE; goto errout; } size = size % fs->blocksize; memset(zero_buf, 0, sizeof(zero_buf)); if (size) { size = fs->blocksize - size; while (size) { c = size; if (c > (int) sizeof(zero_buf)) c = sizeof(zero_buf); actual = write(fd, zero_buf, c); if (actual == -1) { retval = errno; goto errout; } if (actual != c) { retval = EXT2_ET_SHORT_WRITE; goto errout; } size -= c; } } retval = 0; errout: return (retval); }
errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags) { char *ptr, *buf = 0; int size; ssize_t actual; errcode_t retval; if (flags & IMAGER_FLAG_INODEMAP) { if (!fs->inode_map) { retval = ext2fs_read_inode_bitmap(fs); if (retval) return retval; } ptr = fs->inode_map->bitmap; size = (EXT2_INODES_PER_GROUP(fs->super) / 8); } else { if (!fs->block_map) { retval = ext2fs_read_block_bitmap(fs); if (retval) return retval; } ptr = fs->block_map->bitmap; size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; } size = size * fs->group_desc_count; buf = malloc(size); if (!buf) return ENOMEM; actual = read(fd, buf, size); if (actual == -1) { retval = errno; goto errout; } if (actual != size) { retval = EXT2_ET_SHORT_WRITE; goto errout; } memcpy(ptr, buf, size); retval = 0; errout: if (buf) free(buf); return (retval); }
long open_filesystem(char *name, ext2_filsys *fs, ext2_ino_t *root, int rw_mode) { int retval; int closeval; if ((retval = ext2fs_open(name, (rw_mode) ? EXT2_FLAG_RW : 0, 0, 0, unix_io_manager, fs))) { fprintf(stderr, "%s\n", error_message(retval)); *fs = NULL; return retval; } if ((retval = ext2fs_read_inode_bitmap(*fs))) { fprintf(stderr, "%s\n", error_message(retval)); if ((closeval = ext2fs_close(*fs))) fputs(error_message(closeval), stderr); *fs = NULL; return retval; } if ((retval = ext2fs_read_block_bitmap(*fs))) { fprintf(stderr, "%s\n", error_message(retval)); if ((closeval = ext2fs_close(*fs))) fputs(error_message(closeval), stderr); *fs = NULL; return retval; } *root = EXT2_ROOT_INO; return(0); }
static void open_filesystem(char *device, int open_flags, blk_t superblock, blk_t blocksize, int catastrophic, char *data_filename) { int retval; io_channel data_io = 0; if (superblock != 0 && blocksize == 0) { com_err(device, 0, "if you specify the superblock, you must also specify the block size"); current_fs = NULL; return; } if (data_filename) { if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) { com_err(device, 0, "The -d option is only valid when reading an e2image file"); current_fs = NULL; return; } retval = unix_io_manager->open(data_filename, 0, &data_io); if (retval) { com_err(data_filename, 0, "while opening data source"); current_fs = NULL; return; } } if (catastrophic && (open_flags & EXT2_FLAG_RW)) { com_err(device, 0, "opening read-only because of catastrophic mode"); open_flags &= ~EXT2_FLAG_RW; } retval = ext2fs_open(device, open_flags, superblock, blocksize, unix_io_manager, ¤t_fs); if (retval) { com_err(device, retval, "while opening filesystem"); current_fs = NULL; return; } if (catastrophic) com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps"); else { retval = ext2fs_read_inode_bitmap(current_fs); if (retval) { com_err(device, retval, "while reading inode bitmap"); goto errout; } retval = ext2fs_read_block_bitmap(current_fs); if (retval) { com_err(device, retval, "while reading block bitmap"); goto errout; } } if (data_io) { retval = ext2fs_set_data_io(current_fs, data_io); if (retval) { com_err(device, retval, "while setting data source"); goto errout; } } root = cwd = EXT2_ROOT_INO; return; errout: retval = ext2fs_close(current_fs); if (retval) com_err(device, retval, "while trying to close filesystem"); current_fs = NULL; }