|
| 1 | +#include "Driver/localfs.h" |
| 2 | +#include "Driver/spiffs.h" // spiffs_format |
| 3 | +#include "Driver/littlefs.h" // littlefs_format |
| 4 | +#include <cstddef> // NULL |
| 5 | +#include <cstring> |
| 6 | +#include "src/Logging.h" |
| 7 | +#include "esp_partition.h" |
| 8 | + |
| 9 | +const char* localfsName = NULL; |
| 10 | + |
| 11 | +static bool has_partition(const char* label) { |
| 12 | + auto part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, label); |
| 13 | + return part != NULL; |
| 14 | +} |
| 15 | + |
| 16 | +bool localfs_mount() { |
| 17 | + if (has_partition(spiffsName)) { |
| 18 | + if (!spiffs_mount(spiffsName, false)) { |
| 19 | + localfsName = spiffsName; |
| 20 | + return false; |
| 21 | + } |
| 22 | + // Migration - littlefs in spiffs partition |
| 23 | + if (!littlefs_mount(spiffsName, false)) { |
| 24 | + localfsName = littlefsName; |
| 25 | + return false; |
| 26 | + } |
| 27 | + // Try to create a SPIFFS filesystem |
| 28 | + if (!spiffs_mount(spiffsName, true)) { |
| 29 | + localfsName = spiffsName; |
| 30 | + return false; |
| 31 | + } |
| 32 | + log_error("Cannot mount or create a local filesystem in the spiffs partition"); |
| 33 | + return true; |
| 34 | + } |
| 35 | + if (has_partition(littlefsName)) { |
| 36 | + // Mount LittleFS, create if necessary |
| 37 | + if (!littlefs_mount(littlefsName, true)) { |
| 38 | + localfsName = littlefsName; |
| 39 | + return false; |
| 40 | + } |
| 41 | + log_error("Cannot mount or create a local filesystem in the littlefs partition"); |
| 42 | + return true; |
| 43 | + } |
| 44 | + log_error("The partition map has neither a spiffs partition nor a littlefs partition"); |
| 45 | + return true; |
| 46 | +} |
| 47 | +void localfs_unmount() { |
| 48 | + if (localfsName == spiffsName) { |
| 49 | + spiffs_unmount(); |
| 50 | + return; |
| 51 | + } |
| 52 | + if (localfsName == littlefsName) { |
| 53 | + littlefs_unmount(); |
| 54 | + return; |
| 55 | + } |
| 56 | + localfsName = NULL; |
| 57 | +} |
| 58 | +bool localfs_format(const char* fsname) { |
| 59 | + if (!strcasecmp(fsname, "format") || !strcasecmp(fsname, "localfs")) { |
| 60 | + fsname = defaultLocalfsName; |
| 61 | + } |
| 62 | + if (!strcasecmp(fsname, spiffsName)) { |
| 63 | + localfs_unmount(); |
| 64 | + if (!spiffs_format(spiffsName)) { |
| 65 | + if (!spiffs_mount(spiffsName)) { |
| 66 | + localfsName = spiffsName; |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + if (!strcasecmp(fsname, littlefsName)) { |
| 72 | + localfs_unmount(); |
| 73 | + if (!littlefs_format(littlefsName)) { |
| 74 | + if (!littlefs_mount(littlefsName)) { |
| 75 | + localfsName = littlefsName; |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + log_debug("Trying LittleFS in spiffs partition"); |
| 80 | + if (!littlefs_format(spiffsName)) { |
| 81 | + if (!littlefs_mount(spiffsName)) { |
| 82 | + localfsName = littlefsName; |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + localfs_mount(); |
| 87 | + return true; |
| 88 | + } |
| 89 | + localfsName = ""; |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +uint64_t localfs_size() { |
| 94 | + std::error_code ec; |
| 95 | + |
| 96 | + auto space = std::filesystem::space(localfsName, ec); |
| 97 | + if (ec) { |
| 98 | + return 0; |
| 99 | + } |
| 100 | + return space.capacity; |
| 101 | +} |
| 102 | + |
| 103 | +static void insertFsName(char* s, const char* prefix) { |
| 104 | + size_t slen = strlen(s); |
| 105 | + size_t plen = strlen(prefix); |
| 106 | + memmove(s + 1 + plen, s, slen + 1); |
| 107 | + memmove(s + 1, prefix, plen); |
| 108 | + *s = '/'; |
| 109 | +} |
| 110 | + |
| 111 | +static bool replacedFsName(char* s, const char* replaced, const char* with) { |
| 112 | + if (*s != '/') { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + char* head = s + 1; |
| 117 | + const char* tail = strchrnul(head, '/'); // tail string after prefix |
| 118 | + size_t plen = tail - head; // Prefix length |
| 119 | + size_t rlen = strlen(replaced); // replaced length |
| 120 | + |
| 121 | + if (plen != rlen) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + if (strncasecmp(head, replaced, rlen) == 0) { |
| 126 | + // replaced matches the prefix of s |
| 127 | + |
| 128 | + size_t tlen = strlen(tail); |
| 129 | + size_t wlen = strlen(with); |
| 130 | + |
| 131 | + if (wlen != rlen) { |
| 132 | + // Move tail to new location |
| 133 | + memmove(head + wlen, tail, tlen + 1); |
| 134 | + } |
| 135 | + |
| 136 | + // Insert with at the beginning |
| 137 | + memmove(head, with, wlen); |
| 138 | + return true; |
| 139 | + } |
| 140 | + return false; |
| 141 | +} |
| 142 | + |
| 143 | +const char* canonicalPath(const char* filename, const char* defaultFs) { |
| 144 | + static char path[128]; |
| 145 | + strncpy(path, filename, 128); |
| 146 | + |
| 147 | + // log_debug("filename is " << filename << " deffs " << defaultFs); |
| 148 | + |
| 149 | + // Map file system names to canonical form. The input name is case-independent, |
| 150 | + // while the canonical name is lower case. |
| 151 | + if (!(replacedFsName(path, "localfs", localfsName) || replacedFsName(path, spiffsName, localfsName) || |
| 152 | + replacedFsName(path, littlefsName, localfsName) || |
| 153 | + // The following looks like a no-op but it is not because of case independence |
| 154 | + replacedFsName(path, sdName, sdName))) { |
| 155 | + if (*filename != '/') { |
| 156 | + insertFsName(path, ""); |
| 157 | + } |
| 158 | + // path now begins with / |
| 159 | + if (!strcmp(defaultFs, "")) { |
| 160 | + // If the default filesystem is empty, insert |
| 161 | + // the local file system prefix |
| 162 | + insertFsName(path, localfsName); |
| 163 | + } else { |
| 164 | + // If the default filesystem is not empty, insert |
| 165 | + // the defaultFs name as the mountpoint name |
| 166 | + insertFsName(path, defaultFs); |
| 167 | + } |
| 168 | + } |
| 169 | + // log_debug("path is " << path); |
| 170 | + return path; |
| 171 | +} |
0 commit comments