]> gitweb.factorcode.org Git - factor.git/commitdiff
VM: make save-image throw exception on error (#1090)
authorBjörn Lindqvist <bjourne@gmail.com>
Wed, 30 Sep 2015 21:31:47 +0000 (23:31 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Wed, 30 Sep 2015 21:31:47 +0000 (23:31 +0200)
core/memory/memory-tests.factor
vm/image.cpp

index 4f2fe9601c4caa1be91cc20b00a53c313719c2ef..6aa97a6838531b4ef53dc85007b903b8ae793d54 100644 (file)
@@ -6,6 +6,13 @@ IN: memory.tests
 
 [ 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'
 [ [ ] instances ] must-infer
 2 [ [ [ 3 throw ] instances ] must-fail ] times
index 85548d3c4a4ed94d730576572abd5ceb302f4fe4..b1b982aa47aafb5fa4c5fb78257383a1da5346a5 100644 (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);
 }
 
-/* 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,
                            const vm_char* filename) {
   image_header h;
@@ -218,26 +220,18 @@ bool factor_vm::save_image(const vm_char* saving_filename,
 
   FILE* file = OPEN_WRITE(saving_filename);
   if (file == NULL)
-    goto error;
+    return false;
   if (safe_fwrite(&h, sizeof(image_header), 1, file) != 1)
-    goto error;
+    return false;
   if (safe_fwrite((void*)data->tenured->start, h.data_size, 1, file) != 1)
-    goto error;
+    return false;
   if (safe_fwrite((void*)code->allocator->start, h.code_size, 1, file) != 1)
-    goto error;
+    return false;
   if (raw_fclose(file) == -1)
-    goto error;
+    return false;
   if (!move_file(saving_filename, filename))
-    goto error;
+    return false;
   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;
-
 }
 
 /* Allocates memory */
@@ -276,6 +270,10 @@ void factor_vm::primitive_save_image() {
   }
   free(path1_saved);
   free(path2_saved);
+
+  if (!ret) {
+    general_error(ERROR_IO, tag_fixnum(errno), false_object);
+  }
 }
 
 bool factor_vm::embedded_image_p() {