Skip to content

Commit

Permalink
SCTX-1536: Option to use the back-up footer instead of the primary one.
Browse files Browse the repository at this point in the history
Signed-off-by: Thanos Makatos <[email protected]>
Reviewed-by: Keith Petley <[email protected]>

GitHub: closes #55 on xapi-project/blktap
  • Loading branch information
Thanos Makatos committed Oct 4, 2013
1 parent f8b4c0d commit ec6e15f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
11 changes: 8 additions & 3 deletions drivers/td.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ td_query(int type, int argc, char *argv[])
{
char *name;
int c, size = 0, parent = 0, fields = 0, depth = 0, err = 0;
int flags = VHD_OPEN_RDONLY;

while ((c = getopt(argc, argv, "hvpfd")) != -1) {
while ((c = getopt(argc, argv, "hvpfdb")) != -1) {
switch(c) {
case 'v':
size = 1;
Expand All @@ -417,6 +418,9 @@ td_query(int type, int argc, char *argv[])
case 'h':
err = 0;
goto usage;
case 'b':
flags |= VHD_OPEN_USE_BKP_FOOTER;
break;
default:
err = EINVAL;
goto usage;
Expand All @@ -438,7 +442,7 @@ td_query(int type, int argc, char *argv[])
if (type == TD_TYPE_VHD) {
vhd_context_t vhd;

err = vhd_open(&vhd, name, VHD_OPEN_RDONLY);
err = vhd_open(&vhd, name, flags);
if (err) {
printf("failed opening %s: %d\n", name, err);
return err;
Expand Down Expand Up @@ -531,7 +535,8 @@ td_query(int type, int argc, char *argv[])

usage:
fprintf(stderr, "usage: td-util query %s [-h help] [-v virtsize] "
"[-p parent] [-f fields] <FILENAME>\n", td_disk_types[type]);
"[-p parent] [-f fields] [-b don't trust the footer, use the back-up "
"one instead] <FILENAME>\n", td_disk_types[type]);
return err;
}

Expand Down
4 changes: 3 additions & 1 deletion include/libvhd.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <endian.h>
#include <byteswap.h>
#include <uuid/uuid.h>
#include <stdbool.h>

#include "vhd.h"
#include "list.h"
Expand Down Expand Up @@ -63,6 +64,7 @@
#define VHD_OPEN_IGNORE_DISABLED 0x00010
#define VHD_OPEN_CACHED 0x00020
#define VHD_OPEN_IO_WRITE_SPARSE 0x00040
#define VHD_OPEN_USE_BKP_FOOTER 0x00080

#define VHD_FLAG_CREAT_FILE_SIZE_FIXED 0x00001
#define VHD_FLAG_CREAT_PARENT_RAW 0x00002
Expand Down Expand Up @@ -312,7 +314,7 @@ int vhd_change_parent(vhd_context_t *, char *parent_path, int raw);
int vhd_macx_encode_location(char *name, char **out, int *outlen);
int vhd_w2u_encode_location(char *name, char **out, int *outlen);

int vhd_read_footer(vhd_context_t *, vhd_footer_t *);
int vhd_read_footer(vhd_context_t *, vhd_footer_t *, bool use_bkp_footer);
int vhd_read_footer_at(vhd_context_t *, vhd_footer_t *, off64_t);
int vhd_read_footer_strict(vhd_context_t *, vhd_footer_t *);
int vhd_read_header(vhd_context_t *, vhd_header_t *);
Expand Down
21 changes: 12 additions & 9 deletions vhd/lib/libvhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ vhd_get_footer(vhd_context_t *ctx)
if (!vhd_validate_footer(&ctx->footer))
return 0;

return vhd_read_footer(ctx, &ctx->footer);
return vhd_read_footer(ctx, &ctx->footer, false);
}

int
Expand Down Expand Up @@ -935,7 +935,7 @@ vhd_read_footer_at(vhd_context_t *ctx, vhd_footer_t *footer, off64_t off)
}

int
vhd_read_footer(vhd_context_t *ctx, vhd_footer_t *footer)
vhd_read_footer(vhd_context_t *ctx, vhd_footer_t *footer, bool use_bkp_footer)
{
int err;
off64_t off;
Expand All @@ -948,13 +948,15 @@ vhd_read_footer(vhd_context_t *ctx, vhd_footer_t *footer)
if (off == (off64_t)-1)
return -errno;

err = vhd_read_footer_at(ctx, footer, off - 512);
if (err != -EINVAL)
return err;
if (!use_bkp_footer) {
err = vhd_read_footer_at(ctx, footer, off - 512);
if (err != -EINVAL)
return err;

err = vhd_read_short_footer(ctx, footer);
if (err != -EINVAL)
return err;
err = vhd_read_short_footer(ctx, footer);
if (err != -EINVAL)
return err;
}

/*
* Disable the enforcement of VHD_OPEN_STRICT until we figure out how
Expand Down Expand Up @@ -2523,7 +2525,8 @@ vhd_open(vhd_context_t *ctx, const char *file, int flags)
return 0;
}

err = vhd_read_footer(ctx, &ctx->footer);
err = vhd_read_footer(ctx, &ctx->footer,
(flags & VHD_OPEN_USE_BKP_FOOTER) == VHD_OPEN_USE_BKP_FOOTER);
if (err)
goto fail;

Expand Down
2 changes: 1 addition & 1 deletion vhd/lib/vhd-util-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ vhd_dump_headers(const char *name, int hex)

vhd.file = strdup(name);

vhd_read_footer(&vhd, &vhd.footer);
vhd_read_footer(&vhd, &vhd.footer, false);
vhd_read_header(&vhd, &vhd.header);

vhd_print_footer(&vhd.footer, hex);
Expand Down
10 changes: 7 additions & 3 deletions vhd/lib/vhd-util-repair.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ vhd_util_repair(int argc, char **argv)
char *name;
int err, c;
vhd_context_t vhd;
int flags = VHD_OPEN_RDWR;

name = NULL;

if (!argc || !argv)
goto usage;

optind = 0;
while ((c = getopt(argc, argv, "n:h")) != -1) {
while ((c = getopt(argc, argv, "n:bh")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 'b':
flags |= VHD_OPEN_USE_BKP_FOOTER;
break;
case 'h':
default:
goto usage;
Expand All @@ -54,7 +58,7 @@ vhd_util_repair(int argc, char **argv)
if (!name || optind != argc)
goto usage;

err = vhd_open(&vhd, name, VHD_OPEN_RDWR);
err = vhd_open(&vhd, name, flags);
if (err) {
printf("error opening %s: %d\n", name, err);
return err;
Expand All @@ -68,6 +72,6 @@ vhd_util_repair(int argc, char **argv)
return err;

usage:
printf("options: <-n name> [-h help]\n");
printf("options: <-n name> <-b use the back-up footer> [-h help]\n");
return -EINVAL;
}

0 comments on commit ec6e15f

Please sign in to comment.