btrfs-progs: mkfs: fix regression preventing --rootdir to create file
authorQu Wenruo <wqu@suse.com>
Wed, 29 Nov 2017 08:07:34 +0000 (16:07 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 8 Jan 2018 17:15:16 +0000 (18:15 +0100)
Commit 460e93f25754 ("btrfs-progs: mkfs: check the status of file at mkfs")
will try to check the file state before creating fs on it.

The check is mostly fine for normal mkfs case, while for --rootdir
option, it's allowed to create a new file if the destination file
doesn't exist.

Fix it by allowing non-existent file if --rootdir is specified.

Fixes: 460e93f25754 ("btrfs-progs: mkfs: check the status of file at mkfs")
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
mkfs/main.c
utils.c
utils.h

index 16254ee..94d9ef0 100644 (file)
@@ -720,7 +720,7 @@ int main(int argc, char **argv)
        u32 stripesize = 4096;
        int zero_end = 1;
        int fd = -1;
-       int ret;
+       int ret = 0;
        int close_ret;
        int i;
        int mixed = 0;
@@ -888,7 +888,9 @@ int main(int argc, char **argv)
 
        while (dev_cnt-- > 0) {
                file = argv[optind++];
-               if (is_block_device(file) == 1)
+               if (source_dir_set && is_path_exist(file) == 0)
+                       ret = 0;
+               else if (is_block_device(file) == 1)
                        ret = test_dev_for_mkfs(file, force_overwrite);
                else
                        ret = test_status_for_mkfs(file, force_overwrite);
diff --git a/utils.c b/utils.c
index 524f463..22c1375 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -456,6 +456,21 @@ static int is_reg_file(const char *path)
        return S_ISREG(statbuf.st_mode);
 }
 
+int is_path_exist(const char *path)
+{
+       struct stat statbuf;
+       int ret;
+
+       ret = stat(path, &statbuf);
+       if (ret < 0) {
+               if (errno == ENOENT)
+                       return 0;
+               else
+                       return -errno;
+       }
+       return 1;
+}
+
 /*
  * This function checks if the given input parameter is
  * an uuid or a path
diff --git a/utils.h b/utils.h
index a82d46f..860c25d 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -122,6 +122,7 @@ int set_label(const char *btrfs_dev, const char *label);
 char *__strncpy_null(char *dest, const char *src, size_t n);
 int is_block_device(const char *file);
 int is_mount_point(const char *file);
+int is_path_exist(const char *file);
 int check_arg_type(const char *input);
 int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose);
 int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only);