Skip to content

Commit 76f45a5

Browse files
caco3CaCO3SybexX
authored
Remove html directory on update (#3584)
* delete HTML directory on an update * delete HTML directory on an update * rename html folder * swap HTML folders after extracting * . * . * . * . * . * . * move SD card check, SD card directories setup and update to before the PSRAM init. The update should be as early as possible to allow updates even if the PSRAM or cam fails. * . * . * Update Helper.cpp * Update Helper.h * . --------- Co-authored-by: CaCO3 <[email protected]> Co-authored-by: SybexX <[email protected]>
1 parent 063c482 commit 76f45a5

File tree

6 files changed

+103
-52
lines changed

6 files changed

+103
-52
lines changed

code/components/jomjol_fileserver_ota/server_file.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct file_server_data {
6161

6262
using namespace std;
6363

64-
string SUFFIX_ZW = "_0xge";
64+
string SUFFIX_ZW = "_tmp";
6565

6666
static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file);
6767
static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file);
@@ -911,7 +911,7 @@ void delete_all_in_directory(std::string _directory)
911911
closedir(dir);
912912
}
913913

914-
std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main, bool _initial_setup)
914+
std::string unzip_new(std::string _in_zip_file, std::string _html_tmp, std::string _html_final, std::string _target_bin, std::string _main, bool _initial_setup)
915915
{
916916
int i, sort_iter;
917917
mz_bool status;
@@ -993,10 +993,15 @@ std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::st
993993
}
994994
else
995995
{
996-
zw = _target_zip + zw;
996+
zw = _html_tmp + zw;
997997
}
998998

999999
}
1000+
1001+
// files in the html folder shall be redirected to the temporary html folder
1002+
if (zw.find(_html_final) == 0) {
1003+
FindReplace(zw, _html_final, _html_tmp);
1004+
}
10001005

10011006
string filename_zw = zw + SUFFIX_ZW;
10021007

code/components/jomjol_fileserver_ota/server_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
void register_server_file_uri(httpd_handle_t server, const char *base_path);
1010

1111
void unzip(std::string _in_zip_file, std::string _target_directory);
12-
std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main = "/sdcard/", bool _initial_setup = false);
12+
std::string unzip_new(std::string _in_zip_file, std::string _html_tmp, std::string _html_final, std::string _target_bin, std::string _main = "/sdcard/", bool _initial_setup = false);
1313

1414

1515
void delete_all_in_directory(std::string _directory);

code/components/jomjol_fileserver_ota/server_ota.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,35 @@ void task_do_Update_ZIP(void *pvParameter)
7676

7777
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "File: " + _file_name_update + " Filetype: " + filetype);
7878

79-
8079
if (filetype == "ZIP")
8180
{
82-
std::string in, out, outbin, zw, retfirmware;
81+
std::string in, outHtml, outHtmlTmp, outHtmlOld, outbin, zw, retfirmware;
8382

84-
out = "/sdcard/html";
83+
outHtml = "/sdcard/html";
84+
outHtmlTmp = "/sdcard/html_tmp";
85+
outHtmlOld = "/sdcard/html_old";
8586
outbin = "/sdcard/firmware";
8687

87-
retfirmware = unzip_new(_file_name_update, out+"/", outbin+"/", "/sdcard/", initial_setup);
88+
/* Remove the old and tmp html folder in case they still exist */
89+
removeFolder(outHtmlTmp.c_str(), TAG);
90+
removeFolder(outHtmlOld.c_str(), TAG);
91+
92+
/* Extract the ZIP file. The content of the html folder gets extracted to the temporar folder html-temp. */
93+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Extracting ZIP file " + _file_name_update + "...");
94+
retfirmware = unzip_new(_file_name_update, outHtmlTmp+"/", outHtml+"/", outbin+"/", "/sdcard/", initial_setup);
8895
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Files unzipped.");
8996

97+
/* ZIP file got extracted, replace the old html folder with the new one */
98+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Renaming folder " + outHtml + " to " + outHtmlOld + "...");
99+
RenameFolder(outHtml, outHtmlOld);
100+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Renaming folder " + outHtmlTmp + " to " + outHtml + "...");
101+
RenameFolder(outHtmlTmp, outHtml);
102+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Deleting folder " + outHtmlOld + "...");
103+
removeFolder(outHtmlOld.c_str(), TAG);
104+
90105
if (retfirmware.length() > 0)
91106
{
92-
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Found firmware.bin");
107+
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Found firmware.bin");
93108
ota_update_task(retfirmware);
94109
}
95110

@@ -434,7 +449,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
434449
return ESP_OK;
435450
}
436451

