Remove mg_stat

This commit is contained in:
Sergey Lyubka committed 2021-07-24 10:35:48 +01:00
1 parent 7e1d4325ec
commit d6dbf831f7
8 files changed
+63 -53

No files matched your search

+10 -17
View File
@@ -411,15 +411,8 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
#endif #endif
int mg_stat(const char *path, mg_stat_t *st) { // FILE *fopen_packed(const char *path, const char *mode) {
#ifdef _WIN32 //}
wchar_t tmp[MG_PATH_MAX];
MultiByteToWideChar(CP_UTF8, 0, path, -1, tmp, sizeof(tmp) / sizeof(tmp[0]));
return _wstati64(tmp, st);
#else
return stat(path, st);
#endif
}
#ifdef MG_ENABLE_LINES #ifdef MG_ENABLE_LINES
#line 1 "src/http.c" #line 1 "src/http.c"
@@ -797,7 +790,7 @@ static void restore_http_cb(struct mg_connection *c) {
c->pfn = http_cb; c->pfn = http_cb;
} }
char *mg_http_etag(char *buf, size_t len, mg_stat_t *st) { char *mg_http_etag(char *buf, size_t len, struct stat *st) {
snprintf(buf, len, "\"%lx." MG_INT64_FMT "\"", (unsigned long) st->st_mtime, snprintf(buf, len, "\"%lx." MG_INT64_FMT "\"", (unsigned long) st->st_mtime,
(int64_t) st->st_size); (int64_t) st->st_size);
return buf; return buf;
@@ -938,10 +931,10 @@ static int getrange(struct mg_str *s, int64_t *a, int64_t *b) {
void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
const char *path, const char *mime, const char *hdrs) { const char *path, const char *mime, const char *hdrs) {
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match"); struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
mg_stat_t st; struct stat st;
char etag[64]; char etag[64];
FILE *fp = fopen(path, "rb"); FILE *fp = fopen(path, "rb");
if (fp == NULL || mg_stat(path, &st) != 0 || if (fp == NULL || stat(path, &st) != 0 ||
mg_http_etag(etag, sizeof(etag), &st) != etag) { mg_http_etag(etag, sizeof(etag), &st) != etag) {
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path, LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
(void *) fp)); (void *) fp));
@@ -1015,8 +1008,8 @@ bool mg_is_dir(const char *path) {
struct FF_STAT st; struct FF_STAT st;
return (ff_stat(path, &st) == 0) && (st.st_mode & FF_IFDIR); return (ff_stat(path, &st) == 0) && (st.st_mode & FF_IFDIR);
#else #else
mg_stat_t st; struct stat st;
return mg_stat(path, &st) == 0 && S_ISDIR(st.st_mode); return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
#endif #endif
} }
@@ -1132,7 +1125,7 @@ struct dirent *readdir(DIR *d) {
#endif #endif
static void printdirentry(struct mg_connection *c, const char *name, static void printdirentry(struct mg_connection *c, const char *name,
mg_stat_t *stp) { struct stat *stp) {
char size[64], mod[64], path[MG_PATH_MAX]; char size[64], mod[64], path[MG_PATH_MAX];
int is_dir = S_ISDIR(stp->st_mode), n = 0; int is_dir = S_ISDIR(stp->st_mode), n = 0;
const char *slash = is_dir ? "/" : ""; const char *slash = is_dir ? "/" : "";
@@ -1213,14 +1206,14 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
(int) hm->uri.len, hm->uri.ptr); (int) hm->uri.len, hm->uri.ptr);
while ((dp = readdir(dirp)) != NULL) { while ((dp = readdir(dirp)) != NULL) {
mg_stat_t st; struct stat st;
const char *sep = dp->d_name[0] == MG_DIRSEP ? "/" : ""; const char *sep = dp->d_name[0] == MG_DIRSEP ? "/" : "";
// Do not show current dir and hidden files // Do not show current dir and hidden files
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue; if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue;
// SPIFFS can report "/foo.txt" in the dp->d_name // SPIFFS can report "/foo.txt" in the dp->d_name
if (snprintf(path, sizeof(path), "%s%s%s", dir, sep, dp->d_name) < 0) { if (snprintf(path, sizeof(path), "%s%s%s", dir, sep, dp->d_name) < 0) {
LOG(LL_ERROR, ("%s truncated", dp->d_name)); LOG(LL_ERROR, ("%s truncated", dp->d_name));
} else if (mg_stat(path, &st) != 0) { } else if (stat(path, &st) != 0) {
LOG(LL_ERROR, ("%lu stat(%s): %d", c->id, path, errno)); LOG(LL_ERROR, ("%lu stat(%s): %d", c->id, path, errno));
} else { } else {
printdirentry(c, dp->d_name, &st); printdirentry(c, dp->d_name, &st);
+20 -8
View File
@@ -314,7 +314,8 @@ struct timeval {
#if MG_ARCH == MG_ARCH_UNIX #if MG_ARCH == MG_ARCH_UNIX
#define _DARWIN_UNLIMITED_SELECT 1 #define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors
#define _GNU_SOURCE // For fopencookie() on Linux
#include <arpa/inet.h> #include <arpa/inet.h>
#include <ctype.h> #include <ctype.h>
@@ -415,6 +416,16 @@ typedef int socklen_t;
#endif #endif
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
#define fopen(a, b) mg_fopen((a), (b)) #define fopen(a, b) mg_fopen((a), (b))
// Later windows define _stati64 macro, older do not
#ifdef _stati64
#define stat _stat64
#define _stat64(a, b) mg_stat((a), (b))
#else
#define stat _stati64
#define _stati64(a, b) mg_stat((a), (b))
#endif
#ifndef va_copy #ifndef va_copy
#ifdef __va_copy #ifdef __va_copy
#define va_copy __va_copy #define va_copy __va_copy
@@ -429,12 +440,18 @@ typedef int socklen_t;
#define MG_INT64_FMT "%I64d" #define MG_INT64_FMT "%I64d"
static __inline FILE *mg_fopen(const char *path, const char *mode) { static __inline FILE *mg_fopen(const char *path, const char *mode) {
wchar_t b1[PATH_MAX], b2[10]; wchar_t b1[MAX_PATH], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0])); MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0])); MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
return _wfopen(b1, b2); return _wfopen(b1, b2);
} }
static __inline int mg_stat(const char *path, struct stat *st) {
wchar_t tmp[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, tmp, sizeof(tmp) / sizeof(tmp[0]));
return _wstati64(tmp, st);
}
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime // https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) { static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
(void) tm; (void) tm;
@@ -589,12 +606,7 @@ void mg_usleep(unsigned long usecs);
#ifdef _WIN32 FILE *fopen_packed(const char *path, const char *mode);
typedef struct _stati64 mg_stat_t;
#else
typedef struct stat mg_stat_t;
#endif
int mg_stat(const char *path, mg_stat_t *);
+2 -1
View File
@@ -2,7 +2,8 @@
#if MG_ARCH == MG_ARCH_UNIX #if MG_ARCH == MG_ARCH_UNIX
#define _DARWIN_UNLIMITED_SELECT 1 #define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors
#define _GNU_SOURCE // For fopencookie() on Linux
#include <arpa/inet.h> #include <arpa/inet.h>
#include <ctype.h> #include <ctype.h>
+17 -1
View File
@@ -68,6 +68,16 @@ typedef int socklen_t;
#endif #endif
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
#define fopen(a, b) mg_fopen((a), (b)) #define fopen(a, b) mg_fopen((a), (b))
// Later windows define _stati64 macro, older do not
#ifdef _stati64
#define stat _stat64
#define _stat64(a, b) mg_stat((a), (b))
#else
#define stat _stati64
#define _stati64(a, b) mg_stat((a), (b))
#endif
#ifndef va_copy #ifndef va_copy
#ifdef __va_copy #ifdef __va_copy
#define va_copy __va_copy #define va_copy __va_copy
@@ -82,12 +92,18 @@ typedef int socklen_t;
#define MG_INT64_FMT "%I64d" #define MG_INT64_FMT "%I64d"
static __inline FILE *mg_fopen(const char *path, const char *mode) { static __inline FILE *mg_fopen(const char *path, const char *mode) {
wchar_t b1[PATH_MAX], b2[10]; wchar_t b1[MAX_PATH], b2[10];
MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0])); MultiByteToWideChar(CP_UTF8, 0, path, -1, b1, sizeof(b1) / sizeof(b1[0]));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0])); MultiByteToWideChar(CP_UTF8, 0, mode, -1, b2, sizeof(b2) / sizeof(b2[0]));
return _wfopen(b1, b2); return _wfopen(b1, b2);
} }
static __inline int mg_stat(const char *path, struct stat *st) {
wchar_t tmp[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, tmp, sizeof(tmp) / sizeof(tmp[0]));
return _wstati64(tmp, st);
}
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime // https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) { static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
(void) tm; (void) tm;
+2 -9
View File
@@ -1,11 +1,4 @@
#include "fs.h" #include "fs.h"
int mg_stat(const char *path, mg_stat_t *st) { // FILE *fopen_packed(const char *path, const char *mode) {
#ifdef _WIN32 //}
wchar_t tmp[MG_PATH_MAX];
MultiByteToWideChar(CP_UTF8, 0, path, -1, tmp, sizeof(tmp) / sizeof(tmp[0]));
return _wstati64(tmp, st);
#else
return stat(path, st);
#endif
}
+1 -6
View File
@@ -2,9 +2,4 @@
#include "arch.h" #include "arch.h"
#ifdef _WIN32 FILE *fopen_packed(const char *path, const char *mode);
typedef struct _stati64 mg_stat_t;
#else
typedef struct stat mg_stat_t;
#endif
int mg_stat(const char *path, mg_stat_t *);
+8 -8
View File
@@ -371,7 +371,7 @@ static void restore_http_cb(struct mg_connection *c) {
c->pfn = http_cb; c->pfn = http_cb;
} }
char *mg_http_etag(char *buf, size_t len, mg_stat_t *st) { char *mg_http_etag(char *buf, size_t len, struct stat *st) {
snprintf(buf, len, "\"%lx." MG_INT64_FMT "\"", (unsigned long) st->st_mtime, snprintf(buf, len, "\"%lx." MG_INT64_FMT "\"", (unsigned long) st->st_mtime,
(int64_t) st->st_size); (int64_t) st->st_size);
return buf; return buf;
@@ -512,10 +512,10 @@ static int getrange(struct mg_str *s, int64_t *a, int64_t *b) {
void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
const char *path, const char *mime, const char *hdrs) { const char *path, const char *mime, const char *hdrs) {
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match"); struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
mg_stat_t st; struct stat st;
char etag[64]; char etag[64];
FILE *fp = fopen(path, "rb"); FILE *fp = fopen(path, "rb");
if (fp == NULL || mg_stat(path, &st) != 0 || if (fp == NULL || stat(path, &st) != 0 ||
mg_http_etag(etag, sizeof(etag), &st) != etag) { mg_http_etag(etag, sizeof(etag), &st) != etag) {
LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path, LOG(LL_DEBUG, ("404 [%.*s] [%s] %p", (int) hm->uri.len, hm->uri.ptr, path,
(void *) fp)); (void *) fp));
@@ -589,8 +589,8 @@ bool mg_is_dir(const char *path) {
struct FF_STAT st; struct FF_STAT st;
return (ff_stat(path, &st) == 0) && (st.st_mode & FF_IFDIR); return (ff_stat(path, &st) == 0) && (st.st_mode & FF_IFDIR);
#else #else
mg_stat_t st; struct stat st;
return mg_stat(path, &st) == 0 && S_ISDIR(st.st_mode); return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
#endif #endif
} }
@@ -706,7 +706,7 @@ struct dirent *readdir(DIR *d) {
#endif #endif
static void printdirentry(struct mg_connection *c, const char *name, static void printdirentry(struct mg_connection *c, const char *name,
mg_stat_t *stp) { struct stat *stp) {
char size[64], mod[64], path[MG_PATH_MAX]; char size[64], mod[64], path[MG_PATH_MAX];
int is_dir = S_ISDIR(stp->st_mode), n = 0; int is_dir = S_ISDIR(stp->st_mode), n = 0;
const char *slash = is_dir ? "/" : ""; const char *slash = is_dir ? "/" : "";
@@ -787,14 +787,14 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
(int) hm->uri.len, hm->uri.ptr); (int) hm->uri.len, hm->uri.ptr);
while ((dp = readdir(dirp)) != NULL) { while ((dp = readdir(dirp)) != NULL) {
mg_stat_t st; struct stat st;
const char *sep = dp->d_name[0] == MG_DIRSEP ? "/" : ""; const char *sep = dp->d_name[0] == MG_DIRSEP ? "/" : "";
// Do not show current dir and hidden files // Do not show current dir and hidden files
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue; if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue;
// SPIFFS can report "/foo.txt" in the dp->d_name // SPIFFS can report "/foo.txt" in the dp->d_name
if (snprintf(path, sizeof(path), "%s%s%s", dir, sep, dp->d_name) < 0) { if (snprintf(path, sizeof(path), "%s%s%s", dir, sep, dp->d_name) < 0) {
LOG(LL_ERROR, ("%s truncated", dp->d_name)); LOG(LL_ERROR, ("%s truncated", dp->d_name));
} else if (mg_stat(path, &st) != 0) { } else if (stat(path, &st) != 0) {
LOG(LL_ERROR, ("%lu stat(%s): %d", c->id, path, errno)); LOG(LL_ERROR, ("%lu stat(%s): %d", c->id, path, errno));
} else { } else {
printdirentry(c, dp->d_name, &st); printdirentry(c, dp->d_name, &st);
+3 -3
View File
@@ -473,7 +473,6 @@ static void test_http_server(void) {
struct mg_mgr mgr; struct mg_mgr mgr;
const char *url = "http://127.0.0.1:12346"; const char *url = "http://127.0.0.1:12346";
char buf[FETCH_BUF_SIZE]; char buf[FETCH_BUF_SIZE];
mg_stat_t st;
mg_mgr_init(&mgr); mg_mgr_init(&mgr);
mg_http_listen(&mgr, url, eh1, NULL); mg_http_listen(&mgr, url, eh1, NULL);
@@ -493,9 +492,10 @@ static void test_http_server(void) {
ASSERT(cmpbody(buf, "є\n") == 0); ASSERT(cmpbody(buf, "є\n") == 0);
{ {
extern char *mg_http_etag(char *, size_t, mg_stat_t *); extern char *mg_http_etag(char *, size_t, struct stat *);
struct stat st;
char etag[100]; char etag[100];
ASSERT(mg_stat("./test/data/a.txt", &st) == 0); ASSERT(stat("./test/data/a.txt", &st) == 0);
ASSERT(mg_http_etag(etag, sizeof(etag), &st) == etag); ASSERT(mg_http_etag(etag, sizeof(etag), &st) == etag);
ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\nIf-None-Match: %s\n\n", ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\nIf-None-Match: %s\n\n",
etag) == 304); etag) == 304);