Return-Path: Received: from frost.carfax.org.uk ([85.119.82.111]:59875 "EHLO frost.carfax.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755681Ab3BAKaf (ORCPT ); Fri, 1 Feb 2013 05:30:35 -0500 Date: Fri, 1 Feb 2013 10:30:31 +0000 From: Hugo Mills To: Audrius Butkevicius Cc: linux-btrfs@vger.kernel.org Subject: Re: [PATCH] btrfs-progs: add '-b' option to filesystem df and show Message-ID: <20130201103031.GQ16285@carfax.org.uk> References: <20130201095947.GA32445@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="JaBjgNvtdKe5H086" In-Reply-To: <20130201095947.GA32445@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org List-ID: --JaBjgNvtdKe5H086 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Feb 01, 2013 at 09:59:49AM +0000, Audrius Butkevicius wrote: > Add '-b' and '--bytes' options to btrfs filesystem df and show, for easier > integration with scripts. This causes all sizes to be displayed in decimal > bytes instead of pretty-printed with human-readable suffices KB, MB, etc. Please, not this way. The right way to do this would be to modify the pretty-print function so that it can output bytes or SI suffixes (kB, MB) or binary suffixes (KiB, MiB). I'm fairly sure I posted a patch for this a couple of years ago. There was some discussion then about the appropriate command-line options to use. The options should also be applicable to fi show, and to anything else which outputs byte counts. Note also that this may well clash with Goffredo's new free-space display (which hasn't yet made it to David's integration tree, but shouldn't be far away). Hugo. > Signed-off-by: Audrius Butkevicius > --- > cmds-filesystem.c | 93 +++++++++++++++++++++++++++++++++++++++-------------- > 1 file changed, 68 insertions(+), 25 deletions(-) > > diff --git a/cmds-filesystem.c b/cmds-filesystem.c > index 507239a..9392209 100644 > --- a/cmds-filesystem.c > +++ b/cmds-filesystem.c > @@ -40,7 +40,7 @@ static const char * const filesystem_cmd_group_usage[] = { > }; > > static const char * const cmd_df_usage[] = { > - "btrfs filesystem df ", > + "btrfs filesystem df [-b|--bytes] ", > "Show space usage information for a mount point", > NULL > }; > @@ -53,11 +53,23 @@ static int cmd_df(int argc, char **argv) > int fd; > int e; > char *path; > + int start = 1; > + int rawbytes = 0; > + > + while (argc > start) { > + if (!strcmp(argv[start], "--bytes") || > + !strcmp(argv[start], "-b")) { > + rawbytes = 1; > + start += 1; > + } else { > + break; > + } > + } > > - if (check_argc_exact(argc, 2)) > + if (check_argc_exact(argc, start + 1)) > usage(cmd_df_usage); > > - path = argv[1]; > + path = argv[start]; > > fd = open_file_or_dir(path); > if (fd < 0) { > @@ -150,10 +162,18 @@ static int cmd_df(int argc, char **argv) > written += 8; > } > > - total_bytes = pretty_sizes(sargs->spaces[i].total_bytes); > - used_bytes = pretty_sizes(sargs->spaces[i].used_bytes); > - printf("%s: total=%s, used=%s\n", description, total_bytes, > - used_bytes); > + if (rawbytes) { > + printf("%s: total=%llu, used=%llu\n", description, > + (unsigned long long)sargs->spaces[i].total_bytes, > + (unsigned long long)sargs->spaces[i].used_bytes); > + } else { > + total_bytes = pretty_sizes(sargs->spaces[i].total_bytes); > + used_bytes = pretty_sizes(sargs->spaces[i].used_bytes); > + printf("%s: total=%s, used=%s\n", description, total_bytes, > + used_bytes); > + free(total_bytes); > + free(bytes_used); > + } > } > close(fd); > free(sargs); > @@ -182,7 +202,7 @@ static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search) > return 0; > } > > -static void print_one_uuid(struct btrfs_fs_devices *fs_devices) > +static void print_one_uuid(struct btrfs_fs_devices *fs_devices, int rawbytes) > { > char uuidbuf[37]; > struct list_head *cur; > @@ -199,25 +219,39 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices) > else > printf("Label: none "); > > - super_bytes_used = pretty_sizes(device->super_bytes_used); > - > total = device->total_devs; > - printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, > - (unsigned long long)total, super_bytes_used); > > - free(super_bytes_used); > + if (rawbytes) { > + printf(" uuid: %s\n\tTotal devices %llu FS bytes used %llu\n", > + uuidbuf, (unsigned long long)total, > + (unsigned long long)device->super_bytes_used); > + } else { > + super_bytes_used = pretty_sizes(device->super_bytes_used); > + printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", > + uuidbuf, (unsigned long long)total, super_bytes_used); > + free(super_bytes_used); > + } > > list_for_each(cur, &fs_devices->devices) { > char *total_bytes; > char *bytes_used; > device = list_entry(cur, struct btrfs_device, dev_list); > - total_bytes = pretty_sizes(device->total_bytes); > - bytes_used = pretty_sizes(device->bytes_used); > - printf("\tdevid %4llu size %s used %s path %s\n", > - (unsigned long long)device->devid, > - total_bytes, bytes_used, device->name); > - free(total_bytes); > - free(bytes_used); > + if (rawbytes) { > + printf("\tdevid %4llu size %llu used %llu path %s\n", > + (unsigned long long)device->devid, > + (unsigned long long)device->total_bytes, > + (unsigned long long)device->bytes_used, > + device->name); > + } > + else { > + total_bytes = pretty_sizes(device->total_bytes); > + bytes_used = pretty_sizes(device->bytes_used); > + printf("\tdevid %4llu size %s used %s path %s\n", > + (unsigned long long)device->devid, > + total_bytes, bytes_used, device->name); > + free(total_bytes); > + free(bytes_used); > + } > devs_found++; > } > if (devs_found < total) { > @@ -227,7 +261,7 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices) > } > > static const char * const cmd_show_usage[] = { > - "btrfs filesystem show [--all-devices] [|