Add mkdir to the FS API

This commit is contained in:
Sergey Lyubka 2022-01-18 19:19:34 +00:00
parent 56a7438114
commit d0857fe36c
8 changed files with 73 additions and 54 deletions

View File

@ -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);
}
}
}

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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};

View File

@ -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};

View File

@ -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) {
}