[PATCH v2 06/16] erofs-utils: add erofs_read_xattrs_from_disk() helper

Jingbo Xu jefflexu at linux.alibaba.com
Wed Aug 2 19:17:40 AEST 2023


Add erofs_read_xattrs_from_disk() helper to read extended attributes
from disk.

Signed-off-by: Jingbo Xu <jefflexu at linux.alibaba.com>
---
 include/erofs/xattr.h |  1 +
 lib/xattr.c           | 76 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)

diff --git a/include/erofs/xattr.h b/include/erofs/xattr.h
index dc27cf6..634daf9 100644
--- a/include/erofs/xattr.h
+++ b/include/erofs/xattr.h
@@ -85,6 +85,7 @@ int erofs_xattr_write_name_prefixes(struct erofs_sb_info *sbi, FILE *f);
 
 int erofs_setxattr(struct erofs_inode *inode, char *key,
 		   const void *value, size_t size);
+int erofs_read_xattrs_from_disk(struct erofs_inode *inode);
 
 #ifdef __cplusplus
 }
diff --git a/lib/xattr.c b/lib/xattr.c
index 12f580e..8d8f9f0 100644
--- a/lib/xattr.c
+++ b/lib/xattr.c
@@ -493,6 +493,82 @@ int erofs_scan_file_xattrs(struct erofs_inode *inode)
 	return erofs_droid_xattr_set_caps(inode);
 }
 
+static struct xattr_item *erofs_read_xattr_from_disk(struct erofs_inode *inode,
+						     char *key)
+{
+	ssize_t ret;
+	u8 prefix;
+	u16 prefixlen;
+	unsigned int len[2];
+	char *kvbuf;
+
+	if (!match_prefix(key, &prefix, &prefixlen))
+		return ERR_PTR(-ENODATA);
+
+	ret = erofs_getxattr(inode, key, NULL, 0);
+	if (ret < 0)
+		return ERR_PTR(-errno);
+
+	/* allocate key-value buffer */
+	len[0] = strlen(key) - prefixlen;
+	len[1] = ret;
+	kvbuf = malloc(len[0] + len[1]);
+	if (!kvbuf)
+		return ERR_PTR(-ENOMEM);
+	memcpy(kvbuf, key + prefixlen, len[0]);
+	if (len[1]) {
+		ret = erofs_getxattr(inode, key, kvbuf + len[0], len[1]);
+		if (ret < 0) {
+			free(kvbuf);
+			return ERR_PTR(-errno);
+		}
+		if (ret != len[1]) {
+			erofs_err("size of xattr value got changed just now (%u-> %ld)",
+				  len[1], (long)ret);
+			len[1] = ret;
+		}
+	}
+	return get_xattritem(prefix, kvbuf, len);
+}
+
+int erofs_read_xattrs_from_disk(struct erofs_inode *inode)
+{
+	ssize_t kllen;
+	char *keylst, *key;
+	struct xattr_item *item;
+	int ret;
+
+	init_list_head(&inode->i_xattrs);
+	kllen = erofs_listxattr(inode, NULL, 0);
+	if (kllen < 0)
+		return kllen;
+	if (kllen <= 1)
+		return 0;
+
+	keylst = malloc(kllen);
+	if (!keylst)
+		return -ENOMEM;
+
+	ret = erofs_listxattr(inode, keylst, kllen);
+	if (ret < 0)
+		goto err;
+
+	for (key = keylst; key < keylst + kllen; key += strlen(key) + 1) {
+		item = erofs_read_xattr_from_disk(inode, key);
+		if (IS_ERR(item)) {
+			ret = PTR_ERR(item);
+			goto err;
+		}
+
+		ret = erofs_xattr_add(&inode->i_xattrs, item);
+		if (ret < 0)
+			goto err;
+	}
+err:
+	free(keylst);
+	return ret;
+}
+
 int erofs_prepare_xattr_ibody(struct erofs_inode *inode)
 {
 	int ret;
-- 
2.19.1.6.gb485710b



More information about the Linux-erofs mailing list