Skip to contentSkip to conversation

btrfs-progs: cmds: subvolume: add -p option on creating subvol

PATCH Superseded Sidong Yang 2 participants 3 messages Feb 19, 2022 Btrfs file system

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: cmds: subvolume: add -p option on creating subvol

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.

---
 cmds/subvolume.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
cmds/subvolume.c +30 -1
index fbf56566..1070c74a 100644
@@ -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;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 15 lines · Sidong Yang

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.

---
  cmds/subvolume.c | 31 ++++++++++++++++++++++++++++++-
  1 file changed, 30 insertions(+), 1 deletion(-)
cmds/subvolume.c +0 -0
index fbf56566..1070c74a 100644

cmds/subvolume.c static const char * const cmd_subvol_create_usage[] = { · line 91

Quoted 4 lines
cmds/subvolume.c +1 -0
@@ -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",

you should update the man page too

Quoted 3 lines
cmds/subvolume.c +0 -0
  	HELPINFO_INSERT_GLOBALS,
  	HELPINFO_INSERT_QUIET,
  	NULL

[...]

cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 180

Quoted 6 lines
cmds/subvolume.c +6 -0
+	if (create_parents) {
+		char p[PATH_MAX];
+		char dstdir_real[PATH_MAX];
+		char *token;
+
+		realpath(dstdir, dstdir_real);

realpath(3) behaves strangely when the path doesn't exist: if exists only /tmp, realpath("/tmp/1/2/3/4/5") returns "/tmp/1" discarding 2/3/4.... In fact my gcc warns:

$ make
    [CC]     cmds/subvolume.o
cmds/subvolume.c: In function ‘cmd_subvol_create’:
cmds/subvolume.c:180:17: warning: ignoring return value of ‘realpath’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  180 |                 realpath(dstdir, dstdir_real);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

because realpath(3) returns an error if the path doesn't exist.

This causes:

# only /tmp/ exists
btrfs sub cre -p /tmp/1/2/3/4/5/6
ERROR: cannot access '/tmp/1/2/3/4/5': No such file or directory

cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 183

Quoted 3 lines
cmds/subvolume.c +3 -0
+		token = strtok(dstdir_real, "/");
+		while(token) {
+			strcat(p, "/");

where is initialized p ?

Unfortunately it works and the gcc doesn't warn, but I think only because in the stack there are zeros where p is allocated; but this is not guarantee.

Quoted 18 lines
cmds/subvolume.c +15 -0
+			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;
Signature · 2 lines
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it>
Key fingerprint BBF5 1610 0B64 DAC6 5F7D  17B2 0EDA 9B37 8B82 E0B5
#2 lore raw

On Sat, Feb 19, 2022 at 03:11:36PM +0100, Goffredo Baroncelli wrote:

Hi, Thanks for review.

Quoted 11 lines · Sidong Yang
Quoted 10 lines

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.

---
  cmds/subvolume.c | 31 ++++++++++++++++++++++++++++++-
  1 file changed, 30 insertions(+), 1 deletion(-)
Quoted 11 lines

cmds/subvolume.c static const char * const cmd_subvol_create_usage[] = { · line 91

Quoted 9 lines
cmds/subvolume.c +1 -0
index fbf56566..1070c74a 100644
@@ -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",

you should update the man page too

Thanks I'll do it for next version.

Quoted 24 lines
Quoted 4 lines
cmds/subvolume.c +0 -0
  	HELPINFO_INSERT_GLOBALS,
  	HELPINFO_INSERT_QUIET,
  	NULL

[...]

cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 180

Quoted 6 lines
cmds/subvolume.c +6 -0
+	if (create_parents) {
+		char p[PATH_MAX];
+		char dstdir_real[PATH_MAX];
+		char *token;
+
+		realpath(dstdir, dstdir_real);

realpath(3) behaves strangely when the path doesn't exist: if exists only /tmp, realpath("/tmp/1/2/3/4/5") returns "/tmp/1" discarding 2/3/4.... In fact my gcc warns:

$ make
    [CC]     cmds/subvolume.o
cmds/subvolume.c: In function ‘cmd_subvol_create’:
cmds/subvolume.c:180:17: warning: ignoring return value of ‘realpath’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  180 |                 realpath(dstdir, dstdir_real);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

because realpath(3) returns an error if the path doesn't exist.

This causes:

# only /tmp/ exists
btrfs sub cre -p /tmp/1/2/3/4/5/6
ERROR: cannot access '/tmp/1/2/3/4/5': No such file or directory

Yes, It's completely wrong code. I'll correct it. For now, I think I need to check whether the path is absolute or relative for checking dstdir[0] == '/'.

Quoted 7 lines

cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 183

Quoted 5 lines
cmds/subvolume.c +3 -0
+		token = strtok(dstdir_real, "/");
+		while(token) {
+			strcat(p, "/");

where is initialized p ?

I missed it. Thanks.

Quoted 26 lines

Unfortunately it works and the gcc doesn't warn, but I think only because in the stack there are zeros where p is allocated; but this is not guarantee.

Quoted 18 lines
cmds/subvolume.c +15 -0
+			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;
Signature · 2 lines
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it>
Key fingerprint BBF5 1610 0B64 DAC6 5F7D  17B2 0EDA 9B37 8B82 E0B5