]> www.infradead.org Git - mtd-utils.git/commitdiff
Make it possible to compile mkfs.ubifs without zlib
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Sun, 18 Feb 2024 13:22:08 +0000 (14:22 +0100)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Sun, 18 Feb 2024 13:22:08 +0000 (14:22 +0100)
This one is a bit trickier than adding WITH_ZLIB ifdefs. Some parts
of the code assume that zlib is always present and have complicated
fallback behavior. Particularly the "favor_lzo" compression method
that uses either zlib or lzo, whichever produces the better result.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
ubifs-utils/mkfs.ubifs/compr.c
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c

index 07f6d4adaac38489423c88289cb050a2bfc78187..e4324f34aba031f5f4bd66c86f5d5664a92e8e96 100644 (file)
 #include <zstd.h>
 #endif
 
+#ifdef WITH_ZLIB
 #define crc32 __zlib_crc32
 #include <zlib.h>
 #undef crc32
+#endif
 
 #include "compr.h"
 #include "mkfs.ubifs.h"
@@ -45,6 +47,7 @@ static unsigned long long errcnt = 0;
 static struct ubifs_info *c = &info_;
 #endif
 
+#ifdef WITH_ZLIB
 #define DEFLATE_DEF_LEVEL     Z_DEFAULT_COMPRESSION
 #define DEFLATE_DEF_WINBITS   11
 #define DEFLATE_DEF_MEMLEVEL  8
@@ -91,6 +94,7 @@ static int zlib_deflate(void *in_buf, size_t in_len, void *out_buf,
 
        return 0;
 }
+#endif
 
 #ifdef WITH_LZO
 static int lzo_compress(void *in_buf, size_t in_len, void *out_buf,
@@ -140,7 +144,7 @@ static int no_compress(void *in_buf, size_t in_len, void *out_buf,
 
 static char *zlib_buf;
 
-#ifdef WITH_LZO
+#if defined(WITH_LZO) && defined(WITH_ZLIB)
 static int favor_lzo_compress(void *in_buf, size_t in_len, void *out_buf,
                               size_t *out_len, int *type)
 {
@@ -198,21 +202,24 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
                return MKFS_UBIFS_COMPR_NONE;
        }
 
-#ifndef WITH_LZO
-       {
-               switch (type) {
-#else
+#if defined(WITH_LZO) && defined(WITH_ZLIB)
        if (c->favor_lzo)
                ret = favor_lzo_compress(in_buf, in_len, out_buf, out_len, &type);
        else {
+#else
+       {
+#endif
                switch (type) {
+#ifdef WITH_LZO
                case MKFS_UBIFS_COMPR_LZO:
                        ret = lzo_compress(in_buf, in_len, out_buf, out_len);
                        break;
 #endif
+#ifdef WITH_ZLIB
                case MKFS_UBIFS_COMPR_ZLIB:
                        ret = zlib_deflate(in_buf, in_len, out_buf, out_len);
                        break;
+#endif
 #ifdef WITH_ZSTD
                case MKFS_UBIFS_COMPR_ZSTD:
                        ret = zstd_compress(in_buf, in_len, out_buf, out_len);
@@ -244,9 +251,13 @@ int init_compression(void)
                return -1;
 #endif
 
+#ifndef WITH_ZLIB
+       zlib_buf = NULL;
+#else
        zlib_buf = malloc(UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR);
        if (!zlib_buf)
                goto err;
+#endif
 
 #ifdef WITH_ZSTD
        zctx = ZSTD_createCCtx();
index 5d3b80cbfb7b0c46e1d209475974c69591f70a4f..42a47f839e115567de58e8cdfcc50c4202fc7af1 100644 (file)
@@ -539,10 +539,12 @@ static void select_default_compr(void)
                return;
        }
 
-#ifndef WITH_LZO
+#ifdef WITH_LZO
+       c->default_compr = UBIFS_COMPR_LZO;
+#elif defined(WITH_ZLIB)
        c->default_compr = UBIFS_COMPR_ZLIB;
 #else
-       c->default_compr = UBIFS_COMPR_LZO;
+       c->default_compr = UBIFS_COMPR_NONE;
 #endif
 }
 
@@ -681,26 +683,30 @@ static int get_options(int argc, char**argv)
                case 'x':
                        if (strcmp(optarg, "none") == 0)
                                c->default_compr = UBIFS_COMPR_NONE;
+#ifdef WITH_ZLIB
                        else if (strcmp(optarg, "zlib") == 0)
                                c->default_compr = UBIFS_COMPR_ZLIB;
+#endif
 #ifdef WITH_ZSTD
                        else if (strcmp(optarg, "zstd") == 0)
                                c->default_compr = UBIFS_COMPR_ZSTD;
 #endif
 #ifdef WITH_LZO
+                       else if (strcmp(optarg, "lzo") == 0)
+                               c->default_compr = UBIFS_COMPR_LZO;
+#endif
+#if defined(WITH_LZO) && defined(WITH_ZLIB)
                        else if (strcmp(optarg, "favor_lzo") == 0) {
                                c->default_compr = UBIFS_COMPR_LZO;
                                c->favor_lzo = 1;
-                       } else if (strcmp(optarg, "lzo") == 0) {
-                               c->default_compr = UBIFS_COMPR_LZO;
                        }
 #endif
                        else
                                return err_msg("bad compressor name");
                        break;
                case 'X':
-#ifndef WITH_LZO
-                       return err_msg("built without LZO support");
+#if !defined(WITH_LZO) && !defined(WITH_ZLIB)
+                       return err_msg("built without LZO or ZLIB support");
 #else
                        c->favor_percent = strtol(optarg, &endp, 0);
                        if (*endp != '\0' || endp == optarg ||
@@ -1858,10 +1864,12 @@ static int add_file(const char *path_name, struct stat *st, ino_t inum,
                out_len = NODE_BUFFER_SIZE - UBIFS_DATA_NODE_SZ;
                if (c->default_compr == UBIFS_COMPR_NONE &&
                    !c->encrypted && (flags & FS_COMPR_FL))
-#ifndef WITH_LZO
+#ifdef WITH_LZO
+                       use_compr = UBIFS_COMPR_LZO;
+#elif defined(WITH_ZLIB)
                        use_compr = UBIFS_COMPR_ZLIB;
 #else
-                       use_compr = UBIFS_COMPR_LZO;
+                       use_compr = UBIFS_COMPR_NONE;
 #endif
                else
                        use_compr = c->default_compr;