mirror of
https://github.com/kdave/btrfs-progs
synced 2025-02-09 22:26:55 +00:00
btrfs-progs: extend pretty printers with unit mode
The functionality of pretty unit printing was duplicated by df_pretty_sizes, merge it with pretty_size and enhance the interface with more suffix mode. Raw, binary or decimal. Signed-off-by: David Sterba <dsterba@suse.cz>
This commit is contained in:
parent
db5d44d132
commit
6f23efdf27
77
utils.c
77
utils.c
@ -1294,35 +1294,62 @@ out:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *size_strs[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
|
static const char const *unit_suffix_binary[] =
|
||||||
int pretty_size_snprintf(u64 size, char *str, size_t str_bytes)
|
{ "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
|
||||||
{
|
static const char const *unit_suffix_decimal[] =
|
||||||
int num_divs = 0;
|
{ "B", "KB", "MB", "GB", "TB", "PB", "EB"};
|
||||||
float fraction;
|
|
||||||
|
|
||||||
if (str_bytes == 0)
|
int pretty_size_snprintf(u64 size, char *str, size_t str_size, int unit_mode)
|
||||||
|
{
|
||||||
|
int num_divs;
|
||||||
|
float fraction;
|
||||||
|
int base = 0;
|
||||||
|
const char const **suffix = NULL;
|
||||||
|
u64 last_size;
|
||||||
|
|
||||||
|
if (str_size == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if( size < 1024 ){
|
if (unit_mode == UNITS_RAW) {
|
||||||
fraction = size;
|
snprintf(str, str_size, "%llu", size);
|
||||||
num_divs = 0;
|
return 0;
|
||||||
} else {
|
|
||||||
u64 last_size = size;
|
|
||||||
num_divs = 0;
|
|
||||||
while(size >= 1024){
|
|
||||||
last_size = size;
|
|
||||||
size /= 1024;
|
|
||||||
num_divs ++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (num_divs >= ARRAY_SIZE(size_strs)) {
|
|
||||||
str[0] = '\0';
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fraction = (float)last_size / 1024;
|
|
||||||
}
|
}
|
||||||
return snprintf(str, str_bytes, "%.2f%s", fraction,
|
|
||||||
size_strs[num_divs]);
|
if (unit_mode == UNITS_BINARY) {
|
||||||
|
base = 1024;
|
||||||
|
suffix = unit_suffix_binary;
|
||||||
|
} else if (unit_mode == UNITS_DECIMAL) {
|
||||||
|
base = 1000;
|
||||||
|
suffix = unit_suffix_decimal;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Unknown mode */
|
||||||
|
if (!base) {
|
||||||
|
fprintf(stderr, "INTERNAL ERROR: unknown unit base, mode %d",
|
||||||
|
unit_mode);
|
||||||
|
assert(0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
num_divs = 0;
|
||||||
|
last_size = size;
|
||||||
|
|
||||||
|
while (size >= base) {
|
||||||
|
last_size = size;
|
||||||
|
size /= base;
|
||||||
|
num_divs++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num_divs >= ARRAY_SIZE(unit_suffix_binary)) {
|
||||||
|
str[0] = '\0';
|
||||||
|
printf("INTERNAL ERROR: unsupported unit suffix, index %d\n",
|
||||||
|
num_divs);
|
||||||
|
assert(0);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fraction = (float)last_size / base;
|
||||||
|
|
||||||
|
return snprintf(str, str_size, "%.2f%s", fraction, suffix[num_divs]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
21
utils.h
21
utils.h
@ -47,6 +47,14 @@ int check_argc_max(int nargs, int expected);
|
|||||||
void fixup_argv0(char **argv, const char *token);
|
void fixup_argv0(char **argv, const char *token);
|
||||||
void set_argv0(char **argv);
|
void set_argv0(char **argv);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Output mode of byte units
|
||||||
|
*/
|
||||||
|
#define UNITS_RAW (1)
|
||||||
|
#define UNITS_BINARY (2)
|
||||||
|
#define UNITS_DECIMAL (3)
|
||||||
|
#define UNITS_HUMAN UNITS_BINARY
|
||||||
|
|
||||||
int make_btrfs(int fd, const char *device, const char *label,
|
int make_btrfs(int fd, const char *device, const char *label,
|
||||||
char *fs_uuid, u64 blocks[6], u64 num_bytes, u32 nodesize,
|
char *fs_uuid, u64 blocks[6], u64 num_bytes, u32 nodesize,
|
||||||
u32 leafsize, u32 sectorsize, u32 stripesize, u64 features);
|
u32 leafsize, u32 sectorsize, u32 stripesize, u64 features);
|
||||||
@ -68,12 +76,13 @@ int check_mounted_where(int fd, const char *file, char *where, int size,
|
|||||||
int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
|
int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
|
||||||
int super_offset);
|
int super_offset);
|
||||||
|
|
||||||
int pretty_size_snprintf(u64 size, char *str, size_t str_bytes);
|
int pretty_size_snprintf(u64 size, char *str, size_t str_bytes, int unit_mode);
|
||||||
#define pretty_size(size) \
|
#define pretty_size(size) pretty_size_mode(size, UNITS_BINARY)
|
||||||
({ \
|
#define pretty_size_mode(size, mode) \
|
||||||
static __thread char _str[24]; \
|
({ \
|
||||||
(void)pretty_size_snprintf((size), _str, sizeof(_str)); \
|
static __thread char _str[32]; \
|
||||||
_str; \
|
(void)pretty_size_snprintf((size), _str, sizeof(_str), mode); \
|
||||||
|
_str; \
|
||||||
})
|
})
|
||||||
|
|
||||||
int get_mountpt(char *dev, char *mntpt, size_t size);
|
int get_mountpt(char *dev, char *mntpt, size_t size);
|
||||||
|
Loading…
Reference in New Issue
Block a user