From patchwork Tue Apr 29 16:02:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Sterba X-Patchwork-Id: 4088331 X-Patchwork-Delegate: dave@jikos.cz Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 44D55C0ACC for ; Tue, 29 Apr 2014 16:02:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5D6B4201F5 for ; Tue, 29 Apr 2014 16:02:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 52BC12011B for ; Tue, 29 Apr 2014 16:02:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758196AbaD2QCq (ORCPT ); Tue, 29 Apr 2014 12:02:46 -0400 Received: from cantor2.suse.de ([195.135.220.15]:57292 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757863AbaD2QCp (ORCPT ); Tue, 29 Apr 2014 12:02:45 -0400 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id E8D82AD13 for ; Tue, 29 Apr 2014 16:02:44 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id A7E81DA935; Tue, 29 Apr 2014 18:02:44 +0200 (CEST) From: David Sterba To: linux-btrfs@vger.kernel.org Cc: David Sterba Subject: [PATCH 11/14] btrfs-progs: extend pretty printers with unit mode Date: Tue, 29 Apr 2014 18:02:44 +0200 Message-Id: X-Mailer: git-send-email 1.9.0 In-Reply-To: References: Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- cmds-fi-disk_usage.c | 9 ++----- utils.c | 71 ++++++++++++++++++++++++++++++++++++---------------- utils.h | 21 +++++++++++----- 3 files changed, 66 insertions(+), 35 deletions(-) diff --git a/cmds-fi-disk_usage.c b/cmds-fi-disk_usage.c index e472833c646d..c823223ee8a8 100644 --- a/cmds-fi-disk_usage.c +++ b/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); } /* diff --git a/utils.c b/utils.c index 159abf8bd0e4..69112be51cb2 100644 --- a/utils.c +++ b/utils.c @@ -1252,35 +1252,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_bytes == 0) + if (str_size == 0) return 0; - 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 (unit_mode == UNITS_RAW) { + snprintf(str, str_size, "%llu", size); + return 0; + } - if (num_divs >= ARRAY_SIZE(size_strs)) { - str[0] = '\0'; - return -1; - } - fraction = (float)last_size / 1024; + if (unit_mode == UNITS_BINARY) { + base = 1024; + suffix = unit_suffix_binary; + } else if (unit_mode == UNITS_DECIMAL) { + base = 1000; + suffix = unit_suffix_decimal; } - return snprintf(str, str_bytes, "%.2f%s", fraction, - size_strs[num_divs]); + + /* 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]); } /* diff --git a/utils.h b/utils.h index 2d08e0b2a3a4..3aea8b47d9e7 100644 --- a/utils.h +++ b/utils.h @@ -39,6 +39,14 @@ #define BTRFS_UUID_UNPARSED_SIZE 37 +/* + * 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, u64 blocks[6], u64 num_bytes, u32 nodesize, u32 leafsize, u32 sectorsize, u32 stripesize, u64 features); @@ -59,12 +67,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 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);