From d0857fe36c4121d6d32451c5c27a260496de8694 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 18 Jan 2022 19:19:34 +0000 Subject: [PATCH] Add mkdir to the FS API --- examples/file-upload/main.c | 4 +-- mongoose.c | 51 ++++++++++++++++++++++++------------- mongoose.h | 1 + src/fs.h | 1 + src/fs_fat.c | 12 ++++++--- src/fs_packed.c | 11 +++++--- src/fs_posix.c | 28 ++++++++++++-------- test/mongoose_custom.c | 19 +++----------- 8 files changed, 73 insertions(+), 54 deletions(-) diff --git a/examples/file-upload/main.c b/examples/file-upload/main.c index 38520846..8e277755 100644 --- a/examples/file-upload/main.c +++ b/examples/file-upload/main.c @@ -10,10 +10,10 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/upload")) { - mg_http_upload(c, hm, "/tmp"); + mg_http_upload(c, hm, &mg_fs_posix, "/tmp"); } else { struct mg_http_serve_opts opts = {.root_dir = "web_root"}; - mg_http_serve_dir(c, ev_data, &opts); + mg_http_serve_dir(c, hm, &opts); } } } diff --git a/mongoose.c b/mongoose.c index dfef6f8e..6428aadf 100644 --- a/mongoose.c +++ b/mongoose.c @@ -565,15 +565,19 @@ static size_t ff_seek(void *fp, size_t offset) { } static bool ff_rename(const char *from, const char *to) { - return ff_rename(from, to) == FR_OK; + return f_rename(from, to) == FR_OK; } static bool ff_remove(const char *path) { - return ff_remove(path) == 0; + return f_unlink(path) == FR_OK; } -struct mg_fs mg_fs_fat = {ff_stat, ff_list, ff_open, ff_close, ff_read, - ff_write, ff_seek, ff_rename, ff_remove}; +static bool ff_mkdir(const char *path) { + return f_mkdir(path) == FR_OK; +} + +struct mg_fs mg_fs_fat = {ff_stat, ff_list, ff_open, ff_close, ff_read, + ff_write, ff_seek, ff_rename, ff_remove, ff_mkdir}; #endif #ifdef MG_ENABLE_LINES @@ -684,9 +688,14 @@ static bool packed_remove(const char *path) { return false; } -struct mg_fs mg_fs_packed = {packed_stat, packed_list, packed_open, - packed_close, packed_read, packed_write, - packed_seek, packed_rename, packed_remove}; +static bool packed_mkdir(const char *path) { + (void) path; + return false; +} + +struct mg_fs mg_fs_packed = { + packed_stat, packed_list, packed_open, packed_close, packed_read, + packed_write, packed_seek, packed_rename, packed_remove, packed_mkdir}; #ifdef MG_ENABLE_LINES #line 1 "src/fs_posix.c" @@ -891,37 +900,39 @@ static bool p_remove(const char *path) { return remove(path) == 0; } +static bool p_mkdir(const char *path) { +#if defined(__MINGW32__) || defined(__MINGW64__) + return mkdir(path) == 0; +#else + return mkdir(path, 0775) == 0; +#endif +} + #else static int p_stat(const char *path, size_t *size, time_t *mtime) { (void) path, (void) size, (void) mtime; return 0; } - static void p_list(const char *path, void (*fn)(const char *, void *), void *userdata) { (void) path, (void) fn, (void) userdata; } - -static struct mg_fd *p_open(const char *path, int flags) { +static void *p_open(const char *path, int flags) { (void) path, (void) flags; return NULL; } - -static void p_close(struct mg_fd *fd) { - (void) fd; +static void p_close(void *fp) { + (void) fp; } - static size_t p_read(void *fd, void *buf, size_t len) { (void) fd, (void) buf, (void) len; return 0; } - static size_t p_write(void *fd, const void *buf, size_t len) { (void) fd, (void) buf, (void) len; return 0; } - static size_t p_seek(void *fd, size_t offset) { (void) fd, (void) offset; return (size_t) ~0; @@ -934,10 +945,14 @@ static bool p_remove(const char *path) { (void) path; return false; } +static bool p_mkdir(const char *path) { + (void) path; + return false; +} #endif -struct mg_fs mg_fs_posix = {p_stat, p_list, p_open, p_close, p_read, - p_write, p_seek, p_rename, p_remove}; +struct mg_fs mg_fs_posix = {p_stat, p_list, p_open, p_close, p_read, + p_write, p_seek, p_rename, p_remove, p_mkdir}; #ifdef MG_ENABLE_LINES #line 1 "src/http.c" diff --git a/mongoose.h b/mongoose.h index 421ffb88..0438e821 100644 --- a/mongoose.h +++ b/mongoose.h @@ -625,6 +625,7 @@ struct mg_fs { 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 (*mkdir)(const char *path); // Create directory }; // File descriptor diff --git a/src/fs.h b/src/fs.h index d10db8bb..70239b0e 100644 --- a/src/fs.h +++ b/src/fs.h @@ -18,6 +18,7 @@ struct mg_fs { 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 (*mkdir)(const char *path); // Create directory }; // File descriptor diff --git a/src/fs_fat.c b/src/fs_fat.c index 6d5dd463..bf7e1ebf 100644 --- a/src/fs_fat.c +++ b/src/fs_fat.c @@ -73,13 +73,17 @@ static size_t ff_seek(void *fp, size_t offset) { } static bool ff_rename(const char *from, const char *to) { - return ff_rename(from, to) == FR_OK; + return f_rename(from, to) == FR_OK; } static bool ff_remove(const char *path) { - return ff_remove(path) == 0; + return f_unlink(path) == FR_OK; } -struct mg_fs mg_fs_fat = {ff_stat, ff_list, ff_open, ff_close, ff_read, - ff_write, ff_seek, ff_rename, ff_remove}; +static bool ff_mkdir(const char *path) { + return f_mkdir(path) == FR_OK; +} + +struct mg_fs mg_fs_fat = {ff_stat, ff_list, ff_open, ff_close, ff_read, + ff_write, ff_seek, ff_rename, ff_remove, ff_mkdir}; #endif diff --git a/src/fs_packed.c b/src/fs_packed.c index ba69e265..2e6c9a60 100644 --- a/src/fs_packed.c +++ b/src/fs_packed.c @@ -103,6 +103,11 @@ static bool packed_remove(const char *path) { return false; } -struct mg_fs mg_fs_packed = {packed_stat, packed_list, packed_open, - packed_close, packed_read, packed_write, - packed_seek, packed_rename, packed_remove}; +static bool packed_mkdir(const char *path) { + (void) path; + return false; +} + +struct mg_fs mg_fs_packed = { + packed_stat, packed_list, packed_open, packed_close, packed_read, + packed_write, packed_seek, packed_rename, packed_remove, packed_mkdir}; diff --git a/src/fs_posix.c b/src/fs_posix.c index f91d7976..5eba96ad 100644 --- a/src/fs_posix.c +++ b/src/fs_posix.c @@ -198,37 +198,39 @@ static bool p_remove(const char *path) { return remove(path) == 0; } +static bool p_mkdir(const char *path) { +#if defined(__MINGW32__) || defined(__MINGW64__) + return mkdir(path) == 0; +#else + return mkdir(path, 0775) == 0; +#endif +} + #else static int p_stat(const char *path, size_t *size, time_t *mtime) { (void) path, (void) size, (void) mtime; return 0; } - static void p_list(const char *path, void (*fn)(const char *, void *), void *userdata) { (void) path, (void) fn, (void) userdata; } - -static struct mg_fd *p_open(const char *path, int flags) { +static void *p_open(const char *path, int flags) { (void) path, (void) flags; return NULL; } - -static void p_close(struct mg_fd *fd) { - (void) fd; +static void p_close(void *fp) { + (void) fp; } - static size_t p_read(void *fd, void *buf, size_t len) { (void) fd, (void) buf, (void) len; return 0; } - static size_t p_write(void *fd, const void *buf, size_t len) { (void) fd, (void) buf, (void) len; return 0; } - static size_t p_seek(void *fd, size_t offset) { (void) fd, (void) offset; return (size_t) ~0; @@ -241,7 +243,11 @@ static bool p_remove(const char *path) { (void) path; return false; } +static bool p_mkdir(const char *path) { + (void) path; + return false; +} #endif -struct mg_fs mg_fs_posix = {p_stat, p_list, p_open, p_close, p_read, - p_write, p_seek, p_rename, p_remove}; +struct mg_fs mg_fs_posix = {p_stat, p_list, p_open, p_close, p_read, + p_write, p_seek, p_rename, p_remove, p_mkdir}; diff --git a/test/mongoose_custom.c b/test/mongoose_custom.c index fa4de347..eac99850 100644 --- a/test/mongoose_custom.c +++ b/test/mongoose_custom.c @@ -1,20 +1,8 @@ #include "mongoose.h" -int usleep(useconds_t us) { - for (useconds_t i = 0; i < us * 99; i++) asm("nop"); - return 0; -} - -int clock_gettime(clockid_t clock_id, struct timespec *tp) { - (void) clock_id; - memset(tp, 0, sizeof(*tp)); - return 0; -} - -char *realpath(const char *path, char *resolved_path) { - if (resolved_path == NULL) resolved_path = malloc(strlen(path) + 1); - strcpy(resolved_path, path); - return resolved_path; +int mkdir(const char *path, mode_t mode) { + (void) path, (void) mode; + return -1; } struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, @@ -53,6 +41,5 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn, return NULL; } -void _fini(void); void _fini(void) { }