linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
From: Jaegeuk Kim <jaegeuk@kernel.org>
To: linux-f2fs-devel@lists.sourceforge.net
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Subject: [PATCH 2/2] f2fs-tools: create sparse file first before stat
Date: Thu, 15 Nov 2018 22:06:03 -0800	[thread overview]
Message-ID: <20181116060603.51965-2-jaegeuk@kernel.org> (raw)
In-Reply-To: <20181116060603.51965-1-jaegeuk@kernel.org>

We must create a sparse file first before calling stat().

Fixes: eb9d8037ed3b ("f2fs-tools: avoid mounting f2fs if tools already open the device")
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 lib/libf2fs.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/lib/libf2fs.c b/lib/libf2fs.c
index bf2830d..a1a5c02 100644
--- a/lib/libf2fs.c
+++ b/lib/libf2fs.c
@@ -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
-- 
2.19.0.605.g01d371f741-goog

  reply	other threads:[~2018-11-16  6:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-16  6:06 [PATCH 1/2] f2fs-tools: show versions if exist Jaegeuk Kim
2018-11-16  6:06 ` Jaegeuk Kim [this message]
2018-11-17  2:32   ` [PATCH 2/2] f2fs-tools: create sparse file first before stat Chao Yu
2018-11-17  2:27 ` [PATCH 1/2] f2fs-tools: show versions if exist Chao Yu

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=20181116060603.51965-2-jaegeuk@kernel.org \
    --to=jaegeuk@kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    /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).