diff mbox

[2/5] btrfs-progs: property: set/get/print ro property

Message ID 20171102032320.12537-3-suy.fnst@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Su Yue Nov. 2, 2017, 3:23 a.m. UTC
Introduce set_prop_read_only(), get_prop_read_only(),
print_prop_read_only() to set, get and print the subvolume
is read-only or not.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 props.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 62 insertions(+), 10 deletions(-)
diff mbox

Patch

diff --git a/props.c b/props.c
index d6249f53887d..da69f6e9314c 100644
--- a/props.c
+++ b/props.c
@@ -36,12 +36,16 @@ 
 #define ENOATTR ENODATA
 #endif
 
-static int prop_read_only(enum prop_object_type type,
-			  const char *object,
-			  const char *name,
-			  const char *value)
+enum prop_operation {
+	set_prop,
+	get_prop,
+};
+
+static ssize_t prop_read_only(enum prop_object_type type, const char *object,
+			      const char *name, char *value,
+			      enum prop_operation operation)
 {
-	int ret = 0;
+	ssize_t ret = 0;
 	int fd = -1;
 	u64 flags = 0;
 
@@ -60,12 +64,19 @@  static int prop_read_only(enum prop_object_type type,
 		goto out;
 	}
 
-	if (!value) {
+	if (operation == get_prop) {
+		const char *is_ro;
 		if (flags & BTRFS_SUBVOL_RDONLY)
-			fprintf(stdout, "ro=true\n");
+			is_ro = "true";
 		else
-			fprintf(stdout, "ro=false\n");
-		ret = 0;
+			is_ro = "false";
+
+		if (value) {
+			memcpy(value, is_ro, strlen(is_ro));
+			ret = 0;
+		} else {
+			ret = strlen(is_ro);
+		}
 		goto out;
 	}
 
@@ -93,6 +104,47 @@  out:
 	return ret;
 }
 
+static int set_prop_read_only(enum prop_object_type type, const char *object,
+			      const char *name, const char *value)
+{
+	return prop_read_only(type, object, name, (char *)value, set_prop);
+}
+
+static ssize_t get_prop_read_only(enum prop_object_type type,
+			  const char *object, const char *name, char *value)
+{
+	return prop_read_only(type, object, name, value, get_prop);
+}
+
+static int print_prop_read_only(enum prop_object_type type, const char *object,
+				const char *name)
+{
+	ssize_t len;
+	char *value = NULL;
+	int ret;
+
+	len = get_prop_read_only(type, object, name, NULL);
+	if (len <= 0) {
+		ret = -1;
+		goto out;
+	}
+	value = malloc(len + 1);
+	if (!value) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = get_prop_read_only(type, object, name, value);
+	if (ret)
+		goto out;
+	value[len] = '\0';
+	printf("ro=%s\n", value);
+out:
+	if (value)
+		free(value);
+	return ret;
+}
+
 static int prop_label(enum prop_object_type type,
 		      const char *object,
 		      const char *name,
@@ -188,7 +240,7 @@  out:
 
 const struct prop_handler prop_handlers[] = {
 	{"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
-	 NULL, NULL, NULL},
+	 set_prop_read_only, get_prop_read_only, print_prop_read_only},
 
 	{"label", "Set/get label of device.", 0,
 	 prop_object_dev | prop_object_root, NULL, NULL, NULL},