linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Su Yue <suy.fnst@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: <suy.fnst@cn.fujitsu.com>
Subject: [PATCH 1/5] btrfs-progs: property: divide prop_handler_t into setter,getter,printer
Date: Thu, 2 Nov 2017 11:23:16 +0800	[thread overview]
Message-ID: <20171102032320.12537-2-suy.fnst@cn.fujitsu.com> (raw)
In-Reply-To: <20171102032320.12537-1-suy.fnst@cn.fujitsu.com>

Previously, function prototype prop_handler_t() in props.h is
just for setting and getting @value. However, it prints @value
directly instead of returns the value. It was difficult to get
@value by @name.

For code-reuse, here divide prop_handler_t into three handlers:
1) prop_setter_t: set @value as origin.
2) prop_getter_t:
	if arg @value is NULL, return the length of result for
		caller to allocate.
	else copy result to memory space which arg @value points to.

3) prop_printer_t: just call correspond prop_getter_t and
prints result.

Notice:
Since C language has no feature like tpyeinfo in others.
After return of prop_getter_t(), the arg @value will point
to array of characters without null-terminated.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 cmds-property.c |  7 +++++--
 props.c         | 11 +++++++----
 props.h         | 19 +++++++++++++++----
 3 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/cmds-property.c b/cmds-property.c
index 03bafa054701..6795d1795027 100644
--- a/cmds-property.c
+++ b/cmds-property.c
@@ -180,7 +180,7 @@ static int dump_prop(const struct prop_handler *prop,
 
 	if ((types & type) && (prop->types & type)) {
 		if (!name_and_help)
-			ret = prop->handler(type, object, prop->name, NULL);
+			ret = prop->printer(type, object, prop->name);
 		else
 			printf("%-20s%s\n", prop->name, prop->desc);
 	}
@@ -244,7 +244,10 @@ static int setget_prop(int types, const char *object,
 		goto out;
 	}
 
-	ret = prop->handler(types, object, name, value);
+	if (value)
+		ret = prop->setter(types, object, name, value);
+	else
+		ret = prop->printer(types, object, name);
 
 	if (ret < 0)
 		ret = 50;
diff --git a/props.c b/props.c
index cddbd9272fe4..d6249f53887d 100644
--- a/props.c
+++ b/props.c
@@ -188,10 +188,13 @@ out:
 
 const struct prop_handler prop_handlers[] = {
 	{"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
-	 prop_read_only},
+	 NULL, NULL, NULL},
+
 	{"label", "Set/get label of device.", 0,
-	 prop_object_dev | prop_object_root, prop_label},
+	 prop_object_dev | prop_object_root, NULL, NULL, NULL},
+
 	{"compression", "Set/get compression for a file or directory", 0,
-	 prop_object_inode, prop_compression},
-	{NULL, NULL, 0, 0, NULL}
+	 prop_object_inode, NULL, NULL, NULL},
+
+	{NULL, NULL, 0, 0, NULL, NULL, NULL}
 };
diff --git a/props.h b/props.h
index a43cb2537f37..fd5796bff09d 100644
--- a/props.h
+++ b/props.h
@@ -17,6 +17,8 @@
 #ifndef __BTRFS_PROPS_H__
 #define __BTRFS_PROPS_H__
 
+#include <unistd.h>
+
 enum prop_object_type {
 	prop_object_dev		= (1 << 0),
 	prop_object_root	= (1 << 1),
@@ -25,17 +27,26 @@ enum prop_object_type {
 	__prop_object_max,
 };
 
-typedef int (*prop_handler_t)(enum prop_object_type type,
+typedef int (*prop_setter_t)(enum prop_object_type type,
+			     const char *object,
+			     const char *name,
+			     const char *value);
+typedef ssize_t (*prop_getter_t)(enum prop_object_type type,
+				 const char *object,
+				 const char *name,
+				 char *value);
+typedef int (*prop_printer_t)(enum prop_object_type type,
 			      const char *object,
-			      const char *name,
-			      const char *value);
+			      const char *name);
 
 struct prop_handler {
 	const char *name;
 	const char *desc;
 	int read_only;
 	int types;
-	prop_handler_t handler;
+	prop_setter_t setter;
+	prop_getter_t getter;
+	prop_printer_t printer;
 };
 
 extern const struct prop_handler prop_handlers[];
-- 
2.15.0




  reply	other threads:[~2017-11-02  3:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
2017-11-02  3:23 ` Su Yue [this message]
2017-11-02  3:23 ` [PATCH 2/5] btrfs-progs: property: set/get/print ro property Su Yue
2017-11-02  3:23 ` [PATCH 3/5] btrfs-progs: property: set/get/print label property Su Yue
2017-11-02  3:23 ` [PATCH 4/5] btrfs-progs: property: set/get/print compression property Su Yue
2017-11-02  3:23 ` [PATCH 5/5] btrfs-progs: fi defrag: extend -c to drop nocompress flag on files Su Yue
2017-11-02  3:44 ` [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
2017-11-02  4:13   ` Wang Shilong
2017-11-02  5:29     ` Su Yue

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171102032320.12537-2-suy.fnst@cn.fujitsu.com \
    --to=suy.fnst@cn.fujitsu.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).