From 8cd4699dbcdb08834e2cbfc117ece013cc7ab519 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 8 Feb 2022 13:36:04 +0000 Subject: [PATCH] Rename FS API functions to avoid conflicts --- mongoose.c | 42 ++++++++++++++++++++-------------------- mongoose.h | 29 +++++++++++++++------------ src/arch_freertos_lwip.h | 1 + src/fs.c | 16 +++++++-------- src/fs.h | 28 +++++++++++++++------------ src/http.c | 26 ++++++++++++------------- test/unit_test.c | 2 +- 7 files changed, 77 insertions(+), 67 deletions(-) diff --git a/mongoose.c b/mongoose.c index fd48b455..25a0b188 100644 --- a/mongoose.c +++ b/mongoose.c @@ -413,7 +413,7 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) { struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags) { struct mg_fd *fd = (struct mg_fd *) calloc(1, sizeof(*fd)); if (fd != NULL) { - fd->fd = fs->open(path, flags); + fd->fd = fs->op(path, flags); fd->fs = fs; if (fd->fd == NULL) { free(fd); @@ -425,7 +425,7 @@ struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags) { void mg_fs_close(struct mg_fd *fd) { if (fd != NULL) { - fd->fs->close(fd->fd); + fd->fs->cl(fd->fd); free(fd); } } @@ -434,11 +434,11 @@ char *mg_file_read(struct mg_fs *fs, const char *path, size_t *sizep) { struct mg_fd *fd; char *data = NULL; size_t size = 0; - fs->stat(path, &size, NULL); + fs->st(path, &size, NULL); if ((fd = mg_fs_open(fs, path, MG_FS_READ)) != NULL) { data = (char *) calloc(1, size + 1); if (data != NULL) { - if (fs->read(fd->fd, data, size) != size) { + if (fs->rd(fd->fd, data, size) != size) { free(data); data = NULL; } else { @@ -458,13 +458,13 @@ bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf, char tmp[MG_PATH_MAX]; snprintf(tmp, sizeof(tmp), "%s..%d", path, rand()); if ((fd = mg_fs_open(fs, tmp, MG_FS_WRITE)) != NULL) { - result = fs->write(fd->fd, buf, len) == len; + result = fs->wr(fd->fd, buf, len) == len; mg_fs_close(fd); if (result) { - fs->remove(path); - fs->rename(tmp, path); + fs->rm(path); + fs->mv(tmp, path); } else { - fs->remove(tmp); + fs->rm(tmp); } } return result; @@ -1337,8 +1337,8 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data, size_t n, max = 2 * MG_IO_SIZE; if (c->send.size < max) mg_iobuf_resize(&c->send, max); if (c->send.len >= c->send.size) return; // Rate limit - n = fd->fs->read(fd->fd, c->send.buf + c->send.len, - c->send.size - c->send.len); + n = fd->fs->rd(fd->fd, c->send.buf + c->send.len, + c->send.size - c->send.len); if (n > 0) c->send.len += n; if (c->send.len < c->send.size) restore_http_cb(c); } else if (ev == MG_EV_CLOSE) { @@ -1434,7 +1434,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, time_t mtime = 0; struct mg_str *inm = NULL; - if (fd == NULL || fs->stat(path, &size, &mtime) == 0) { + if (fd == NULL || fs->st(path, &size, &mtime) == 0) { LOG(LL_DEBUG, ("404 [%s] %p", path, (void *) fd)); mg_http_reply(c, 404, "", "%s", "Not found\n"); mg_fs_close(fd); @@ -1467,7 +1467,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, "Content-Range: bytes " MG_INT64_FMT "-" MG_INT64_FMT "/" MG_INT64_FMT "\r\n", r1, r1 + cl - 1, (int64_t) size); - fs->seek(fd->fd, (size_t) r1); + fs->sk(fd->fd, (size_t) r1); } } @@ -1504,7 +1504,7 @@ static void printdirentry(const char *name, void *userdata) { // LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name)); if (snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) < 0) { LOG(LL_ERROR, ("%s truncated", name)); - } else if ((flags = fs->stat(path, &size, &t)) == 0) { + } else if ((flags = fs->st(path, &size, &t)) == 0) { LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno)); } else { const char *slash = flags & MG_FS_DIR ? "/" : ""; @@ -1583,7 +1583,7 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm, (int) uri.len, uri.ptr, sort_js_code, sort_js_code2, (int) uri.len, uri.ptr); - fs->list(dir, printdirentry, &d); + fs->ls(dir, printdirentry, &d); mg_printf(c, "
" "
Mongoose v.%s
\n", @@ -1614,7 +1614,7 @@ static void remove_double_dots(char *s) { *p = '\0'; } -// Resolve requested file into `path` and return its fs->stat() result +// Resolve requested file into `path` and return its fs->st() result static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, struct mg_fs *fs, struct mg_str url, struct mg_str dir, char *path, size_t path_size) { @@ -1623,7 +1623,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, size_t n = (size_t) snprintf(path, path_size, "%.*s", (int) dir.len, dir.ptr); if (n > path_size) n = path_size; path[path_size - 1] = '\0'; - if ((fs->stat(path, NULL, NULL) & MG_FS_DIR) == 0) { + if ((fs->st(path, NULL, NULL) & MG_FS_DIR) == 0) { mg_http_reply(c, 400, "", "Invalid web root [%.*s]\n", (int) dir.len, dir.ptr); } else { @@ -1636,7 +1636,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, LOG(LL_VERBOSE_DEBUG, ("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path)); while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes - flags = fs->stat(path, NULL, NULL); // Does it exist? + flags = fs->st(path, NULL, NULL); // Does it exist? if (flags == 0) { mg_http_reply(c, 404, "", "Not found\n"); // Does not exist, doh } else if ((flags & MG_FS_DIR) && hm->uri.len > 0 && @@ -1650,9 +1650,9 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, flags = 0; } else if (flags & MG_FS_DIR) { if (((snprintf(path + n, path_size - n, "/" MG_HTTP_INDEX) > 0 && - (tmp = fs->stat(path, NULL, NULL)) != 0) || + (tmp = fs->st(path, NULL, NULL)) != 0) || (snprintf(path + n, path_size - n, "/index.shtml") > 0 && - (tmp = fs->stat(path, NULL, NULL)) != 0))) { + (tmp = fs->st(path, NULL, NULL)) != 0))) { flags = tmp; } else { path[n] = '\0'; // Remove appended index file name @@ -1852,12 +1852,12 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name); remove_double_dots(path); LOG(LL_DEBUG, ("%d bytes @ %ld [%s]", (int) hm->body.len, oft, path)); - if (oft == 0) fs->remove(path); + if (oft == 0) fs->rm(path); if ((fd = mg_fs_open(fs, path, MG_FS_WRITE)) == NULL) { mg_http_reply(c, 400, "", "open(%s): %d", path, errno); return -2; } else { - int written = (int) fs->write(fd->fd, hm->body.ptr, hm->body.len); + int written = (int) fs->wr(fd->fd, hm->body.ptr, hm->body.len); mg_fs_close(fd); mg_http_reply(c, 200, "", "%d", written); return (int) hm->body.len; diff --git a/mongoose.h b/mongoose.h index ec25c709..ef547387 100644 --- a/mongoose.h +++ b/mongoose.h @@ -165,6 +165,7 @@ static __inline struct tm *localtime_r(const time_t *t, struct tm *tm) { #if MG_ARCH == MG_ARCH_FREERTOS_LWIP +#include #include #include #include @@ -642,19 +643,23 @@ void mg_timer_poll(int64_t current_time_ms); enum { MG_FS_READ = 1, MG_FS_WRITE = 2, MG_FS_DIR = 4 }; // Filesystem API functions -// stat() returns MG_FS_* flags and populates file size and modification time -// list() calls fn() for every directory entry, allowing to list a directory +// st() returns MG_FS_* flags and populates file size and modification time +// ls() calls fn() for every directory entry, allowing to list a directory +// +// NOTE: UNIX-style shorthand names for the API functions are deliberately +// chosen to avoid conflicts with some libraries that make macros for e.g. +// stat(), write(), read() calls. struct mg_fs { - int (*stat)(const char *path, size_t *size, time_t *mtime); - void (*list)(const char *path, void (*fn)(const char *, void *), void *); - void *(*open)(const char *path, int flags); // Open file - void (*close)(void *fd); // Close file - size_t (*read)(void *fd, void *buf, size_t len); // Read file - size_t (*write)(void *fd, const void *buf, size_t len); // Write file - size_t (*seek)(void *fd, size_t offset); // Set file position - bool (*rename)(const char *from, const char *to); // Rename - bool (*remove)(const char *path); // Delete file - bool (*mkd)(const char *path); // Create directory + int (*st)(const char *path, size_t *size, time_t *mtime); // stat file + void (*ls)(const char *path, void (*fn)(const char *, void *), void *); + void *(*op)(const char *path, int flags); // Open file + void (*cl)(void *fd); // Close file + size_t (*rd)(void *fd, void *buf, size_t len); // Read file + size_t (*wr)(void *fd, const void *buf, size_t len); // Write file + size_t (*sk)(void *fd, size_t offset); // Set file position + bool (*mv)(const char *from, const char *to); // Rename file + bool (*rm)(const char *path); // Delete file + bool (*mkd)(const char *path); // Create directory }; extern struct mg_fs mg_fs_posix; // POSIX open/close/read/write/seek diff --git a/src/arch_freertos_lwip.h b/src/arch_freertos_lwip.h index 0707250f..ecec11d0 100644 --- a/src/arch_freertos_lwip.h +++ b/src/arch_freertos_lwip.h @@ -2,6 +2,7 @@ #if MG_ARCH == MG_ARCH_FREERTOS_LWIP +#include #include #include #include diff --git a/src/fs.c b/src/fs.c index 514cd618..9ae6113a 100644 --- a/src/fs.c +++ b/src/fs.c @@ -4,7 +4,7 @@ struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags) { struct mg_fd *fd = (struct mg_fd *) calloc(1, sizeof(*fd)); if (fd != NULL) { - fd->fd = fs->open(path, flags); + fd->fd = fs->op(path, flags); fd->fs = fs; if (fd->fd == NULL) { free(fd); @@ -16,7 +16,7 @@ struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags) { void mg_fs_close(struct mg_fd *fd) { if (fd != NULL) { - fd->fs->close(fd->fd); + fd->fs->cl(fd->fd); free(fd); } } @@ -25,11 +25,11 @@ char *mg_file_read(struct mg_fs *fs, const char *path, size_t *sizep) { struct mg_fd *fd; char *data = NULL; size_t size = 0; - fs->stat(path, &size, NULL); + fs->st(path, &size, NULL); if ((fd = mg_fs_open(fs, path, MG_FS_READ)) != NULL) { data = (char *) calloc(1, size + 1); if (data != NULL) { - if (fs->read(fd->fd, data, size) != size) { + if (fs->rd(fd->fd, data, size) != size) { free(data); data = NULL; } else { @@ -49,13 +49,13 @@ bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf, char tmp[MG_PATH_MAX]; snprintf(tmp, sizeof(tmp), "%s..%d", path, rand()); if ((fd = mg_fs_open(fs, tmp, MG_FS_WRITE)) != NULL) { - result = fs->write(fd->fd, buf, len) == len; + result = fs->wr(fd->fd, buf, len) == len; mg_fs_close(fd); if (result) { - fs->remove(path); - fs->rename(tmp, path); + fs->rm(path); + fs->mv(tmp, path); } else { - fs->remove(tmp); + fs->rm(tmp); } } return result; diff --git a/src/fs.h b/src/fs.h index 39e62b0f..f7cce376 100644 --- a/src/fs.h +++ b/src/fs.h @@ -6,19 +6,23 @@ enum { MG_FS_READ = 1, MG_FS_WRITE = 2, MG_FS_DIR = 4 }; // Filesystem API functions -// stat() returns MG_FS_* flags and populates file size and modification time -// list() calls fn() for every directory entry, allowing to list a directory +// st() returns MG_FS_* flags and populates file size and modification time +// ls() calls fn() for every directory entry, allowing to list a directory +// +// NOTE: UNIX-style shorthand names for the API functions are deliberately +// chosen to avoid conflicts with some libraries that make macros for e.g. +// stat(), write(), read() calls. struct mg_fs { - int (*stat)(const char *path, size_t *size, time_t *mtime); - void (*list)(const char *path, void (*fn)(const char *, void *), void *); - void *(*open)(const char *path, int flags); // Open file - void (*close)(void *fd); // Close file - size_t (*read)(void *fd, void *buf, size_t len); // Read file - size_t (*write)(void *fd, const void *buf, size_t len); // Write file - size_t (*seek)(void *fd, size_t offset); // Set file position - bool (*rename)(const char *from, const char *to); // Rename - bool (*remove)(const char *path); // Delete file - bool (*mkd)(const char *path); // Create directory + int (*st)(const char *path, size_t *size, time_t *mtime); // stat file + void (*ls)(const char *path, void (*fn)(const char *, void *), void *); + void *(*op)(const char *path, int flags); // Open file + void (*cl)(void *fd); // Close file + size_t (*rd)(void *fd, void *buf, size_t len); // Read file + size_t (*wr)(void *fd, const void *buf, size_t len); // Write file + size_t (*sk)(void *fd, size_t offset); // Set file position + bool (*mv)(const char *from, const char *to); // Rename file + bool (*rm)(const char *path); // Delete file + bool (*mkd)(const char *path); // Create directory }; extern struct mg_fs mg_fs_posix; // POSIX open/close/read/write/seek diff --git a/src/http.c b/src/http.c index 186142d1..3b528d41 100644 --- a/src/http.c +++ b/src/http.c @@ -385,8 +385,8 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data, size_t n, max = 2 * MG_IO_SIZE; if (c->send.size < max) mg_iobuf_resize(&c->send, max); if (c->send.len >= c->send.size) return; // Rate limit - n = fd->fs->read(fd->fd, c->send.buf + c->send.len, - c->send.size - c->send.len); + n = fd->fs->rd(fd->fd, c->send.buf + c->send.len, + c->send.size - c->send.len); if (n > 0) c->send.len += n; if (c->send.len < c->send.size) restore_http_cb(c); } else if (ev == MG_EV_CLOSE) { @@ -482,7 +482,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, time_t mtime = 0; struct mg_str *inm = NULL; - if (fd == NULL || fs->stat(path, &size, &mtime) == 0) { + if (fd == NULL || fs->st(path, &size, &mtime) == 0) { LOG(LL_DEBUG, ("404 [%s] %p", path, (void *) fd)); mg_http_reply(c, 404, "", "%s", "Not found\n"); mg_fs_close(fd); @@ -515,7 +515,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, "Content-Range: bytes " MG_INT64_FMT "-" MG_INT64_FMT "/" MG_INT64_FMT "\r\n", r1, r1 + cl - 1, (int64_t) size); - fs->seek(fd->fd, (size_t) r1); + fs->sk(fd->fd, (size_t) r1); } } @@ -552,7 +552,7 @@ static void printdirentry(const char *name, void *userdata) { // LOG(LL_DEBUG, ("[%s] [%s]", d->dir, name)); if (snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) < 0) { LOG(LL_ERROR, ("%s truncated", name)); - } else if ((flags = fs->stat(path, &size, &t)) == 0) { + } else if ((flags = fs->st(path, &size, &t)) == 0) { LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno)); } else { const char *slash = flags & MG_FS_DIR ? "/" : ""; @@ -631,7 +631,7 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm, (int) uri.len, uri.ptr, sort_js_code, sort_js_code2, (int) uri.len, uri.ptr); - fs->list(dir, printdirentry, &d); + fs->ls(dir, printdirentry, &d); mg_printf(c, "
" "
Mongoose v.%s
\n", @@ -662,7 +662,7 @@ static void remove_double_dots(char *s) { *p = '\0'; } -// Resolve requested file into `path` and return its fs->stat() result +// Resolve requested file into `path` and return its fs->st() result static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, struct mg_fs *fs, struct mg_str url, struct mg_str dir, char *path, size_t path_size) { @@ -671,7 +671,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, size_t n = (size_t) snprintf(path, path_size, "%.*s", (int) dir.len, dir.ptr); if (n > path_size) n = path_size; path[path_size - 1] = '\0'; - if ((fs->stat(path, NULL, NULL) & MG_FS_DIR) == 0) { + if ((fs->st(path, NULL, NULL) & MG_FS_DIR) == 0) { mg_http_reply(c, 400, "", "Invalid web root [%.*s]\n", (int) dir.len, dir.ptr); } else { @@ -684,7 +684,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, LOG(LL_VERBOSE_DEBUG, ("%lu %.*s -> %s", c->id, (int) hm->uri.len, hm->uri.ptr, path)); while (n > 0 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes - flags = fs->stat(path, NULL, NULL); // Does it exist? + flags = fs->st(path, NULL, NULL); // Does it exist? if (flags == 0) { mg_http_reply(c, 404, "", "Not found\n"); // Does not exist, doh } else if ((flags & MG_FS_DIR) && hm->uri.len > 0 && @@ -698,9 +698,9 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm, flags = 0; } else if (flags & MG_FS_DIR) { if (((snprintf(path + n, path_size - n, "/" MG_HTTP_INDEX) > 0 && - (tmp = fs->stat(path, NULL, NULL)) != 0) || + (tmp = fs->st(path, NULL, NULL)) != 0) || (snprintf(path + n, path_size - n, "/index.shtml") > 0 && - (tmp = fs->stat(path, NULL, NULL)) != 0))) { + (tmp = fs->st(path, NULL, NULL)) != 0))) { flags = tmp; } else { path[n] = '\0'; // Remove appended index file name @@ -900,12 +900,12 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name); remove_double_dots(path); LOG(LL_DEBUG, ("%d bytes @ %ld [%s]", (int) hm->body.len, oft, path)); - if (oft == 0) fs->remove(path); + if (oft == 0) fs->rm(path); if ((fd = mg_fs_open(fs, path, MG_FS_WRITE)) == NULL) { mg_http_reply(c, 400, "", "open(%s): %d", path, errno); return -2; } else { - int written = (int) fs->write(fd->fd, hm->body.ptr, hm->body.len); + int written = (int) fs->wr(fd->fd, hm->body.ptr, hm->body.len); mg_fs_close(fd); mg_http_reply(c, 200, "", "%d", written); return (int) hm->body.len; diff --git a/test/unit_test.c b/test/unit_test.c index 04d5d2ce..71247afd 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -609,7 +609,7 @@ static void test_http_server(void) { char etag[100]; size_t size = 0; time_t mtime = 0; - ASSERT(mg_fs_posix.stat("./test/data/a.txt", &size, &mtime) != 0); + ASSERT(mg_fs_posix.st("./test/data/a.txt", &size, &mtime) != 0); ASSERT(mg_http_etag(etag, sizeof(etag), size, mtime) == etag); ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\nIf-None-Match: %s\n\n", etag) == 304);