Skip to content

Commit

Permalink
erofs-utils: improve the usage and version text of non-fuse commands
Browse files Browse the repository at this point in the history
For each command:

 - Change the format of --help to be closer to the usual GNU format
 - Have the --version text mention that it is part of erofs-utils
 - Include compile-time feature flags in -V
 - Have --help and --version print on stdout not stderr
 - Exit with 0 from --help and --version
 - Have flag errors print a message saying to use --help instead of
   printing the full help text

For fsck.erofs:

 - Consolidate the descriptions of --[no-]preserve[-<owner|perms>
 - Clarify the range that -d accepts

For mkfs.erofs:

 - Print supported algorithms and their level ranges+defaults
 - Clarify the range that -d accepts

For mkfs.erofs to have access to the algorithms' level ranges and
defaults, it is necessary to modify
z_erofs_list_available_compressors() to return the full `struct
erofs_algorithm` instead of just the `->name`.

Signed-off-by: Luke T. Shumaker <lukeshu@umorpha.io>
Link: https://lore.kernel.org/r/20231102193122.140921-3-lukeshu@lukeshu.com
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
  • Loading branch information
Luke T. Shumaker authored and hsiangkao committed Nov 6, 2023
1 parent f528b82 commit 197e329
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 111 deletions.
40 changes: 23 additions & 17 deletions dump/main.c
Expand Up @@ -106,25 +106,31 @@ static struct erofsdump_feature feature_lists[] = {

static int erofsdump_readdir(struct erofs_dir_context *ctx);

static void usage(void)
static void usage(int argc, char **argv)
{
fputs("usage: [options] IMAGE\n\n"
"Dump erofs layout from IMAGE, and [options] are:\n"
" -S show statistic information of the image\n"
" -V, --version print the version number of dump.erofs and exit.\n"
" -e show extent info (INODE required)\n"
" -s show information about superblock\n"
" --device=X specify an extra device to be used together\n"
" --ls show directory contents (INODE required)\n"
" --nid=# show the target inode info of nid #\n"
" --path=X show the target inode info of path X\n"
" -h, --help display this help and exit.\n",
stderr);
// " 1 2 3 4 5 6 7 8 "
// "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
printf(
"Usage: %s [OPTIONS] IMAGE\n"
"Dump erofs layout from IMAGE.\n"
"\n"
"General options:\n"
" -V, --version print the version number of dump.erofs and exit\n"
" -h, --help display this help and exit\n"
"\n"
" -S show statistic information of the image\n"
" -e show extent info (INODE required)\n"
" -s show information about superblock\n"
" --device=X specify an extra device to be used together\n"
" --ls show directory contents (INODE required)\n"
" --nid=# show the target inode info of nid #\n"
" --path=X show the target inode info of path X\n",
argv[0]);
}

static void erofsdump_print_version(void)
{
printf("dump.erofs %s\n", cfg.c_version);
printf("dump.erofs (erofs-utils) %s\n", cfg.c_version);
}

static int erofsdump_parse_options_cfg(int argc, char **argv)
Expand Down Expand Up @@ -155,7 +161,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
++dumpcfg.totalshow;
break;
case 'h':
usage();
usage(argc, argv);
exit(0);
case 3:
err = blob_open_ro(&sbi, optarg);
Expand Down Expand Up @@ -663,7 +669,7 @@ int main(int argc, char **argv)
err = erofsdump_parse_options_cfg(argc, argv);
if (err) {
if (err == -EINVAL)
usage();
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
goto exit;
}

Expand All @@ -690,7 +696,7 @@ int main(int argc, char **argv)
erofsdump_print_statistic();

if (dumpcfg.show_extent && !dumpcfg.show_inode) {
usage();
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
goto exit_put_super;
}

Expand Down
68 changes: 41 additions & 27 deletions fsck/main.c
Expand Up @@ -14,6 +14,7 @@
#include "erofs/compress.h"
#include "erofs/decompress.h"
#include "erofs/dir.h"
#include "../lib/compressor.h"

static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid);

Expand Down Expand Up @@ -64,45 +65,58 @@ static void print_available_decompressors(FILE *f, const char *delim)
{
int i = 0;
bool comma = false;
const char *s;
const struct erofs_algorithm *s;

while ((s = z_erofs_list_available_compressors(&i)) != NULL) {
if (comma)
fputs(delim, f);
fputs(s, f);
fputs(s->name, f);
comma = true;
}
fputc('\n', f);
}

static void usage(void)
static void usage(int argc, char **argv)
{
fputs("usage: [options] IMAGE\n\n"
"Check erofs filesystem compatibility and integrity of IMAGE, and [options] are:\n"
" -V, --version print the version number of fsck.erofs and exit\n"
" -d# set output message level to # (maximum 9)\n"
" -p print total compression ratio of all files\n"
" --device=X specify an extra device to be used together\n"
" --extract[=X] check if all files are well encoded, optionally extract to X\n"
" -h, --help display this help and exit\n"
"\nExtraction options (--extract=X is required):\n"
" --force allow extracting to root\n"
" --overwrite overwrite files that already exist\n"
" --preserve extract with the same ownership and permissions as on the filesystem\n"
" (default for superuser)\n"
" --preserve-owner extract with the same ownership as on the filesystem\n"
" --preserve-perms extract with the same permissions as on the filesystem\n"
" --no-preserve extract as yourself and apply user's umask on permissions\n"
" (default for ordinary users)\n"
" --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, ", ");
// " 1 2 3 4 5 6 7 8 "
// "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
printf(
"Usage: %s [OPTIONS] IMAGE\n"
"Check erofs filesystem compatibility and integrity of IMAGE.\n"
"\n"
"This version of fsck.erofs is capable of checking images that use any of the\n"
"following algorithms: ", argv[0]);
print_available_decompressors(stdout, ", ");
printf("\n"
"General options:\n"
" -V, --version print the version number of fsck.erofs and exit\n"
" -h, --help display this help and exit\n"
"\n"
" -d<0-9> set output verbosity; 0=quiet, 9=verbose (default=%i)\n"
" -p print total compression ratio of all files\n"
" --device=X specify an extra device to be used together\n"
" --extract[=X] check if all files are well encoded, optionally\n"
" extract to X\n"
"\n"
"Extraction options (--extract=X is required):\n"
" --force allow extracting to root\n"
" --overwrite overwrite files that already exist\n"
" --[no-]preserve same as --[no-]preserve-owner --[no-]preserve-perms\n"
" --[no-]preserve-owner whether to preserve the ownership from the\n"
" filesystem (default for superuser), or to extract as\n"
" yourself (default for ordinary users)\n"
" --[no-]preserve-perms whether to preserve the exact permissions from the\n"
" filesystem without applying umask (default for\n"
" superuser), or to modify the permissions by applying\n"
" umask (default for ordinary users)\n",
EROFS_WARN);
}

static void erofsfsck_print_version(void)
{
printf("fsck.erofs %s\n", cfg.c_version);
printf("fsck.erofs (erofs-utils) %s\navailable decompressors: ",
cfg.c_version);
print_available_decompressors(stdout, ", ");
}

static int erofsfsck_parse_options_cfg(int argc, char **argv)
Expand All @@ -128,7 +142,7 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)
fsckcfg.print_comp_ratio = true;
break;
case 'h':
usage();
usage(argc, argv);
exit(0);
case 2:
fsckcfg.check_decomp = true;
Expand Down Expand Up @@ -919,7 +933,7 @@ int main(int argc, char *argv[])
err = erofsfsck_parse_options_cfg(argc, argv);
if (err) {
if (err == -EINVAL)
usage();
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
goto exit;
}

Expand Down
2 changes: 1 addition & 1 deletion include/erofs/compress.h
Expand Up @@ -24,7 +24,7 @@ int z_erofs_compress_init(struct erofs_sb_info *sbi,
int z_erofs_compress_exit(void);

const char *z_erofs_list_supported_algorithms(int i, unsigned int *mask);
const char *z_erofs_list_available_compressors(int *i);
const struct erofs_algorithm *z_erofs_list_available_compressors(int *i);

static inline bool erofs_is_packed_inode(struct erofs_inode *inode)
{
Expand Down
13 changes: 3 additions & 10 deletions lib/compressor.c
Expand Up @@ -8,14 +8,7 @@
#include "compressor.h"
#include "erofs/print.h"

static const struct erofs_algorithm {
char *name;
const struct erofs_compressor *c;
unsigned int id;

/* its name won't be shown as a supported algorithm */
bool optimisor;
} erofs_algs[] = {
static const struct erofs_algorithm erofs_algs[] = {
{ "lz4",
#if LZ4_ENABLED
&erofs_compressor_lz4,
Expand Down Expand Up @@ -63,12 +56,12 @@ const char *z_erofs_list_supported_algorithms(int i, unsigned int *mask)
return "";
}

const char *z_erofs_list_available_compressors(int *i)
const struct erofs_algorithm *z_erofs_list_available_compressors(int *i)
{
for (;*i < ARRAY_SIZE(erofs_algs); ++*i) {
if (!erofs_algs[*i].c)
continue;
return erofs_algs[(*i)++].name;
return &erofs_algs[(*i)++];
}
return NULL;
}
Expand Down
9 changes: 8 additions & 1 deletion lib/compressor.h
Expand Up @@ -24,7 +24,14 @@ struct erofs_compressor {
void *dst, unsigned int dstsize);
};

struct erofs_algorithm;
struct erofs_algorithm {
char *name;
const struct erofs_compressor *c;
unsigned int id;

/* its name won't be shown as a supported algorithm */
bool optimisor;
};

struct erofs_compress {
struct erofs_sb_info *sbi;
Expand Down
10 changes: 6 additions & 4 deletions man/mkfs.erofs.1
Expand Up @@ -21,10 +21,12 @@ from \fISOURCE\fR directory.
.SH OPTIONS
.TP
.BI "\-z " compression-algorithm \fR[\fP, # \fR][\fP: ... \fR]\fP
Set a primary algorithm for data compression, which can be set with an optional
compression level (1 to 12 for LZ4HC, 0 to 9 for LZMA and 100 to 109 for LZMA
extreme compression) separated by a comma. Alternative algorithms could be
specified and separated by colons.
Set a primary algorithm for data compression, which can be set with an
optional compression level. Alternative algorithms could be specified
and separated by colons. See the output of
.B mkfs.erofs \-\-help
for a listing of the algorithms that \fBmkfs.erofs\fR is compiled with
and what their respective level ranges are.
.TP
.BI "\-b " block-size
Set the fundamental block size of the filesystem in bytes. In other words,
Expand Down

0 comments on commit 197e329

Please sign in to comment.