Add shims for mongoose file operations

PUBLISHED_FROM=b5b12cdf4273f21dbcdac9bfb6a06063152914c9
This commit is contained in:
Alexander Alashkin 2017-03-01 09:50:46 +00:00 committed by Cesanta Bot
parent aed6d05d8c
commit 920e8d9145
6 changed files with 55 additions and 5 deletions

View File

@ -8,6 +8,8 @@ items:
- { name: mg_basic_auth_header.md }
- { name: mg_conn_addr_to_str.md }
- { name: mg_fopen.md }
- { name: mg_fread.md }
- { name: mg_fwrite.md }
- { name: mg_hexdump.md }
- { name: mg_hexdump_connection.md }
- { name: mg_hexdumpf.md }

View File

@ -0,0 +1,12 @@
---
title: "mg_fread()"
decl_name: "mg_fread"
symbol_kind: "func"
signature: |
size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f);
---
Reads data from the given file stream.
Return value is a number of bytes readen.

View File

@ -0,0 +1,12 @@
---
title: "mg_fwrite()"
decl_name: "mg_fwrite"
symbol_kind: "func"
signature: |
size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f);
---
Writes data to the given file stream.
Return value is a number of bytes wtitten.

View File

@ -9,3 +9,4 @@ title: Tunables
- `MG_SSL_CRYPTO_MODERN`, `MG_SSL_CRYPTO_OLD` - choose either "Modern" or "Old" ciphers
instead of the default "Intermediate" setting.
See [this article](https://wiki.mozilla.org/Security/Server_Side_TLS#Recommended_configurations) for details.
- `MG_USER_FILE_FUNCTIONS` allow you to use custom file operation, by defining you own `mg_stat`, `mg_fopen`, `mg_open`, `mg_fread` and `mg_fwrite` functions

View File

@ -5374,7 +5374,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) {
if (to_read == 0) {
/* Rate limiting. send_mbuf is too full, wait until it's drained. */
} else if (pd->file.sent < pd->file.cl &&
(n = fread(buf, 1, to_read, pd->file.fp)) > 0) {
(n = mg_fread(buf, 1, to_read, pd->file.fp)) > 0) {
mg_send(nc, buf, n);
pd->file.sent += n;
} else {
@ -5384,7 +5384,7 @@ static void mg_http_transfer_file_data(struct mg_connection *nc) {
} else if (pd->file.type == DATA_PUT) {
struct mbuf *io = &nc->recv_mbuf;
size_t to_write = left <= 0 ? 0 : left < io->len ? (size_t) left : io->len;
size_t n = fwrite(io->buf, 1, to_write, pd->file.fp);
size_t n = mg_fwrite(io->buf, 1, to_write, pd->file.fp);
if (n > 0) {
mbuf_remove(io, n);
pd->file.sent += n;
@ -7515,7 +7515,7 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
struct file_upload_state *fus =
(struct file_upload_state *) mp->user_data;
if (fus == NULL || fus->fp == NULL) break;
if (fwrite(mp->data.p, 1, mp->data.len, fus->fp) != mp->data.len) {
if (mg_fwrite(mp->data.p, 1, mp->data.len, fus->fp) != mp->data.len) {
LOG(LL_ERROR, ("Failed to write to %s: %d, wrote %d", fus->lfn,
mg_get_errno(), (int) fus->num_recd));
if (mg_get_errno() == ENOSPC
@ -8328,7 +8328,7 @@ static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm,
static void mg_send_file_data(struct mg_connection *nc, FILE *fp) {
char buf[BUFSIZ];
size_t n;
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
while ((n = mg_fread(buf, 1, sizeof(buf), fp)) > 0) {
mg_send(nc, buf, n);
}
}
@ -9209,7 +9209,7 @@ static int lowercase(const char *s) {
return tolower(*(const unsigned char *) s);
}
#if MG_ENABLE_FILESYSTEM
#if MG_ENABLE_FILESYSTEM && !defined(MG_USER_FILE_FUNCTIONS)
int mg_stat(const char *path, cs_stat_t *st) {
#ifdef _WIN32
wchar_t wpath[MAX_PATH_SIZE];
@ -9241,6 +9241,14 @@ int mg_open(const char *path, int flag, int mode) { /* LCOV_EXCL_LINE */
return open(path, flag, mode); /* LCOV_EXCL_LINE */
#endif
}
size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f) {
return fread(ptr, size, count, f);
}
size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f) {
return fwrite(ptr, size, count, f);
}
#endif
void mg_base64_encode(const unsigned char *src, int src_len, char *dst) {

View File

@ -3898,6 +3898,21 @@ FILE *mg_fopen(const char *path, const char *mode);
* Return value is the same as for the `open()` syscall.
*/
int mg_open(const char *path, int flag, int mode);
/*
* Reads data from the given file stream.
*
* Return value is a number of bytes readen.
*/
size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f);
/*
* Writes data to the given file stream.
*
* Return value is a number of bytes wtitten.
*/
size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f);
#endif /* MG_ENABLE_FILESYSTEM */
#if MG_ENABLE_THREADS