Btrfs-progs: let get_label return the label instead of printing it
authorFilipe David Borba Manana <fdmanana@gmail.com>
Tue, 12 Nov 2013 13:41:42 +0000 (13:41 +0000)
committerChris Mason <clm@fb.com>
Fri, 31 Jan 2014 16:22:02 +0000 (08:22 -0800)
get_label prints the label at the moment. Change this so that
the label is returned and printing is done by the caller.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
cmds-filesystem.c
utils.c
utils.h

index ef426d30a7b8d5c4f3d5b93aa383f7ad0297f616..678343894c1672545c0a5693f31f5716aeb19fd3 100644 (file)
@@ -896,10 +896,18 @@ static int cmd_label(int argc, char **argv)
        if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
                usage(cmd_label_usage);
 
-       if (argc > 2)
+       if (argc > 2) {
                return set_label(argv[1], argv[2]);
-       else
-               return get_label(argv[1]);
+       } else {
+               char label[BTRFS_LABEL_SIZE];
+               int ret;
+
+               ret = get_label(argv[1], label);
+               if (!ret)
+                       fprintf(stdout, "%s\n", label);
+
+               return ret;
+       }
 }
 
 const struct cmd_group filesystem_cmd_group = {
diff --git a/utils.c b/utils.c
index 2422338b072089156be9cca49f8511231480ad1a..c46e09a46588d0d8ec81f0141eac34ed1aed727a 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -1345,7 +1345,7 @@ static int set_label_mounted(const char *mount_path, const char *label)
        return 0;
 }
 
-static int get_label_unmounted(const char *dev)
+static int get_label_unmounted(const char *dev, char *label)
 {
        struct btrfs_root *root;
        int ret;
@@ -1368,7 +1368,7 @@ static int get_label_unmounted(const char *dev)
        if(!root)
                return -1;
 
-       fprintf(stdout, "%s\n", root->fs_info->super_copy->label);
+       memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
 
        /* Now we close it since we are done. */
        close_ctree(root);
@@ -1403,18 +1403,15 @@ int get_label_mounted(const char *mount_path, char *labelp)
        return 0;
 }
 
-int get_label(const char *btrfs_dev)
+int get_label(const char *btrfs_dev, char *label)
 {
        int ret;
-       char label[BTRFS_LABEL_SIZE];
 
        if (is_existing_blk_or_reg_file(btrfs_dev))
-               ret = get_label_unmounted(btrfs_dev);
-       else {
+               ret = get_label_unmounted(btrfs_dev, label);
+       else
                ret = get_label_mounted(btrfs_dev, label);
-               if (!ret)
-                       fprintf(stdout, "%s\n", label);
-       }
+
        return ret;
 }
 
diff --git a/utils.h b/utils.h
index 6f4b10c19edafb82c1ea2c28dcb3091f65e04831..b38bd3a21e4c29fb8eeccf569c1cdb5ec97ee006 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -72,7 +72,7 @@ int open_file_or_dir(const char *fname, DIR **dirstream);
 void close_file_or_dir(int fd, DIR *dirstream);
 int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
                struct btrfs_ioctl_dev_info_args **di_ret);
-int get_label(const char *btrfs_dev);
+int get_label(const char *btrfs_dev, char *label);
 int set_label(const char *btrfs_dev, const char *label);
 
 char *__strncpy__null(char *dest, const char *src, size_t n);