437-
438452
if ((filetype == "TFLITE") || (filetype == "TFL"))
439453
{
440454
std::string out = "/sdcard/config/" + getFileFullFileName(fn);

code/components/jomjol_helper/Helper.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,19 +365,35 @@ size_t findDelimiterPos(string input, string delimiter)
365365

366366
bool RenameFile(string from, string to)
367367
{
368-
// ESP_LOGI(logTag, "Deleting file: %s", fn.c_str());
369-
/* Delete file */
368+
// ESP_LOGI(logTag, "Renaming File: %s", from.c_str());
370369
FILE *fpSourceFile = fopen(from.c_str(), "rb");
371370

372-
// Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
371+
// Sourcefile does not exist otherwise there is a mistake when renaming!
373372
if (!fpSourceFile)
374373
{
375-
ESP_LOGE(TAG, "DeleteFile: File %s existiert nicht!", from.c_str());
374+
ESP_LOGE(TAG, "RenameFile: File %s does not exist!", from.c_str());
376375
return false;
377376
}
378377

379378
fclose(fpSourceFile);
379+
rename(from.c_str(), to.c_str());
380380

381+
return true;
382+
}
383+
384+
bool RenameFolder(string from, string to)
385+
{
386+
// ESP_LOGI(logTag, "Renaming Folder: %s", from.c_str());
387+
DIR *fpSourceFolder = opendir(from.c_str());
388+
389+
// Sourcefolder does not exist otherwise there is a mistake when renaming!
390+
if (!fpSourceFolder)
391+
{
392+
ESP_LOGE(TAG, "RenameFolder: Folder %s does not exist!", from.c_str());
393+
return false;
394+
}
395+
396+
closedir(fpSourceFolder);
381397
rename(from.c_str(), to.c_str());
382398

383399
return true;
@@ -387,7 +403,7 @@ bool FileExists(string filename)
387403
{
388404
FILE *fpSourceFile = fopen(filename.c_str(), "rb");
389405

390-
// Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
406+
// Sourcefile does not exist
391407
if (!fpSourceFile)
392408
{
393409
return false;
@@ -398,22 +414,36 @@ bool FileExists(string filename)
398414
return true;
399415
}
400416

401-
bool DeleteFile(string fn)
417+
bool FolderExists(string foldername)
402418
{
403-
// ESP_LOGI(logTag, "Deleting file: %s", fn.c_str());
419+
DIR *fpSourceFolder = opendir(foldername.c_str());
420+
421+
// Sourcefolder does not exist
422+
if (!fpSourceFolder)
423+
{
424+
return false;
425+
}
426+
427+
closedir(fpSourceFolder);
428+
429+
return true;
430+
}
431+
432+
bool DeleteFile(string filename)
433+
{
434+
// ESP_LOGI(logTag, "Deleting file: %s", filename.c_str());
404435
/* Delete file */
405-
FILE *fpSourceFile = fopen(fn.c_str(), "rb");
436+
FILE *fpSourceFile = fopen(filename.c_str(), "rb");
406437

407-
// Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
438+
// Sourcefile does not exist otherwise there is a mistake in copying!
408439
if (!fpSourceFile)
409440
{
410-
ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", fn.c_str());
441+
ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", filename.c_str());
411442
return false;
412443
}
413444

414445
fclose(fpSourceFile);
415-
416-
unlink(fn.c_str());
446+
unlink(filename.c_str());
417447

418448
return true;
419449
}

code/components/jomjol_helper/Helper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ std::size_t file_size(const std::string& file_name);
1616
void FindReplace(std::string& line, std::string& oldString, std::string& newString);
1717

1818
bool CopyFile(string input, string output);
19-
bool DeleteFile(string fn);
19+
bool DeleteFile(string filename);
2020
bool RenameFile(string from, string to);
21+
bool RenameFolder(string from, string to);
2122
bool MakeDir(std::string _what);
2223
bool FileExists(string filename);
24+
bool FolderExists(string foldername);
2325

2426
string RundeOutput(double _in, int _anzNachkomma);
2527

code/main/main.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,35 @@ extern "C" void app_main(void)
254254
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "==================== Start ======================");
255255
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "=================================================");
256256

257+
// SD card: basic R/W check
258+
// ********************************************
259+
int iSDCardStatus = SDCardCheckRW();
260+
if (iSDCardStatus < 0) {
261+
if (iSDCardStatus <= -1 && iSDCardStatus >= -2) { // write error
262+
StatusLED(SDCARD_CHECK, 1, true);
263+
}
264+
else if (iSDCardStatus <= -3 && iSDCardStatus >= -5) { // read error
265+
StatusLED(SDCARD_CHECK, 2, true);
266+
}
267+
else if (iSDCardStatus == -6) { // delete error
268+
StatusLED(SDCARD_CHECK, 3, true);
269+
}
270+
setSystemStatusFlag(SYSTEM_STATUS_SDCARD_CHECK_BAD); // reduced web interface going to be loaded
271+
}
272+
273+
// SD card: Create further mandatory directories (if not already existing)
274+
// Correct creation of these folders will be checked with function "SDCardCheckFolderFilePresence"
275+
// ********************************************
276+
MakeDir("/sdcard/firmware"); // mandatory for OTA firmware update
277+
MakeDir("/sdcard/img_tmp"); // mandatory for setting up alignment marks
278+
MakeDir("/sdcard/demo"); // mandatory for demo mode
279+
MakeDir("/sdcard/config/certs"); // mandatory for mqtt certificates
280+
281+
// Check for updates
282+
// ********************************************
283+
CheckOTAUpdate();
284+
CheckUpdate();
285+
257286
// Init external PSRAM
258287
// ********************************************
259288
esp_err_t PSRAMStatus = esp_psram_init();
@@ -352,22 +381,6 @@ extern "C" void app_main(void)
352381
}
353382
}
354383

