VM: make save-image throw exception on error (#1090)

db4
Björn Lindqvist 2015-09-30 23:31:47 +02:00
parent 2536b6cd93
commit 85784c5265
2 changed files with 20 additions and 15 deletions

View File

@ -6,6 +6,13 @@ IN: memory.tests
[ save-image-and-exit ] must-fail [ save-image-and-exit ] must-fail
[ "does/not/exist" save-image ] must-fail
[
os windows? "C:\\windows\\hello-windows" "/usr/bin/hello-unix" ?
save-image
] must-fail
! Tests for 'instances' ! Tests for 'instances'
[ [ ] instances ] must-infer [ [ ] instances ] must-infer
2 [ [ [ 3 throw ] instances ] must-fail ] times 2 [ [ [ 3 throw ] instances ] must-fail ] times

View File

@ -195,7 +195,9 @@ void factor_vm::load_image(vm_parameters* p) {
special_objects[OBJ_IMAGE] = allot_alien(false_object, (cell)p->image_path); special_objects[OBJ_IMAGE] = allot_alien(false_object, (cell)p->image_path);
} }
/* Save the current image to disk */ /* Save the current image to disk. We don't throw any exceptions here
because if the 'then-die' argument is t it is not safe to do
so. Instead we signal failure by returning false. */
bool factor_vm::save_image(const vm_char* saving_filename, bool factor_vm::save_image(const vm_char* saving_filename,
const vm_char* filename) { const vm_char* filename) {
image_header h; image_header h;
@ -218,26 +220,18 @@ bool factor_vm::save_image(const vm_char* saving_filename,
FILE* file = OPEN_WRITE(saving_filename); FILE* file = OPEN_WRITE(saving_filename);
if (file == NULL) if (file == NULL)
goto error;
if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1)
goto error;
if (safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1)
goto error;
if (safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1)
goto error;
if (raw_fclose(file) == -1)
goto error;
if (!move_file(saving_filename, filename))
goto error;
return true;
error:
std::cout << "save_image failed." << std::endl;
char *msg = threadsafe_strerror(errno);
std::cout << "strerror:4: " << msg << std::endl;
free(msg);
return false; return false;
if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1)
return false;
if (safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1)
return false;
if (safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1)
return false;
if (raw_fclose(file) == -1)
return false;
if (!move_file(saving_filename, filename))
return false;
return true;
} }
/* Allocates memory */ /* Allocates memory */
@ -276,6 +270,10 @@ void factor_vm::primitive_save_image() {
} }
free(path1_saved); free(path1_saved);
free(path2_saved); free(path2_saved);
if (!ret) {
general_error(ERROR_IO, tag_fixnum(errno), false_object);
}
} }
bool factor_vm::embedded_image_p() { bool factor_vm::embedded_image_p() {