From 078618d82220e84609cffd074655faafbada4c24 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Mon, 12 Oct 2015 21:23:02 +0800 Subject: btrfs-progs: use btrfs_open_dir in open_path_or_dev_mnt Use btrfs_open_dir() in open_path_or_dev_mnt() to make the function return error when target is neither block device nor btrfs mount point. Also add "verbose" argument to let function output common error message instead of putting duplicated lines in caller. Before patch: # ./btrfs device stats /mnt/tmp1 ERROR: getting dev info for devstats failed: Inappropriate ioctl for device # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1 ERROR: ioctl(DEV_REPLACE_STATUS) failed on "/mnt/tmp1": Inappropriate ioctl for device After patch: # ./btrfs device stats /mnt/tmp1 ERROR: not a btrfs filesystem: /mnt/tmp1 # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1 ERROR: not a btrfs filesystem: /mnt/tmp1 Signed-off-by: Zhao Lei Signed-off-by: David Sterba --- utils.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index f1e3248..6f5df23 100644 --- a/utils.c +++ b/utils.c @@ -1081,27 +1081,28 @@ out: * * On error, return -1, errno should be set. */ -int open_path_or_dev_mnt(const char *path, DIR **dirstream) +int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose) { char mp[PATH_MAX]; - int fdmnt; - - fdmnt = is_block_device(path); - if (fdmnt == 1) { - int ret; + int ret; + if (is_block_device(path)) { ret = get_btrfs_mount(path, mp, sizeof(mp)); if (ret < 0) { /* not a mounted btrfs dev */ + error_on(verbose, "'%s' is not a mounted btrfs device", + path); errno = EINVAL; return -1; } - fdmnt = open_file_or_dir(mp, dirstream); - } else if (fdmnt == 0) { - fdmnt = open_file_or_dir(path, dirstream); + ret = open_file_or_dir(mp, dirstream); + error_on(verbose && ret < 0, "can't access '%s': %s", + path, strerror(errno)); + } else { + ret = btrfs_open_dir(path, dirstream, 1); } - return fdmnt; + return ret; } /* -- cgit v1.2.1