linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] Btrfs-progs: make sure we are opening a file or dir with open_file_or_dir()
@ 2013-12-12  7:46 Wang Shilong
  0 siblings, 0 replies; only message in thread
From: Wang Shilong @ 2013-12-12  7:46 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-12-12  7:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-12  7:46 [PATCH v2] Btrfs-progs: make sure we are opening a file or dir with open_file_or_dir() Wang Shilong

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).