- 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
#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 *rcsid = "$Id$";
char *svnid = "$Id$";
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)
{
@ -75,8 +92,8 @@ void print_banner()
{
printf("%s\n", divider);
bx_center_print(stdout, "bxcommit\n", 72);
bx_center_print(stdout, "Undoable Disk Image Commit Tool for Bochs\n", 72);
bx_center_print(stdout, rcsid, 72);
bx_center_print(stdout, "Growing / Undoable Disk Image Commit Tool for Bochs\n", 72);
bx_center_print(stdout, svnid, 72);
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));
if (illegal || n<min || n>max) {
printf ("Your choice (%s) was not an integer between %d and %d.\n\n",
clean, min, max);
printf("Your choice (%s) was not an integer between %d and %d.\n\n",
clean, min, max);
} else {
// choice is okay
*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++) {
if (!strcmp(choice[i], clean)) {
// matched, return the choice number
*out = i;
return 0;
// matched, return the choice number
*out = i;
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++) {
if (i>0) printf(", ");
printf("%s", choice[i]);
@ -195,7 +212,6 @@ int ask_string(const char *prompt, char *the_default, char *out)
{
char buffer[1024];
char *clean;
out[0] = 0;
printf("%s", prompt);
printf("[%s] ", the_default);
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 */
int commit_redolog(const char *flatname, const char *redologname)
int commit_redolog()
{
int flatfd, redologfd;
int flatfd = -1, redologfd;
redolog_header_t header;
Bit32u *catalog, catalog_size;
Bit8u *bitmap;
Bit32u i, bitmap_blocs, extent_blocs;
Bit32u i, bitmap_blocks, extent_blocks;
Bit8u buffer[512];
// check if flat file exists
flatfd = open (flatname, O_WRONLY
if (bxcommit_mode == BXCOMMIT_MODE_COMMIT_UNDOABLE) {
// check if flat file exists
flatfd = open(bx_flat_filename, O_WRONLY
#ifdef O_BINARY
| O_BINARY
| O_BINARY
#endif
);
if (flatfd<0) {
fatal("ERROR: flat file not found or not writable");
if (flatfd < 0) {
fatal("ERROR: flat file not found or not writable");
}
}
// Check if redolog exists
printf("%s\n",redologname);
redologfd = open(redologname, O_RDONLY
printf("\nOpening '%s'\n", bx_redolog_name);
redologfd = open(bx_redolog_name, O_RDONLY
#ifdef O_BINARY
| O_BINARY
| O_BINARY
#endif
);
if (redologfd<0) {
);
if (redologfd < 0) {
fatal("ERROR: redolog file not found");
}
printf ("\nReading redolog header: [");
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
printf("Type='%s', Subtype='%s', Version=%d.%d] Done.",
header.standard.type, header.standard.subtype,
dtoh32(header.standard.version)/0x10000,
dtoh32(header.standard.version)%0x10000);
header.standard.type, header.standard.subtype,
dtoh32(header.standard.version)/0x10000,
dtoh32(header.standard.version)%0x10000);
printf("\nChecking redolog header: [");
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)
fatal("\nERROR: bad type in redolog header!");
fatal("\nERROR: bad type in redolog header!");
if (strcmp((char *)header.standard.subtype, REDOLOG_SUBTYPE_UNDOABLE) != 0)
fatal("\nERROR: bad subtype in redolog header!");
if (bxcommit_mode == BXCOMMIT_MODE_COMMIT_UNDOABLE) {
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))
fatal("\nERROR: bad version in redolog header!");
fatal("\nERROR: bad version in redolog header!");
printf("#entries=%d, bitmap size=%d, exent size = %d] Done.",
dtoh32(header.specific.catalog),
dtoh32(header.specific.bitmap),
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));
bitmap = (Bit8u*)malloc(dtoh32(header.specific.bitmap));
printf("\nReading Catalog: [");
@ -279,62 +331,58 @@ int commit_redolog(const char *flatname, const char *redologname)
catalog_size = dtoh32(header.specific.catalog) * sizeof(Bit32u);
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%%]");
bitmap_blocs = 1 + (dtoh32(header.specific.bitmap) - 1) / 512;
extent_blocs = 1 + (dtoh32(header.specific.extent) - 1) / 512;
bitmap_blocks = 1 + (dtoh32(header.specific.bitmap) - 1) / 512;
extent_blocks = 1 + (dtoh32(header.specific.extent) - 1) / 512;
for(i=0; i<dtoh32(header.specific.catalog); i++)
{
printf("\x8\x8\x8\x8\x8%3d%%]", (i+1)*100/dtoh32(header.specific.catalog));
fflush(stdout);
for(i=0; i<dtoh32(header.specific.catalog); i++) {
printf("\x8\x8\x8\x8\x8%3d%%]", (i+1)*100/dtoh32(header.specific.catalog));
fflush(stdout);
if (dtoh32(catalog[i]) != REDOLOG_PAGE_NOT_ALLOCATED) {
Bit64s bitmap_offset;
Bit32u bitmap_size, j;
if (dtoh32(catalog[i]) != REDOLOG_PAGE_NOT_ALLOCATED) {
Bit64s bitmap_offset;
Bit32u bitmap_size, j;
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)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap_offset += (Bit64s)512 * dtoh32(catalog[i]) * (extent_blocks + bitmap_blocks);
// Read bitmap
lseek(redologfd, bitmap_offset, SEEK_SET);
// Read bitmap
lseek(redologfd, bitmap_offset, SEEK_SET);
bitmap_size = dtoh32(header.specific.bitmap);
if ((Bit32u) read(redologfd, bitmap, bitmap_size) != bitmap_size)
fatal("\nERROR: while reading bitmap from redolog !");
bitmap_size = dtoh32(header.specific.bitmap);
if ((Bit32u) read(redologfd, bitmap, bitmap_size) != bitmap_size)
fatal("\nERROR: while reading bitmap from redolog !");
for(j=0; j<dtoh32(header.specific.bitmap); j++)
{
Bit32u bit;
for(j=0; j<dtoh32(header.specific.bitmap); j++) {
Bit32u bit;
for(bit=0; bit<8; bit++)
{
if ( (bitmap[j] & (1<<bit)) != 0)
{
Bit64s flat_offset, bloc_offset;
for(bit=0; bit<8; bit++) {
if ( (bitmap[j] & (1<<bit)) != 0) {
Bit64s flat_offset, block_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)
fatal("\nERROR: while reading bloc from redolog !");
if (read(redologfd, buffer, 512) != 512)
fatal("\nERROR: while reading block from redolog !");
flat_offset = (Bit64s)i * (dtoh32(header.specific.extent));
flat_offset += (Bit64s)512 * ((j * 8) + bit);
flat_offset = (Bit64s)i * (dtoh32(header.specific.extent));
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)
fatal("\nERROR: while writing bloc in flatfile !");
}
}
if (write(flatfd, buffer, 512) != 512)
fatal("\nERROR: while writing block in flat file !");
}
}
}
}
}
}
printf(" Done.");
@ -346,33 +394,129 @@ int commit_redolog(const char *flatname, const char *redologname)
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 redologname[256];
int remove;
if (!parse_cmdline(argc, argv))
myexit(1);
print_banner();
filename[0] = 0;
redologname[0] = 0;
if (ask_string("\nWhat is the flat image name?\n", "c.img", filename) < 0)
fatal(EOF_ERR);
if (bx_interactive) {
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)
fatal(EOF_ERR);
case BXCOMMIT_MODE_COMMIT_UNDOABLE:
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)
fatal(EOF_ERR);
case BXCOMMIT_MODE_GROWING_TO_FLAT:
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 (unlink(redologname) != 0)
fatal("ERROR: while removing the redolog !\n");
if (bx_remove) {
if (unlink(bx_redolog_name) != 0)
fatal("ERROR: while removing the redolog !\n");
}
// make picky compilers (c++, gcc) happy,