diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-23 02:14:52 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-23 02:37:08 +0200 |
commit | 0b22d6ad0ebed2af239259dbfa36cd9920c6f4a2 (patch) | |
tree | 0680c237006ed11fc200e9d3d4717d25455e9599 | |
parent | 5f7a1a092495c84c4c4cd208c3c983c3f16c8951 (diff) |
Move all handling of compressor names to libcompress.a
This commit removes handling of compressor names from gensquashfs. Instead,
functions are added to libcompress to obtain name from ID, ID from name
and to print out defaults.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | doc/gensquashfs.1 | 5 | ||||
-rw-r--r-- | include/compress.h | 6 | ||||
-rw-r--r-- | lib/comp/compressor.c | 45 | ||||
-rw-r--r-- | mkfs/mkfs.h | 2 | ||||
-rw-r--r-- | mkfs/options.c | 36 |
5 files changed, 62 insertions, 32 deletions
diff --git a/doc/gensquashfs.1 b/doc/gensquashfs.1 index 4b739fd..8050dc6 100644 --- a/doc/gensquashfs.1 +++ b/doc/gensquashfs.1 @@ -21,8 +21,9 @@ directory into a SquashFS image. The directory becomes the root of the file system. .TP \fB\-\-compressor\fR, \fB\-c\fR <name> -Select the compressor to use (defaults to 'xz'). -Run \fBgensquashfs \-\-help\fR to get a list of all available compressors. +Select the compressor to use. +Run \fBgensquashfs \-\-help\fR to get a list of all available compressors +and the default selection. .TP \fB\-\-comp\-extra\fR, \fB\-X\fR <options> A comma seperated list of extra options for the selected compressor. Specify diff --git a/include/compress.h b/include/compress.h index c9a4e08..7937646 100644 --- a/include/compress.h +++ b/include/compress.h @@ -49,6 +49,12 @@ compressor_t *compressor_create(E_SQFS_COMPRESSOR id, bool compress, void compressor_print_help(E_SQFS_COMPRESSOR id); +void compressor_print_available(void); + E_SQFS_COMPRESSOR compressor_get_default(void); +const char *compressor_name_from_id(E_SQFS_COMPRESSOR id); + +int compressor_id_from_name(const char *name, E_SQFS_COMPRESSOR *out); + #endif /* COMPRESS_H */ diff --git a/lib/comp/compressor.c b/lib/comp/compressor.c index 6dff416..506df46 100644 --- a/lib/comp/compressor.c +++ b/lib/comp/compressor.c @@ -47,6 +47,15 @@ static const compressor_help_fun_t helpfuns[SQFS_COMP_MAX + 1] = { #endif }; +static const char *names[] = { + [SQFS_COMP_GZIP] = "gzip", + [SQFS_COMP_LZMA] = "lzma", + [SQFS_COMP_LZO] = "lzo", + [SQFS_COMP_XZ] = "xz", + [SQFS_COMP_LZ4] = "lz4", + [SQFS_COMP_ZSTD] = "zstd", +}; + int generic_write_options(int fd, const void *data, size_t size) { uint8_t buffer[size + 2]; @@ -129,6 +138,42 @@ void compressor_print_help(E_SQFS_COMPRESSOR id) 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()]); +} + +const char *compressor_name_from_id(E_SQFS_COMPRESSOR id) +{ + if (id < 0 || (size_t)id > sizeof(names) / sizeof(names[0])) + return NULL; + + return names[id]; +} + +int compressor_id_from_name(const char *name, E_SQFS_COMPRESSOR *out) +{ + size_t i; + + for (i = 0; i < sizeof(names) / sizeof(names[0]); ++i) { + if (names[i] != NULL && strcmp(names[i], name) == 0) { + *out = i; + return 0; + } + } + + return -1; +} + E_SQFS_COMPRESSOR compressor_get_default(void) { #if defined(WITH_XZ) diff --git a/mkfs/mkfs.h b/mkfs/mkfs.h index cea9ebc..4a39eb5 100644 --- a/mkfs/mkfs.h +++ b/mkfs/mkfs.h @@ -28,8 +28,8 @@ typedef struct { unsigned int def_gid; unsigned int def_mode; unsigned int def_mtime; + E_SQFS_COMPRESSOR compressor; int outmode; - int compressor; int blksz; int devblksz; bool quiet; diff --git a/mkfs/options.c b/mkfs/options.c index a746ba4..e298a6c 100644 --- a/mkfs/options.c +++ b/mkfs/options.c @@ -61,7 +61,6 @@ static const char *help_string = "\n" " --compressor, -c <name> Select the compressor to use.\n" " A list of available compressors is below.\n" -" Defaults to '%s'.\n" " --comp-extra, -X <options> A comma seperated list of extra options for\n" " the selected compressor. Specify 'help' to\n" " get a list of available options.\n" @@ -126,15 +125,6 @@ static const char *help_string = " file /bin/bash 0755 0 0" "\n\n"; -static const char *compressors[] = { - [SQFS_COMP_GZIP] = "gzip", - [SQFS_COMP_LZMA] = "lzma", - [SQFS_COMP_LZO] = "lzo", - [SQFS_COMP_XZ] = "xz", - [SQFS_COMP_LZ4] = "lz4", - [SQFS_COMP_ZSTD] = "zstd", -}; - static long read_number(const char *name, const char *str, long min, long max) { long result = strtol(str, NULL, 0); @@ -217,17 +207,13 @@ void process_command_line(options_t *opt, int argc, char **argv) switch (i) { case 'c': - have_compressor = false; + have_compressor = true; - for (i = SQFS_COMP_MIN; i <= SQFS_COMP_MAX; ++i) { - if (strcmp(compressors[i], optarg) == 0) { - if (compressor_exists(i)) { - have_compressor = true; - opt->compressor = i; - break; - } - } - } + if (compressor_id_from_name(optarg, &opt->compressor)) + have_compressor = false; + + if (!compressor_exists(opt->compressor)) + have_compressor = false; if (!have_compressor) { fprintf(stderr, "Unsupported compressor '%s'\n", @@ -268,16 +254,8 @@ void process_command_line(options_t *opt, int argc, char **argv) #endif case 'h': printf(help_string, __progname, - compressors[compressor_get_default()], SQFS_DEFAULT_BLOCK_SIZE, SQFS_DEVBLK_SIZE); - - fputs("Available compressors:\n", stdout); - - for (i = SQFS_COMP_MIN; i <= SQFS_COMP_MAX; ++i) { - if (compressor_exists(i)) - printf("\t%s\n", compressors[i]); - } - + compressor_print_available(); exit(EXIT_SUCCESS); case 'V': print_version(); |