summaryrefslogtreecommitdiff
path: root/flash.c
diff options
context:
space:
mode:
Diffstat (limited to 'flash.c')
-rw-r--r--flash.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/flash.c b/flash.c
index 9bc9e9d..62565d3 100644
--- a/flash.c
+++ b/flash.c
@@ -1,6 +1,4 @@
#include "globals.h"
-#include "lcd.h"
-#include "version.h"
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
@@ -118,6 +116,7 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
// create a buffer for the existing data
void* payload = malloc(total);
if (payload == NULL) {
+ close(fd);
errno = PERSIST_ERR_COULDNTALLOCATEBUFFER;
return false;
}
@@ -126,6 +125,8 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
persistencehdr hdr;
if (read(fd, &hdr, sizeof(persistencehdr)) != sizeof(persistencehdr)) {
errno = PERSIST_ERR_COULDNTREADHDR;
+ free(payload);
+ close(fd);
return false;
}
@@ -133,6 +134,8 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
persistence_printheader(&hdr);
if (read(fd, payload, total) != total) {
errno = PERSIST_ERR_COULDNTREADDATA;
+ free(payload);
+ close(fd);
return false;
}
@@ -140,6 +143,8 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
uint32_t calculatedcrc32 = crc32((uint8_t*) payload, hdr.length);
if (calculatedcrc32 != hdr.crc32) {
errno = PERSIST_ERR_BADCRC32;
+ free(payload);
+ close(fd);
return false;
}
// overlay the payload with the existing data
@@ -164,6 +169,7 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
// write the header
if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
errno = PERSIST_ERR_COULDNTWRITE;
+ close(fd);
return false;
}
@@ -171,12 +177,14 @@ bool persistence_freeze(char* dest, void* data, unsigned int offset, unsigned in
if (lseek(fd, offset, SEEK_CUR) < 0) {
errno = PERSIST_ERR_COULDNTSEEK; // shouldn't ever happen really because if we're actually
// seeking the file should already be the total size.
+ close(fd);
return false;
}
// write the data out to disk
if (write(fd, ((char*) data) + offset, len) != len) {
errno = PERSIST_ERR_COULDNTWRITE;
+ close(fd);
return false;
}