mongoose/src/fs_packed.c

75 lines
2.1 KiB
C
Raw Normal View History

2021-07-29 04:11:07 +08:00
#include "fs.h"
struct packed_file {
const char *data;
size_t size;
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;
return NULL;
}
static char *packed_realpath(const char *path, char *resolved_path) {
2021-07-29 21:21:20 +08:00
if (resolved_path == NULL) resolved_path = (char *) malloc(strlen(path) + 1);
2021-07-29 04:11:07 +08:00
strcpy(resolved_path, path);
return resolved_path;
}
2021-07-29 21:21:20 +08:00
static int packed_stat(const char *path, size_t *size, time_t *mtime) {
2021-07-29 04:11:07 +08:00
const char *data = mg_unpack(path, size);
if (mtime) *mtime = 0;
2021-07-29 21:21:20 +08:00
return data == NULL ? MG_FS_DIR : MG_FS_READ;
2021-07-29 04:11:07 +08:00
}
static void packed_list(const char *path, void (*fn)(const char *, void *),
void *userdata) {
(void) path, (void) fn, (void) userdata;
}
static struct mg_fd *packed_open(const char *path, int flags) {
size_t size = 0;
const char *data = mg_unpack(path, &size);
struct packed_file *fp = NULL;
struct mg_fd *fd = NULL;
if (data == NULL) return NULL;
if (flags & MG_FS_WRITE) return NULL;
2021-07-29 21:21:20 +08:00
fp = (struct packed_file *) calloc(1, sizeof(*fp));
fd = (struct mg_fd *) calloc(1, sizeof(*fd));
2021-07-29 04:11:07 +08:00
fp->size = size;
fp->data = data;
fd->fd = fp;
fd->fs = &mg_fs_packed;
return fd;
}
static void packed_close(struct mg_fd *fd) {
if (fd) free(fd->fd), free(fd);
}
static size_t packed_read(void *fd, void *buf, size_t len) {
struct packed_file *fp = (struct packed_file *) fd;
if (fp->pos + len > fp->size) len = fp->size - fp->pos;
memcpy(buf, &fp->data[fp->pos], len);
fp->pos += len;
return len;
}
static size_t packed_write(void *fd, const void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t packed_seek(void *fd, size_t offset) {
struct packed_file *fp = (struct packed_file *) fd;
fp->pos = offset;
if (fp->pos > fp->size) fp->pos = fp->size;
return fp->pos;
}
struct mg_fs mg_fs_packed = {packed_realpath, packed_stat, packed_list,
packed_open, packed_close, packed_read,
packed_write, packed_seek};