From patchwork Sat Dec 3 06:19:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zygo Blaxell X-Patchwork-Id: 9459555 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7744960459 for ; Sat, 3 Dec 2016 06:27:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 65FE928549 for ; Sat, 3 Dec 2016 06:27:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 57EEA28556; Sat, 3 Dec 2016 06:27:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B82A028549 for ; Sat, 3 Dec 2016 06:27:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751220AbcLCG1d (ORCPT ); Sat, 3 Dec 2016 01:27:33 -0500 Received: from startkeylogger.hungrycats.org ([207.192.69.118]:50795 "EHLO neville.hungrycats.org" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1750890AbcLCG1d (ORCPT ); Sat, 3 Dec 2016 01:27:33 -0500 X-Greylist: delayed 468 seconds by postgrey-1.27 at vger.kernel.org; Sat, 03 Dec 2016 01:27:33 EST X-Envelope-Mail-From: zblaxell@waya.furryterror.org X-Envelope-Mail-From: zblaxell@waya.furryterror.org Received: from waya.furryterror.org (waya.vpn7.hungrycats.org [10.132.226.63]) by neville.hungrycats.org (Postfix) with ESMTP id E1FFF60B61; Sat, 3 Dec 2016 01:19:43 -0500 (EST) Received: from zblaxell by waya.furryterror.org with local (Exim 4.84_2) (envelope-from ) id 1cD3fi-0000Aj-Rv; Sat, 03 Dec 2016 01:19:42 -0500 From: Zygo Blaxell To: linux-btrfs@vger.kernel.org Subject: [PATCH] btrfs-progs: utils: negative numbers are more plausible than sizes over 8 EiB Date: Sat, 3 Dec 2016 01:19:38 -0500 Message-Id: <1480745978-24996-1-git-send-email-ce3g8jdj@umail.furryterror.org> X-Mailer: git-send-email 2.1.4 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP I got tired of seeing "16.00EiB" whenever btrfs-progs encounters a negative size value. e.g. during filesystem shrink we see: Unallocated: /dev/mapper/testvol0 16.00EiB Interpreting this as a signed quantity is much more useful: Unallocated: /dev/mapper/testvol0 -26.29GiB Signed-off-by: Zygo Blaxell --- utils.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/utils.c b/utils.c index 69b580a..bd2b66e 100644 --- a/utils.c +++ b/utils.c @@ -2594,20 +2594,23 @@ static const char* unit_suffix_binary[] = static const char* unit_suffix_decimal[] = { "B", "kB", "MB", "GB", "TB", "PB", "EB"}; -int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mode) +int pretty_size_snprintf(u64 usize, char *str, size_t str_size, unsigned unit_mode) { int num_divs; float fraction; - u64 base = 0; + s64 base = 0; int mult = 0; const char** suffix = NULL; - u64 last_size; + s64 last_size; if (str_size == 0) return 0; + /* Negative numbers are more plausible than sizes over 8 EiB. */ + s64 size = (s64)usize; + if ((unit_mode & ~UNITS_MODE_MASK) == UNITS_RAW) { - snprintf(str, str_size, "%llu", size); + snprintf(str, str_size, "%lld", size); return 0; } @@ -2642,7 +2645,7 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mod num_divs = 0; break; default: - while (size >= mult) { + while ((size < 0 ? -size : size) >= mult) { last_size = size; size /= mult; num_divs++;