@@ -442,7 +442,7 @@ bool handleFileRead(AsyncWebServerRequest* request, String path){
442442
443443// copy a file, delete destination file if incomplete to prevent corrupted files
444444bool copyFile (const char * src_path, const char * dst_path) {
445- DEBUG_PRINTF (" copyFile from %s to %s" , src_path, dst_path);
445+ DEBUG_PRINTF (" copyFile from %s to %s\n " , src_path, dst_path);
446446 if (!WLED_FS.exists (src_path)) {
447447 DEBUG_PRINTLN (F (" file not found" ));
448448 return false ;
@@ -481,8 +481,11 @@ bool copyFile(const char* src_path, const char* dst_path) {
481481static const char s_backup_json[] PROGMEM = " .bkp.json" ;
482482
483483bool backupFile (const char * filename) {
484- DEBUG_PRINTF (" backup %s" , filename);
485- if (!WLED_FS.exists (filename)) return false ;
484+ DEBUG_PRINTF (" backup %s \n " , filename);
485+ if (!validateJsonFile (filename)) {
486+ DEBUG_PRINTLN (F (" broken file" ));
487+ return false ;
488+ }
486489 char backupname[32 ];
487490 strcpy (backupname, filename);
488491 strcat (backupname, s_backup_json);
@@ -496,7 +499,7 @@ bool backupFile(const char* filename) {
496499}
497500
498501bool restoreFile (const char * filename) {
499- DEBUG_PRINTF (" restore %s" , filename);
502+ DEBUG_PRINTF (" restore %s \n " , filename);
500503 char backupname[32 ];
501504 strcpy (backupname, filename);
502505 strcat (backupname, s_backup_json);
@@ -506,6 +509,11 @@ bool restoreFile(const char* filename) {
506509 return false ;
507510 }
508511
512+ if (!validateJsonFile (backupname)) {
513+ DEBUG_PRINTLN (F (" broken backup" ));
514+ return false ;
515+ }
516+
509517 if (copyFile (backupname, filename)) {
510518 DEBUG_PRINTLN (F (" restore ok" ));
511519 return true ;
@@ -514,6 +522,21 @@ bool restoreFile(const char* filename) {
514522 return false ;
515523}
516524
525+ bool validateJsonFile (const char * filename) {
526+ if (!WLED_FS.exists (filename)) return false ;
527+ File file = WLED_FS.open (filename, " r" );
528+ if (!file) return false ;
529+ StaticJsonDocument<0 > doc, filter; // https://arduinojson.org/v6/how-to/validate-json/
530+ bool result = deserializeJson (doc, file, DeserializationOption::Filter (filter)) == DeserializationError::Ok;
531+ file.close ();
532+ if (!result) {
533+ DEBUG_PRINTF (" Invalid JSON file %s\n " , filename);
534+ } else {
535+ DEBUG_PRINTF (" Valid JSON file %s\n " , filename);
536+ }
537+ return result;
538+ }
539+
517540// print contents of all files in root dir to Serial except wsec files
518541void dumpFilesToSerial () {
519542 File rootdir = WLED_FS.open (" /" , " r" );
0 commit comments