static int vhd_print_headers(vhd_context_t *vhd, int hex) { int err; vhd_print_footer(&vhd->footer, hex); if (vhd_type_dynamic(vhd)) { vhd_print_header(vhd, &vhd->header, hex); if (vhd->footer.type == HD_TYPE_DIFF) vhd_print_parent_locators(vhd, hex); if (vhd_has_batmap(vhd)) { err = vhd_get_batmap(vhd); if (err) { printf("failed to get batmap header\n"); return err; } vhd_print_batmap_header(vhd, &vhd->batmap, hex); } } return 0; }
static int vhd_print_batmap(vhd_context_t *vhd) { int err, gcc; size_t size; err = vhd_get_batmap(vhd); if (err) { printf("failed to read batmap: %d\n", err); return err; } size = vhd_sectors_to_bytes(vhd->batmap.header.batmap_size); gcc = write(STDOUT_FILENO, vhd->batmap.map, size); if (gcc) ; return 0; }
static int vhd_test_batmap(vhd_context_t *vhd, uint64_t block, int count, int hex) { int i, err; uint64_t cur; if (check_block_range(vhd, block + count, hex)) return -ERANGE; err = vhd_get_batmap(vhd); if (err) { fprintf(stderr, "failed to get batmap\n"); return err; } for (i = 0; i < count; i++) { cur = block + i; fprintf(stderr, "batmap for block %s: %d\n", conv(hex, cur), vhd_batmap_test(vhd, &vhd->batmap, cur)); } return 0; }
int vhd_journal_create(vhd_journal_t *j, const char *file, const char *jfile) { int err; memset(j, 0, sizeof(vhd_journal_t)); j->jfd = -1; j->jname = strdup(jfile); if (j->jname == NULL) { err = -ENOMEM; goto fail1; } if (access(j->jname, F_OK) == 0) { err = vhd_test_file_fixed(j->jname, &j->is_block); if (err) goto fail1; if (!j->is_block) { err = -EEXIST; goto fail1; } } if (j->is_block) j->jfd = open(j->jname, O_LARGEFILE | O_RDWR, 0644); else j->jfd = open(j->jname, O_CREAT | O_TRUNC | O_LARGEFILE | O_RDWR, 0644); if (j->jfd == -1) { err = -errno; goto fail1; } err = vhd_open(&j->vhd, file, VHD_OPEN_RDWR | VHD_OPEN_STRICT); if (err) goto fail1; err = vhd_get_bat(&j->vhd); if (err) goto fail2; if (vhd_has_batmap(&j->vhd)) { err = vhd_get_batmap(&j->vhd); if (err) goto fail2; } err = vhd_journal_add_journal_header(j); if (err) goto fail2; err = vhd_journal_add_metadata(j); if (err) goto fail2; err = vhd_journal_disable_vhd(j); if (err) goto fail2; err = vhd_journal_sync(j); if (err) goto fail2; return 0; fail1: if (j->jfd != -1) { close(j->jfd); if (!j->is_block) unlink(j->jname); } free(j->jname); memset(j, 0, sizeof(vhd_journal_t)); return err; fail2: vhd_journal_remove(j); return err; }
int vhd_journal_open(vhd_journal_t *j, const char *file, const char *jfile) { int err; vhd_context_t *vhd; memset(j, 0, sizeof(vhd_journal_t)); j->jfd = -1; vhd = &j->vhd; j->jname = strdup(jfile); if (j->jname == NULL) return -ENOMEM; j->jfd = open(j->jname, O_LARGEFILE | O_RDWR); if (j->jfd == -1) { err = -errno; goto fail; } err = vhd_test_file_fixed(j->jname, &j->is_block); if (err) goto fail; vhd->fd = open(file, O_LARGEFILE | O_RDWR | O_DIRECT); if (vhd->fd == -1) { err = -errno; goto fail; } err = vhd_test_file_fixed(file, &vhd->is_block); if (err) goto fail; err = vhd_journal_read_journal_header(j, &j->header); if (err) goto fail; err = vhd_journal_restore_metadata(j); if (err) goto fail; close(vhd->fd); free(vhd->bat.bat); free(vhd->batmap.map); err = vhd_open(vhd, file, VHD_OPEN_RDWR); if (err) goto fail; err = vhd_get_bat(vhd); if (err) goto fail; if (vhd_has_batmap(vhd)) { err = vhd_get_batmap(vhd); if (err) goto fail; } err = vhd_journal_disable_vhd(j); if (err) goto fail; return 0; fail: vhd_journal_close(j); return err; }