btrfs-progs: cmds: subvolume: add -p option on creating subvol
Subsystems (2)
PATCH v2 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.
---
v2: fixed the case using realpath() that path nonexists, added description on docs.
--- Documentation/btrfs-subvolume.rst | 3 +++ cmds/subvolume.c | 34 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-)
Documentation/btrfs-subvolume.rst +3 -0
@@ -61,6 +61,9 @@ create [-i <qgroupid>] [<dest>/]<name> Add the newly created subvolume to a qgroup. This option can be given multiple times. + -p + Make any missing parent directories for each argument. + delete [options] [<subvolume> [<subvolume>...]], delete -i|--subvolid <subvolid> <path> Delete the subvolume(s) from the filesystem.
cmds/subvolume.c +33 -1
@@ -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,33 @@ static int cmd_subvol_create(const struct cmd_struct *cmd, goto out; } +if (create_parents) { +char p[PATH_MAX ] = {0 }; +char dstdir_dup[PATH_MAX ]; +char *token; + +strcpy (dstdir_dup, dstdir); +if (dstdir_dup[0 ] =='/' ) +strcat (p,"/" ); + + token =strtok (dstdir_dup,"/" ); +while (token) { +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; + } +strcat (p,"/" ); + token =strtok (NULL ,"/" ); + } + } + fddst =btrfs_open_dir (dstdir, &dirstream,1 );if (fddst <0 ) goto out;
Signature · 1 line
2.25.1
Quoted 64 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.
Signed-off-by Sidong Yang <[email protected]>---
v2: fixed the case usingrealpath()that path nonexists, added description on docs.--- Documentation/btrfs-subvolume.rst | 3 +++ cmds/subvolume.c | 34 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-)Documentation/btrfs-subvolume.rst +3
-0@@ -61,6 +61,9 @@ create [-i <qgroupid>] [<dest>/]<name> Add the newly created subvolume to a qgroup. This option can be given multiple times. + -p + Make any missing parent directories for each argument. + delete [options] [<subvolume> [<subvolume>...]], delete -i|--subvolid <subvolid> <path> Delete the subvolume(s) from the filesystem.cmds/subvolume.c +6
-1@@ -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); }
cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 176
Quoted 5 lines
cmds/subvolume.c +2
-0@@ -167,6 +172,33 @@ static int cmd_subvol_create(const struct cmd_struct *cmd, goto out; } +if (create_parents) { +char p[PATH_MAX ] = {0 };
I'm not a fan of single letter variable names, especially for substantive objects such as large character arrays (as opposed to a pointer keeping track of a position in some other object). But maybe that is just me.
cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 180
Quoted 4 lines
cmds/subvolume.c +4
-0+char dstdir_dup[PATH_MAX ]; +char *token; + +strcpy (dstdir_dup, dstdir);
Has dstdir already been checked its length is PATH_MAX-1 or less? Wouldn't it be safer to at least use strncpy plus overwriting the final byte with zero?
cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 189
Quoted 9 lines
cmds/subvolume.c +9
-0+if (dstdir_dup[0 ] =='/' ) +strcat (p,"/" ); + + token =strtok (dstdir_dup,"/" ); +while (token) { +strcat (p, token); + res =path_is_dir (p); +if (res == -ENOENT ) { +if (mkdir (p,0755 )) {
Why 0755? Personally I make use of groups and run with umask 0002. Why not just use 0777? I realise this is unlikely to be significantly important but I dislike programs overriding the user's choice of umask. I assume the requestor's intention was that the behaviour should be the same as "mkdir -p".
Graham
Quoted 15 lines
cmds/subvolume.c +12
-0+error ("failed to make dir: %s" , p); + goto out; + } + }else if (res <=0 ) { +error ("failed to check dir: %s" , p); + goto out; + } +strcat (p,"/" ); + token =strtok (NULL ,"/" ); + } + } + fddst =btrfs_open_dir (dstdir, &dirstream,1 );if (fddst <0 ) goto out;
On Sat, Feb 19, 2022 at 04:38:03PM +0000, Graham Cobb wrote:
Hi, Cobb.
Thanks for review.
Quoted 68 lines · Sidong Yang
Quoted 67 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.
Signed-off-by Sidong Yang <[email protected]>---
v2: fixed the case usingrealpath()that path nonexists, added description on docs.--- Documentation/btrfs-subvolume.rst | 3 +++ cmds/subvolume.c | 34 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-)Documentation/btrfs-subvolume.rst +3
-0@@ -61,6 +61,9 @@ create [-i <qgroupid>] [<dest>/]<name> Add the newly created subvolume to a qgroup. This option can be given multiple times. + -p + Make any missing parent directories for each argument. + delete [options] [<subvolume> [<subvolume>...]], delete -i|--subvolid <subvolid> <path> Delete the subvolume(s) from the filesystem.cmds/subvolume.c +6
-1@@ -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,33 @@ static int cmd_subvol_create(const struct cmd_struct *cmd, goto out; }
Quoted 8 lines
cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 176
Quoted 2 lines
cmds/subvolume.c +2
-0+if (create_parents) { +char p[PATH_MAX ] = {0 };I'm not a fan of single letter variable names, especially for substantive objects such as large character arrays (as opposed to a pointer keeping track of a position in some other object). But maybe that is just me.
I think it would be better that adding longopt "--parents" and it seems that requestor wants it.
Quoted 9 lines
cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 180
Quoted 4 lines
cmds/subvolume.c +4
-0+char dstdir_dup[PATH_MAX ]; +char *token; + +strcpy (dstdir_dup, dstdir);Has dstdir already been checked its length is PATH_MAX-1 or less? Wouldn't it be safer to at least use strncpy plus overwriting the final byte with zero?
Agree, It's not guaranteed that strlen(dstdir) is less than PATH_MAX. strncpy() is a safer way than just strcpy().
Quoted 7 lines
Quoted 7 lines
cmds/subvolume.c +7
-0+if (dstdir_dup[0 ] =='/' ) +strcat (p,"/" ); + + token =strtok (dstdir_dup,"/" ); +while (token) { +strcat (p, token); + res =path_is_dir (p);
Quoted 8 lines
cmds/subvolume.c static int cmd_subvol_create(const struct cmd_struct *cmd, · line 189
Quoted 2 lines
cmds/subvolume.c +2
-0+if (res == -ENOENT ) { +if (mkdir (p,0755 )) {Why 0755? Personally I make use of groups and run with umask 0002. Why not just use 0777? I realise this is unlikely to be significantly important but I dislike programs overriding the user's choice of umask. I assume the requestor's intention was that the behaviour should be the same as "mkdir -p".
Thanks, I didn't know that mode parameter in mkdir() works in (mode & ~umask & 0777). Now I realize that using 0777 is correct way to follow user's choice. I'll fix this for next version.
Thanks,
Sidong
Quoted 19 lines
Graham
Quoted 15 lines
cmds/subvolume.c +12
-0+error ("failed to make dir: %s" , p); + goto out; + } + }else if (res <=0 ) { +error ("failed to check dir: %s" , p); + goto out; + } +strcat (p,"/" ); + token =strtok (NULL ,"/" ); + } + } + fddst =btrfs_open_dir (dstdir, &dirstream,1 );if (fddst <0 ) goto out;