From patchwork Thu Nov 2 03:23:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Su Yue X-Patchwork-Id: 10037899 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 38687603B5 for ; Thu, 2 Nov 2017 03:20:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F75728D25 for ; Thu, 2 Nov 2017 03:20:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2493828D3A; Thu, 2 Nov 2017 03:20:03 +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 B37CA28D37 for ; Thu, 2 Nov 2017 03:20:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934200AbdKBDUB (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 S934040AbdKBDT6 (ORCPT ); Wed, 1 Nov 2017 23:19:58 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="29831585" 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 569A2482CFF0 for ; Thu, 2 Nov 2017 11:19:57 +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:55 +0800 From: Su Yue To: CC: Subject: [PATCH 3/5] btrfs-progs: property: set/get/print label property Date: Thu, 2 Nov 2017 11:23:18 +0800 Message-ID: <20171102032320.12537-4-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: 569A2482CFF0.AD65C 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_label(), get_prop_label(), print_prop_label() to set/get/print label. Signed-off-by: Su Yue --- props.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/props.c b/props.c index da69f6e9314c..28a81b6bdc81 100644 --- a/props.c +++ b/props.c @@ -145,23 +145,68 @@ out: return ret; } -static int prop_label(enum prop_object_type type, - const char *object, - const char *name, - const char *value) +static ssize_t prop_label(enum prop_object_type type, const char *object, + const char *name, char *value, enum prop_operation operation) { - int ret; + ssize_t ret; - if (value) { + if (operation == set_prop) { ret = set_label((char *) object, (char *) value); } else { char label[BTRFS_LABEL_SIZE]; ret = get_label((char *) object, label); - if (!ret) - fprintf(stdout, "label=%s\n", label); + if (ret) + goto out; + + if (!value) + ret = strlen(label); + else + memcpy(value, label, strlen(label)); + } +out: + return ret; +} + +static int set_prop_label(enum prop_object_type type, const char *object, + const char *name, const char *value) +{ + return prop_label(type, object, name, (char *)value, set_prop); +} + +static ssize_t get_prop_label(enum prop_object_type type, const char *object, + const char *name, char *value) +{ + return prop_label(type, object, name, value, get_prop); +} + +static int print_prop_label(enum prop_object_type type, const char *object, + const char *name) +{ + int ret; + ssize_t len; + char *value = NULL; + + len = get_prop_label(type, object, name, NULL); + if (len < 0) { + ret = len; + goto out; } + value = malloc(len + 1); + if (!value) { + ret = -ENOMEM; + goto out; + } + ret = get_prop_label(type, object, name, value); + if (ret) + goto out; + + value[len] = '\0'; + printf("label=%s\n", value); +out: + if (value) + free(value); return ret; } @@ -243,7 +288,8 @@ const struct prop_handler prop_handlers[] = { 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}, + prop_object_dev | prop_object_root, + set_prop_label, get_prop_label, print_prop_label}, {"compression", "Set/get compression for a file or directory", 0, prop_object_inode, NULL, NULL, NULL},