btrfsprogs/0028-btrfs-progs-extend-pretty-printers-with-unit-mode.patch
David Sterba 3821ed946a Accepting request 246571 from home:dsterba:branches:filesystems
- version 3.16
  - show-super: skip unrecognized sb, add option to force
  - debug-tree: print tree by id
  - mkfs: new option to specify UUID
  - receive: new option to limit number of errors
  - check: new option to verify quotas
  - check: reduced memory requirements
  - check: new option to print extent sharing
  - restore: check length before decompression
  - restore: more error handling
  - balance: new filter 'limit'
  - recover: allow to read all sb copies
  - restore: option to loop during restoring
  - mkfs: drop experimental notice
  - btrfstune: new option to force dangerous changes
  - documentation updates
- Deleted patches (upstream):
  * 0001-btrfs-progs-doc-fix-symlink-target-for-btrfsck.8.patch
  * 0200-btrfs-progs-print-qgroup-excl-as-unsigned.patch
  * 0201-btrfs-progs-import-ulist.patch
  * 0202-btrfs-progs-add-quota-group-verify-code.patch
  * 0203-btrfs-progs-show-extent-state-for-a-subvolume.patch
  * 0204-btrfs-progs-ignore-orphaned-qgroups-by-default.patch
  * btrfs-progs-canonicalize-pathnames-for-device-commands
- Refreshed patches:
  * 0006-Btrfs-progs-fsck-clear-out-log-tree-in-repair-mode.patch
  * 0011-btrfs-progs-Enhance-the-command-btrfs-filesystem-df.patch
  * 0012-btrfs-progs-Add-helpers-functions-to-handle-the-prin.patch
  * 0013-btrfs-progs-Add-command-btrfs-filesystem-disk-usage.patch
  * 0028-btrfs-progs-extend-pretty-printers-with-unit-mode.patch
  * 0164-btrfs-progs-convert-set-label-or-copy-from-origin.patch

OBS-URL: https://build.opensuse.org/request/show/246571
OBS-URL: https://build.opensuse.org/package/show/filesystems/btrfsprogs?expand=0&rev=167
2014-08-26 18:51:30 +00:00

171 lines
4.7 KiB
Diff

From f54a92b54b57ea8be8d55ea012c9b69c9f0db5ff Mon Sep 17 00:00:00 2001
From: David Sterba <dsterba@suse.cz>
Date: Mon, 28 Apr 2014 18:04:48 +0200
Subject: [PATCH 39/42] 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>
---
cmds-fi-disk_usage.c | 9 ++-----
utils.c | 71 ++++++++++++++++++++++++++++++++++++----------------
utils.h | 21 +++++++++++-----
3 files changed, 66 insertions(+), 35 deletions(-)
Index: btrfs-progs-v3.16/cmds-fi-disk_usage.c
===================================================================
--- btrfs-progs-v3.16.orig/cmds-fi-disk_usage.c
+++ btrfs-progs-v3.16/cmds-fi-disk_usage.c
@@ -33,18 +33,13 @@
/*
* Pretty print the size
- * PAY ATTENTION: it return a statically buffer
*/
char *df_pretty_sizes(u64 size, int mode)
{
- static char buf[30];
-
if (mode & DF_HUMAN_UNIT)
- (void)pretty_size_snprintf(size, buf, sizeof(buf));
+ return pretty_size_mode(size, UNITS_HUMAN);
else
- sprintf(buf, "%llu", size);
-
- return buf;
+ return pretty_size_mode(size, UNITS_RAW);
}
/*
Index: btrfs-progs-v3.16/utils.c
===================================================================
--- btrfs-progs-v3.16.orig/utils.c
+++ btrfs-progs-v3.16/utils.c
@@ -1378,35 +1378,62 @@ out:
return ret;
}
-static char *size_strs[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
-int pretty_size_snprintf(u64 size, char *str, size_t str_bytes)
+static const char const *unit_suffix_binary[] =
+ { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
+static const char const *unit_suffix_decimal[] =
+ { "B", "KB", "MB", "GB", "TB", "PB", "EB"};
+
+int pretty_size_snprintf(u64 size, char *str, size_t str_size, int unit_mode)
{
- int num_divs = 0;
+ int num_divs;
float fraction;
+ int base = 0;
+ const char const **suffix = NULL;
+ u64 last_size;
+
+ if (str_size == 0)
+ return 0;
- if (str_bytes == 0)
+ if (unit_mode == UNITS_RAW) {
+ snprintf(str, str_size, "%llu", size);
return 0;
+ }
+
+ if (unit_mode == UNITS_BINARY) {
+ base = 1024;
+ suffix = unit_suffix_binary;
+ } else if (unit_mode == UNITS_DECIMAL) {
+ base = 1000;
+ suffix = unit_suffix_decimal;
+ }
- if( size < 1024 ){
- fraction = size;
- num_divs = 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;
+ /* Unknown mode */
+ if (!base) {
+ fprintf(stderr, "INTERNAL ERROR: unknown unit base, mode %d",
+ unit_mode);
+ assert(0);
+ return -1;
}
- return snprintf(str, str_bytes, "%.2f%s", fraction,
- size_strs[num_divs]);
+
+ 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]);
}
/*
Index: btrfs-progs-v3.16/utils.h
===================================================================
--- btrfs-progs-v3.16.orig/utils.h
+++ btrfs-progs-v3.16/utils.h
@@ -49,6 +49,14 @@ int check_argc_max(int nargs, int expect
void fixup_argv0(char **argv, const char *token);
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,
char *fs_uuid, u64 blocks[6], u64 num_bytes, u32 nodesize,
u32 leafsize, u32 sectorsize, u32 stripesize, u64 features);
@@ -71,12 +79,13 @@ int check_mounted_where(int fd, const ch
int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
int super_offset);
-int pretty_size_snprintf(u64 size, char *str, size_t str_bytes);
-#define pretty_size(size) \
- ({ \
- static __thread char _str[24]; \
- (void)pretty_size_snprintf((size), _str, sizeof(_str)); \
- _str; \
+int pretty_size_snprintf(u64 size, char *str, size_t str_bytes, int unit_mode);
+#define pretty_size(size) pretty_size_mode(size, UNITS_BINARY)
+#define pretty_size_mode(size, mode) \
+ ({ \
+ static __thread char _str[32]; \
+ (void)pretty_size_snprintf((size), _str, sizeof(_str), mode); \
+ _str; \
})
int get_mountpt(char *dev, char *mntpt, size_t size);