Btrfs-progs: fix closing of opendir()
[platform/upstream/btrfs-progs.git] / cmds-replace.c
index d14c9b5..5d3c2f0 100644 (file)
@@ -77,10 +77,13 @@ static int is_numerical(const char *str)
 static int dev_replace_cancel_fd = -1;
 static void dev_replace_sigint_handler(int signal)
 {
+       int ret;
        struct btrfs_ioctl_dev_replace_args args = {0};
 
        args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL;
-       ioctl(dev_replace_cancel_fd, BTRFS_IOC_DEV_REPLACE, &args);
+       ret = ioctl(dev_replace_cancel_fd, BTRFS_IOC_DEV_REPLACE, &args);
+       if (ret < 0)
+               perror("Device replace cancel failed");
 }
 
 static int dev_replace_handle_sigint(int fd)
@@ -140,6 +143,7 @@ static int cmd_start_replace(int argc, char **argv)
        u64 dstdev_block_count;
        int do_not_background = 0;
        int mixed = 0;
+       DIR *dirstream = NULL;
 
        while ((c = getopt(argc, argv, "Brf")) != -1) {
                switch (c) {
@@ -165,7 +169,9 @@ static int cmd_start_replace(int argc, char **argv)
        if (check_argc_exact(argc - optind, 3))
                usage(cmd_start_replace_usage);
        path = argv[optind + 2];
-       fdmnt = open_file_or_dir(path);
+
+       fdmnt = open_path_or_dev_mnt(path, &dirstream);
+
        if (fdmnt < 0) {
                fprintf(stderr, "ERROR: can't access \"%s\": %s\n",
                        path, strerror(errno));
@@ -212,7 +218,7 @@ static int cmd_start_replace(int argc, char **argv)
                }
                start_args.start.srcdevid = (__u64)atoi(srcdev);
 
-               ret = get_fs_info(fdmnt, path, &fi_args, &di_args);
+               ret = get_fs_info(path, &fi_args, &di_args);
                if (ret) {
                        fprintf(stderr, "ERROR: getting dev info for devstats failed: "
                                        "%s\n", strerror(-ret));
@@ -228,6 +234,7 @@ static int cmd_start_replace(int argc, char **argv)
                for (i = 0; i < fi_args.num_devices; i++)
                        if (start_args.start.srcdevid == di_args[i].devid)
                                break;
+               free(di_args);
                if (i == fi_args.num_devices) {
                        fprintf(stderr, "Error: '%s' is not a valid devid for filesystem '%s'\n",
                                srcdev, path);
@@ -235,7 +242,7 @@ static int cmd_start_replace(int argc, char **argv)
                }
        } else {
                fdsrcdev = open(srcdev, O_RDWR);
-               if (!fdsrcdev) {
+               if (fdsrcdev < 0) {
                        fprintf(stderr, "Error: Unable to open device '%s'\n",
                                srcdev);
                        goto leave_with_error;
@@ -330,7 +337,7 @@ static int cmd_start_replace(int argc, char **argv)
                        goto leave_with_error;
                }
        }
-       close(fdmnt);
+       close_file_or_dir(fdmnt, dirstream);
        return 0;
 
 leave_with_error:
@@ -361,6 +368,7 @@ static int cmd_status_replace(int argc, char **argv)
        char *path;
        int once = 0;
        int ret;
+       DIR *dirstream = NULL;
 
        while ((c = getopt(argc, argv, "1")) != -1) {
                switch (c) {
@@ -377,7 +385,7 @@ static int cmd_status_replace(int argc, char **argv)
                usage(cmd_status_replace_usage);
 
        path = argv[optind];
-       fd = open_file_or_dir(path);
+       fd = open_file_or_dir(path, &dirstream);
        e = errno;
        if (fd < 0) {
                fprintf(stderr, "ERROR: can't access \"%s\": %s\n",
@@ -386,7 +394,7 @@ static int cmd_status_replace(int argc, char **argv)
        }
 
        ret = print_replace_status(fd, path, once);
-       close(fd);
+       close_file_or_dir(fd, dirstream);
        return ret;
 }
 
@@ -527,6 +535,7 @@ static int cmd_cancel_replace(int argc, char **argv)
        int fd;
        int e;
        char *path;
+       DIR *dirstream = NULL;
 
        while ((c = getopt(argc, argv, "")) != -1) {
                switch (c) {
@@ -540,7 +549,7 @@ static int cmd_cancel_replace(int argc, char **argv)
                usage(cmd_cancel_replace_usage);
 
        path = argv[optind];
-       fd = open_file_or_dir(path);
+       fd = open_file_or_dir(path, &dirstream);
        if (fd < 0) {
                fprintf(stderr, "ERROR: can't access \"%s\": %s\n",
                        path, strerror(errno));
@@ -550,7 +559,7 @@ static int cmd_cancel_replace(int argc, char **argv)
        args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL;
        ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args);
        e = errno;
-       close(fd);
+       close_file_or_dir(fd, dirstream);
        if (ret) {
                fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %s, %s\n",
                        path, strerror(e),