From 87c25626c4bef0700c4b165ddc128061ea31bd58 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Wed, 26 Aug 2015 17:04:22 +0800 Subject: [PATCH] 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 [renamed from open_btrfs_dir, adjusted error messages] Signed-off-by: David Sterba --- utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 1 + 2 files changed, 57 insertions(+) diff --git a/utils.c b/utils.c index 280637d8..aa9c2c9a 100644 --- a/utils.c +++ b/utils.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include #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; diff --git a/utils.h b/utils.h index 94606eda..10d68e9a 100644 --- a/utils.h +++ b/utils.h @@ -158,6 +158,7 @@ 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 btrfs_open_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))