Merge pull request #3477 from cesanta/mem

Make packed fs always available, and manually settable
This commit is contained in:
Sergio R. Caprile authored and GitHub committed 2026-04-01 14:36:58 -03:00
commit ae5a2263ac
83 files changed
+289 -8052

No files matched your search

-4
View File
@@ -78,10 +78,6 @@
#define MG_ENABLE_CUSTOM_MILLIS 0
#endif
#ifndef MG_ENABLE_PACKED_FS
#define MG_ENABLE_PACKED_FS 0
#endif
#ifndef MG_ENABLE_ASSERT
#define MG_ENABLE_ASSERT 0
#endif
+24 -3
View File
@@ -44,7 +44,28 @@ struct mg_str mg_file_read(struct mg_fs *fs, const char *path);
bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t);
bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);
// Packed API
const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
const char *mg_unlist(size_t no); // Get no'th packed filename
// A piece of memory (flash or RAM) represented as a file
struct mg_mem_file {
const char *path;
const unsigned char *data;
size_t size; // Data size
time_t mtime; // Modification time
};
// Global variable that points to the array of memory files. It can be
// changed to point to the user-defined file array. The last element
// should have NULL path.
//
// static const struct mg_mem_file my_files[] = {
// {"/web_root/index.html", "hi", 2, 0},
// {NULL, NULL, 0, 0},
// };
// mg_mem_files = my_files;
extern const struct mg_mem_file *mg_mem_files;
// const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
// const char *mg_unlist(size_t no); // Get no'th packed filename
struct mg_str mg_unpacked(const char *path); // Packed file as mg_str
extern const struct mg_mem_file mg_packed_files[]; // Generated by "pack" util
+17 -11
View File
@@ -9,19 +9,25 @@ struct packed_file {
size_t pos;
};
#if MG_ENABLE_PACKED_FS
#else
const char *mg_unpack(const char *path, size_t *size, time_t *mtime) {
if (size != NULL) *size = 0;
if (mtime != NULL) *mtime = 0;
(void) path;
const struct mg_mem_file *mg_mem_files;
static int mg_scmp(const char *a, const char *b) {
while (*a && (*a == *b)) a++, b++;
return *(const unsigned char *) a - *(const unsigned char *) b;
}
static const char *mg_unlist(size_t no) {
return mg_mem_files == NULL ? NULL : mg_mem_files[no].path;
}
static const char *mg_unpack(const char *path, size_t *size, time_t *mtime) {
const struct mg_mem_file *p;
for (p = mg_mem_files; p != NULL && p->path != NULL; p++) {
if (mg_scmp(p->path, path) != 0) continue;
if (size != NULL) *size = p->size;
if (mtime != NULL) *mtime = p->mtime;
return (const char *) p->data;
}
return NULL;
}
const char *mg_unlist(size_t no) {
(void) no;
return NULL;
}
#endif
struct mg_str mg_unpacked(const char *path) {
size_t len = 0;