Skip to contentSkip to conversation

erofs-utils: lib: add erofs_sys_l{list,get}xattr wrappers

PATCH New Gao Xiang 1 participant 2 messages Jun 21, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs-utils: lib: add erofs_sys_l{list,get}xattr wrappers

It avoids macro messiness.

---
 lib/xattr.c | 67 +++++++++++++++++++++++++----------------------------
 1 file changed, 32 insertions(+), 35 deletions(-)
lib/xattr.c +32 -35
index 68a96cc..b382ee4 100644
@@ -75,6 +75,28 @@
 #define OVL_XATTR_ORIGIN OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_ORIGIN_POSTFIX
 #endif
 
+static ssize_t erofs_sys_llistxattr(const char *path, char *list, size_t size)
+{
+#ifdef HAVE_LLISTXATTR
+	return llistxattr(path, list, size);
+#elif defined(__APPLE__)
+	return listxattr(path, list, size, XATTR_NOFOLLOW);
+#endif
+	return 0;
+}
+
+static ssize_t erofs_sys_lgetxattr(const char *path, const char *name,
+				   void *value, size_t size)
+{
+#ifdef HAVE_LGETXATTR
+	return lgetxattr(path, name, value, size);
+#elif defined(__APPLE__)
+	return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
+#endif
+	errno = ENODATA;
+	return -1;
+}
+
 #define EA_HASHTABLE_BITS 16
 
 /* one extra byte for the trailing `\0` of attribute name */
