linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.cz
Subject: [PATCH v2] Btrfs-progs: make sure we are opening a file or dir with open_file_or_dir()
Date: Thu, 12 Dec 2013 15:46:18 +0800	[thread overview]
Message-ID: <1386834378-876-1-git-send-email-wangsl.fnst@cn.fujitsu.com> (raw)

Previously, open_file_or_dir() will open block device successfully, however,
we should enhance such checks to make sure we are really opening a file or dir.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
---
v1->v2:
	this is more nature to fix the problem.
---
 utils.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/utils.c b/utils.c
index f499023..bf81704 100644
--- a/utils.c
+++ b/utils.c
@@ -1568,13 +1568,20 @@ int open_file_or_dir(const char *fname, DIR **dirstream)
 	if (S_ISDIR(st.st_mode)) {
 		*dirstream = opendir(fname);
 		if (!*dirstream)
-			return -2;
+			return -1;
 		fd = dirfd(*dirstream);
-	} else {
+	} else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
 		fd = open(fname, O_RDWR);
+	} else {
+		/*
+		 * we set this on purpose, in case the caller output
+		 * strerror(errno) as success
+		 */
+		errno = EINVAL;
+		return -1;
 	}
 	if (fd < 0) {
-		fd = -3;
+		fd = -1;
 		if (*dirstream)
 			closedir(*dirstream);
 	}
-- 
1.8.3.1


                 reply	other threads:[~2013-12-12  7:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1386834378-876-1-git-send-email-wangsl.fnst@cn.fujitsu.com \
    --to=wangsl.fnst@cn.fujitsu.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /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).