[PATCH 2/4] erofs-utils: lib: unify all identical compressor print function
Guo Xuenan
guoxuenan at huawei.com
Fri Jun 9 18:50:39 AEST 2023
{dump,fsck}.erofs use the same compressor print function,
available_compressors means which algothrims are currently
supported by binary tools.
supported_compressors including all algothrims that are ready
for erofs.
Signed-off-by: Guo Xuenan <guoxuenan at huawei.com>
---
fsck/main.c | 15 +-----------
include/erofs/compress.h | 3 ++-
lib/compressor.c | 51 ++++++++++++++++++++++++++--------------
mkfs/main.c | 15 +-----------
4 files changed, 38 insertions(+), 46 deletions(-)
diff --git a/fsck/main.c b/fsck/main.c
index f816bec..978d198 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -49,19 +49,6 @@ static struct option long_options[] = {
{0, 0, 0, 0},
};
-static void print_available_decompressors(FILE *f, const char *delim)
-{
- unsigned int i = 0;
- const char *s;
-
- while ((s = z_erofs_list_available_compressors(i)) != NULL) {
- if (i++)
- fputs(delim, f);
- fputs(s, f);
- }
- fputc('\n', f);
-}
-
static void usage(void)
{
fputs("usage: [options] IMAGE\n\n"
@@ -84,7 +71,7 @@ static void usage(void)
" --no-preserve-owner extract as yourself\n"
" --no-preserve-perms apply user's umask when extracting permissions\n"
"\nSupported algorithms are: ", stderr);
- print_available_decompressors(stderr, ", ");
+ zerofs_print_available_compressors(stderr);
}
static void erofsfsck_print_version(void)
diff --git a/include/erofs/compress.h b/include/erofs/compress.h
index 08af9e3..714f304 100644
--- a/include/erofs/compress.h
+++ b/include/erofs/compress.h
@@ -22,7 +22,8 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd);
int z_erofs_compress_init(struct erofs_buffer_head *bh);
int z_erofs_compress_exit(void);
-const char *z_erofs_list_available_compressors(unsigned int i);
+void zerofs_print_available_compressors(FILE *f);
+void zerofs_print_supported_compressors(FILE *f, unsigned int mask);
static inline bool erofs_is_packed_inode(struct erofs_inode *inode)
{
diff --git a/lib/compressor.c b/lib/compressor.c
index 0fa7105..5aee4e0 100644
--- a/lib/compressor.c
+++ b/lib/compressor.c
@@ -10,18 +10,6 @@
#define EROFS_CONFIG_COMPR_DEF_BOUNDARY (128)
-static const struct erofs_compressor *compressors[] = {
-#if LZ4_ENABLED
-#if LZ4HC_ENABLED
- &erofs_compressor_lz4hc,
-#endif
- &erofs_compressor_lz4,
-#endif
-#if HAVE_LIBLZMA
- &erofs_compressor_lzma,
-#endif
-};
-
/* compressing configuration specified by users */
static struct erofs_supported_algothrim {
int algtype;
@@ -85,9 +73,38 @@ int erofs_compress_destsize(const struct erofs_compress *c,
return ret;
}
-const char *z_erofs_list_available_compressors(unsigned int i)
+void zerofs_print_supported_compressors(FILE *f, unsigned int mask)
+{
+ unsigned int i = 0;
+ int comma = false;
+ const char *s;
+
+ while (i < erofs_compressor_num()) {
+ s = erofs_ccfg.compressors[i].name;
+ if (s && (mask & (1 << erofs_ccfg.compressors[i++].algorithmtype))) {
+ if (comma)
+ fputs(", ", f);
+ else
+ comma = true;
+ fputs(s, f);
+ }
+ }
+ fputc('\n', f);
+}
+
+void zerofs_print_available_compressors(FILE *f)
{
- return i >= ARRAY_SIZE(compressors) ? NULL : compressors[i]->name;
+ unsigned int i = 0;
+ const char *s;
+
+ while (i < erofs_compressor_num() &&
+ erofs_ccfg.compressors[i].handle.alg &&
+ (s = erofs_ccfg.compressors[i].name)) {
+ if (i++)
+ fputs(", ", f);
+ fputs(s, f);
+ }
+ fputc('\n', f);
}
int erofs_compressor_setlevel(struct erofs_compress *c, int compression_level)
@@ -120,11 +137,11 @@ int erofs_compressor_init(struct erofs_compress *c, char *alg_name)
}
ret = -EINVAL;
- for (i = 0; i < ARRAY_SIZE(compressors); ++i) {
- if (alg_name && strcmp(alg_name, compressors[i]->name))
+ for (i = 0; i < erofs_compressor_num(); ++i) {
+ if (alg_name && strcmp(alg_name, erofs_ccfg.compressors[i].name))
continue;
- ret = compressors[i]->init(c);
+ ret = erofs_ccfg.compressors[i].handle.alg->init(c);
if (!ret) {
DBG_BUGON(!c->alg);
return 0;
diff --git a/mkfs/main.c b/mkfs/main.c
index ac208e5..dbd168e 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -66,19 +66,6 @@ static struct option long_options[] = {
{0, 0, 0, 0},
};
-static void print_available_compressors(FILE *f, const char *delim)
-{
- unsigned int i = 0;
- const char *s;
-
- while ((s = z_erofs_list_available_compressors(i)) != NULL) {
- if (i++)
- fputs(delim, f);
- fputs(s, f);
- }
- fputc('\n', f);
-}
-
static void usage(void)
{
fputs("usage: [options] FILE DIRECTORY\n\n"
@@ -126,7 +113,7 @@ static void usage(void)
" --block-list-file=X X=block_list file\n"
#endif
"\nAvailable compressors are: ", stderr);
- print_available_compressors(stderr, ", ");
+ zerofs_print_available_compressors(stderr);
}
static unsigned int pclustersize_packed, pclustersize_max;
--
2.31.1
More information about the Linux-erofs
mailing list