diff mbox

[v2,1/2] btrfs-progs: Introduce open_btrfs_dir wrapper

Message ID f1ac41c81d460158be008fa8a02a06b22495a221.1440579753.git.zhaolei@cn.fujitsu.com (mailing list archive)
State Accepted
Headers show

Commit Message

Zhaolei Aug. 26, 2015, 9:04 a.m. UTC
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>
---
 utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h |  1 +
 2 files changed, 57 insertions(+)

Comments

David Sterba Aug. 26, 2015, 3:56 p.m. UTC | #1
On Wed, Aug 26, 2015 at 05:04:22PM +0800, Zhao Lei wrote:
> 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>

I've renamed it to btrfs_open_dir and tweaked some error messages and
applied, thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/utils.c b/utils.c
index 1dfcc2d..eb9a605 100644
--- a/utils.c
+++ b/utils.c
@@ -36,6 +36,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"
@@ -1081,6 +1083,60 @@  int open_path_or_dev_mnt(const char *path, DIR **dirstream)
 	return fdmnt;
 }
 
+/*
+ * Do following check before open_file_or_dir():
+ * 1: path is in a btrfs filesystem
+ * 2: path is a dir
+ */
+int open_btrfs_dir(const char *path, DIR **dirstream, int output)
+{
+	struct statfs stfs;
+	struct stat st;
+	int ret;
+
+	if (statfs(path, &stfs) != 0) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: can't access '%s', %s\n",
+				path, strerror(errno));
+		return -1;
+	}
+
+	if (stfs.f_type != BTRFS_SUPER_MAGIC) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: not btrfs filesystem: %s\n",
+				path);
+		return -2;
+	}
+
+	if (stat(path, &st) != 0) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: can't access '%s', %s\n",
+				path, strerror(errno));
+		return -1;
+	}
+
+	if (!S_ISDIR(st.st_mode)) {
+		if (output)
+			fprintf(stderr,
+				"ERROR: not directory: %s\n",
+				path);
+		return -3;
+	}
+
+	ret = open_file_or_dir(path, dirstream);
+	if (ret < 0) {
+		if (output)
+			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 8ec23c9..87c0d08 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 open_btrfs_dir(const char *path, DIR **dirstream, int output);
 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))