diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-01 22:42:49 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-01 22:42:49 +0200 |
commit | 307107ecd2fc3ffbf6fe91497daf767700f3572f (patch) | |
tree | 87c2c5993ab10cd4aa791a4e6d34f251db208ed2 /lib/sqfs/comp/compressor.c | |
parent | 2e28c45601a57b1d23e9cad33d2bdcc59e8a3f4f (diff) |
Move command line processing stuff out of compressor code
This commit moves stuff like printing help text, command line option
processing and enumerating available processors on stdout out of
the generic compressor code.
The option string is replaced with a structure that directly exposese
the tweakable parameters for all compressors. A function for parsing
the command line arguments into this structure is added in sqfshelper.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/comp/compressor.c')
-rw-r--r-- | lib/sqfs/comp/compressor.c | 100 |
1 files changed, 33 insertions, 67 deletions
diff --git a/lib/sqfs/comp/compressor.c b/lib/sqfs/comp/compressor.c index e59d948..d436100 100644 --- a/lib/sqfs/comp/compressor.c +++ b/lib/sqfs/comp/compressor.c @@ -13,10 +13,7 @@ #include "internal.h" #include "util.h" -typedef compressor_t *(*compressor_fun_t)(bool compress, size_t block_size, - char *options); - -typedef void (*compressor_help_fun_t)(void); +typedef compressor_t *(*compressor_fun_t)(const compressor_config_t *cfg); static compressor_fun_t compressors[SQFS_COMP_MAX + 1] = { #ifdef WITH_GZIP @@ -36,24 +33,6 @@ static compressor_fun_t compressors[SQFS_COMP_MAX + 1] = { #endif }; -static const compressor_help_fun_t helpfuns[SQFS_COMP_MAX + 1] = { -#ifdef WITH_GZIP - [SQFS_COMP_GZIP] = compressor_gzip_print_help, -#endif -#ifdef WITH_XZ - [SQFS_COMP_XZ] = compressor_xz_print_help, -#endif -#ifdef WITH_LZO - [SQFS_COMP_LZO] = compressor_lzo_print_help, -#endif -#ifdef WITH_LZ4 - [SQFS_COMP_LZ4] = compressor_lz4_print_help, -#endif -#ifdef WITH_ZSTD - [SQFS_COMP_ZSTD] = compressor_zstd_print_help, -#endif -}; - static const char *names[] = { [SQFS_COMP_GZIP] = "gzip", [SQFS_COMP_LZMA] = "lzma", @@ -105,41 +84,15 @@ bool compressor_exists(E_SQFS_COMPRESSOR id) return (compressors[id] != NULL); } -compressor_t *compressor_create(E_SQFS_COMPRESSOR id, bool compress, - size_t block_size, char *options) +compressor_t *compressor_create(const compressor_config_t *cfg) { - if (id < SQFS_COMP_MIN || id > SQFS_COMP_MAX) + if (cfg == NULL || cfg->id < SQFS_COMP_MIN || cfg->id > SQFS_COMP_MAX) return NULL; - if (compressors[id] == NULL) + if (compressors[cfg->id] == NULL) return NULL; - return compressors[id](compress, block_size, options); -} - -void compressor_print_help(E_SQFS_COMPRESSOR id) -{ - if (id < SQFS_COMP_MIN || id > SQFS_COMP_MAX) - return; - - if (compressors[id] == NULL) - return; - - helpfuns[id](); -} - -void compressor_print_available(void) -{ - size_t i; - - fputs("Available compressors:\n", stdout); - - for (i = 0; i < sizeof(names) / sizeof(names[0]); ++i) { - if (compressor_exists(i)) - printf("\t%s\n", names[i]); - } - - printf("\nDefault compressor: %s\n", names[compressor_get_default()]); + return compressors[cfg->id](cfg); } const char *compressor_name_from_id(E_SQFS_COMPRESSOR id) @@ -164,20 +117,33 @@ int compressor_id_from_name(const char *name, E_SQFS_COMPRESSOR *out) return -1; } -E_SQFS_COMPRESSOR compressor_get_default(void) +int compressor_config_init(compressor_config_t *cfg, E_SQFS_COMPRESSOR id, + size_t block_size, uint16_t flags) { -#if defined(WITH_XZ) - return SQFS_COMP_XZ; -#elif defined(WITH_ZSTD) - return SQFS_COMP_ZSTD; -#elif defined(WITH_GZIP) - return SQFS_COMP_GZIP; -#elif defined(WITH_LZO) - return SQFS_COMP_LZO; -#elif defined(WITH_LZ4) - return SQFS_COMP_LZ4; -#else - fputs("No compressor implementation available!\n", stderr); - exit(EXIT_FAILURE); -#endif + memset(cfg, 0, sizeof(*cfg)); + + cfg->id = id; + cfg->flags = flags; + cfg->block_size = block_size; + + switch (id) { + case SQFS_COMP_GZIP: + cfg->opt.gzip.level = SQFS_GZIP_DEFAULT_LEVEL; + cfg->opt.gzip.window_size = SQFS_GZIP_DEFAULT_WINDOW; + break; + case SQFS_COMP_LZO: + cfg->opt.lzo.algorithm = SQFS_LZO_DEFAULT_ALG; + cfg->opt.lzo.level = SQFS_LZO_DEFAULT_LEVEL; + break; + case SQFS_COMP_ZSTD: + cfg->opt.zstd.level = SQFS_ZSTD_DEFAULT_LEVEL; + break; + case SQFS_COMP_XZ: + cfg->opt.xz.dict_size = block_size; + break; + default: + break; + } + + return 0; } |