All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: inspect-internal rootid: Allow a file to be specified
@ 2017-09-04  5:05 Misono, Tomohiro
  2017-09-04 16:09 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: Misono, Tomohiro @ 2017-09-04  5:05 UTC (permalink / raw)
  To: linux-btrfs

Since cmd_inspect_rootid() calls btrfs_open_dir(), it rejects a file to
be spcified. But as the document says, a file should be supported.

This patch introduces btrfs_open_file_or_dir(), which is a counterpart
of btrfs_open_dir(), to safely check and open btrfs file or directory.
The original btrfs_open_dir() codes are moved to btrfs_open() and shared
by both function.

Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com>
---
 cmds-inspect.c |  2 +-
 utils.c        | 16 +++++++++++++---
 utils.h        |  2 ++
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/cmds-inspect.c b/cmds-inspect.c
index d1a3a0e..885f3ab 100644
--- a/cmds-inspect.c
+++ b/cmds-inspect.c
@@ -318,7 +318,7 @@ static int cmd_inspect_rootid(int argc, char **argv)
 	if (check_argc_exact(argc - optind, 1))
 		usage(cmd_inspect_rootid_usage);
 
-	fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+	fd = btrfs_open_file_or_dir(argv[optind], &dirstream, 1);
 	if (fd < 0) {
 		ret = -ENOENT;
 		goto out;
diff --git a/utils.c b/utils.c
index bb04913..9db39eb 100644
--- a/utils.c
+++ b/utils.c
@@ -568,9 +568,9 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
 /*
  * Do the following checks before calling open_file_or_dir():
  * 1: path is in a btrfs filesystem
- * 2: path is a directory
+ * 2: path is a directory if dir_only is 1
  */
-int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
+int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only)
 {
 	struct statfs stfs;
 	struct stat st;
@@ -593,7 +593,7 @@ int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
 		return -1;
 	}
 
-	if (!S_ISDIR(st.st_mode)) {
+	if (dir_only && !S_ISDIR(st.st_mode)) {
 		error_on(verbose, "not a directory: %s", path);
 		return -3;
 	}
@@ -607,6 +607,16 @@ int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
 	return ret;
 }
 
+int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
+{
+	return btrfs_open(path, dirstream, verbose, 1);
+}
+
+int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose)
+{
+	return btrfs_open(path, dirstream, verbose, 0);
+}
+
 /* checks if a device is a loop device */
 static int is_loop_device (const char* device) {
 	struct stat statbuf;
diff --git a/utils.h b/utils.h
index 091f8fa..d28a05a 100644
--- a/utils.h
+++ b/utils.h
@@ -108,7 +108,9 @@ int is_block_device(const char *file);
 int is_mount_point(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);
 int btrfs_open_dir(const char *path, DIR **dirstream, int verbose);
+int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose);
 u64 btrfs_device_size(int fd, struct stat *st);
 /* Helper to always get proper size of the destination string */
 #define strncpy_null(dest, src) __strncpy_null(dest, src, sizeof(dest))
-- 
2.9.5


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] btrfs-progs: inspect-internal rootid: Allow a file to be specified
  2017-09-04  5:05 [PATCH] btrfs-progs: inspect-internal rootid: Allow a file to be specified Misono, Tomohiro
@ 2017-09-04 16:09 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2017-09-04 16:09 UTC (permalink / raw)
  To: Misono, Tomohiro; +Cc: linux-btrfs

On Mon, Sep 04, 2017 at 02:05:34PM +0900, Misono, Tomohiro wrote:
> Since cmd_inspect_rootid() calls btrfs_open_dir(), it rejects a file to
> be spcified. But as the document says, a file should be supported.
> 
> This patch introduces btrfs_open_file_or_dir(), which is a counterpart
> of btrfs_open_dir(), to safely check and open btrfs file or directory.
> The original btrfs_open_dir() codes are moved to btrfs_open() and shared
> by both function.
> 
> Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com>

Applied, thanks. Can you please send a testcase for that?

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-09-04 16:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-04  5:05 [PATCH] btrfs-progs: inspect-internal rootid: Allow a file to be specified Misono, Tomohiro
2017-09-04 16:09 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.