From patchwork Thu Nov 2 03:23:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Su Yue X-Patchwork-Id: 10037901 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 05867603B5 for ; Thu, 2 Nov 2017 03:20:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2094B28D37 for ; Thu, 2 Nov 2017 03:20:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 155FC28D3A; Thu, 2 Nov 2017 03:20:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B00BA28D37 for ; Thu, 2 Nov 2017 03:20:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934204AbdKBDUB (ORCPT ); Wed, 1 Nov 2017 23:20:01 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:20531 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752397AbdKBDT7 (ORCPT ); Wed, 1 Nov 2017 23:19:59 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="29831584" Received: from localhost (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 02 Nov 2017 11:19:56 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id D7FE8482CFEE for ; Thu, 2 Nov 2017 11:19:55 +0800 (CST) Received: from archlinux.g08.fujitsu.local (10.167.226.129) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.361.1; Thu, 2 Nov 2017 11:19:54 +0800 From: Su Yue To: CC: Subject: [PATCH 2/5] btrfs-progs: property: set/get/print ro property Date: Thu, 2 Nov 2017 11:23:17 +0800 Message-ID: <20171102032320.12537-3-suy.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171102032320.12537-1-suy.fnst@cn.fujitsu.com> References: <20171102032320.12537-1-suy.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.129] X-yoursite-MailScanner-ID: D7FE8482CFEE.AF765 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: suy.fnst@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- props.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 10 deletions(-) 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},