Browse Source

catch opening non-existing file for read on OTA

CaCO3 3 years ago
parent
commit
4e19066e26

+ 5 - 0
code/components/jomjol_fileserver_ota/server_ota.cpp

@@ -99,6 +99,11 @@ static bool ota_update_task(std::string fn)
     int data_read;     
     int data_read;     
 
 
     FILE* f = OpenFileAndWait(fn.c_str(), "rb");     // vorher  nur "r"
     FILE* f = OpenFileAndWait(fn.c_str(), "rb");     // vorher  nur "r"
+
+    if (f == NULL) { // File does not exist
+        return false;
+    }
+
     data_read = fread(ota_write_data, 1, BUFFSIZE, f);
     data_read = fread(ota_write_data, 1, BUFFSIZE, f);
 
 
     while (data_read > 0) {
     while (data_read > 0) {

+ 10 - 1
code/components/jomjol_helper/Helper.cpp

@@ -88,8 +88,17 @@ void memCopyGen(uint8_t* _source, uint8_t* _target, int _size)
 
 
 FILE* OpenFileAndWait(const char* nm, const char* _mode, int _waitsec)
 FILE* OpenFileAndWait(const char* nm, const char* _mode, int _waitsec)
 {
 {
+	FILE *pfile;
+
 	printf("open file %s in mode %s\n", nm, _mode);
 	printf("open file %s in mode %s\n", nm, _mode);
-	FILE *pfile = fopen(nm, _mode);
+
+	if ((pfile = fopen(nm, _mode)) != NULL) {
+		printf("File %s successfully opened\n", nm);
+	}
+	else {
+		printf("Error: file %s does not exist!\n", nm);
+		return NULL;
+	}
 
 
 /*
 /*
 	if (pfile == NULL)
 	if (pfile == NULL)