Improve ff_write - save large blocks in chunks

This commit is contained in:
Sergey Lyubka 2022-01-19 08:43:34 +00:00
parent 1259ca9ac9
commit 4cd830a0bf
6 changed files with 38 additions and 20 deletions

View File

@ -495,10 +495,6 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) {
#if MG_ENABLE_FATFS #if MG_ENABLE_FATFS
#include <ff.h> #include <ff.h>
#if !defined(MG_FATFS_ROOT)
#define MG_FATFS_ROOT "/"
#endif
static int ff_stat(const char *path, size_t *size, time_t *mtime) { static int ff_stat(const char *path, size_t *size, time_t *mtime) {
FILINFO fi; FILINFO fi;
if (path[0] == '\0' || strcmp(path, MG_FATFS_ROOT) == 0) { 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) { static size_t ff_write(void *fp, const void *buf, size_t len) {
unsigned n = 0; unsigned n, sum = 0, bs = MG_FATFS_BSIZE;
f_write((FIL *) fp, buf, len, &n); while ((size_t) sum < len &&
return n; 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) { 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); mg_http_reply(c, 400, "", "open(%s): %d", path, errno);
return -2; return -2;
} else { } 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_fs_close(fd);
mg_http_reply(c, 200, "", ""); mg_http_reply(c, 200, "", "%d", written);
return (int) hm->body.len; return (int) hm->body.len;
} }
} }

View File

@ -446,6 +446,14 @@ static __inline struct tm *localtime_r(const time_t *t, struct tm *tm) {
#define MG_ENABLE_FATFS 0 #define MG_ENABLE_FATFS 0
#endif #endif
#ifndef MG_FATFS_ROOT
#define MG_FATFS_ROOT "/"
#endif
#ifndef MG_FATFS_BSIZE
#define MG_FATFS_BSIZE 64
#endif
#ifndef MG_ENABLE_SOCKET #ifndef MG_ENABLE_SOCKET
#define MG_ENABLE_SOCKET 1 #define MG_ENABLE_SOCKET 1
#endif #endif
@ -626,7 +634,7 @@ struct mg_fs {
size_t (*seek)(void *fd, size_t offset); // Set file position size_t (*seek)(void *fd, size_t offset); // Set file position
bool (*rename)(const char *from, const char *to); // Rename bool (*rename)(const char *from, const char *to); // Rename
bool (*remove)(const char *path); // Delete file bool (*remove)(const char *path); // Delete file
bool (*mkdir)(const char *path); // Create directory bool (*mkd)(const char *path); // Create directory
}; };
// File descriptor // File descriptor

View File

@ -4,6 +4,14 @@
#define MG_ENABLE_FATFS 0 #define MG_ENABLE_FATFS 0
#endif #endif
#ifndef MG_FATFS_ROOT
#define MG_FATFS_ROOT "/"
#endif
#ifndef MG_FATFS_BSIZE
#define MG_FATFS_BSIZE 64
#endif
#ifndef MG_ENABLE_SOCKET #ifndef MG_ENABLE_SOCKET
#define MG_ENABLE_SOCKET 1 #define MG_ENABLE_SOCKET 1
#endif #endif

View File

@ -18,7 +18,7 @@ struct mg_fs {
size_t (*seek)(void *fd, size_t offset); // Set file position size_t (*seek)(void *fd, size_t offset); // Set file position
bool (*rename)(const char *from, const char *to); // Rename bool (*rename)(const char *from, const char *to); // Rename
bool (*remove)(const char *path); // Delete file bool (*remove)(const char *path); // Delete file
bool (*mkdir)(const char *path); // Create directory bool (*mkd)(const char *path); // Create directory
}; };
// File descriptor // File descriptor

View File

@ -3,10 +3,6 @@
#if MG_ENABLE_FATFS #if MG_ENABLE_FATFS
#include <ff.h> #include <ff.h>
#if !defined(MG_FATFS_ROOT)
#define MG_FATFS_ROOT "/"
#endif
static int ff_stat(const char *path, size_t *size, time_t *mtime) { static int ff_stat(const char *path, size_t *size, time_t *mtime) {
FILINFO fi; FILINFO fi;
if (path[0] == '\0' || strcmp(path, MG_FATFS_ROOT) == 0) { 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) { static size_t ff_write(void *fp, const void *buf, size_t len) {
unsigned n = 0; unsigned n, sum = 0, bs = MG_FATFS_BSIZE;
f_write((FIL *) fp, buf, len, &n); while ((size_t) sum < len &&
return n; 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) { static size_t ff_seek(void *fp, size_t offset) {

View File

@ -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); mg_http_reply(c, 400, "", "open(%s): %d", path, errno);
return -2; return -2;
} else { } 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_fs_close(fd);
mg_http_reply(c, 200, "", ""); mg_http_reply(c, 200, "", "%d", written);
return (int) hm->body.len; return (int) hm->body.len;
} }
} }