From patchwork Mon Jun 18 08:40:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Misono Tomohiro X-Patchwork-Id: 10470181 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 0BD766029B for ; Mon, 18 Jun 2018 08:41:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF59C2899F for ; Mon, 18 Jun 2018 08:41:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E28F3289AD; Mon, 18 Jun 2018 08:41:11 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, 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 80ACD28995 for ; Mon, 18 Jun 2018 08:41:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S968220AbeFRIiU (ORCPT ); Mon, 18 Jun 2018 04:38:20 -0400 Received: from mgwkm02.jp.fujitsu.com ([202.219.69.169]:30074 "EHLO mgwkm02.jp.fujitsu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968202AbeFRIiR (ORCPT ); Mon, 18 Jun 2018 04:38:17 -0400 Received: from kw-mxq.gw.nic.fujitsu.com (unknown [192.168.231.130]) by mgwkm02.jp.fujitsu.com with smtp id 2a8d_9a7b_b2a17853_f193_4f31_8f5f_d3036ebac839; Mon, 18 Jun 2018 17:38:10 +0900 Received: from g01jpfmpwkw01.exch.g01.fujitsu.local (g01jpfmpwkw01.exch.g01.fujitsu.local [10.0.193.38]) by kw-mxq.gw.nic.fujitsu.com (Postfix) with ESMTP id CDBD7AC011E for ; Mon, 18 Jun 2018 17:38:09 +0900 (JST) Received: from g01jpexchkw33.g01.fujitsu.local (unknown [10.0.193.4]) by g01jpfmpwkw01.exch.g01.fujitsu.local (Postfix) with ESMTP id 03DC66925F3 for ; Mon, 18 Jun 2018 17:38:09 +0900 (JST) Received: from luna3.soft.fujitsu.com (10.124.196.199) by g01jpexchkw33.g01.fujitsu.local (10.0.193.36) with Microsoft SMTP Server id 14.3.352.0; Mon, 18 Jun 2018 17:38:07 +0900 From: Misono Tomohiro To: Subject: [PATCH v2 08/20] btrfs-progs: sub list: factor out main part of btrfs_list_subvols Date: Mon, 18 Jun 2018 17:40:56 +0900 Message-ID: X-Mailer: git-send-email 2.14.4 In-Reply-To: References: MIME-Version: 1.0 X-SecurityPolicyCheck-GC: OK by FENCE-Mail X-TM-AS-MML: disable 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 No functional changes. This is a preparation work for reworking "subvolume list". Signed-off-by: Misono Tomohiro --- cmds-subvolume.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/cmds-subvolume.c b/cmds-subvolume.c index c54a8003..279a6e51 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -1125,22 +1125,15 @@ out: return subvols; } -static struct subvol_list *btrfs_list_subvols(int fd, - struct btrfs_list_filter_set_v2 *filter_set) +static void get_subvols_info(struct subvol_list **subvols, + struct btrfs_list_filter_set_v2 *filter_set, + int fd, + size_t *capacity) { - struct subvol_list *subvols; - size_t capacity = 0; struct btrfs_util_subvolume_iterator *iter; enum btrfs_util_error err; int ret = -1; - subvols = malloc(sizeof(*subvols)); - if (!subvols) { - error("out of memory"); - return NULL; - } - subvols->num = 0; - err = btrfs_util_create_subvolume_iterator_fd(fd, BTRFS_FS_TREE_OBJECTID, 0, &iter); @@ -1168,11 +1161,11 @@ static struct subvol_list *btrfs_list_subvols(int fd, continue; } - if (subvols->num >= capacity) { + if ((*subvols)->num >= *capacity) { struct subvol_list *new_subvols; - size_t new_capacity = max_t(size_t, 1, capacity * 2); + size_t new_capacity = max_t(size_t, 1, *capacity * 2); - new_subvols = realloc(subvols, + new_subvols = realloc(*subvols, sizeof(*new_subvols) + new_capacity * sizeof(new_subvols->subvols[0])); @@ -1181,12 +1174,12 @@ static struct subvol_list *btrfs_list_subvols(int fd, goto out; } - subvols = new_subvols; - capacity = new_capacity; + *subvols = new_subvols; + *capacity = new_capacity; } - subvols->subvols[subvols->num] = subvol; - subvols->num++; + (*subvols)->subvols[(*subvols)->num] = subvol; + (*subvols)->num++; } ret = 0; @@ -1194,9 +1187,26 @@ out: if (iter) btrfs_util_destroy_subvolume_iterator(iter); if (ret) { - free_subvol_list(subvols); - subvols = NULL; + free_subvol_list(*subvols); + *subvols = NULL; + } +} + +static struct subvol_list *btrfs_list_subvols(int fd, + struct btrfs_list_filter_set_v2 *filter_set) +{ + struct subvol_list *subvols; + size_t capacity = 0; + + subvols = malloc(sizeof(*subvols)); + if (!subvols) { + error("out of memory"); + return NULL; } + subvols->num = 0; + + get_subvols_info(&subvols, filter_set, fd, &capacity); + return subvols; }