From 88ef0b8397fa13f6114dc3ba5c8e88a7b7c992f9 Mon Sep 17 00:00:00 2001 From: "Misono, Tomohiro" Date: Mon, 4 Sep 2017 14:05:34 +0900 Subject: btrfs-progs: inspect rootid: Allow a file to be specified Since cmd_inspect_rootid() calls btrfs_open_dir(), it rejects a file to be specified. 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() content is moved to btrfs_open() and shared by both function. Signed-off-by: Tomohiro Misono Signed-off-by: David Sterba --- utils.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 2d0ff8b3..7a2710fe 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; -- cgit v1.2.3