From ce531762ff2cd0e33a01c67f3ce856ffa880e908 Mon Sep 17 00:00:00 2001 From: "Michael J. Chudobiak" Date: Wed, 22 Aug 2012 15:16:00 -0400 Subject: flash copy/load fix --- flash.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/flash.c b/flash.c index d2bbbea..9861b84 100644 --- a/flash.c +++ b/flash.c @@ -52,11 +52,19 @@ static void persistence_printheader(persistencehdr* hdr) } // copy a file from one place to another.. this is not portable, linux only +// this returns true if it is safe to continue. If there was nothing to backup +// that is considered "safe to continue" bool persistence_copyfile(char* source, char* dest) { mode_t filemode = S_IRUSR | S_IWUSR; - int src = open(source, O_RDONLY | O_CREAT); // should create the file for us if this is the first run - // You would think that O_RDONLY would stop the file creation, but it seems to work + + int src = open(source, O_RDONLY); + + if (src < 0 && errno == ENOENT) { // If the source file doesn't exist, there isn't anything to copy + close(src); + return true; + } + int dst = open(dest, O_SYNC | O_RDWR | O_CREAT, filemode); if (src < 0 || dst < 0) { @@ -207,6 +215,7 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v persistencehdr hdr; if (read(fd, &hdr, sizeof(persistencehdr)) != sizeof(persistencehdr)) { errno = PERSIST_ERR_COULDNTREADHDR; + close(fd); return false; } @@ -215,6 +224,7 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v // check that the length of this frozen object is what we are expecting if (hdr.length != len) { errno = PERSIST_ERR_HDRLENMISMATCH; + close(fd); return false; } @@ -222,6 +232,7 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v // but if you want to change the header at some point it'll be useful if (hdr.version != version) { errno = PERSIST_ERR_VERSIONMISMATCH; + close(fd); return false; } @@ -229,6 +240,7 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v // that the header said there was the header is either wrong or the file is truncated. if (read(fd, result, hdr.length) != hdr.length) { errno = PERSIST_ERR_COULDNTREADDATA; + close(fd); return false; } @@ -237,9 +249,11 @@ bool persistence_unfreeze(char* dest, void* result, unsigned int len, uint32_t v if (calculatedcrc32 != hdr.crc32) { printf("Calculated CRC is 0x%08"PRIx32"\n", calculatedcrc32); errno = PERSIST_ERR_BADCRC32; + close(fd); return false; } + close(fd); return true; } @@ -702,7 +716,7 @@ static void initFlashValues(FlashStruct *mem) mem->extended_relay_delay_in_sec=0.5; mem->wait_states_after_sock_init=10000; - /*0123456789012345678901234567890123456789*/ + /*0123456789012345678901234567890123456789*/ strcpy(mem->aux_error_message,"PRF too high! Output disabled."); @@ -746,14 +760,14 @@ void initFlash(FlashStruct *mem, int starting_location) int read_size = readUserBlock(mem); if ( (read_size == 0) || - (mem->fully_programmed == Not_Programmed) || - ((starting_location > 0) && (starting_location < sizeof(*mem))) ) { + (mem->fully_programmed == Not_Programmed) || + ((starting_location > 0) && (starting_location < sizeof(*mem))) ) { g_print_debug ("initializing flash memory\n"); LCD_write(0,0,"Initialize Flash Memory ..."); gchar *message = g_strdup_printf ("Initialize Flash Memory, %d - %d", starting_location, (int) sizeof(*mem)); - LCD_write(0,0,message); + LCD_write(0,0,message); g_free (message); // uninitialized device! @@ -766,7 +780,7 @@ void initFlash(FlashStruct *mem, int starting_location) readUserBlock(mem); } - LCD_write(1,0,"Flash Init, Done! "); + LCD_write(1,0,"Flash Init, Done! "); } } -- cgit