f2fs-tools: create sparse file first before stat
authorJaegeuk Kim <jaegeuk@kernel.org>
Fri, 16 Nov 2018 05:54:02 +0000 (21:54 -0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Wed, 21 Nov 2018 19:38:23 +0000 (11:38 -0800)
We must create a sparse file first before calling stat().

Fixes: eb9d8037ed3b ("f2fs-tools: avoid mounting f2fs if tools already open the device")
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
lib/libf2fs.c

index bf2830d..a1a5c02 100644 (file)
@@ -793,17 +793,24 @@ int get_device_info(int i)
 #endif
        struct device_info *dev = c.devices + i;
 
+       if (c.sparse_mode) {
+               fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
+               if (fd < 0) {
+                       MSG(0, "\tError: Failed to open a sparse file!\n");
+                       return -1;
+               }
+       }
+
        stat_buf = malloc(sizeof(struct stat));
        ASSERT(stat_buf);
-       if (stat(dev->path, stat_buf) < 0 ) {
-               MSG(0, "\tError: Failed to get the device stat!\n");
-               free(stat_buf);
-               return -1;
-       }
 
-       if (c.sparse_mode) {
-               fd = open(dev->path, O_RDWR | O_CREAT | O_BINARY, 0644);
-       } else {
+       if (!c.sparse_mode) {
+               if (stat(dev->path, stat_buf) < 0 ) {
+                       MSG(0, "\tError: Failed to get the device stat!\n");
+                       free(stat_buf);
+                       return -1;
+               }
+
                if (S_ISBLK(stat_buf->st_mode))
                        fd = open(dev->path, O_RDWR | O_EXCL);
                else