Skip to contentSkip to conversation

erofs-utils: lib: introduce erofs_sys_lsetxattr()

PATCH New Gao Xiang 1 participant 1 message Dec 24, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: introduce erofs_sys_lsetxattr()

To wrap raw function calls for better portability.

---
 fsck/main.c           | 12 ++----------
 include/erofs/xattr.h |  3 +++
 lib/xattr.c           | 18 ++++++++++++++++++
 3 files changed, 23 insertions(+), 10 deletions(-)
fsck/main.c +2 -10
index 8aba964ceff1..ab697bed20c6 100644
@@ -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);
include/erofs/xattr.h +3 -0
index ef80123fd9c8..4e74cc523dae 100644
@@ -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);
lib/xattr.c +18 -0
index 8f0332b44a02..68236690d5b3 100644
@@ -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])
Signature · 1 line
2.43.5