Skip to content

Commit

Permalink
Use mg_fopen only on win32
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Jul 22, 2021
1 parent a771d6a commit 55e2077
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 40 deletions.
27 changes: 8 additions & 19 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
LOG(LL_DEBUG,
("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name));
if ((fp = mg_fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
if ((fp = fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno);
return -2;
} else {
Expand Down Expand Up @@ -926,7 +926,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
mg_stat_t st;
char etag[64];
FILE *fp = mg_fopen(path, "rb");
FILE *fp = fopen(path, "rb");
if (fp == NULL || mg_stat(path, &st) != 0 ||
mg_http_etag(etag, sizeof(etag), &st) != etag) {
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
Expand Down Expand Up @@ -1267,14 +1267,14 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len,
hm->uri.ptr);
} else {
FILE *fp = mg_fopen(t2, "r");
FILE *fp = fopen(t2, "r");
#if MG_ENABLE_SSI
if (is_index && fp == NULL) {
char *p = t2 + strlen(t2);
while (p > t2 && p[-1] != '/') p--;
strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2));
t2[sizeof(t2) - 1] = '\0';
fp = mg_fopen(t2, "r");
fp = fopen(t2, "r");
}
#endif
if (is_index && fp == NULL) {
Expand Down Expand Up @@ -3209,7 +3209,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
#if MG_ENABLE_SSI
static char *mg_ssi(const char *path, const char *root, int depth) {
struct mg_iobuf b = {NULL, 0, 0};
FILE *fp = mg_fopen(path, "rb");
FILE *fp = fopen(path, "rb");
if (fp != NULL) {
char buf[BUFSIZ], arg[sizeof(buf)];
int ch, intag = 0;
Expand Down Expand Up @@ -3921,17 +3921,6 @@ int mg_stat(const char *path, mg_stat_t *st) {
#endif
}

FILE *mg_fopen(const char *path, const char *mode) {
#ifdef _WIN32
wchar_t b1[MG_PATH_MAX], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
return _wfopen(b1, b2);
#else
return fopen(path, mode);
#endif
}

int64_t mg_file_size(const char *path) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF)
struct FF_STAT st;
Expand All @@ -3946,7 +3935,7 @@ char *mg_file_read(const char *path, size_t *sizep) {
FILE *fp;
char *data = NULL;
size_t size = (size_t) mg_file_size(path);
if ((fp = mg_fopen(path, "rb")) != NULL) {
if ((fp = fopen(path, "rb")) != NULL) {
data = (char *) calloc(1, size + 1);
if (data != NULL) {
if (fread(data, 1, size, fp) != size) {
Expand All @@ -3967,7 +3956,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) {
FILE *fp;
char tmp[MG_PATH_MAX];
snprintf(tmp, sizeof(tmp), "%s.%d", path, rand());
fp = mg_fopen(tmp, "wb");
fp = fopen(tmp, "wb");
if (fp != NULL) {
result = fwrite(buf, 1, len, fp) == len;
fclose(fp);
Expand Down Expand Up @@ -4002,7 +3991,7 @@ void mg_random(void *buf, size_t len) {
while (len--) *p++ = (unsigned char) (esp_random() & 255);
#elif MG_ARCH == MG_ARCH_WIN32
#elif MG_ARCH_UNIX && MG_ENABLE_FS
FILE *fp = mg_fopen("/dev/urandom", "rb");
FILE *fp = fopen("/dev/urandom", "rb");
if (fp != NULL) {
if (fread(buf, 1, len, fp) == len) done = true;
fclose(fp);
Expand Down
9 changes: 8 additions & 1 deletion mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ typedef int socklen_t;
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
#define fopen(a, b) mg_fopen((a), (b))
#ifndef va_copy
#ifdef __va_copy
#define va_copy __va_copy
Expand All @@ -373,6 +374,13 @@ typedef int socklen_t;

#define MG_INT64_FMT "%I64d"

static __inline FILE *mg_fopen(const char *path, const char *mode) {
wchar_t b1[PATH_MAX], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
return _wfopen(b1, b2);
}

// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
(void) tm;
Expand Down Expand Up @@ -560,7 +568,6 @@ typedef struct _stati64 mg_stat_t;
typedef struct stat mg_stat_t;
#endif
int mg_stat(const char *path, mg_stat_t *);
FILE *mg_fopen(const char *fp, const char *mode);
#endif

#define mg_htons(x) mg_ntohs(x)
Expand Down
8 changes: 8 additions & 0 deletions src/arch_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef int socklen_t;
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
#define fopen(a, b) mg_fopen((a), (b))
#ifndef va_copy
#ifdef __va_copy
#define va_copy __va_copy
Expand All @@ -80,6 +81,13 @@ typedef int socklen_t;

#define MG_INT64_FMT "%I64d"

static __inline FILE *mg_fopen(const char *path, const char *mode) {
wchar_t b1[PATH_MAX], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
return _wfopen(b1, b2);
}

// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
(void) tm;
Expand Down
8 changes: 4 additions & 4 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
LOG(LL_DEBUG,
("%p %d bytes @ %d [%s]", c->fd, (int) hm->body.len, (int) oft, name));
if ((fp = mg_fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
if ((fp = fopen(path, oft == 0 ? "wb" : "ab")) == NULL) {
mg_http_reply(c, 400, "", "fopen(%s): %d", name, errno);
return -2;
} else {
Expand Down Expand Up @@ -515,7 +515,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
mg_stat_t st;
char etag[64];
FILE *fp = mg_fopen(path, "rb");
FILE *fp = fopen(path, "rb");
if (fp == NULL || mg_stat(path, &st) != 0 ||
mg_http_etag(etag, sizeof(etag), &st) != etag) {
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
Expand Down Expand Up @@ -856,14 +856,14 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
mg_http_reply(c, 404, "", "Invalid URI [%.*s]\n", (int) hm->uri.len,
hm->uri.ptr);
} else {
FILE *fp = mg_fopen(t2, "r");
FILE *fp = fopen(t2, "r");
#if MG_ENABLE_SSI
if (is_index && fp == NULL) {
char *p = t2 + strlen(t2);
while (p > t2 && p[-1] != '/') p--;
strncpy(p, "index.shtml", (size_t)(&t2[sizeof(t2)] - p - 2));
t2[sizeof(t2) - 1] = '\0';
fp = mg_fopen(t2, "r");
fp = fopen(t2, "r");
}
#endif
if (is_index && fp == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/ssi.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#if MG_ENABLE_SSI
static char *mg_ssi(const char *path, const char *root, int depth) {
struct mg_iobuf b = {NULL, 0, 0};
FILE *fp = mg_fopen(path, "rb");
FILE *fp = fopen(path, "rb");
if (fp != NULL) {
char buf[BUFSIZ], arg[sizeof(buf)];
int ch, intag = 0;
Expand Down
17 changes: 3 additions & 14 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ int mg_stat(const char *path, mg_stat_t *st) {
#endif
}

FILE *mg_fopen(const char *path, const char *mode) {
#ifdef _WIN32
wchar_t b1[MG_PATH_MAX], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
return _wfopen(b1, b2);
#else
return fopen(path, mode);
#endif
}

int64_t mg_file_size(const char *path) {
#if MG_ARCH == MG_ARCH_FREERTOS_TCP && defined(MG_ENABLE_FF)
struct FF_STAT st;
Expand All @@ -38,7 +27,7 @@ char *mg_file_read(const char *path, size_t *sizep) {
FILE *fp;
char *data = NULL;
size_t size = (size_t) mg_file_size(path);
if ((fp = mg_fopen(path, "rb")) != NULL) {
if ((fp = fopen(path, "rb")) != NULL) {
data = (char *) calloc(1, size + 1);
if (data != NULL) {
if (fread(data, 1, size, fp) != size) {
Expand All @@ -59,7 +48,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) {
FILE *fp;
char tmp[MG_PATH_MAX];
snprintf(tmp, sizeof(tmp), "%s.%d", path, rand());
fp = mg_fopen(tmp, "wb");
fp = fopen(tmp, "wb");
if (fp != NULL) {
result = fwrite(buf, 1, len, fp) == len;
fclose(fp);
Expand Down Expand Up @@ -94,7 +83,7 @@ void mg_random(void *buf, size_t len) {
while (len--) *p++ = (unsigned char) (esp_random() & 255);
#elif MG_ARCH == MG_ARCH_WIN32
#elif MG_ARCH_UNIX && MG_ENABLE_FS
FILE *fp = mg_fopen("/dev/urandom", "rb");
FILE *fp = fopen("/dev/urandom", "rb");
if (fp != NULL) {
if (fread(buf, 1, len, fp) == len) done = true;
fclose(fp);
Expand Down
1 change: 0 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ typedef struct _stati64 mg_stat_t;
typedef struct stat mg_stat_t;
#endif
int mg_stat(const char *path, mg_stat_t *);
FILE *mg_fopen(const char *fp, const char *mode);
#endif

#define mg_htons(x) mg_ntohs(x)
Expand Down

0 comments on commit 55e2077

Please sign in to comment.