From: Sidong Yang <realwakka@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Sidong Yang <realwakka@gmail.com>
Subject: [PATCH] btrfs-progs: cmds: subvolume: add -p option on creating subvol
Date: Sat, 19 Feb 2022 09:01:23 +0000 [thread overview]
Message-ID: <20220219090123.51185-1-realwakka@gmail.com> (raw)
This patch resolves github issue #429. This patch adds an option that
create parent directories when it creates subvolumes on nonexisting
parents. This option tokenizes dstdir and checks each paths whether
it's existing directory and make directory for creating subvolume.
Signed-off-by: Sidong Yang <realwakka@gmail.com>
---
cmds/subvolume.c | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/cmds/subvolume.c b/cmds/subvolume.c
index fbf56566..1070c74a 100644
--- a/cmds/subvolume.c
+++ b/cmds/subvolume.c
@@ -88,6 +88,7 @@ static const char * const cmd_subvol_create_usage[] = {
"",
"-i <qgroupid> add the newly created subvolume to a qgroup. This",
" option can be given multiple times.",
+ "-p make any missing parent directories for each argument",
HELPINFO_INSERT_GLOBALS,
HELPINFO_INSERT_QUIET,
NULL
@@ -105,10 +106,11 @@ static int cmd_subvol_create(const struct cmd_struct *cmd,
char *dst;
struct btrfs_qgroup_inherit *inherit = NULL;
DIR *dirstream = NULL;
+ bool create_parents = false;
optind = 0;
while (1) {
- int c = getopt(argc, argv, "c:i:");
+ int c = getopt(argc, argv, "c:i:p");
if (c < 0)
break;
@@ -127,6 +129,9 @@ static int cmd_subvol_create(const struct cmd_struct *cmd,
goto out;
}
break;
+ case 'p':
+ create_parents = true;
+ break;
default:
usage_unknown_option(cmd, argv);
}
@@ -167,6 +172,30 @@ static int cmd_subvol_create(const struct cmd_struct *cmd,
goto out;
}
+ if (create_parents) {
+ char p[PATH_MAX];
+ char dstdir_real[PATH_MAX];
+ char *token;
+
+ realpath(dstdir, dstdir_real);
+ token = strtok(dstdir_real, "/");
+ while(token) {
+ strcat(p, "/");
+ strcat(p, token);
+ res = path_is_dir(p);
+ if (res == -ENOENT) {
+ if (mkdir(p, 0755)) {
+ error("failed to make dir: %s", p);
+ goto out;
+ }
+ } else if (res <= 0) {
+ error("failed to check dir: %s", p);
+ goto out;
+ }
+ token = strtok(NULL, "/");
+ }
+ }
+
fddst = btrfs_open_dir(dstdir, &dirstream, 1);
if (fddst < 0)
goto out;
--
2.25.1
next reply other threads:[~2022-02-19 9:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-19 9:01 Sidong Yang [this message]
2022-02-19 14:11 ` [PATCH] btrfs-progs: cmds: subvolume: add -p option on creating subvol Goffredo Baroncelli
2022-02-19 14:40 ` Sidong Yang
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=20220219090123.51185-1-realwakka@gmail.com \
--to=realwakka@gmail.com \
--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