From 55d102e450fe7f5b466d62aaec3998f21bbb343e Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 27 Jul 2021 09:50:11 +0100 Subject: [PATCH] Enable mg_fopen_packed in newlib --- mongoose.c | 10 +++++----- mongoose.h | 2 +- src/fs.c | 10 +++++----- src/fs.h | 2 +- test/unit_test.c | 9 ++++++--- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/mongoose.c b/mongoose.c index 98805040..94fe4ffe 100644 --- a/mongoose.c +++ b/mongoose.c @@ -437,13 +437,13 @@ ssize_t packed_write(void *cookie, const char *buf, size_t size) { return -1; } -int packed_seek(void *cookie, off64_t *offset, int whence) { +int packed_seek(void *cookie, long *offset, int whence) { struct packed_file *fp = (struct packed_file *) cookie; if (whence == SEEK_SET) fp->pos = (size_t) *offset; - if (whence == SEEK_END) fp->pos = (size_t)((off64_t) fp->size + *offset); - if (whence == SEEK_CUR) fp->pos = (size_t)((off64_t) fp->pos + *offset); + if (whence == SEEK_END) fp->pos = (size_t)((long) fp->size + *offset); + if (whence == SEEK_CUR) fp->pos = (size_t)((long) fp->pos + *offset); if (fp->pos > fp->size) fp->pos = fp->size; - *offset = (off64_t) fp->pos; + *offset = (long) fp->pos; return 0; } @@ -461,7 +461,7 @@ FILE *mg_fopen_packed(const char *path, const char *mode) { }; struct packed_file *cookie = NULL; size_t size = 0; - const char *data = unpack(path, &size); + const char *data = mg_unpack(path, &size); if (data == NULL) return NULL; if ((cookie = calloc(1, sizeof(*cookie))) == NULL) return NULL; cookie->data = data; diff --git a/mongoose.h b/mongoose.h index 2586096e..4a09f906 100644 --- a/mongoose.h +++ b/mongoose.h @@ -608,7 +608,7 @@ void mg_usleep(unsigned long usecs); FILE *mg_fopen_packed(const char *path, const char *mode); -#if defined(__linux__) && defined(GCC) +#if (defined(__linux__) && defined(__GNUC__)) || defined(__NEWLIB__) #define MG_ENABLE_PACKED_FS 1 #define MG_FOPENCOOKIE #else diff --git a/src/fs.c b/src/fs.c index cae613dd..fe2064f7 100644 --- a/src/fs.c +++ b/src/fs.c @@ -26,13 +26,13 @@ ssize_t packed_write(void *cookie, const char *buf, size_t size) { return -1; } -int packed_seek(void *cookie, off64_t *offset, int whence) { +int packed_seek(void *cookie, long *offset, int whence) { struct packed_file *fp = (struct packed_file *) cookie; if (whence == SEEK_SET) fp->pos = (size_t) *offset; - if (whence == SEEK_END) fp->pos = (size_t)((off64_t) fp->size + *offset); - if (whence == SEEK_CUR) fp->pos = (size_t)((off64_t) fp->pos + *offset); + if (whence == SEEK_END) fp->pos = (size_t)((long) fp->size + *offset); + if (whence == SEEK_CUR) fp->pos = (size_t)((long) fp->pos + *offset); if (fp->pos > fp->size) fp->pos = fp->size; - *offset = (off64_t) fp->pos; + *offset = (long) fp->pos; return 0; } @@ -50,7 +50,7 @@ FILE *mg_fopen_packed(const char *path, const char *mode) { }; struct packed_file *cookie = NULL; size_t size = 0; - const char *data = unpack(path, &size); + const char *data = mg_unpack(path, &size); if (data == NULL) return NULL; if ((cookie = calloc(1, sizeof(*cookie))) == NULL) return NULL; cookie->data = data; diff --git a/src/fs.h b/src/fs.h index 589cd72d..9fd0b957 100644 --- a/src/fs.h +++ b/src/fs.h @@ -4,7 +4,7 @@ FILE *mg_fopen_packed(const char *path, const char *mode); -#if defined(__linux__) && defined(GCC) +#if (defined(__linux__) && defined(__GNUC__)) || defined(__NEWLIB__) #define MG_ENABLE_PACKED_FS 1 #define MG_FOPENCOOKIE #else diff --git a/test/unit_test.c b/test/unit_test.c index 3def40dd..f37d9e89 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1336,11 +1336,14 @@ static void test_multipart(void) { } static void test_packed(void) { - FILE *fp = mg_fopen_packed("Makefile", "r"); -#if defined(__linux__) && defined(GCC) + const char *path = "Makefile"; + FILE *fp = mg_fopen_packed(path, "r"); +#if MG_ENABLE_PACKED_FS + struct stat st; + ASSERT(stat(path, &st) == 0); ASSERT(fp != NULL); fseek(fp, 0, SEEK_END); - ASSERT(ftell(fp) > 0); + ASSERT(ftell(fp) == st.st_size); fclose(fp); #else ASSERT(fp == NULL);