linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Goffredo Baroncelli <kreijack@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Goffredo Baroncelli <kreijack@inwind.it>
Subject: [PATCH 4/7] recursive btrfs subvol delete
Date: Sat, 16 Nov 2013 18:09:04 +0100	[thread overview]
Message-ID: <1384621747-25441-5-git-send-email-kreijack@inwind.it> (raw)
In-Reply-To: <1384621747-25441-1-git-send-email-kreijack@inwind.it>

Add the -R switch to allow to delete recursively a subvolume.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
---
 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 <subvolume> [<subvolume>...]",
-	"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] <subvolume> [<subvolume>...]",
+	"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;
 }
-- 
1.8.4.3


  parent reply	other threads:[~2013-11-16 17:09 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-16 17:09 [PATCH] BTRFS-PROG: recursively subvolume snapshot and delete Goffredo Baroncelli
2013-11-16 17:09 ` [PATCH 1/7] Recursive btrfs sub snapshot/delete: create get_root_info() function Goffredo Baroncelli
2013-11-16 17:09 ` [PATCH 2/7] recursive btrfs sub snapshot/delete: create pathjoin() function Goffredo Baroncelli
2013-11-16 17:09 ` [PATCH 3/7] recursive btrfs snapshot/delete: create traverse_list_subvol_rec() Goffredo Baroncelli
2013-11-16 17:09 ` Goffredo Baroncelli [this message]
2013-11-16 17:09 ` [PATCH 5/7] recursively btrfs subvolume snapshot Goffredo Baroncelli
2013-11-16 17:09 ` [PATCH 6/7] btrfs subvolume snapshot -R: update man page Goffredo Baroncelli
2013-11-16 17:09 ` [PATCH 7/7] Document the -R switch for the "btrfs subvolume delete" command Goffredo Baroncelli
2013-11-25 21:23 ` [PATCH] BTRFS-PROG: recursively subvolume snapshot and delete Goffredo Baroncelli
2013-11-26 15:12   ` Konstantinos Skarlatos
2013-11-26 17:44     ` Goffredo Baroncelli
2013-11-27  9:15       ` Konstantinos Skarlatos
2013-11-27 17:04         ` Goffredo Baroncelli
2013-11-28 18:31 ` David Sterba
2013-11-28 19:23   ` Goffredo Baroncelli
2013-11-29 18:07     ` David Sterba
2013-11-29 19:09       ` Goffredo Baroncelli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1384621747-25441-5-git-send-email-kreijack@inwind.it \
    --to=kreijack@gmail.com \
    --cc=kreijack@inwind.it \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).