From patchwork Sat Nov 16 17:09:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 3192981 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 76471C045B for ; Sat, 16 Nov 2013 17:09:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 914E3208BD for ; Sat, 16 Nov 2013 17:09:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AA118207BF for ; Sat, 16 Nov 2013 17:09:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752483Ab3KPRJj (ORCPT ); Sat, 16 Nov 2013 12:09:39 -0500 Received: from mail-we0-f179.google.com ([74.125.82.179]:41480 "EHLO mail-we0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751248Ab3KPRJQ (ORCPT ); Sat, 16 Nov 2013 12:09:16 -0500 Received: by mail-we0-f179.google.com with SMTP id q59so2602835wes.38 for ; Sat, 16 Nov 2013 09:09:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hhdkSbwBGXLh99pxSTx/yv7HjeLymou7Roqo91vTGlQ=; b=OU1uyuMvWjcuIHvCkFeqE559YVcNuk4rwUJVrcrY6Fs9CyLxArKmj3DR/CXgdnOsRK D4zAh+esilPxCI0Jxtz2XGQ/i4EbWfx0SM9tSFzIGmq1fUymVPTcYLcDxMUhS7zBFYIp 0tYi2kCB5vDoJUz3attMqHnZ7vw2yvfBPYIhZo6a3BiE3CrkmacaqYepsZoGFvpGlh0C uhG90i2yLpJUb5K0HD4FcPxXGXtWxxGzcnUJcz4SOxDWkfQZsdXWVx+3Mu0m3G6fV5g3 /EoYaPEFd7+M454/Ej8VpLp8+HCgRQKP71fFxJeOEWsxMoHRGKIl63dinaPoB0Kb3qqG qz6g== X-Received: by 10.180.221.38 with SMTP id qb6mr11070797wic.8.1384621755217; Sat, 16 Nov 2013 09:09:15 -0800 (PST) Received: from venice.bhome (ppp-80-52.24-151.libero.it. [151.24.52.80]) by mx.google.com with ESMTPSA id e1sm6584799wij.6.2013.11.16.09.09.14 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Nov 2013 09:09:15 -0800 (PST) From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 4/7] recursive btrfs subvol delete Date: Sat, 16 Nov 2013 18:09:04 +0100 Message-Id: <1384621747-25441-5-git-send-email-kreijack@inwind.it> X-Mailer: git-send-email 1.8.4.3 In-Reply-To: <1384621747-25441-1-git-send-email-kreijack@inwind.it> References: <1384621747-25441-1-git-send-email-kreijack@inwind.it> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the -R switch to allow to delete recursively a subvolume. Signed-off-by: Goffredo Baroncelli --- cmds-subvolume.c | 107 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 18 deletions(-) diff --git a/cmds-subvolume.c b/cmds-subvolume.c index 477919c..422e1fc 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -201,28 +201,15 @@ int test_issubvolume(char *path) return (st.st_ino == 256) && S_ISDIR(st.st_mode); } -static const char * const cmd_subvol_delete_usage[] = { - "btrfs subvolume delete [...]", - "Delete subvolume(s)", - NULL -}; - -static int cmd_subvol_delete(int argc, char **argv) +static int do_subvol_delete(char *path) { - int res, fd, len, e, cnt = 1, ret = 0; + int res, fd, len, e, ret = 0; struct btrfs_ioctl_vol_args args; char *dname, *vname, *cpath; char *dupdname = NULL; char *dupvname = NULL; - char *path; DIR *dirstream = NULL; - if (argc < 2) - usage(cmd_subvol_delete_usage); - -again: - path = argv[cnt]; - res = test_issubvolume(path); if (res < 0) { fprintf(stderr, "ERROR: error accessing '%s'\n", path); @@ -290,9 +277,93 @@ out: free(dupvname); dupdname = NULL; dupvname = NULL; - cnt++; - if (cnt < argc) - goto again; + + return ret; +} + +static int do_subvol_delete_func(char *real_root, char *relative_root, + char *path, void *data) +{ + char *dpath, *real_path; + int ret; + (void)data; /* ignore the parameter */ + (void)real_root; /* ignore the parameter */ + + dpath = pathjoin(relative_root, path, NULL); + if (!dpath) { + fprintf(stderr, "ERROR: not enough memory\n"); + return -1; + } + real_path = realpath(dpath, NULL); + if (!real_path) { + free(dpath); + fprintf(stderr, "ERROR: not enough memory\n"); + return -1; + } + + ret = do_subvol_delete(real_path); + free(real_path); + free(dpath); + + return ret; +} + +static inline int do_subvol_delete_rec(char *path) +{ + return traverse_list_subvol_rec(path, /* filesystem subvolume */ + 0, /* false = only below this path */ + 1, /* ascending order */ + do_subvol_delete_func, /* func to call */ + NULL); /* parameter to pass to func */ +} + +static const char * const cmd_subvol_delete_usage[] = { + "btrfs subvolume delete [-R] [...]", + "Delete subvolume(s)", + "", + "-R delete recursively", + NULL +}; + +static int cmd_subvol_delete(int argc, char **argv) +{ + int rec = 0, ret = 0, i; + + optind = 1; + while(1) { + int c; + c = getopt_long(argc, argv, "R", NULL, NULL); + if (c < 0) + break; + + switch(c) { + case 'R': + rec = 1; + break; + default: + ret = 2; + goto out; + } + } + + if (check_argc_min(argc - optind, 1)) { + usage(cmd_subvol_delete_usage); + ret = 1; + goto out; + } + + for ( i = optind ; i < argc ; i++ ) { + + if (rec) + ret = do_subvol_delete_rec(argv[i]); + else + ret = do_subvol_delete(argv[i]); + + if (ret) + break; + + } +out: return ret; }