erofs-utils: fsck: add --xattr option
PATCH 1/1 erofs-utils: fsck: add --xattr option
The current fsck --extract does not support exporting the extended attributes of files. This patch adds --xattr option to dump the extended attributes.
--- fsck/main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
fsck/main.c +61 -0
@@ -9,6 +9,7 @@#include <utime.h> #include <unistd.h> #include <sys/stat.h> +#include <sys/xattr.h> #include "erofs/print.h" #include "erofs/compress.h" #include "erofs/decompress.h"
@@ -31,6 +32,7 @@ struct erofsfsck_cfg {bool overwrite;bool preserve_owner;bool preserve_perms; +bool xattr; };static struct erofsfsck_cfg fsckcfg;
@@ -48,6 +50,7 @@ static struct option long_options[] = { {"no-preserve-owner", no_argument, 0, 10}, {"no-preserve-perms", no_argument, 0, 11}, {"offset", required_argument, 0, 12}, + {"xattr", no_argument, 0, 13}, {0, 0, 0, 0}, };
@@ -98,6 +101,7 @@ static void usage(int argc, char **argv)" --extract[=X] check if all files are well encoded, optionally\n" " extract to X\n" " --offset=# skip # bytes at the beginning of IMAGE\n" +" --xattr dump extended attributes\n" "\n" " -a, -A, -y no-op, for compatibility with fsck of other filesystems\n" "\n"
@@ -225,6 +229,9 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)return -EINVAL ; }break ; +case 13 : + fsckcfg.xattr = true; +break ;default :return -EINVAL ; }
@@ -411,6 +418,53 @@ out:return ret; } +static int erofs_dump_xattr (struct erofs_inode *inode) +{ +char *keylst, *key; +ssize_t kllen; +int ret; + +if (!fsckcfg.extract_path) +return 0 ; + kllen =erofs_listxattr (inode,NULL ,0 ); +if (kllen <=0 ) +return kllen; + keylst =malloc (kllen); +if (!keylst) +return -ENOMEM ; + ret =erofs_listxattr (inode, keylst, kllen); +if (ret <0 ) + goto out; +for (key = keylst; key < keylst + kllen; key +=strlen (key) +1 ) { +void *value =NULL ; +size_t size =0 ; + + ret =erofs_getxattr (inode, key,NULL ,0 ); +if (ret <0 ) + goto out; +if (ret) { + size = ret; + value =malloc (size); +if (!value) { + ret = -ENOMEM ; + goto out; + } + ret =erofs_getxattr (inode, key, value, size); +if (ret <0 ) { +free (value); + goto out; + } + ret =setxattr (fsckcfg.extract_path, key, value, size,0 ); +free (value); +if (ret) + goto out; + } + } +out: +free (keylst); +return ret; +} +static int erofs_verify_inode_data (struct erofs_inode *inode,int outfd) {struct erofs_map_blocks map = {
@@ -909,6 +963,12 @@ static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid)if (ret && ret != -ECANCELED ) goto out; +if (fsckcfg.xattr) { + ret =erofs_dump_xattr (&inode); +if (ret) +return ret; + } +/* XXXX: the dir depth should be restricted in order to avoid loops */ if (S_ISDIR (inode.i_mode)) {struct erofs_dir_context ctx = {
@@ -955,6 +1015,7 @@ int main(int argc, char *argv[]) fsckcfg.overwrite = false; fsckcfg.preserve_owner = fsckcfg.superuser; fsckcfg.preserve_perms = fsckcfg.superuser; + fsckcfg.xattr = false; err =erofsfsck_parse_options_cfg (argc, argv);if (err) {
Signature · 1 line
2.43.5
Quoted 3 lines · Hongzhen Luo
The current
fsck --extractdoes not support exporting the extended attributes of files. This patch adds--xattroption to dump the extended attributes.
only --xattrs is not helpful, you'd better to add both --xattrs and --no-xattrs, and makes --xattrs enabled by default.
Quoted 19 lines
Signed-off-by Hongzhen Luo <[email protected]>--- fsck/main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)fsck/main.c +1
-0@@ -9,6 +9,7 @@#include <utime.h> #include <unistd.h> #include <sys/stat.h> +#include <sys/xattr.h> #include "erofs/print.h" #include "erofs/compress.h" #include "erofs/decompress.h"
fsck/main.c struct erofsfsck_cfg { · line 35
Quoted 4 lines
fsck/main.c +1
-0@@ -31,6 +32,7 @@ struct erofsfsck_cfg {bool overwrite;bool preserve_owner;bool preserve_perms; +bool xattr;
bool dump_xattrs;
fsck/main.c static struct option long_options[] = { · line 53
Quoted 8 lines
fsck/main.c +1
-0};static struct erofsfsck_cfg fsckcfg;@@ -48,6 +50,7 @@ static struct option long_options[] = { {"no-preserve-owner", no_argument, 0, 10}, {"no-preserve-perms", no_argument, 0, 11}, {"offset", required_argument, 0, 12}, + {"xattr", no_argument, 0, 13},
--xattrs, --no-xattrs
Quoted 22 lines
fsck/main.c +4
-0{0, 0, 0, 0}, };@@ -98,6 +101,7 @@ static void usage(int argc, char **argv)" --extract[=X] check if all files are well encoded, optionally\n" " extract to X\n" " --offset=# skip # bytes at the beginning of IMAGE\n" +" --xattr dump extended attributes\n" "\n" " -a, -A, -y no-op, for compatibility with fsck of other filesystems\n" "\n" @@ -225,6 +229,9 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)return -EINVAL ; }break ; +case 13 : + fsckcfg.xattr = true; +break ;default :return -EINVAL ; }
fsck/main.c out: · line 421
Quoted 4 lines
fsck/main.c +1
-0@@ -411,6 +418,53 @@ out:return ret; } +static int erofs_dump_xattr (struct erofs_inode *inode)
erofsfsck_dump_xattrs
fsck/main.c out: · line 427
Quoted 6 lines
fsck/main.c +6
-0+{ +char *keylst, *key; +ssize_t kllen; +int ret; + +if (!fsckcfg.extract_path)
If extract_path is none, please keep reading the xattrs but don't dump these.
Quoted 7 lines
fsck/main.c +7
-0+return 0 ; + kllen =erofs_listxattr (inode,NULL ,0 ); +if (kllen <=0 ) +return kllen; + keylst =malloc (kllen); +if (!keylst) +return -ENOMEM ;
fsck/main.c out: · line 444
Quoted 10 lines
fsck/main.c +10
-0+ ret =erofs_listxattr (inode, keylst, kllen); +if (ret <0 ) + goto out; +for (key = keylst; key < keylst + kllen; key +=strlen (key) +1 ) { +void *value =NULL ; +size_t size =0 ; + + ret =erofs_getxattr (inode, key,NULL ,0 ); +if (ret <0 ) + goto out;
why not just break; this?
fsck/main.c out: · line 450
Quoted 6 lines
fsck/main.c +6
-0+if (ret) { + size = ret; + value =malloc (size); +if (!value) { + ret = -ENOMEM ; + goto out;
same here.
fsck/main.c out: · line 455
Quoted 5 lines
fsck/main.c +5
-0+ } + ret =erofs_getxattr (inode, key, value, size); +if (ret <0 ) { +free (value); + goto out;
same here.
fsck/main.c out: · line 460
Quoted 5 lines
fsck/main.c +5
-0+ } + ret =setxattr (fsckcfg.extract_path, key, value, size,0 ); +free (value); +if (ret) + goto out;
same here.
Thanks,
Gao Xiang
Quoted 6 lines
fsck/main.c +6
-0+ } + } +out: +free (keylst); +return ret; +}
Quoted 11 lines · Gao Xiang
Quoted 3 lines · Hongzhen Luo
The current
fsck --extractdoes not support exporting the extended attributes of files. This patch adds--xattroption to dump the extended attributes.only
--xattrsis not helpful, you'd better to add both--xattrsand--no-xattrs, and makes--xattrsenabled by default.
Sure, I will do this in the next patch.
Quoted 91 lines
Quoted 19 lines
Signed-off-by Hongzhen Luo <[email protected]>---
fsck/main.c| 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 61 insertions(+)fsck/main.c +1
-0@@ -9,6 +9,7 @@ #include <utime.h> #include <unistd.h> #include <sys/stat.h> +#include <sys/xattr.h> #include "erofs/print.h" #include "erofs/compress.h" #include "erofs/decompress.h"fsck/main.c struct erofsfsck_cfg { · line 35
Quoted 4 lines
fsck/main.c +1
-0@@ -31,6 +32,7 @@ struct erofsfsck_cfg { bool overwrite; bool preserve_owner; bool preserve_perms; + bool xattr;bool
dump_xattrs;Quoted 7 lines
fsck/main.c +0
-0}; static struct erofsfsck_cfg fsckcfg; @@ -48,6 +50,7 @@ static struct option long_options[] = {{"no-preserve-owner",
no_argument, 0, 10},
{"no-preserve-perms",no_argument, 0, 11},
{"offset",required_argument, 0, 12},
+ {"xattr",no_argument, 0, 13},--xattrs, --no-xattrs
Quoted 22 lines
{0, 0, 0, 0},
};
@@ -98,6 +101,7 @@ static voidusage(int argc, char **argv)
" --extract[=X] check if all files are well encoded, optionally\n" " extract to X\n"
" --offset=# skip # bytes at the beginning of IMAGE\n"
+ " --xattr dump extended attributes\n"
"\n"
" -a, -A, -y no-op, for compatibility with fsck of other filesystems\n" "\n"fsck/main.c +3
-0@@ -225,6 +229,9 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv) return -EINVAL; } break; + case 13: + fsckcfg.xattr = true; + break;default:
return-EINVAL;
}fsck/main.c out: · line 420
Quoted 3 lines
fsck/main.c +0
-0@@ -411,6 +418,53 @@ out: return ret; } +static int erofs_dump_xattr(struct erofs_inode *inode)
erofsfsck_dump_xattrsfsck/main.c out: · line 427
Quoted 6 lines
fsck/main.c +6
-0+{ + char *keylst, *key; + ssize_t kllen; + int ret; + + if (!fsckcfg.extract_path)If
extract_pathis none, please keep reading the xattrs but don't dump these.Quoted 7 lines
fsck/main.c +7
-0+ return 0; + kllen = erofs_listxattr(inode, NULL, 0); + if (kllen <= 0) + return kllen; + keylst = malloc(kllen); + if (!keylst) + return -ENOMEM;fsck/main.c out: · line 444
Quoted 10 lines
fsck/main.c +10
-0+ ret = erofs_listxattr(inode, keylst, kllen); + if (ret < 0) + goto out; + for (key = keylst; key < keylst + kllen; key += strlen(key) + 1) { + void *value = NULL; + size_t size = 0; + + ret = erofs_getxattr(inode, key, NULL, 0); + if (ret < 0) + goto out;
Quoted 1 line
why not just
break;this?
My original intention was to think that goto out; and break; were equivalent here. But, yes, I will make changes in the next version.
Quoted 9 lines
fsck/main.c out: · line 450
Quoted 7 lines
fsck/main.c +6
-0+ if (ret) { + size = ret; + value = malloc(size); + if (!value) { + ret = -ENOMEM; + goto out;same here.
Ditto.
Quoted 20 lines
fsck/main.c out: · line 455
Quoted 6 lines
fsck/main.c +5
-0+ } + ret = erofs_getxattr(inode, key, value, size); + if (ret < 0) { + free(value); + goto out;same here.
fsck/main.c out: · line 460
Quoted 5 lines
fsck/main.c +5
-0+ } + ret = setxattr(fsckcfg.extract_path, key, value, size, 0); + free(value); + if (ret) + goto out;same here.
Thanks,
Gao Xiang
Quoted 6 lines
fsck/main.c out: · line 466
Quoted 6 lines
fsck/main.c +6
-0+ } + } +out: + free(keylst); + return ret; +}
---
Thanks,
Hongzhen Luo