- sparse: simplified restore code

- redolog class: added support for opening redolog file in read-only mode
- open backups for header check in read-only mode (growing, undoable, volatile, vpc)
This commit is contained in:
Volker Ruppert 2012-10-04 17:01:17 +00:00
parent f69bc016d2
commit fb778e706c
3 changed files with 16 additions and 19 deletions

View File

@ -1181,29 +1181,20 @@ bx_bool sparse_image_t::save_state(const char *backup_fname)
void sparse_image_t::restore_state(const char *backup_fname)
{
sparse_header_t temp_header;
int backup_fd;
Bit64u imgsize = 0;
char *temp_pathname;
int backup_fd = ::open(backup_fname, O_RDONLY
#ifdef O_BINARY
| O_BINARY
#endif
);
if (backup_fd < 0) {
if ((backup_fd = hdimage_open_file(backup_fname, O_RDONLY, &imgsize, NULL)) < 0) {
BX_PANIC(("Could not open sparse image backup"));
return;
}
if (::read(backup_fd, &temp_header, sizeof(header)) != sizeof(header)) {
if (check_format(backup_fd, imgsize) != HDIMAGE_FORMAT_OK) {
::close(backup_fd);
BX_PANIC(("Could not read sparse image header"));
return;
}
::close(backup_fd);
if ((dtoh32(temp_header.magic) != SPARSE_HEADER_MAGIC) ||
(dtoh32(temp_header.version) != SPARSE_HEADER_VERSION)) {
BX_PANIC(("Could not detect sparse image header"));
return;
}
::close(backup_fd);
temp_pathname = strdup(pathname);
close();
if (!hdimage_copy_file(backup_fname, temp_pathname)) {
@ -1418,7 +1409,12 @@ int redolog_t::create(int filedes, const char* type, Bit64u size)
int redolog_t::open(const char* filename, const char *type)
{
fd = ::open(filename, O_RDWR
return open(filename, type, O_RDWR);
}
int redolog_t::open(const char* filename, const char *type, int flags)
{
fd = ::open(filename, flags
#ifdef O_BINARY
| O_BINARY
#endif
@ -1795,7 +1791,7 @@ bx_bool growing_image_t::save_state(const char *backup_fname)
void growing_image_t::restore_state(const char *backup_fname)
{
redolog_t *temp_redolog = new redolog_t();
if (temp_redolog->open(backup_fname, REDOLOG_SUBTYPE_GROWING) < 0) {
if (temp_redolog->open(backup_fname, REDOLOG_SUBTYPE_GROWING, O_RDONLY) < 0) {
delete temp_redolog;
BX_PANIC(("Can't open growing image backup '%s'", backup_fname));
return;
@ -1949,7 +1945,7 @@ bx_bool undoable_image_t::save_state(const char *backup_fname)
void undoable_image_t::restore_state(const char *backup_fname)
{
redolog_t *temp_redolog = new redolog_t();
if (temp_redolog->open(backup_fname, REDOLOG_SUBTYPE_UNDOABLE) < 0) {
if (temp_redolog->open(backup_fname, REDOLOG_SUBTYPE_UNDOABLE, O_RDONLY) < 0) {
delete temp_redolog;
BX_PANIC(("Can't open undoable redolog backup '%s'", backup_fname));
return;
@ -2101,7 +2097,7 @@ bx_bool volatile_image_t::save_state(const char *backup_fname)
void volatile_image_t::restore_state(const char *backup_fname)
{
redolog_t *temp_redolog = new redolog_t();
if (temp_redolog->open(backup_fname, REDOLOG_SUBTYPE_VOLATILE) < 0) {
if (temp_redolog->open(backup_fname, REDOLOG_SUBTYPE_VOLATILE, O_RDONLY) < 0) {
delete temp_redolog;
BX_PANIC(("Can't open volatile redolog backup '%s'", backup_fname));
return;

View File

@ -402,6 +402,7 @@ class redolog_t
int create(const char* filename, const char* type, Bit64u size);
int create(int filedes, const char* type, Bit64u size);
int open(const char* filename, const char* type);
int open(const char* filename, const char* type, int flags);
void close();
Bit64u get_size();
Bit32u get_timestamp();

View File

@ -293,7 +293,7 @@ void vpc_image_t::restore_state(const char *backup_fname)
int temp_fd;
Bit64u imgsize;
if ((temp_fd = hdimage_open_file(backup_fname, O_RDWR, &imgsize, NULL)) < 0) {
if ((temp_fd = hdimage_open_file(backup_fname, O_RDONLY, &imgsize, NULL)) < 0) {
BX_PANIC(("cannot open vpc image backup '%s'", backup_fname));
return;
}