Skip to contentSkip to conversation

erofs-utils: fsck: add --xattrs and --no-xattrs options

PATCH In review Hongzhen Luo 2 participants 3 messages Sep 3, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 erofs-utils: fsck: add --xattrs and --no-xattrs options

The current fsck --extract does not support exporting the extended attributes of files. This patch adds --xattrs option to dump the extended attributes, as well as the --no-xattrs option to not dump the extended attributes.

---
v2: In addition to "--xattrs", the option "--no-xattrs" has been added.
v1: https://lore.kernel.org/all/[email protected]/

---
 fsck/main.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
fsck/main.c +74 -0
index 28f1e7e..273bf73 100644
@@ -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 dump_xattrs;
 };
 static struct erofsfsck_cfg fsckcfg;
 
@@ -48,6 +50,8 @@ static struct option long_options[] = {
 	{"no-preserve-owner", no_argument, 0, 10},
 	{"no-preserve-perms", no_argument, 0, 11},
 	{"offset", required_argument, 0, 12},
+	{"xattrs", no_argument, 0, 13},
+	{"no-xattrs", no_argument, 0, 14},
 	{0, 0, 0, 0},
 };
 
@@ -98,6 +102,8 @@ 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"
+		" --xattrs               dump extended attributes (default)\n"
+		" --no-xattrs            do not dump extended attributes\n"
 		"\n"
 		" -a, -A, -y             no-op, for compatibility with fsck of other filesystems\n"
 		"\n"
@@ -225,6 +231,16 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)
 				return -EINVAL;
 			}
 			break;
+		case 13:
+			if (!fsckcfg.dump_xattrs) {
+				erofs_err("--xattrs conflicts with --no-xattrs");
+				return -EINVAL;
+			}
+			fsckcfg.dump_xattrs = true;
+			break;
+		case 14:
+			fsckcfg.dump_xattrs = false;
+			break;
 		default:
 			return -EINVAL;
 		}
@@ -411,6 +427,57 @@ out:
 	return ret;
 }
 
+static int erofsfsck_dump_xattrs(struct erofs_inode *inode)
+{
+	char *keylst, *key;
+	ssize_t kllen;
+	int ret;
+
+	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)
+			break;
+		if (ret) {
+			size = ret;
+			value = malloc(size);
+			if (!value) {
+				ret = -ENOMEM;
+				break;
+			}
+			ret = erofs_getxattr(inode, key, value, size);
+			if (ret < 0) {
+				free(value);
+				break;
+			}
+			if (fsckcfg.extract_path) {
+				ret = setxattr(fsckcfg.extract_path, key, value,
+					       size, 0);
+				if (ret) {
+					free(value);
+					break;
+				}
+			} else
+				ret = 0;
+			free(value);
+		}
+	}
+out:
+	free(keylst);
+	return ret;
+}
+
 static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 {
 	struct erofs_map_blocks map = {
@@ -909,6 +976,12 @@ static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid)
 	if (ret && ret != -ECANCELED)
 		goto out;
 
+	if (fsckcfg.dump_xattrs) {
+		ret = erofsfsck_dump_xattrs(&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 +1028,7 @@ int main(int argc, char *argv[])
 	fsckcfg.overwrite = false;
 	fsckcfg.preserve_owner = fsckcfg.superuser;
 	fsckcfg.preserve_perms = fsckcfg.superuser;
+	fsckcfg.dump_xattrs= true;
 
 	err = erofsfsck_parse_options_cfg(argc, argv);
 	if (err) {
Signature · 1 line
2.43.5
#1 lore raw
Quoted 52 lines · Hongzhen Luo

The current fsck --extract does not support exporting the extended attributes of files. This patch adds --xattrs option to dump the extended attributes, as well as the --no-xattrs option to not dump the extended attributes.

---
v2: In addition to "--xattrs", the option "--no-xattrs" has been added.
v1: https://lore.kernel.org/all/[email protected]/

---
  fsck/main.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 74 insertions(+)
fsck/main.c +6 -0
index 28f1e7e..273bf73 100644
@@ -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 dump_xattrs;
  };
  static struct erofsfsck_cfg fsckcfg;
 
@@ -48,6 +50,8 @@ static struct option long_options[] = {
  	{"no-preserve-owner", no_argument, 0, 10},
  	{"no-preserve-perms", no_argument, 0, 11},
  	{"offset", required_argument, 0, 12},
+	{"xattrs", no_argument, 0, 13},
+	{"no-xattrs", no_argument, 0, 14},
  	{0, 0, 0, 0},
  };
 
@@ -98,6 +102,8 @@ 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"
+		" --xattrs               dump extended attributes (default)\n"
+		" --no-xattrs            do not dump extended attributes\n"
  		"\n"
  		" -a, -A, -y             no-op, for compatibility with fsck of other filesystems\n"
  		"\n"

fsck/main.c static int erofsfsck_parse_options_cfg(int argc, char **argv) · line 238

Quoted 8 lines
fsck/main.c +5 -0
@@ -225,6 +231,16 @@ static int erofsfsck_parse_options_cfg(int argc, char **argv)
  				return -EINVAL;
  			}
  			break;
+		case 13:
+			if (!fsckcfg.dump_xattrs) {
+				erofs_err("--xattrs conflicts with --no-xattrs");
+				return -EINVAL;
+			}

No need this, the latter option will override the previous ones.

Quoted 29 lines
fsck/main.c +22 -0
+			fsckcfg.dump_xattrs = true;
+			break;
+		case 14:
+			fsckcfg.dump_xattrs = false;
+			break;
  		default:
  			return -EINVAL;
  		}
@@ -411,6 +427,57 @@ out:
  	return ret;
  }
 
