Use 64bit file ops even on 32bit systems

This commit is contained in:
cpq committed 2021-01-26 17:43:31 +00:00
1 parent 5b4b7f7dc5
commit 5118355732
10 files changed
+58 -23

No files matched your search

+11 -10
View File
@@ -671,9 +671,9 @@ static void restore_http_cb(struct mg_connection *c) {
free(d);
}
char *mg_http_etag(char *buf, size_t len, struct stat *st) {
snprintf(buf, len, "\"%lx.%lu\"", (unsigned long) st->st_mtime,
(unsigned long) st->st_size);
char *mg_http_etag(char *buf, size_t len, mg_stat_t *st) {
snprintf(buf, len, "\"%lx." MG_INT64_FMT "\"", (unsigned long) st->st_mtime,
(int64_t) st->st_size);
return buf;
}
@@ -785,10 +785,10 @@ static const char *guess_content_type(const char *filename) {
void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
const char *path, const char *mime, const char *hdrs) {
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
struct stat st;
mg_stat_t st;
FILE *fp = fopen(path, "rb");
char etag[64];
if (fp == NULL || stat(path, &st) != 0 ||
if (fp == NULL || mg_stat(path, &st) != 0 ||
mg_http_etag(etag, sizeof(etag), &st) != etag) {
mg_http_reply(c, 404, "", "%s", "Not found\n");
} else if (inm != NULL && mg_vcasecmp(inm, etag) == 0) {
@@ -797,8 +797,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
} else {
mg_printf(c,
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n"
"Etag: %s\r\nContent-Length: %lu\r\n%s\r\n",
mime, etag, (unsigned long) st.st_size, hdrs ? hdrs : "");
"Etag: %s\r\nContent-Length: " MG_INT64_FMT "\r\n%s\r\n",
mime, etag, (int64_t) st.st_size, hdrs ? hdrs : "");
if (mg_vcasecmp(&hm->method, "HEAD") == 0) {
fclose(fp);
} else {
@@ -949,7 +949,7 @@ struct dirent *readdir(DIR *d) {
#endif
static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
const char *name, struct stat *stp) {
const char *name, mg_stat_t *stp) {
char size[64], mod[64]; //, path[PATH_MAX];
int is_dir = S_ISDIR(stp->st_mode);
const char *slash = is_dir ? "/" : "";
@@ -997,11 +997,11 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
"<tr><td colspan=\"3\"><hr></td></tr></thead><tbody>\n",
(int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr);
while ((dp = readdir(dirp)) != NULL) {
struct stat st;
mg_stat_t st;
// Do not show current dir and hidden files
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue;
snprintf(path, sizeof(path), "%s/%s", dir, dp->d_name);
if (stat(path, &st) != 0) {
if (mg_stat(path, &st) != 0) {
LOG(LL_ERROR, ("%lu stat(%s): %d", c->id, path, errno));
continue;
}
@@ -3711,6 +3711,7 @@ struct mg_str mg_url_pass(const char *url) {
#endif
#if MG_ENABLE_FS
size_t mg_file_size(const char *path) {
#if MG_ARCH == MG_ARCH_FREERTOS
+17
View File
@@ -121,6 +121,7 @@
#include <dirent.h>
#include <netdb.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#endif
@@ -133,6 +134,7 @@
#include <stdbool.h>
#include <sys/time.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#endif
@@ -146,6 +148,7 @@
#include <FreeRTOS_Sockets.h>
#include <stdbool.h>
#define MG_INT64_FMT "%lld"
#define MG_DIRSEP '/'
#define IPPROTO_TCP FREERTOS_IPPROTO_TCP
#define IPPROTO_UDP FREERTOS_IPPROTO_UDP
@@ -206,6 +209,7 @@ static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) {
#include <arpa/inet.h>
#include <dirent.h>
#include <inttypes.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <signal.h>
@@ -217,6 +221,7 @@ static inline int ff_vfprintf(FF_FILE *fp, const char *fmt, va_list ap) {
#include <unistd.h>
#define MG_DIRSEP '/'
#define MG_ENABLE_POSIX 1
#define MG_INT64_FMT "%" PRId64
#endif
@@ -273,6 +278,8 @@ typedef int socklen_t;
#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
#endif
#define MG_INT64_FMT "%I64d"
#endif
@@ -432,6 +439,16 @@ double mg_time(void);
unsigned long mg_millis(void);
void mg_usleep(unsigned long usecs);
#if MG_ENABLE_FS
#ifdef _WIN32
typedef struct _stati64 mg_stat_t;
#define mg_stat(a, b) _stati64((a), (b))
#else
typedef struct stat mg_stat_t;
#define mg_stat(a, b) stat((a), (b))
#endif
#endif
#define mg_htons(x) mg_ntohs(x)
#define mg_htonl(x) mg_ntohl(x)
+1
View File
@@ -5,5 +5,6 @@
#include <dirent.h>
#include <netdb.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#endif
+1
View File
@@ -8,5 +8,6 @@
#include <stdbool.h>
#include <sys/time.h>
#define MG_DIRSEP '/'
#define MG_INT64_FMT "%lld"
#endif
+1
View File
@@ -9,6 +9,7 @@
#include <FreeRTOS_Sockets.h>
#include <stdbool.h>
#define MG_INT64_FMT "%lld"
#define MG_DIRSEP '/'
#define IPPROTO_TCP FREERTOS_IPPROTO_TCP
#define IPPROTO_UDP FREERTOS_IPPROTO_UDP
+2
View File
@@ -6,6 +6,7 @@
#include <arpa/inet.h>
#include <dirent.h>
#include <inttypes.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <signal.h>
@@ -17,5 +18,6 @@
#include <unistd.h>
#define MG_DIRSEP '/'
#define MG_ENABLE_POSIX 1
#define MG_INT64_FMT "%" PRId64
#endif
+2
View File
@@ -52,4 +52,6 @@ typedef int socklen_t;
#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
#endif
#define MG_INT64_FMT "%I64d"
#endif
+10 -10
View File
@@ -251,9 +251,9 @@ static void restore_http_cb(struct mg_connection *c) {
free(d);
}
char *mg_http_etag(char *buf, size_t len, struct stat *st) {
snprintf(buf, len, "\"%lx.%lu\"", (unsigned long) st->st_mtime,
(unsigned long) st->st_size);
char *mg_http_etag(char *buf, size_t len, mg_stat_t *st) {
snprintf(buf, len, "\"%lx." MG_INT64_FMT "\"", (unsigned long) st->st_mtime,
(int64_t) st->st_size);
return buf;
}
@@ -365,10 +365,10 @@ static const char *guess_content_type(const char *filename) {
void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
const char *path, const char *mime, const char *hdrs) {
struct mg_str *inm = mg_http_get_header(hm, "If-None-Match");
struct stat st;
mg_stat_t st;
FILE *fp = fopen(path, "rb");
char etag[64];
if (fp == NULL || stat(path, &st) != 0 ||
if (fp == NULL || mg_stat(path, &st) != 0 ||
mg_http_etag(etag, sizeof(etag), &st) != etag) {
mg_http_reply(c, 404, "", "%s", "Not found\n");
} else if (inm != NULL && mg_vcasecmp(inm, etag) == 0) {
@@ -377,8 +377,8 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
} else {
mg_printf(c,
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n"
"Etag: %s\r\nContent-Length: %lu\r\n%s\r\n",
mime, etag, (unsigned long) st.st_size, hdrs ? hdrs : "");
"Etag: %s\r\nContent-Length: " MG_INT64_FMT "\r\n%s\r\n",
mime, etag, (int64_t) st.st_size, hdrs ? hdrs : "");
if (mg_vcasecmp(&hm->method, "HEAD") == 0) {
fclose(fp);
} else {
@@ -529,7 +529,7 @@ struct dirent *readdir(DIR *d) {
#endif
static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
const char *name, struct stat *stp) {
const char *name, mg_stat_t *stp) {
char size[64], mod[64]; //, path[PATH_MAX];
int is_dir = S_ISDIR(stp->st_mode);
const char *slash = is_dir ? "/" : "";
@@ -577,11 +577,11 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
"<tr><td colspan=\"3\"><hr></td></tr></thead><tbody>\n",
(int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr);
while ((dp = readdir(dirp)) != NULL) {
struct stat st;
mg_stat_t st;
// Do not show current dir and hidden files
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue;
snprintf(path, sizeof(path), "%s/%s", dir, dp->d_name);
if (stat(path, &st) != 0) {
if (mg_stat(path, &st) != 0) {
LOG(LL_ERROR, ("%lu stat(%s): %d", c->id, path, errno));
continue;
}
+10
View File
@@ -23,6 +23,16 @@ double mg_time(void);
unsigned long mg_millis(void);
void mg_usleep(unsigned long usecs);
#if MG_ENABLE_FS
#ifdef _WIN32
typedef struct _stati64 mg_stat_t;
#define mg_stat(a, b) _stati64((a), (b))
#else
typedef struct stat mg_stat_t;
#define mg_stat(a, b) stat((a), (b))
#endif
#endif
#define mg_htons(x) mg_ntohs(x)
#define mg_htonl(x) mg_ntohl(x)
+3 -3
View File
@@ -451,7 +451,7 @@ static void test_http_server(void) {
struct mg_mgr mgr;
const char *url = "http://127.0.0.1:12346";
char buf[FETCH_BUF_SIZE];
struct stat st;
mg_stat_t st;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, url, eh1, NULL);
@@ -463,9 +463,9 @@ static void test_http_server(void) {
ASSERT(cmpbody(buf, "hello\n") == 0);
{
extern char *mg_http_etag(char *, size_t, struct stat *);
extern char *mg_http_etag(char *, size_t, mg_stat_t *);
char etag[100];
ASSERT(stat("./test/data/a.txt", &st) == 0);
ASSERT(mg_stat("./test/data/a.txt", &st) == 0);
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",
etag) == 304);