|
37 | 37 |
|
38 | 38 | \*/ |
39 | 39 |
|
40 | | -#ifndef _ESP_TGZ_H |
41 | | -#define _ESP_TGZ_H |
| 40 | +#pragma once |
42 | 41 |
|
43 | 42 | #include <FS.h> |
| 43 | +#include "ESP32-targz-log.hpp" |
| 44 | + |
| 45 | + |
| 46 | +#if defined ESP32 |
44 | 47 |
|
45 | | -#if defined( ESP32 ) |
46 | 48 | #include <Update.h> |
47 | 49 | #define HAS_OTA_SUPPORT |
48 | | -#elif defined( ESP8266 ) |
49 | | - //#ifdef USE_LittleFS |
50 | | - // #define SPIFFS LittleFS |
51 | | - // #include <LittleFS.h> |
52 | | - //#endif |
| 50 | + |
| 51 | + // Figure out the chosen fs::FS library to load for the **destination** filesystem |
| 52 | + #if defined DEST_FS_USES_SPIFFS |
| 53 | + #include <SPIFFS.h> |
| 54 | + #define tarGzFS SPIFFS |
| 55 | + #define FS_NAME "SPIFFS" |
| 56 | + #elif defined DEST_FS_USES_FFAT |
| 57 | + #include <FFat.h> |
| 58 | + #define tarGzFS FFat |
| 59 | + #define FS_NAME "FFAT" |
| 60 | + #elif defined DEST_FS_USES_SD |
| 61 | + #include <SD.h> |
| 62 | + #define tarGzFS SD |
| 63 | + #define FS_NAME "SD" |
| 64 | + #elif defined DEST_FS_USES_SD_MMC |
| 65 | + #include <SD_MMC.h> |
| 66 | + #define tarGzFS SD_MMC |
| 67 | + #define FS_NAME "SD_MMC" |
| 68 | + #elif defined DEST_FS_USES_LITTLEFS |
| 69 | + #if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) |
| 70 | + // littlefs is built-in since sdk 2.0.0 |
| 71 | + #include <LittleFS.h> |
| 72 | + #define tarGzFS LittleFS |
| 73 | + #define FS_NAME "LittleFS (builtin)" |
| 74 | + #else |
| 75 | + // get "littlefs_esp32" from library manager |
| 76 | + #include <LITTLEFS.h> |
| 77 | + #define tarGzFS LITTLEFS |
| 78 | + #define FS_NAME "LITTLEFS (extlib)" |
| 79 | + #endif |
| 80 | + #elif defined DEST_FS_USES_PSRAMFS |
| 81 | + #include <PSRamFS.h> // https://github.com/tobozo/ESP32-PsRamFS |
| 82 | + #define tarGzFS PSRamFS |
| 83 | + #define FS_NAME "PSRamFS" |
| 84 | + #else |
| 85 | + // no filesystem, no helpers available, power user ? |
| 86 | + #endif |
| 87 | + |
| 88 | +#elif defined ESP8266 |
| 89 | + |
53 | 90 | #include <Updater.h> |
54 | 91 | #define HAS_OTA_SUPPORT |
| 92 | + |
| 93 | + // ESP8266 has no SD_MMC or FFat.h library, so these are implicitely invalidated |
| 94 | + #undef DEST_FS_USES_SD_MMC // unsupported |
| 95 | + #undef DEST_FS_USES_FFAT // unsupported |
| 96 | + // the fuck with spamming the console |
| 97 | + #pragma GCC diagnostic push |
| 98 | + #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 99 | + |
| 100 | + // Figure out the chosen fs::FS library to load for the **destination** filesystem |
| 101 | + |
| 102 | + #if defined DEST_FS_USES_SD |
| 103 | + #include <SD.h> |
| 104 | + #define tarGzFS SDFS |
| 105 | + #define FS_NAME "SDFS" |
| 106 | + #else |
| 107 | + #if defined DEST_FS_USES_LITTLEFS |
| 108 | + #include <LittleFS.h> |
| 109 | + #define tarGzFS LittleFS |
| 110 | + #define FS_NAME "LITTLEFS (extlib)" |
| 111 | + #elif defined DEST_FS_USES_SPIFFS |
| 112 | + #if defined USE_LittleFS // emulate SPIFFS using LittleFS |
| 113 | + #include <LittleFS.h> |
| 114 | + #define tarGzFS SPIFFS |
| 115 | + #define FS_NAME "LITTLEFS (subst)" |
| 116 | + #else // use core SPIFFS |
| 117 | + #define tarGzFS SPIFFS |
| 118 | + #define FS_NAME "SPIFFS" |
| 119 | + #endif |
| 120 | + #else // no destination filesystem defined in sketch |
| 121 | + #warning "Unspecified or invalid destination filesystem, please #define one of these before including the library: DEST_FS_USES_SPIFFS, DEST_FS_USES_LITTLEFS, DEST_FS_USES_SD, DEST_FS_USES_PSRAMFS" |
| 122 | + // however, check for USE_LittleFS as it is commonly defined since SPIFFS deprecation |
| 123 | + #if defined USE_LittleFS |
| 124 | + #include <LittleFS.h> |
| 125 | + #define tarGzFS LittleFS |
| 126 | + #warning "Defaulting to LittleFS" |
| 127 | + #define DEST_FS_USES_LITTLEFS |
| 128 | + #define FS_NAME "LITTLEFS (defaulted)" |
| 129 | + #else |
| 130 | + #define tarGzFS SPIFFS |
| 131 | + #warning "Defaulting to SPIFFS (soon deprecated)" |
| 132 | + #define DEST_FS_USES_SPIFFS |
| 133 | + #define FS_NAME "SPIFFS" |
| 134 | + #endif |
| 135 | + #endif |
| 136 | + #endif |
| 137 | + |
| 138 | + static FSInfo fsinfo; |
| 139 | + |
55 | 140 | #elif defined ARDUINO_ARCH_RP2040 |
56 | | - // TODO: RP2040 OTA implementation? |
| 141 | + |
| 142 | + #pragma message "Experimental RP2040 support" |
| 143 | + |
| 144 | + #undef DEST_FS_USES_SD_MMC // unsupported |
| 145 | + #undef DEST_FS_USES_FFAT // unsupported |
| 146 | + #undef DEST_FS_USES_SPIFFS // unsupported |
| 147 | + |
| 148 | + // Figure out the chosen fs::FS library to load for the **destination** filesystem |
| 149 | + #if defined DEST_FS_USES_SD |
| 150 | + #include <SD.h> |
| 151 | + #define tarGzFS SDFS |
| 152 | + #define FS_NAME "SD" |
| 153 | + #else |
| 154 | + #include <LittleFS.h> |
| 155 | + #define tarGzFS LittleFS |
| 156 | + #define FS_NAME "LITTLEFS (picolib)" |
| 157 | + #endif |
| 158 | + |
| 159 | + static FSInfo fsinfo; |
| 160 | + |
57 | 161 | #else |
58 | | - #error Unsupported architecture |
| 162 | + |
| 163 | + #error "Only ESP32, ESP8266 and RP2040/Pico architectures are supported" |
| 164 | + |
| 165 | +#endif |
| 166 | + |
| 167 | +#if defined DEST_FS_USES_SPIFFS || defined DEST_FS_USES_LITTLEFS || defined DEST_FS_USES_FFAT |
| 168 | + #define WARN_LIMITED_FS |
59 | 169 | #endif |
60 | 170 |
|
| 171 | +#include <stddef.h> // platformio whines about missing definition for 'size_t' 🤦 |
| 172 | + |
| 173 | +// required filesystem helpers are declared outside the main library |
| 174 | +// because ESP32/ESP8266 <FS.h> use different abstraction flavours :) |
| 175 | +__attribute__((unused)) static size_t targzFreeBytesFn() { |
| 176 | + #if defined DEST_FS_USES_SPIFFS || defined DEST_FS_USES_SD || defined DEST_FS_USES_SD_MMC || defined DEST_FS_USES_LITTLEFS || defined DEST_FS_USES_PSRAMFS |
| 177 | + #if defined ESP32 |
| 178 | + return tarGzFS.totalBytes() - tarGzFS.usedBytes(); |
| 179 | + #elif defined ESP8266 || defined ARDUINO_ARCH_RP2040 |
| 180 | + if( tarGzFS.info( fsinfo ) ) { |
| 181 | + return fsinfo.totalBytes - fsinfo.usedBytes; |
| 182 | + } else { |
| 183 | + // fail |
| 184 | + return 0; |
| 185 | + } |
| 186 | + #else |
| 187 | + #error "Only ESP32, ESP8266 and RP2040/Pico are supported" |
| 188 | + #endif |
| 189 | + #elif defined DEST_FS_USES_FFAT |
| 190 | + return tarGzFS.freeBytes(); |
| 191 | + #else |
| 192 | + // no filesystem, no helpers available, power user ? |
| 193 | + return 0; |
| 194 | + #endif |
| 195 | +} |
| 196 | + |
| 197 | +__attribute__((unused)) static size_t targzTotalBytesFn() { |
| 198 | + #if defined DEST_FS_USES_SPIFFS || defined DEST_FS_USES_SD || defined DEST_FS_USES_SD_MMC || defined DEST_FS_USES_LITTLEFS || defined DEST_FS_USES_FFAT || defined DEST_FS_USES_PSRAMFS |
| 199 | + #if defined ESP32 |
| 200 | + return tarGzFS.totalBytes(); |
| 201 | + #elif defined ESP8266 || defined ARDUINO_ARCH_RP2040 |
| 202 | + if( tarGzFS.info( fsinfo ) ) { |
| 203 | + return fsinfo.totalBytes; |
| 204 | + } else { |
| 205 | + // fail |
| 206 | + return 0; |
| 207 | + } |
| 208 | + #else |
| 209 | + #error "Only ESP32, ESP8266 and RP2040/Pico are supported" |
| 210 | + #endif |
| 211 | + #else |
| 212 | + // no filesystem, no helpers available, power user ? |
| 213 | + return 0; |
| 214 | + #endif |
| 215 | +} |
| 216 | + |
61 | 217 | #define GZIP_DICT_SIZE 32768 |
62 | 218 |
|
63 | 219 | #if defined ESP8266 |
@@ -304,7 +460,7 @@ struct TarGzUnpacker : public TarUnpacker, public GzUnpacker |
304 | 460 |
|
305 | 461 |
|
306 | 462 |
|
307 | | -#if defined ESP32 |
| 463 | +#if defined ESP32 && defined HAS_OTA_SUPPORT |
308 | 464 |
|
309 | 465 | // this class was inspired by https://github.com/vortigont/esp32-flashz |
310 | 466 |
|
@@ -390,4 +546,4 @@ struct TarGzUnpacker : public TarUnpacker, public GzUnpacker |
390 | 546 | #include "helpers/path_tools.h" |
391 | 547 |
|
392 | 548 |
|
393 | | -#endif // #ifdef _ESP_TGZ_H |
| 549 | +//#endif // #ifdef _ESP_TGZ_H |
0 commit comments