btrfs-progs: factor out compression type name parsing to common utils

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2021-09-06 16:06:55 +02:00
parent 0c3991e171
commit f3a132fa1b
3 changed files with 26 additions and 11 deletions

View File

@ -830,18 +830,16 @@ static int cmd_filesystem_sync(const struct cmd_struct *cmd,
}
static DEFINE_SIMPLE_COMMAND(filesystem_sync, "sync");
static int parse_compress_type(char *s)
static int parse_compress_type_arg(char *s)
{
if (strcmp(optarg, "zlib") == 0)
return BTRFS_COMPRESS_ZLIB;
else if (strcmp(optarg, "lzo") == 0)
return BTRFS_COMPRESS_LZO;
else if (strcmp(optarg, "zstd") == 0)
return BTRFS_COMPRESS_ZSTD;
else {
error("unknown compression type %s", s);
int ret;
ret = parse_compress_type(s);
if (ret) {
error("unknown compression type: %s", s);
exit(1);
};
}
return ret;
}
static const char * const cmd_filesystem_defrag_usage[] = {
@ -940,7 +938,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
case 'c':
compress_type = BTRFS_COMPRESS_ZLIB;
if (optarg)
compress_type = parse_compress_type(optarg);
compress_type = parse_compress_type_arg(optarg);
break;
case 'f':
flush = 1;

View File

@ -227,6 +227,22 @@ enum btrfs_csum_type parse_csum_type(const char *s)
return 0;
}
/*
* Parse name of the supported compression algorithm, without level, case
* insensitive
*/
int parse_compress_type(const char *type)
{
if (strcasecmp(type, "zlib") == 0)
return BTRFS_COMPRESS_ZLIB;
else if (strcasecmp(type, "lzo") == 0)
return BTRFS_COMPRESS_LZO;
else if (strcasecmp(type, "zstd") == 0)
return BTRFS_COMPRESS_ZSTD;
else
return -EINVAL;
}
/*
* Find last set bit in a 64-bit word. Returns 0 if value is 0 or the position
* of the last set bit if value is nonzero. The last (most significant) bit is

View File

@ -28,6 +28,7 @@ int parse_range_u32(const char *range, u32 *start, u32 *end);
int parse_range(const char *range, u64 *start, u64 *end);
int parse_range_strict(const char *range, u64 *start, u64 *end);
int parse_bg_profile(const char *profile, u64 *flags);
int parse_compress_type(const char *type);
int fls64(u64 x);
#endif