355-
// SD card: basic R/W check
356-
// ********************************************
357-
int iSDCardStatus = SDCardCheckRW();
358-
if (iSDCardStatus < 0) {
359-
if (iSDCardStatus <= -1 && iSDCardStatus >= -2) { // write error
360-
StatusLED(SDCARD_CHECK, 1, true);
361-
}
362-
else if (iSDCardStatus <= -3 && iSDCardStatus >= -5) { // read error
363-
StatusLED(SDCARD_CHECK, 2, true);
364-
}
365-
else if (iSDCardStatus == -6) { // delete error
366-
StatusLED(SDCARD_CHECK, 3, true);
367-
}
368-
setSystemStatusFlag(SYSTEM_STATUS_SDCARD_CHECK_BAD); // reduced web interface going to be loaded
369-
}
370-
371384
// Migrate parameter in config.ini to new naming (firmware 15.0 and newer)
372385
// ********************************************
373386
migrateConfiguration();
@@ -380,19 +393,6 @@ extern "C" void app_main(void)
380393
// ********************************************
381394
setCpuFrequency();
382395

383-
// SD card: Create further mandatory directories (if not already existing)
384-
// Correct creation of these folders will be checked with function "SDCardCheckFolderFilePresence"
385-
// ********************************************
386-
MakeDir("/sdcard/firmware"); // mandatory for OTA firmware update
387-
MakeDir("/sdcard/img_tmp"); // mandatory for setting up alignment marks
388-
MakeDir("/sdcard/demo"); // mandatory for demo mode
389-
MakeDir("/sdcard/config/certs"); // mandatory for mqtt certificates
390-
391-
// Check for updates
392-
// ********************************************
393-
CheckOTAUpdate();
394-
CheckUpdate();
395-
396396
// Start SoftAP for initial remote setup
397397
// Note: Start AP if no wlan.ini and/or config.ini available, e.g. SD card empty; function does not exit anymore until reboot
398398
// ********************************************

0 commit comments

Comments
 (0)