From patchwork Thu Nov 8 09:52:41 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao Xie X-Patchwork-Id: 1714761 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 159EEDF280 for ; Thu, 8 Nov 2012 09:52:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755169Ab2KHJwZ (ORCPT ); Thu, 8 Nov 2012 04:52:25 -0500 Received: from cn.fujitsu.com ([222.73.24.84]:48620 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1755143Ab2KHJwZ (ORCPT ); Thu, 8 Nov 2012 04:52:25 -0500 X-IronPort-AV: E=Sophos;i="4.80,737,1344182400"; d="scan'208";a="6161425" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 08 Nov 2012 17:50:46 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id qA89qNDR003587; Thu, 8 Nov 2012 17:52:23 +0800 Received: from [10.167.225.199] ([10.167.225.199]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2012110817522806-1024219 ; Thu, 8 Nov 2012 17:52:28 +0800 Message-ID: <509B80E9.7070703@cn.fujitsu.com> Date: Thu, 08 Nov 2012 17:52:41 +0800 From: Miao Xie Reply-To: miaox@cn.fujitsu.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121016 Thunderbird/16.0.1 MIME-Version: 1.0 To: Linux Btrfs CC: Arne Jansen , wangshilong Subject: [PATCH 2/3] Btrfs-progs: clean up reduplicate parse_qgroupid() and replace atoi with strtoull References: <509B7B13.2030505@cn.fujitsu.com> In-Reply-To: <509B7B13.2030505@cn.fujitsu.com> X-Forwarded-Message-Id: <509B7B13.2030505@cn.fujitsu.com> X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/11/08 17:52:28, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2012/11/08 17:52:28, Serialize complete at 2012/11/08 17:52:28 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Wang Shilong 1. parse_qgroupid() is implemented twice, clean up the reduplicate code. 2. atoi() can not detect errors, so use strtoull() instead of it. Signed-off-by: Wang Shilong Signed-off-by: Miao Xie --- cmds-qgroup.c | 15 +-------------- qgroup.c | 22 ++++++++++++++++++---- qgroup.h | 2 ++ 3 files changed, 21 insertions(+), 18 deletions(-) -- 1.7.7.6 -- 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 --git a/cmds-qgroup.c b/cmds-qgroup.c index 129a4f0..c4122bf 100644 --- a/cmds-qgroup.c +++ b/cmds-qgroup.c @@ -24,26 +24,13 @@ #include "ioctl.h" #include "commands.h" +#include "qgroup.h" static const char * const qgroup_cmd_group_usage[] = { "btrfs qgroup [options] ", NULL }; -static u64 parse_qgroupid(char *p) -{ - char *s = strchr(p, '/'); - u64 level; - u64 id; - - if (!s) - return atoll(p); - level = atoll(p); - id = atoll(s + 1); - - return (level << 48) | id; -} - static int qgroup_assign(int assign, int argc, char **argv) { int ret = 0; diff --git a/qgroup.c b/qgroup.c index 4083b57..dafde12 100644 --- a/qgroup.c +++ b/qgroup.c @@ -22,15 +22,29 @@ u64 parse_qgroupid(char *p) { char *s = strchr(p, '/'); + char *ptr_src_end = p + strlen(p); + char *ptr_parse_end = NULL; u64 level; u64 id; - if (!s) - return atoll(p); - level = atoll(p); - id = atoll(s + 1); + if (!s) { + id = strtoull(p, &ptr_parse_end, 10); + if (ptr_parse_end != ptr_src_end) + goto err; + return id; + } + level = strtoull(p, &ptr_parse_end, 10); + if (ptr_parse_end != s) + goto err; + + id = strtoull(s+1, &ptr_parse_end, 10); + if (ptr_parse_end != ptr_src_end) + goto err; return (level << 48) | id; +err: + fprintf(stderr, "ERROR:invalid qgroupid\n"); + exit(-1); } int qgroup_inherit_size(struct btrfs_qgroup_inherit *p) diff --git a/qgroup.h b/qgroup.h index f7af8c5..ad14c88 100644 --- a/qgroup.h +++ b/qgroup.h @@ -20,7 +20,9 @@ #define _BTRFS_QGROUP_H #include "ioctl.h" +#include "kerncompat.h" +u64 parse_qgroupid(char *p); int qgroup_inherit_size(struct btrfs_qgroup_inherit *p); int qgroup_inherit_realloc(struct btrfs_qgroup_inherit **inherit, int incgroups, int inccopies);