diff options
author | Zhao Lei <zhaolei@cn.fujitsu.com> | 2015-08-26 17:04:22 +0800 |
---|---|---|
committer | David Sterba <dsterba@suse.com> | 2015-08-31 19:25:14 +0200 |
commit | 87c25626c4bef0700c4b165ddc128061ea31bd58 (patch) | |
tree | c52cffe9e9a7d4b7a5d30f88e9a4969e2bf4e10b /utils.c | |
parent | 64650e13bba21c325a1ca6ea584a3250e9203b8a (diff) | |
download | btrfs-progs-87c25626c4bef0700c4b165ddc128061ea31bd58.tar.gz |
btrfs-progs: Introduce btrfs_open_dir wrapper
This patch introduce open_btrfs_dir() to open a dir in btrfs
filesystem.
It can be used for several tools in btrfs-progs.
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[renamed from open_btrfs_dir, adjusted error messages]
Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -35,6 +35,8 @@ #include <limits.h> #include <blkid/blkid.h> #include <sys/vfs.h> +#include <sys/statfs.h> +#include <linux/magic.h> #include "kerncompat.h" #include "radix-tree.h" @@ -1080,6 +1082,60 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream) return fdmnt; } +/* + * Do the following checks before calling open_file_or_dir(): + * 1: path is in a btrfs filesystem + * 2: path is a directory + */ +int btrfs_open_dir(const char *path, DIR **dirstream, int verbose) +{ + struct statfs stfs; + struct stat st; + int ret; + + if (statfs(path, &stfs) != 0) { + if (verbose) + fprintf(stderr, + "ERROR: can't access '%s': %s\n", + path, strerror(errno)); + return -1; + } + + if (stfs.f_type != BTRFS_SUPER_MAGIC) { + if (verbose) + fprintf(stderr, + "ERROR: not a btrfs filesystem: %s\n", + path); + return -2; + } + + if (stat(path, &st) != 0) { + if (verbose) + fprintf(stderr, + "ERROR: can't access '%s': %s\n", + path, strerror(errno)); + return -1; + } + + if (!S_ISDIR(st.st_mode)) { + if (verbose) + fprintf(stderr, + "ERROR: not a directory: %s\n", + path); + return -3; + } + + ret = open_file_or_dir(path, dirstream); + if (ret < 0) { + if (verbose) + fprintf(stderr, + "ERROR: can't access '%s': %s\n", + path, strerror(errno)); + } + + return ret; +} + /* checks if a device is a loop device */ static int is_loop_device (const char* device) { struct stat statbuf; |