diff --git a/mongoose.c b/mongoose.c index 6428aadf..6c1b2950 100644 --- a/mongoose.c +++ b/mongoose.c @@ -495,10 +495,6 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) { #if MG_ENABLE_FATFS #include -#if !defined(MG_FATFS_ROOT) -#define MG_FATFS_ROOT "/" -#endif - static int ff_stat(const char *path, size_t *size, time_t *mtime) { FILINFO fi; if (path[0] == '\0' || strcmp(path, MG_FATFS_ROOT) == 0) { @@ -554,9 +550,14 @@ static size_t ff_read(void *fp, void *buf, size_t len) { } static size_t ff_write(void *fp, const void *buf, size_t len) { - unsigned n = 0; - f_write((FIL *) fp, buf, len, &n); - return n; + unsigned n, sum = 0, bs = MG_FATFS_BSIZE; + while ((size_t) sum < len && + f_write((FIL *) fp, (char *) buf + sum, + sum + bs > len ? len - sum : bs, &n) == FR_OK && + n > 0) { + sum += n; + } + return sum; } static size_t ff_seek(void *fp, size_t offset) { @@ -1865,9 +1866,9 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, mg_http_reply(c, 400, "", "open(%s): %d", path, errno); return -2; } else { - fs->write(fd->fd, hm->body.ptr, hm->body.len); + int written = (int) fs->write(fd->fd, hm->body.ptr, hm->body.len); mg_fs_close(fd); - mg_http_reply(c, 200, "", ""); + mg_http_reply(c, 200, "", "%d", written); return (int) hm->body.len; } } diff --git a/mongoose.h b/mongoose.h index 82a06c62..b8650524 100644 --- a/mongoose.h +++ b/mongoose.h @@ -446,6 +446,14 @@ static __inline struct tm *localtime_r(const time_t *t, struct tm *tm) { #define MG_ENABLE_FATFS 0 #endif +#ifndef MG_FATFS_ROOT +#define MG_FATFS_ROOT "/" +#endif + +#ifndef MG_FATFS_BSIZE +#define MG_FATFS_BSIZE 64 +#endif + #ifndef MG_ENABLE_SOCKET #define MG_ENABLE_SOCKET 1 #endif @@ -626,7 +634,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 + bool (*mkd)(const char *path); // Create directory }; // File descriptor diff --git a/src/config.h b/src/config.h index 8fbeba60..d1c637e6 100644 --- a/src/config.h +++ b/src/config.h @@ -4,6 +4,14 @@ #define MG_ENABLE_FATFS 0 #endif +#ifndef MG_FATFS_ROOT +#define MG_FATFS_ROOT "/" +#endif + +#ifndef MG_FATFS_BSIZE +#define MG_FATFS_BSIZE 64 +#endif + #ifndef MG_ENABLE_SOCKET #define MG_ENABLE_SOCKET 1 #endif diff --git a/src/fs.h b/src/fs.h index 70239b0e..c84c4ee5 100644 --- a/src/fs.h +++ b/src/fs.h @@ -18,7 +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 + bool (*mkd)(const char *path); // Create directory }; // File descriptor diff --git a/src/fs_fat.c b/src/fs_fat.c index bf7e1ebf..bd1b5457 100644 --- a/src/fs_fat.c +++ b/src/fs_fat.c @@ -3,10 +3,6 @@ #if MG_ENABLE_FATFS #include -#if !defined(MG_FATFS_ROOT) -#define MG_FATFS_ROOT "/" -#endif - static int ff_stat(const char *path, size_t *size, time_t *mtime) { FILINFO fi; if (path[0] == '\0' || strcmp(path, MG_FATFS_ROOT) == 0) { @@ -62,9 +58,14 @@ static size_t ff_read(void *fp, void *buf, size_t len) { } static size_t ff_write(void *fp, const void *buf, size_t len) { - unsigned n = 0; - f_write((FIL *) fp, buf, len, &n); - return n; + unsigned n, sum = 0, bs = MG_FATFS_BSIZE; + while ((size_t) sum < len && + f_write((FIL *) fp, (char *) buf + sum, + sum + bs > len ? len - sum : bs, &n) == FR_OK && + n > 0) { + sum += n; + } + return sum; } static size_t ff_seek(void *fp, size_t offset) { diff --git a/src/http.c b/src/http.c index a9276828..a9195d89 100644 --- a/src/http.c +++ b/src/http.c @@ -906,9 +906,9 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, mg_http_reply(c, 400, "", "open(%s): %d", path, errno); return -2; } else { - fs->write(fd->fd, hm->body.ptr, hm->body.len); + int written = (int) fs->write(fd->fd, hm->body.ptr, hm->body.len); mg_fs_close(fd); - mg_http_reply(c, 200, "", ""); + mg_http_reply(c, 200, "", "%d", written); return (int) hm->body.len; } }