From b6a0b685943ecb4fa68f0305964c2334fb55abbc Mon Sep 17 00:00:00 2001
From: Chris Greening <chris@cmgresearch.com>
Date: Sun, 3 Oct 2021 18:07:41 +0100
Subject: [PATCH] Try and fix SPIFFS not always writing files

---
 lib/Epub/EpubList/EpubList.cpp                | 27 +++++--
 lib/Epub/Renderer/EpdRenderer.h               | 74 +++++++++++++------
 .../RubbishHtmlParser/RubbishHtmlParser.cpp   |  1 +
 sdkconfig.lilygo_t5_47                        |  2 +-
 sdkconfig.lilygo_t5_47.old                    |  6 +-
 5 files changed, 76 insertions(+), 34 deletions(-)

diff --git a/lib/Epub/EpubList/EpubList.cpp b/lib/Epub/EpubList/EpubList.cpp
index 4e250fe..5043640 100644
--- a/lib/Epub/EpubList/EpubList.cpp
+++ b/lib/Epub/EpubList/EpubList.cpp
@@ -105,21 +105,34 @@ bool EpubList::load(const char *path)
 void EpubList::dehydrate()
 {
   ESP_LOGI(TAG, "Dehydrating epub list");
-  FILE *fp = fopen("/fs/.epublist", "wb");
-  fwrite(state, sizeof(EpubListState), 1, fp);
-  fclose(fp);
-  ESP_LOGI(TAG, "Dehydrated epub list");
+  FILE *fp = fopen("/fs/.epublist", "w");
+  if (fp)
+  {
+    int written = fwrite(state, sizeof(EpubListState), 1, fp);
+    if (written == 0)
+    {
+      ESP_LOGI(TAG, "Failed to write epub list state - trying again");
+      // try again?
+      written = fwrite(state, sizeof(EpubListState), 1, fp);
+    }
+    fclose(fp);
+    ESP_LOGI(TAG, "Dehydrated epub list %d", written);
+  }
+  else
+  {
+    ESP_LOGE(TAG, "Failed to dehydrate epub list");
+  }
 }
 
 bool EpubList::hydrate()
 {
   ESP_LOGI(TAG, "Hydrating epub list");
-  FILE *fp = fopen("/fs/.epublist", "rb");
+  FILE *fp = fopen("/fs/.epublist", "r");
   if (fp)
   {
-    fread(state, sizeof(EpubListState), 1, fp);
+    int read = fread(state, sizeof(EpubListState), 1, fp);
     fclose(fp);
-    ESP_LOGI(TAG, "Hydrated epub list");
+    ESP_LOGI(TAG, "Hydrated epub list %d", read);
     return true;
   }
   ESP_LOGE(TAG, "Failed to hydrate");
diff --git a/lib/Epub/Renderer/EpdRenderer.h b/lib/Epub/Renderer/EpdRenderer.h
index f27d7e4..1918c0e 100644
--- a/lib/Epub/Renderer/EpdRenderer.h
+++ b/lib/Epub/Renderer/EpdRenderer.h
@@ -157,33 +157,47 @@ class EpdRenderer : public Renderer
   {
     ESP_LOGI("EPD", "Dehydrating EPD");
     // save the two buffers - the front and the back buffers
-    size_t compressed_size = 0;
-    void *compressed = tdefl_compress_mem_to_heap(m_frame_buffer, EPD_WIDTH * EPD_HEIGHT / 2, &compressed_size, 0);
-    if (compressed)
+    size_t front_buffer_compressed_size = 0;
+    void *front_buffer_compressed = tdefl_compress_mem_to_heap(m_frame_buffer, EPD_WIDTH * EPD_HEIGHT / 2, &front_buffer_compressed_size, 0);
+    if (front_buffer_compressed)
     {
-      ESP_LOGD("EPD", "Front buffer compressed size: %d", compressed_size);
-      FILE *fp = fopen("/fs/front_buffer.z", "wb");
-      if (fp)
+      ESP_LOGI("EPD", "Front buffer compressed size: %d", front_buffer_compressed_size);
+      FILE *front_fp = fopen("/fs/front_buffer.z", "w");
+      if (front_fp)
       {
-        fwrite(compressed, 1, compressed_size, fp);
-        fclose(fp);
+        int written = fwrite(front_buffer_compressed, 1, front_buffer_compressed_size, front_fp);
+        if (written == 0)
+        {
+          // try again?
+          ESP_LOGI("EPD", "Retrying front buffer save");
+          written = fwrite(front_buffer_compressed, 1, front_buffer_compressed_size, front_fp);
+        }
+        fclose(front_fp);
+        ESP_LOGI("EPD", "Front buffer saved %d", written);
       }
-      free(compressed);
+      free(front_buffer_compressed);
     }
     else
     {
       ESP_LOGE("EPD", "Failed to compress front buffer");
     }
-    compressed_size = 0;
-    compressed = tdefl_compress_mem_to_heap(m_hl.back_fb, EPD_WIDTH * EPD_HEIGHT / 2, &compressed_size, 0);
-    if (compressed)
+    size_t back_buffer_compressed_size = 0;
+    void *back_buffer_compressed = tdefl_compress_mem_to_heap(m_hl.back_fb, EPD_WIDTH * EPD_HEIGHT / 2, &back_buffer_compressed_size, 0);
+    if (back_buffer_compressed)
     {
-      ESP_LOGD("EPD", "Back buffer compressed size: %d", compressed_size);
-      FILE *fp = fopen("/fs/back_buffer.z", "wb");
-      if (fp)
+      ESP_LOGI("EPD", "Back buffer compressed size: %d", back_buffer_compressed_size);
+      FILE *back_fp = fopen("/fs/back_buffer.z", "w");
+      if (back_fp)
       {
-        fwrite(compressed, 1, compressed_size, fp);
-        fclose(fp);
+        int written = fwrite(back_buffer_compressed, 1, back_buffer_compressed_size, back_fp);
+        if (written == 0)
+        {
+          // try again?
+          ESP_LOGI("EPD", "Retrying back buffer save");
+          written = fwrite(back_buffer_compressed, 1, back_buffer_compressed_size, back_fp);
+        }
+        fclose(back_fp);
+        ESP_LOGI("EPD", "Back buffer saved %d", written);
       }
     }
     else
@@ -197,19 +211,28 @@ class EpdRenderer : public Renderer
   {
     ESP_LOGI("EPD", "Hydrating EPD");
     // load the two buffers - the front and the back buffers
-    FILE *fp = fopen("/fs/front_buffer.z", "rb");
+    FILE *fp = fopen("/fs/front_buffer.z", "r");
     if (fp)
     {
       fseek(fp, 0, SEEK_END);
       size_t compressed_size = ftell(fp);
+      ESP_LOGI("EPD", "Front buffer compressed size: %d", compressed_size);
       fseek(fp, 0, SEEK_SET);
       void *compressed = malloc(compressed_size);
       if (compressed)
       {
         fread(compressed, 1, compressed_size, fp);
-        tinfl_decompress_mem_to_mem(m_hl.front_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
+        int result = tinfl_decompress_mem_to_mem(m_hl.front_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
+        if (result == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED)
+        {
+          ESP_LOGE("EPD", "Failed to decompress front buffer");
+        }
         free(compressed);
       }
+      else
+      {
+        ESP_LOGE("EPD", "Failed to allocate memory for front buffer");
+      }
       fclose(fp);
     }
     else
@@ -218,19 +241,28 @@ class EpdRenderer : public Renderer
       reset();
       return;
     }
-    fp = fopen("/fs/back_buffer.z", "rb");
+    fp = fopen("/fs/back_buffer.z", "r");
     if (fp)
     {
       fseek(fp, 0, SEEK_END);
       size_t compressed_size = ftell(fp);
       fseek(fp, 0, SEEK_SET);
+      ESP_LOGI("EPD", "Back buffer compressed size: %d", compressed_size);
       void *compressed = malloc(compressed_size);
       if (compressed)
       {
         fread(compressed, 1, compressed_size, fp);
-        tinfl_decompress_mem_to_mem(m_hl.back_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
+        int result = tinfl_decompress_mem_to_mem(m_hl.back_fb, EPD_HEIGHT * EPD_WIDTH / 2, compressed, compressed_size, 0);
+        if (result == TINFL_DECOMPRESS_MEM_TO_MEM_FAILED)
+        {
+          ESP_LOGE("EPD", "Failed to decompress back buffer");
+        }
         free(compressed);
       }
+      else
+      {
+        ESP_LOGE("EPD", "Failed to allocate memory for back buffer");
+      }
       fclose(fp);
     }
     else
diff --git a/lib/Epub/RubbishHtmlParser/RubbishHtmlParser.cpp b/lib/Epub/RubbishHtmlParser/RubbishHtmlParser.cpp
index 05ffe36..58deda4 100644
--- a/lib/Epub/RubbishHtmlParser/RubbishHtmlParser.cpp
+++ b/lib/Epub/RubbishHtmlParser/RubbishHtmlParser.cpp
@@ -227,5 +227,6 @@ void RubbishHtmlParser::layout(Renderer *renderer, Epub *epub)
 void RubbishHtmlParser::render_page(int page_index, Renderer *renderer, Epub *epub)
 {
   renderer->clear_screen();
+  // renderer->fill_rect(0, 0, renderer->get_page_width(), renderer->get_page_height(), 0xFF);
   pages[page_index]->render(renderer, epub);
 }
diff --git a/sdkconfig.lilygo_t5_47 b/sdkconfig.lilygo_t5_47
index 4c77df7..68bf96d 100644
--- a/sdkconfig.lilygo_t5_47
+++ b/sdkconfig.lilygo_t5_47
@@ -1084,7 +1084,7 @@ CONFIG_SPIFFS_CACHE_WR=y
 # end of SPIFFS Cache Configuration
 
 CONFIG_SPIFFS_PAGE_CHECK=y
-CONFIG_SPIFFS_GC_MAX_RUNS=10
+CONFIG_SPIFFS_GC_MAX_RUNS=20
 # CONFIG_SPIFFS_GC_STATS is not set
 CONFIG_SPIFFS_PAGE_SIZE=256
 CONFIG_SPIFFS_OBJ_NAME_LEN=32
diff --git a/sdkconfig.lilygo_t5_47.old b/sdkconfig.lilygo_t5_47.old
index def9ff8..ba99d18 100644
--- a/sdkconfig.lilygo_t5_47.old
+++ b/sdkconfig.lilygo_t5_47.old
@@ -873,11 +873,7 @@ CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096
 #
 # Certificate Bundle
 #
-CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
-CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y
-# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set
-# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE is not set
-# CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE is not set
+# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE is not set
 # end of Certificate Bundle
 
 # CONFIG_MBEDTLS_ECP_RESTARTABLE is not set