static int vhd_test_bitmap(vhd_context_t *vhd, uint64_t sector, int count, int hex) { char *buf; uint64_t cur; int i, err, bit; uint32_t blk, bm_blk, sec; if (vhd_sectors_to_bytes(sector + count) > vhd->footer.curr_size) { printf("sector %s past end of file\n", conv(hex, sector)); return -ERANGE; } bm_blk = -1; buf = NULL; for (i = 0; i < count; i++) { cur = sector + i; blk = cur / vhd->spb; sec = cur % vhd->spb; if (blk != bm_blk) { bm_blk = blk; free(buf); buf = NULL; if (vhd->bat.bat[blk] != DD_BLK_UNUSED) { err = vhd_read_bitmap(vhd, blk, &buf); if (err) goto out; } } if (vhd->bat.bat[blk] == DD_BLK_UNUSED) bit = 0; else bit = vhd_bitmap_test(vhd, buf, sec); printf("block %s: ", conv(hex, blk)); printf("sec: %s: %d\n", conv(hex, sec), bit); } err = 0; out: free(buf); return err; }
static int vhd_print_bitmap(vhd_context_t *vhd, uint64_t block, int count, int hex) { char *buf; int i, err; uint64_t cur; ssize_t n; if (check_block_range(vhd, block + count, hex)) return -ERANGE; for (i = 0; i < count; i++) { cur = block + i; if (vhd->bat.bat[cur] == DD_BLK_UNUSED) { printf("block %s not allocated\n", conv(hex, cur)); continue; } err = vhd_read_bitmap(vhd, cur, &buf); if (err) goto out; n = write(STDOUT_FILENO, buf, vhd_sectors_to_bytes(vhd->bm_secs)); if (n < 0) { err = -errno; goto out; } free(buf); } err = 0; out: return err; }
int vhd_journal_add_block(vhd_journal_t *j, uint32_t block, char mode) { int err; char *buf; off64_t off; size_t size; uint64_t blk; vhd_context_t *vhd; buf = NULL; vhd = &j->vhd; if (!vhd_type_dynamic(vhd)) return -EINVAL; err = vhd_get_bat(vhd); if (err) return err; if (block >= vhd->bat.entries) return -ERANGE; blk = vhd->bat.bat[block]; if (blk == DD_BLK_UNUSED) return 0; off = vhd_sectors_to_bytes(blk); if (mode & VHD_JOURNAL_METADATA) { size = vhd_sectors_to_bytes(vhd->bm_secs); err = vhd_read_bitmap(vhd, block, &buf); if (err) return err; err = vhd_journal_update(j, off, buf, size, VHD_JOURNAL_ENTRY_TYPE_DATA); free(buf); if (err) return err; } if (mode & VHD_JOURNAL_DATA) { off += vhd_sectors_to_bytes(vhd->bm_secs); size = vhd_sectors_to_bytes(vhd->spb); err = vhd_read_block(vhd, block, &buf); if (err) return err; err = vhd_journal_update(j, off, buf, size, VHD_JOURNAL_ENTRY_TYPE_DATA); free(buf); if (err) return err; } return vhd_journal_sync(j); }