From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 10/13] btrfs-progs: move out print in cmd_df to another function
Date: Fri, 21 Jun 2013 15:58:02 +0800 [thread overview]
Message-ID: <1371801485-14571-3-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1371801485-14571-1-git-send-email-anand.jain@oracle.com>
This is a prepatory work for the following btrfs fi show command
fixes. So that we have a function get_df to get the fs sizes.
Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
cmds-filesystem.c | 146 +++++++++++++++++++++++++++++-------------------------
1 file changed, 79 insertions(+), 67 deletions(-)
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 9b7bcf1..ea69328 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -44,71 +44,9 @@ static const char * const cmd_df_usage[] = {
NULL
};
-static int cmd_df(int argc, char **argv)
+static void print_df(struct btrfs_ioctl_space_args *sargs)
{
- struct btrfs_ioctl_space_args *sargs, *sargs_orig;
- u64 count = 0, i;
- int ret;
- int fd;
- int e;
- char *path;
-
- if (check_argc_exact(argc, 2))
- usage(cmd_df_usage);
-
- path = argv[1];
-
- fd = open_file_or_dir(path);
- if (fd < 0) {
- fprintf(stderr, "ERROR: can't access to '%s'\n", path);
- return 12;
- }
-
- sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
- if (!sargs)
- return -ENOMEM;
-
- sargs->space_slots = 0;
- sargs->total_spaces = 0;
-
- ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
- e = errno;
- if (ret) {
- fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
- path, strerror(e));
- close(fd);
- free(sargs);
- return ret;
- }
- if (!sargs->total_spaces) {
- close(fd);
- free(sargs);
- return 0;
- }
-
- count = sargs->total_spaces;
-
- sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
- (count * sizeof(struct btrfs_ioctl_space_info)));
- if (!sargs) {
- close(fd);
- free(sargs_orig);
- return -ENOMEM;
- }
-
- sargs->space_slots = count;
- sargs->total_spaces = 0;
-
- ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
- e = errno;
- if (ret) {
- fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
- path, strerror(e));
- close(fd);
- free(sargs);
- return ret;
- }
-
+ u64 i;
for (i = 0; i < sargs->total_spaces; i++) {
char description[80];
char *total_bytes;
@@ -160,10 +98,84 @@ static int cmd_df(int argc, char **argv)
printf("%s: total=%s, used=%s\n", description, total_bytes,
used_bytes);
}
- close(fd);
- free(sargs);
+}
- return 0;
+static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
+{
+ u64 count = 0;
+ int ret, e;
+ struct btrfs_ioctl_space_args *sargs_orig;
+ struct btrfs_ioctl_space_args *sargs;
+
+ sargs_orig = sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
+ if (!sargs)
+ return -ENOMEM;
+
+ sargs->space_slots = 0;
+ sargs->total_spaces = 0;
+
+ ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
+ e = errno;
+ if (ret) {
+ free(sargs);
+ return -e;
+ }
+ if (!sargs->total_spaces) {
+ free(sargs);
+ return 0;
+ }
+
+ count = sargs->total_spaces;
+
+ sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
+ (count * sizeof(struct btrfs_ioctl_space_info)));
+ if (!sargs) {
+ free(sargs_orig);
+ return -ENOMEM;
+ }
+
+ sargs->space_slots = count;
+ sargs->total_spaces = 0;
+
+ ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
+ e = errno;
+ if (ret) {
+ free(sargs);
+ return -e;
+ }
+ *sargs_ret = sargs;
+ return ret;
+}
+
+static int cmd_df(int argc, char **argv)
+{
+ struct btrfs_ioctl_space_args *sargs = NULL;
+ int ret;
+ int fd;
+ char *path;
+
+ if (check_argc_exact(argc, 2))
+ usage(cmd_df_usage);
+
+ path = argv[1];
+
+ fd = open_file_or_dir(path);
+ if (fd < 0) {
+ fprintf(stderr, "ERROR: can't access to '%s'\n", path);
+ return 12;
+ }
+ ret = get_df(fd, &sargs);
+
+ if (!ret) {
+ if (sargs) {
+ print_df(sargs);
+ free(sargs);
+ }
+ } else
+ fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
+
+ close(fd);
+ return ret;
}
static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
--
1.8.1.227.g44fe835
next prev parent reply other threads:[~2013-06-21 7:54 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-21 7:58 [PATCH 00/13] Introduce show --kernel Anand Jain
2013-06-21 7:58 ` [PATCH 09/13] btrfs-progs: function to release a specific fsid from the list Anand Jain
2013-06-21 7:58 ` Anand Jain [this message]
2013-06-21 7:58 ` [PATCH 11/13] btrfs-progs: get string for the group profile and type Anand Jain
2013-06-21 7:58 ` [PATCH 12/13] btrfs-progs: obtain used_bytes in BTRFS_IOC_FS_INFO ioctl Anand Jain
2013-06-25 8:39 ` Anand Jain
2013-06-21 7:58 ` [PATCH 13/13 v3] btrfs-progs: introduce btrfs filesystem show --kernel Anand Jain
2013-06-25 8:44 ` [PATCH 12/12 v4] " Anand Jain
2013-06-25 8:50 ` Anand Jain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1371801485-14571-3-git-send-email-anand.jain@oracle.com \
--to=anand.jain@oracle.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).