+static int erofsfsck_dump_xattrs(struct erofs_inode *inode)
+{
+	char *keylst, *key;
+	ssize_t kllen;
+	int ret;
+
+	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;

fsck/main.c out: · line 456

Quoted 10 lines
fsck/main.c +10 -0
+		size_t size = 0;
+
+		ret = erofs_getxattr(inode, key, NULL, 0);
+		if (ret < 0)
+			break;
+		if (ret) {
+			size = ret;
+			value = malloc(size);
+			if (!value) {
+				ret = -ENOMEM;

I think error message is needed here.

fsck/main.c out: · line 460

Quoted 4 lines
fsck/main.c +4 -0
+				break;
+			}
+			ret = erofs_getxattr(inode, key, value, size);
+			if (ret < 0) {

Same here.

fsck/main.c out: · line 468

Quoted 8 lines
fsck/main.c +8 -0
+				free(value);
+				break;
+			}
+			if (fsckcfg.extract_path) {
+				ret = setxattr(fsckcfg.extract_path, key, value,
+					       size, 0);
+				if (ret) {
+					free(value);

It seems that is unneeded.

fsck/main.c out: · line 472

Quoted 4 lines
fsck/main.c +4 -0
+					break;
+				}
+			} else
+				ret = 0;

why not just

			if (ret) {
				erofs_err(...);
				break;
			}

here?

Quoted 25 lines
fsck/main.c +14 -0
+			free(value);
+		}
+	}
+out:
+	free(keylst);
+	return ret;
+}
+
  static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
  {
  	struct erofs_map_blocks map = {
@@ -909,6 +976,12 @@ static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid)
  	if (ret && ret != -ECANCELED)
  		goto out;
 
+	if (fsckcfg.dump_xattrs) {
+		ret = erofsfsck_dump_xattrs(&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 = {

fsck/main.c int main(int argc, char *argv[]) · line 1031

Quoted 4 lines
fsck/main.c +1 -0
@@ -955,6 +1028,7 @@ int main(int argc, char *argv[])
  	fsckcfg.overwrite = false;
  	fsckcfg.preserve_owner = fsckcfg.superuser;
  	fsckcfg.preserve_perms = fsckcfg.superuser;
+	fsckcfg.dump_xattrs= true;

Missing space.

Thanks,
Gao Xiang

Quoted 3 lines
fsck/main.c +0 -0
  	err = erofsfsck_parse_options_cfg(argc, argv);
  	if (err) {
#2 lore raw
Quoted 129 lines · Gao Xiang
Quoted 50 lines · Hongzhen Luo

The current fsck --extract does not support exporting the extended attributes of files. This patch adds --xattrs option to dump the extended attributes, as well as the --no-xattrs option to not dump the extended attributes.

---
v2: In addition to "--xattrs", the option "--no-xattrs" has been added.
v1: https://lore.kernel.org/all/[email protected]/
---
  fsck/main.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++

  1 file changed, 74 insertions(+)
fsck/main.c +2 -0
index 28f1e7e..273bf73 100644
@@ -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 dump_xattrs;
   };
   static struct erofsfsck_cfg fsckcfg;
   @@ -48,6 +50,8 @@ static struct option long_options[] = {

      {"no-preserve-owner", no_argument, 0, 10},
      {"no-preserve-perms", no_argument, 0, 11},
      {"offset", required_argument, 0, 12},
+    {"xattrs", no_argument, 0, 13},
+    {"no-xattrs", no_argument, 0, 14},
      {0, 0, 0, 0},
  };
  @@ -98,6 +102,8 @@ 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"
+        " --xattrs               dump extended attributes (default)\n"
+        " --no-xattrs            do not dump extended attributes\n"
          "\n"
          " -a, -A, -y             no-op, for compatibility with fsck of other filesystems\n" "\n"

fsck/main.c static int erofsfsck_parse_options_cfg(int argc, · line 239

Quoted 9 lines
fsck/main.c +5 -0
@@ -225,6 +231,16 @@ static int erofsfsck_parse_options_cfg(int argc, 
 char **argv)
                   return -EINVAL;
               }
               break;
+        case 13:
+            if (!fsckcfg.dump_xattrs) {
+                erofs_err("--xattrs conflicts with --no-xattrs");
+                return -EINVAL;
+            }

No need this, the latter option will override the previous ones.

Quoted 28 lines
fsck/main.c +5 -0
+            fsckcfg.dump_xattrs = true;
+            break;
+        case 14:
+            fsckcfg.dump_xattrs = false;
+            break;

          default:
              return -EINVAL;
          }

fsck/main.c +16 -0
@@ -411,6 +427,57 @@ out:
       return ret;
   }
   +static int erofsfsck_dump_xattrs(struct erofs_inode *inode)
+{
+    char *keylst, *key;
+    ssize_t kllen;
+    int ret;
+
+    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;

fsck/main.c out: · line 455

Quoted 10 lines
fsck/main.c +10 -0
+        size_t size = 0;
+
+        ret = erofs_getxattr(inode, key, NULL, 0);
+        if (ret < 0)
+            break;
+        if (ret) {
+            size = ret;
+            value = malloc(size);
+            if (!value) {
+                ret = -ENOMEM;

I think error message is needed here.

fsck/main.c out: · line 460

Quoted 4 lines
fsck/main.c +4 -0
+                break;
+            }
+            ret = erofs_getxattr(inode, key, value, size);
+            if (ret < 0) {

Same here.

fsck/main.c out: · line 468

Quoted 8 lines
fsck/main.c +8 -0
+                free(value);
+                break;
+            }
+            if (fsckcfg.extract_path) {
+                ret = setxattr(fsckcfg.extract_path, key, value,
+                           size, 0);
+                if (ret) {
+                    free(value);

It seems that is unneeded.

fsck/main.c out: · line 472

Quoted 4 lines
fsck/main.c +4 -0
+                    break;
+                }
+            } else
+                ret = 0;
Quoted 8 lines

why not just
            if (ret) {
                erofs_err(...);
                break;
            }

here?

Thanks for pointing this out. I will make the changes in the next version.

Thanks,

Hongzhen

Quoted 38 lines
Quoted 26 lines
fsck/main.c +13 -0
+            free(value);
+        }
+    }
+out:
+    free(keylst);
+    return ret;
+}
+
   static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
   {
       struct erofs_map_blocks map = {
@@ -909,6 +976,12 @@ static int erofsfsck_check_inode(erofs_nid_t 
 pnid, erofs_nid_t nid)
       if (ret && ret != -ECANCELED)
           goto out;
   +    if (fsckcfg.dump_xattrs) {
+        ret = erofsfsck_dump_xattrs(&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 = {

fsck/main.c int main(int argc, char *argv[]) · line 1031

Quoted 4 lines
fsck/main.c +1 -0
@@ -955,6 +1028,7 @@ int main(int argc, char *argv[])
       fsckcfg.overwrite = false;
       fsckcfg.preserve_owner = fsckcfg.superuser;
       fsckcfg.preserve_perms = fsckcfg.superuser;
+    fsckcfg.dump_xattrs= true;

Missing space.

Thanks,
Gao Xiang

Quoted 2 lines
fsck/main.c +0 -0
         err = erofsfsck_parse_options_cfg(argc, argv);
       if (err) {