@@ -241,13 +263,7 @@ static struct xattr_item *parse_one_xattr(const char *path, const char *key,
 	len[0] = keylen;
 
 	/* determine length of the value */
-#ifdef HAVE_LGETXATTR
-	ret = lgetxattr(path, key, NULL, 0);
-#elif defined(__APPLE__)
-	ret = getxattr(path, key, NULL, 0, 0, XATTR_NOFOLLOW);
-#else
-	return ERR_PTR(-EOPNOTSUPP);
-#endif
+	ret = erofs_sys_lgetxattr(path, key, NULL, 0);
 	if (ret < 0)
 		return ERR_PTR(-errno);
 	len[1] = ret;
@@ -259,16 +275,9 @@ static struct xattr_item *parse_one_xattr(const char *path, const char *key,
 	memcpy(kvbuf, key, EROFS_XATTR_KSIZE(len));
 	if (len[1]) {
 		/* copy value to buffer */
-#ifdef HAVE_LGETXATTR
-		ret = lgetxattr(path, key, kvbuf + EROFS_XATTR_KSIZE(len),
-				len[1]);
-#elif defined(__APPLE__)
-		ret = getxattr(path, key, kvbuf + EROFS_XATTR_KSIZE(len),
-			       len[1], 0, XATTR_NOFOLLOW);
-#else
-		ret = -EOPNOTSUPP;
-		goto out;
-#endif
+		ret = erofs_sys_lgetxattr(path, key,
+					  kvbuf + EROFS_XATTR_KSIZE(len),
+					  len[1]);
 		if (ret < 0) {
 			ret = -errno;
 			goto out;
@@ -392,21 +401,15 @@ static bool erofs_is_skipped_xattr(const char *key)
 static int read_xattrs_from_file(const char *path, mode_t mode,
 				 struct list_head *ixattrs)
 {
-#ifdef HAVE_LLISTXATTR
-	ssize_t kllen = llistxattr(path, NULL, 0);
-#elif defined(__APPLE__)
-	ssize_t kllen = listxattr(path, NULL, 0, XATTR_NOFOLLOW);
-#else
-	ssize_t kllen = 0;
-#endif
-	int ret;
+	ssize_t kllen = erofs_sys_llistxattr(path, NULL, 0);
 	char *keylst, *key, *klend;
 	unsigned int keylen;
 	struct xattr_item *item;
+	int ret;
 
 	if (kllen < 0 && errno != ENODATA && errno != EOPNOTSUPP) {
-		erofs_err("llistxattr to get the size of names for %s failed",
-			  path);
+		erofs_err("failed to get the size of the xattr list for %s: %s",
+			  path, strerror(errno));
 		return -errno;
 	}
 
@@ -419,19 +422,13 @@ static int read_xattrs_from_file(const char *path, mode_t mode,
 		return -ENOMEM;
 
 	/* copy the list of attribute keys to the buffer.*/
-#ifdef HAVE_LLISTXATTR
-	kllen = llistxattr(path, keylst, kllen);
-#elif defined(__APPLE__)
-	kllen = listxattr(path, keylst, kllen, XATTR_NOFOLLOW);
+	kllen = erofs_sys_llistxattr(path, keylst, kllen);
 	if (kllen < 0) {
 		erofs_err("llistxattr to get names for %s failed", path);
 		ret = -errno;
 		goto err;
 	}
-#else
-	ret = -EOPNOTSUPP;
-	goto err;
-#endif
+
 	/*
 	 * loop over the list of zero terminated strings with the
 	 * attribute keys. Use the remaining buffer length to determine
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/2 erofs-utils: mkfs: skip xattrs where getxattr() returns ENODATA

For example, unprivileged users may not have access to the trusted.* namespace.

It also dumps unidentified xattrs with index 0, which the kernel/FUSE implementations will ignore.

---
 lib/xattr.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
lib/xattr.c +11 -16
index b382ee4..091c88c 100644
@@ -165,6 +165,8 @@ bool erofs_xattr_prefix_matches(const char *key, unsigned int *index,
 {
 	struct xattr_prefix *p;
 
+	*index = 0;
+	*len = 0;
 	for (p = xattr_types; p < xattr_types + ARRAY_SIZE(xattr_types); ++p) {
 		if (p->prefix && !strncmp(p->prefix, key, p->prefix_len)) {
 			*len = p->prefix_len;
@@ -219,13 +221,9 @@ static struct xattr_item *get_xattritem(char *kvbuf, unsigned int len[2])
 	if (!item)
 		return ERR_PTR(-ENOMEM);
 
-	if (!erofs_xattr_prefix_matches(kvbuf, &item->base_index,
-					&item->prefix_len)) {
-		free(item);
-		return ERR_PTR(-ENODATA);
-	}
+	(void)erofs_xattr_prefix_matches(kvbuf, &item->base_index,
+					 &item->prefix_len);
 	DBG_BUGON(len[0] < item->prefix_len);
-
 	INIT_HLIST_NODE(&item->node);
 	item->count = 1;
 	item->kvbuf = kvbuf;
@@ -292,12 +290,7 @@ static struct xattr_item *parse_one_xattr(const char *path, const char *key,
 	item = get_xattritem(kvbuf, len);
 	if (!IS_ERR(item))
 		return item;
-	if (item == ERR_PTR(-ENODATA)) {
-		erofs_warn("skipped unidentified xattr: %s", key);
-		ret = 0;
-	} else {
-		ret = PTR_ERR(item);
-	}
+	ret = PTR_ERR(item);
 out:
 	free(kvbuf);
 	return ERR_PTR(ret);
@@ -443,13 +436,16 @@ static int read_xattrs_from_file(const char *path, mode_t mode,
 			continue;
 
 		item = parse_one_xattr(path, key, keylen);
+		/* skip inaccessible xattrs */
+		if (item == ERR_PTR(-ENODATA) || !item) {
+			erofs_warn("skipped inaccessible xattr %s in %s",
+				   key, path);
+			continue;
+		}
 		if (IS_ERR(item)) {
 			ret = PTR_ERR(item);
 			goto err;
 		}
-		/* skip unidentified xattrs */
-		if (!item)
-			continue;
 
 		ret = erofs_xattr_add(ixattrs, item);
 		if (ret < 0)
@@ -1430,7 +1426,6 @@ int erofs_getxattr(struct erofs_inode *vi, const char *name, char *buffer,
 
 	if (!erofs_xattr_prefix_matches(name, &prefix, &prefixlen))
 		return -ENODATA;
-
 	it.it.sbi = vi->sbi;
 	it.index = prefix;
 	it.name = name + prefixlen;
Signature · 1 line
2.43.5