[PATCH] erofs-utils: lib: introduce erofs_sys_lsetxattr()

Gao Xiang hsiangkao at linux.alibaba.com
Wed Dec 24 14:59:59 AEDT 2025


To wrap raw function calls for better portability.

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 fsck/main.c           | 12 ++----------
 include/erofs/xattr.h |  3 +++
 lib/xattr.c           | 18 ++++++++++++++++++
 3 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/fsck/main.c b/fsck/main.c
index 8aba964ceff1..ab697bed20c6 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -9,7 +9,6 @@
 #include <utime.h>
 #include <unistd.h>
 #include <sys/stat.h>
-#include <sys/xattr.h>
 #include "erofs/print.h"
 #include "erofs/decompress.h"
 #include "erofs/dir.h"
@@ -457,15 +456,8 @@ static int erofsfsck_dump_xattrs(struct erofs_inode *inode)
 			break;
 		}
 		if (fsckcfg.extract_path)
-#ifdef HAVE_LSETXATTR
-			ret = lsetxattr(fsckcfg.extract_path, key, value, size,
-					0);
-#elif defined(__APPLE__)
-			ret = setxattr(fsckcfg.extract_path, key, value, size,
-				       0, XATTR_NOFOLLOW);
-#else
-			ret = -EOPNOTSUPP;
-#endif
+			ret = erofs_sys_lsetxattr(fsckcfg.extract_path, key,
+						  value, size);
 		else
 			ret = 0;
 		free(value);
diff --git a/include/erofs/xattr.h b/include/erofs/xattr.h
index ef80123fd9c8..4e74cc523dae 100644
--- a/include/erofs/xattr.h
+++ b/include/erofs/xattr.h
@@ -45,6 +45,9 @@ static inline unsigned int xattrblock_offset(struct erofs_inode *vi,
 
 struct erofs_importer;
 
+ssize_t erofs_sys_lsetxattr(const char *path, const char *name,
+			    void *value, size_t size);
+
 int erofs_xattr_init(struct erofs_sb_info *sbi);
 int erofs_scan_file_xattrs(struct erofs_inode *inode);
 int erofs_prepare_xattr_ibody(struct erofs_inode *inode, bool noroom);
diff --git a/lib/xattr.c b/lib/xattr.c
index 8f0332b44a02..68236690d5b3 100644
--- a/lib/xattr.c
+++ b/lib/xattr.c
@@ -99,6 +99,24 @@ static ssize_t erofs_sys_lgetxattr(const char *path, const char *name,
 	return -1;
 }
 
+ssize_t erofs_sys_lsetxattr(const char *path, const char *name,
+			    void *value, size_t size)
+{
+	int ret;
+
+#ifdef HAVE_LSETXATTR
+	ret = lsetxattr(path, name, value, size, 0);
+#elif defined(__APPLE__)
+	ret = setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
+#else
+	ret = -1;
+	errno = ENODATA;
+#endif
+	if (ret < 0)
+		return errno;
+	return ret;
+}
+
 /* one extra byte for the trailing `\0` of attribute name */
 #define EROFS_XATTR_KSIZE(kvlen)	(kvlen[0] + 1)
 #define EROFS_XATTR_KVSIZE(kvlen)	(EROFS_XATTR_KSIZE(kvlen) + kvlen[1])
-- 
2.43.5



More information about the Linux-erofs mailing list