Better packed test

This commit is contained in:
Sergey Lyubka committed 2021-07-30 13:19:20 +01:00
1 parent 2af5d07467
commit 3a46055e52
14 files changed
+161 -80

No files matched your search

+8
View File
@@ -41,6 +41,14 @@
#define MG_ENABLE_SOCKETPAIR 0
#endif
#ifndef MG_ENABLE_CUSTOM_RANDOM
#define MG_ENABLE_CUSTOM_RANDOM 0
#endif
#ifndef MG_ENABLE_PACKED_FS
#define MG_ENABLE_PACKED_FS 0
#endif
// Granularity of the send/recv IO buffer growth
#ifndef MG_IO_SIZE
#define MG_IO_SIZE 2048
+31 -8
View File
@@ -6,32 +6,55 @@ struct packed_file {
size_t pos;
};
const char *mg_unpack(const char *, size_t *) WEAK;
const char *mg_unpack(const char *path, size_t *size) {
(void) path, (void) size;
const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
const char *mg_unlist(size_t no);
#if MG_ENABLE_PACKED_FS
#else
const char *mg_unpack(const char *path, size_t *size, time_t *mtime) {
(void) path, (void) size, (void) mtime;
return NULL;
}
const char *mg_unlist(size_t no) {
(void) no;
return NULL;
}
#endif
static char *packed_realpath(const char *path, char *resolved_path) {
if (resolved_path == NULL) resolved_path = (char *) malloc(strlen(path) + 1);
while (*path == '.' || *path == '/') path++;
strcpy(resolved_path, path);
return resolved_path;
}
static int is_dir_prefix(const char *prefix, size_t n, const char *path) {
return n < strlen(path) && memcmp(prefix, path, n) == 0 &&
(n == 0 || path[n] == MG_DIRSEP);
}
static int packed_stat(const char *path, size_t *size, time_t *mtime) {
const char *data = mg_unpack(path, size);
if (mtime) *mtime = 0;
return data == NULL ? MG_FS_DIR : MG_FS_READ;
const char *p;
size_t i, n = strlen(path);
if (mg_unpack(path, size, mtime)) return MG_FS_READ; // Regular file
// Scan all files. If `path` is a dir prefix for any of them, it's a dir
for (i = 0; (p = mg_unlist(i)) != NULL; i++) {
if (is_dir_prefix(path, n, p)) return MG_FS_DIR;
}
return 0;
}
static void packed_list(const char *path, void (*fn)(const char *, void *),
void *userdata) {
(void) path, (void) fn, (void) userdata;
const char *p;
size_t i, n = strlen(path);
for (i = 0; (p = mg_unlist(i)) != NULL; i++) {
if (is_dir_prefix(path, n, p)) fn(&p[n], userdata);
}
}
static struct mg_fd *packed_open(const char *path, int flags) {
size_t size = 0;
const char *data = mg_unpack(path, &size);
const char *data = mg_unpack(path, &size, NULL);
struct packed_file *fp = NULL;
struct mg_fd *fd = NULL;
if (data == NULL) return NULL;
+1 -1
View File
@@ -596,7 +596,7 @@ static void printdirentry(const char *name, void *userdata) {
char path[MG_PATH_MAX], sz[64], mod[64];
int flags, n = 0;
if (snprintf(path, sizeof(path), "%s%c%s", d->dir, MG_DIRSEP, name) < 0) {
if (snprintf(path, sizeof(path), "%s%c%s", d->dir, '/', name) < 0) {
LOG(LL_ERROR, ("%s truncated", name));
} else if ((flags = fs->stat(path, &size, &mtime)) == 0) {
LOG(LL_ERROR, ("%lu stat(%s): %d", d->c->id, path, errno));
+3
View File
@@ -57,6 +57,8 @@ bool mg_file_printf(const char *path, const char *fmt, ...) {
return result;
}
#if MG_ENABLE_CUSTOM_RANDOM
#else
void mg_random(void *buf, size_t len) {
bool done = false;
unsigned char *p = (unsigned char *) buf;
@@ -75,6 +77,7 @@ void mg_random(void *buf, size_t len) {
while (len--) *p++ = (unsigned char) (rand() & 255);
}
}
#endif
bool mg_globmatch(const char *s1, size_t n1, const char *s2, size_t n2) {
size_t i = 0, j = 0, ni = 0, nj = 0;
+1 -14
View File
@@ -3,23 +3,10 @@
#include "arch.h"
#include "str.h"
// WEAK symbol makes it possible to define a "default" function implementation,
// which could be overridden by the user who can define a function with the
// same name without linking conflict
#if !defined(WEAK)
#if (defined(__GNUC__) || defined(__clang__) || \
defined(__TI_COMPILER_VERSION__)) && \
!defined(_WIN32) && !defined(__CYGWIN__)
#define WEAK __attribute__((weak))
#else
#define WEAK
#endif
#endif
char *mg_file_read(const char *path, size_t *size);
bool mg_file_write(const char *path, const void *buf, size_t len);
bool mg_file_printf(const char *path, const char *fmt, ...);
void mg_random(void *buf, size_t len) WEAK;
void mg_random(void *buf, size_t len);
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
bool mg_next_comma_entry(struct mg_str *s, struct mg_str *k, struct mg_str *v);
uint16_t mg_ntohs(uint16_t net);