From 99cd66496187edf6e2c360c1a4d85475ac7df30e Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 21 Jan 2009 15:02:27 -0600 Subject: [PATCH 1/2] better save_image that doesn't corrupt your factor image if you're out of disk space. should throw exception instead of printing error messages if saving fails.. --- vm/image.c | 12 +++++++++++- vm/os-unix.h | 2 ++ vm/os-windows.h | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/vm/image.c b/vm/image.c index 5f4492e537..08aae5898c 100755 --- a/vm/image.c +++ b/vm/image.c @@ -112,7 +112,9 @@ bool save_image(const F_CHAR *filename) FILE* file; F_HEADER h; - file = OPEN_WRITE(filename); + F_CHAR temporary_filename[] = "##saving-factor-image##"; + + file = OPEN_WRITE(temporary_filename); if(file == NULL) { print_string("Cannot open image file: "); print_native_string(filename); nl(); @@ -163,6 +165,14 @@ bool save_image(const F_CHAR *filename) return false; } + if(MOVE_FILE_FAILS(temporary_filename, filename)) + { + print_string("Failed to rename tempoarary image file: "); print_string(strerror(errno)); nl(); + if(DELETE_FILE_FAILS(temporary_filename)) + print_string("Failed to clean up temporary image file: "); print_string(strerror(errno)); nl(); + return false; + } + return true; } diff --git a/vm/os-unix.h b/vm/os-unix.h index d2f34b4bc4..9f911acded 100755 --- a/vm/os-unix.h +++ b/vm/os-unix.h @@ -22,6 +22,8 @@ typedef char F_SYMBOL; #define STRCMP strcmp #define STRNCMP strncmp #define STRDUP strdup +#define MOVE_FILE_FAILS(old,new) (rename((old),(new)) < 0) +#define DELETE_FILE_FAILS(old) (unlink((old)) < 0) #define FIXNUM_FORMAT "%ld" #define CELL_FORMAT "%lu" diff --git a/vm/os-windows.h b/vm/os-windows.h index a9c3f6d803..beec7ad37c 100755 --- a/vm/os-windows.h +++ b/vm/os-windows.h @@ -19,6 +19,8 @@ typedef wchar_t F_CHAR; #define STRCMP wcscmp #define STRNCMP wcsncmp #define STRDUP _wcsdup +#define MOVE_FILE_FAILS(old,new) (MoveFile((old),(new)) == 0) +#define DELETE_FILE_FAILS(old) (DeleteFile((old)) == 0) #ifdef WIN64 #define CELL_FORMAT "%Iu" From a9277c71fd734ece4b78f8e915f49cf547709c23 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 21 Jan 2009 18:07:26 -0600 Subject: [PATCH 2/2] fix macosx initialization of executable_path --- vm/factor.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vm/factor.c b/vm/factor.c index b3020e3171..d9042c9455 100755 --- a/vm/factor.c +++ b/vm/factor.c @@ -53,8 +53,7 @@ INLINE bool factor_arg(const F_CHAR* str, const F_CHAR* arg, CELL* value) void init_parameters_from_args(F_PARAMETERS *p, int argc, F_CHAR **argv) { default_parameters(p); - const F_CHAR *executable_path = vm_executable_path(); - p->executable_path = executable_path ? executable_path : argv[0]; + p->executable_path = argv[0]; int i = 0; @@ -106,6 +105,11 @@ void init_factor(F_PARAMETERS *p) /* OS-specific initialization */ early_init(); + const F_CHAR *executable_path = vm_executable_path(); + + if(executable_path) + p->executable_path = executable_path; + if(p->image_path == NULL) p->image_path = default_image_path();