- added support for converting growing to flat mode images

- added command line arguments and non-interactive (quiet) mode
This commit is contained in:
Volker Ruppert 2011-12-26 12:49:36 +00:00
parent bfcbb81602
commit add7af8c13

View File

@ -48,9 +48,26 @@ int snprintf(char *s, size_t maxlen, const char *format, ...)
#define HDIMAGE_HEADERS_ONLY 1 #define HDIMAGE_HEADERS_ONLY 1
#include "../iodev/hdimage.h" #include "../iodev/hdimage.h"
#define BXCOMMIT_MODE_COMMIT_UNDOABLE 1
#define BXCOMMIT_MODE_GROWING_TO_FLAT 2
int bxcommit_mode;
int bx_remove;
int bx_interactive;
char bx_flat_filename[256];
char bx_redolog_name[256];
char *EOF_ERR = "ERROR: End of input"; char *EOF_ERR = "ERROR: End of input";
char *rcsid = "$Id$"; char *svnid = "$Id$";
char *divider = "========================================================================"; char *divider = "========================================================================";
const char *main_menu_prompt =
"\n"
"1. Commit 'undoable' redolog to 'flat' file\n"
"2. Create 'flat' disk image from 'growing' disk image\n"
"\n"
"0. Quit\n"
"\n"
"Please choose one ";
void myexit(int code) void myexit(int code)
{ {
@ -75,8 +92,8 @@ void print_banner()
{ {
printf("%s\n", divider); printf("%s\n", divider);
bx_center_print(stdout, "bxcommit\n", 72); bx_center_print(stdout, "bxcommit\n", 72);
bx_center_print(stdout, "Undoable Disk Image Commit Tool for Bochs\n", 72); bx_center_print(stdout, "Growing / Undoable Disk Image Commit Tool for Bochs\n", 72);
bx_center_print(stdout, rcsid, 72); bx_center_print(stdout, svnid, 72);
printf("\n%s\n", divider); printf("\n%s\n", divider);
} }
@ -124,8 +141,8 @@ int ask_int(const char *prompt, int min, int max, int the_default, int *out)
} }
illegal = (1 != sscanf(buffer, "%d", &n)); illegal = (1 != sscanf(buffer, "%d", &n));
if (illegal || n<min || n>max) { if (illegal || n<min || n>max) {
printf ("Your choice (%s) was not an integer between %d and %d.\n\n", printf("Your choice (%s) was not an integer between %d and %d.\n\n",
clean, min, max); clean, min, max);
} else { } else {
// choice is okay // choice is okay
*out = n; *out = n;
@ -153,12 +170,12 @@ int ask_menu(const char *prompt, int n_choices, char *choice[], int the_default,
} }
for (i=0; i<n_choices; i++) { for (i=0; i<n_choices; i++) {
if (!strcmp(choice[i], clean)) { if (!strcmp(choice[i], clean)) {
// matched, return the choice number // matched, return the choice number
*out = i; *out = i;
return 0; return 0;
} }
} }
printf ("Your choice (%s) did not match any of the choices:\n", clean); fprintf(stderr, "Your choice (%s) did not match any of the choices:\n", clean);
for (i=0; i<n_choices; i++) { for (i=0; i<n_choices; i++) {
if (i>0) printf(", "); if (i>0) printf(", ");
printf("%s", choice[i]); printf("%s", choice[i]);
@ -195,7 +212,6 @@ int ask_string(const char *prompt, char *the_default, char *out)
{ {
char buffer[1024]; char buffer[1024];
char *clean; char *clean;
out[0] = 0;
printf("%s", prompt); printf("%s", prompt);
printf("[%s] ", the_default); printf("[%s] ", the_default);
if (!fgets(buffer, sizeof(buffer), stdin)) if (!fgets(buffer, sizeof(buffer), stdin))
@ -211,66 +227,102 @@ int ask_string(const char *prompt, char *the_default, char *out)
} }
/* produce the image file */ /* produce the image file */
int commit_redolog(const char *flatname, const char *redologname) int commit_redolog()
{ {
int flatfd, redologfd; int flatfd = -1, redologfd;
redolog_header_t header; redolog_header_t header;
Bit32u *catalog, catalog_size; Bit32u *catalog, catalog_size;
Bit8u *bitmap; Bit8u *bitmap;
Bit32u i, bitmap_blocs, extent_blocs; Bit32u i, bitmap_blocks, extent_blocks;
Bit8u buffer[512]; Bit8u buffer[512];
// check if flat file exists if (bxcommit_mode == BXCOMMIT_MODE_COMMIT_UNDOABLE) {
flatfd = open (flatname, O_WRONLY // check if flat file exists
flatfd = open(bx_flat_filename, O_WRONLY
#ifdef O_BINARY #ifdef O_BINARY
| O_BINARY | O_BINARY
#endif #endif
); );
if (flatfd<0) { if (flatfd < 0) {
fatal("ERROR: flat file not found or not writable"); fatal("ERROR: flat file not found or not writable");
}
} }
// Check if redolog exists // Check if redolog exists
printf("%s\n",redologname); printf("\nOpening '%s'\n", bx_redolog_name);
redologfd = open(redologname, O_RDONLY redologfd = open(bx_redolog_name, O_RDONLY
#ifdef O_BINARY #ifdef O_BINARY
| O_BINARY | O_BINARY
#endif #endif
); );
if (redologfd<0) { if (redologfd < 0) {
fatal("ERROR: redolog file not found"); fatal("ERROR: redolog file not found");
} }
printf ("\nReading redolog header: ["); printf ("\nReading redolog header: [");
if (read(redologfd, &header, STANDARD_HEADER_SIZE) != STANDARD_HEADER_SIZE) if (read(redologfd, &header, STANDARD_HEADER_SIZE) != STANDARD_HEADER_SIZE)
fatal("\nERROR: while reading redolog header!"); fatal("\nERROR: while reading redolog header!");
// Print infos on redlog // Print infos on redlog
printf("Type='%s', Subtype='%s', Version=%d.%d] Done.", printf("Type='%s', Subtype='%s', Version=%d.%d] Done.",
header.standard.type, header.standard.subtype, header.standard.type, header.standard.subtype,
dtoh32(header.standard.version)/0x10000, dtoh32(header.standard.version)/0x10000,
dtoh32(header.standard.version)%0x10000); dtoh32(header.standard.version)%0x10000);
printf("\nChecking redolog header: ["); printf("\nChecking redolog header: [");
if (strcmp((char *)header.standard.magic, STANDARD_HEADER_MAGIC) != 0) if (strcmp((char *)header.standard.magic, STANDARD_HEADER_MAGIC) != 0)
fatal("\nERROR: bad magic in redolog header!"); fatal("\nERROR: bad magic in redolog header!");
if (strcmp((char *)header.standard.type, REDOLOG_TYPE) != 0) if (strcmp((char *)header.standard.type, REDOLOG_TYPE) != 0)
fatal("\nERROR: bad type in redolog header!"); fatal("\nERROR: bad type in redolog header!");
if (strcmp((char *)header.standard.subtype, REDOLOG_SUBTYPE_UNDOABLE) != 0) if (bxcommit_mode == BXCOMMIT_MODE_COMMIT_UNDOABLE) {
fatal("\nERROR: bad subtype in redolog header!"); if (strcmp((char *)header.standard.subtype, REDOLOG_SUBTYPE_UNDOABLE) != 0)
fatal("\nERROR: bad subtype in redolog header!");
} else if (bxcommit_mode == BXCOMMIT_MODE_GROWING_TO_FLAT) {
if (strcmp((char *)header.standard.subtype, REDOLOG_SUBTYPE_GROWING) != 0)
fatal("\nERROR: bad subtype in redolog header!");
} else {
fatal("\nERROR: unknown bxcommit mode!");
}
if (header.standard.version != htod32(STANDARD_HEADER_VERSION)) if (header.standard.version != htod32(STANDARD_HEADER_VERSION))
fatal("\nERROR: bad version in redolog header!"); fatal("\nERROR: bad version in redolog header!");
printf("#entries=%d, bitmap size=%d, exent size = %d] Done.", printf("#entries=%d, bitmap size=%d, exent size = %d] Done.",
dtoh32(header.specific.catalog), dtoh32(header.specific.catalog),
dtoh32(header.specific.bitmap), dtoh32(header.specific.bitmap),
dtoh32(header.specific.extent)); dtoh32(header.specific.extent));
if (bxcommit_mode == BXCOMMIT_MODE_GROWING_TO_FLAT) {
flatfd = open(bx_flat_filename, O_RDONLY
#ifdef O_BINARY
| O_BINARY
#endif
);
if (flatfd >= 0) {
close(flatfd);
fatal("ERROR: flat file already exists");
}
printf("\nCreating flat image file: [");
flatfd = open(bx_flat_filename, O_WRONLY | O_CREAT
#ifdef O_BINARY
| O_BINARY
#endif
, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP
);
if (flatfd < 0) {
fatal("ERROR: flat file is not writable");
}
lseek(flatfd, header.specific.disk - 512, SEEK_SET);
if (write(flatfd, buffer, 512) != 512)
fatal("ERROR: while writing block in flat file !");
printf("...] Done.");
}
catalog = (Bit32u*)malloc(dtoh32(header.specific.catalog) * sizeof(Bit32u)); catalog = (Bit32u*)malloc(dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap = (Bit8u*)malloc(dtoh32(header.specific.bitmap)); bitmap = (Bit8u*)malloc(dtoh32(header.specific.bitmap));
printf("\nReading Catalog: ["); printf("\nReading Catalog: [");
@ -279,62 +331,58 @@ int commit_redolog(const char *flatname, const char *redologname)
catalog_size = dtoh32(header.specific.catalog) * sizeof(Bit32u); catalog_size = dtoh32(header.specific.catalog) * sizeof(Bit32u);
if ((Bit32u) read(redologfd, catalog, catalog_size) != catalog_size) if ((Bit32u) read(redologfd, catalog, catalog_size) != catalog_size)
fatal("\nERROR: while reading redolog catalog!"); fatal("\nERROR: while reading redolog catalog!");
printf("] Done."); printf("...] Done.");
printf("\nCommitting changes to flat file: [ 0%%]"); printf("\nCommitting changes to flat file: [ 0%%]");
bitmap_blocs = 1 + (dtoh32(header.specific.bitmap) - 1) / 512; bitmap_blocks = 1 + (dtoh32(header.specific.bitmap) - 1) / 512;
extent_blocs = 1 + (dtoh32(header.specific.extent) - 1) / 512; extent_blocks = 1 + (dtoh32(header.specific.extent) - 1) / 512;
for(i=0; i<dtoh32(header.specific.catalog); i++) for(i=0; i<dtoh32(header.specific.catalog); i++) {
{ printf("\x8\x8\x8\x8\x8%3d%%]", (i+1)*100/dtoh32(header.specific.catalog));
printf("\x8\x8\x8\x8\x8%3d%%]", (i+1)*100/dtoh32(header.specific.catalog)); fflush(stdout);
fflush(stdout);
if (dtoh32(catalog[i]) != REDOLOG_PAGE_NOT_ALLOCATED) { if (dtoh32(catalog[i]) != REDOLOG_PAGE_NOT_ALLOCATED) {
Bit64s bitmap_offset; Bit64s bitmap_offset;
Bit32u bitmap_size, j; Bit32u bitmap_size, j;
bitmap_offset = (Bit64s)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u)); bitmap_offset = (Bit64s)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap_offset += (Bit64s)512 * dtoh32(catalog[i]) * (extent_blocs + bitmap_blocs); bitmap_offset += (Bit64s)512 * dtoh32(catalog[i]) * (extent_blocks + bitmap_blocks);
// Read bitmap // Read bitmap
lseek(redologfd, bitmap_offset, SEEK_SET); lseek(redologfd, bitmap_offset, SEEK_SET);
bitmap_size = dtoh32(header.specific.bitmap); bitmap_size = dtoh32(header.specific.bitmap);
if ((Bit32u) read(redologfd, bitmap, bitmap_size) != bitmap_size) if ((Bit32u) read(redologfd, bitmap, bitmap_size) != bitmap_size)
fatal("\nERROR: while reading bitmap from redolog !"); fatal("\nERROR: while reading bitmap from redolog !");
for(j=0; j<dtoh32(header.specific.bitmap); j++) for(j=0; j<dtoh32(header.specific.bitmap); j++) {
{ Bit32u bit;
Bit32u bit;
for(bit=0; bit<8; bit++) for(bit=0; bit<8; bit++) {
{ if ( (bitmap[j] & (1<<bit)) != 0) {
if ( (bitmap[j] & (1<<bit)) != 0) Bit64s flat_offset, block_offset;
{
Bit64s flat_offset, bloc_offset;
bloc_offset = bitmap_offset + ((Bit64s)512 * (bitmap_blocs + ((j*8)+bit))); block_offset = bitmap_offset + ((Bit64s)512 * (bitmap_blocks + ((j*8)+bit)));
lseek(redologfd, (off_t)bloc_offset, SEEK_SET); lseek(redologfd, (off_t)block_offset, SEEK_SET);
if (read(redologfd, buffer, 512) != 512) if (read(redologfd, buffer, 512) != 512)
fatal("\nERROR: while reading bloc from redolog !"); fatal("\nERROR: while reading block from redolog !");
flat_offset = (Bit64s)i * (dtoh32(header.specific.extent)); flat_offset = (Bit64s)i * (dtoh32(header.specific.extent));
flat_offset += (Bit64s)512 * ((j * 8) + bit); flat_offset += (Bit64s)512 * ((j * 8) + bit);
lseek(flatfd, (off_t)flat_offset, SEEK_SET); lseek(flatfd, (off_t)flat_offset, SEEK_SET);
if (write(flatfd, buffer, 512) != 512) if (write(flatfd, buffer, 512) != 512)
fatal("\nERROR: while writing bloc in flatfile !"); fatal("\nERROR: while writing block in flat file !");
} }
}
} }
} }
}
} }
printf(" Done."); printf(" Done.");
@ -346,33 +394,129 @@ int commit_redolog(const char *flatname, const char *redologname)
return 0; return 0;
} }
int CDECL main() void print_usage()
{
fprintf(stderr,
"Usage: bxcommit [options] [flat filename] [redolog filename]\n\n"
"Supported options:\n"
" -mode=undoable commit undoable redolog to flat file\n"
" -mode=growing create flat disk image from growing disk image\n"
" -d delete redolog file after commit\n"
" -q quiet mode (don't prompt for user input)\n"
" --help display this help and exit\n\n");
}
int parse_cmdline(int argc, char *argv[])
{
int arg = 1;
int ret = 1;
int fnargs = 0;
bxcommit_mode = 0;
bx_remove = 0;
bx_interactive = 1;
bx_flat_filename[0] = 0;
bx_redolog_name[0] = 0;
while ((arg < argc) && (ret == 1)) {
// parse next arg
if (!strcmp("--help", argv[arg]) || !strncmp("/?", argv[arg], 2)) {
print_usage();
ret = 0;
}
else if (!strncmp("-mode=", argv[arg], 6)) {
if (!strcmp(&argv[arg][6], "undoable")) {
bxcommit_mode = BXCOMMIT_MODE_COMMIT_UNDOABLE;
} else if (!strcmp(&argv[arg][6], "growing")) {
bxcommit_mode = BXCOMMIT_MODE_GROWING_TO_FLAT;
} else {
printf("Unknown bxcommit mode '%s'\n\n", &argv[arg][6]);
}
}
else if (!strcmp("-d", argv[arg])) {
bx_remove = 1;
}
else if (!strcmp("-q", argv[arg])) {
bx_interactive = 0;
}
else if (argv[arg][0] == '-') {
printf("Unknown option: %s\n\n", argv[arg]);
ret = 0;
} else {
if (fnargs == 0) {
strcpy(bx_flat_filename, argv[arg]);
} else if (fnargs == 1) {
strcpy(bx_redolog_name, argv[arg]);
} else {
printf("Ignoring extra parameter: %s\n\n", argv[arg]);
}
fnargs++;
}
arg++;
}
if ((bxcommit_mode == 0) ||
(fnargs < 2)) {
bx_interactive = 1;
}
return ret;
}
int CDECL main(int argc, char *argv[])
{ {
char filename[256];
char tmplogname[256]; char tmplogname[256];
char redologname[256];
int remove; if (!parse_cmdline(argc, argv))
myexit(1);
print_banner(); print_banner();
filename[0] = 0;
redologname[0] = 0;
if (ask_string("\nWhat is the flat image name?\n", "c.img", filename) < 0) if (bx_interactive) {
fatal(EOF_ERR); if (ask_int(main_menu_prompt, 0, 2, bxcommit_mode, &bxcommit_mode) < 0)
fatal(EOF_ERR);
snprintf(tmplogname,256,"%s%s", filename, UNDOABLE_REDOLOG_EXTENSION); switch (bxcommit_mode) {
case 0:
myexit(0);
break;
if (ask_string("\nWhat is the redolog name?\n", tmplogname, redologname) < 0) case BXCOMMIT_MODE_COMMIT_UNDOABLE:
fatal(EOF_ERR); if (!strlen(bx_flat_filename)) {
strcpy(bx_flat_filename, "c.img");
}
if (ask_string("\nWhat is the flat image name?\n", bx_flat_filename, bx_flat_filename) < 0)
fatal(EOF_ERR);
if (!strlen(bx_redolog_name)) {
snprintf(tmplogname, 256, "%s%s", bx_flat_filename, UNDOABLE_REDOLOG_EXTENSION);
} else {
strcpy(tmplogname, bx_redolog_name);
}
if (ask_string("\nWhat is the redolog name?\n", tmplogname, bx_redolog_name) < 0)
fatal(EOF_ERR);
if (ask_yn("\nShould the redolog been removed afterwards?\n", 1, &bx_remove) < 0)
fatal(EOF_ERR);
break;
if (ask_yn("\nShall I remove the redolog afterwards?\n", 1, &remove) < 0) case BXCOMMIT_MODE_GROWING_TO_FLAT:
fatal(EOF_ERR); if (!strlen(bx_redolog_name)) {
strcpy(bx_redolog_name, "c.img");
}
if (ask_string("\nWhat is the 'growing' image name?\n", bx_redolog_name, bx_redolog_name) < 0)
fatal(EOF_ERR);
if (!strlen(bx_flat_filename)) {
strcpy(bx_flat_filename, "flat.img");
}
if (ask_string("\nWhat should be the name of the 'flat' image?\n", bx_flat_filename, bx_flat_filename) < 0)
fatal(EOF_ERR);
if (ask_yn("\nShould the 'growing' image been removed afterwards?\n", 0, &bx_remove) < 0)
fatal(EOF_ERR);
break;
}
}
commit_redolog(filename, redologname); commit_redolog();
if (remove) { if (bx_remove) {
if (unlink(redologname) != 0) if (unlink(bx_redolog_name) != 0)
fatal("ERROR: while removing the redolog !\n"); fatal("ERROR: while removing the redolog !\n");
} }
// make picky compilers (c++, gcc) happy, // make picky compilers (c++, gcc) happy,