]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
xattr-util: add new getxattr_at_bool() helper
authorLennart Poettering <lennart@poettering.net>
Wed, 25 Oct 2023 20:59:53 +0000 (22:59 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 26 Oct 2023 07:27:00 +0000 (09:27 +0200)
This carefully combines getxattr_at_malloc() and parse_bool(), which is
something we use at multiple places.

src/basic/xattr-util.c
src/basic/xattr-util.h

index 700fdf44b6eebb0762c5a52013e9d541f54514c6..d2daf87ec91286dd46a9fdffeb06cb07c4347b04 100644 (file)
@@ -12,6 +12,7 @@
 #include "fd-util.h"
 #include "macro.h"
 #include "missing_syscall.h"
+#include "parse-util.h"
 #include "sparse-endian.h"
 #include "stat-util.h"
 #include "stdio-util.h"
@@ -118,6 +119,20 @@ int getxattr_at_malloc(
         }
 }
 
+int getxattr_at_bool(int fd, const char *path, const char *name, int flags) {
+        _cleanup_free_ char *v = NULL;
+        int r;
+
+        r = getxattr_at_malloc(fd, path, name, flags, &v);
+        if (r < 0)
+                return r;
+
+        if (memchr(v, 0, r)) /* Refuse embedded NUL byte */
+                return -EINVAL;
+
+        return parse_boolean(v);
+}
+
 static int parse_crtime(le64_t le, usec_t *usec) {
         uint64_t u;
 
index 649a842fe262d2582eb5e654ab17960c37e91014..19ee3e1c45255ba47ce7e9308595a01a8cec7091 100644 (file)
@@ -18,6 +18,8 @@ static inline int fgetxattr_malloc(int fd, const char *name, char **ret) {
         return getxattr_at_malloc(fd, NULL, name, AT_EMPTY_PATH, ret);
 }
 
+int getxattr_at_bool(int fd, const char *path, const char *name, int flags);
+
 int fd_setcrtime(int fd, usec_t usec);
 
 int fd_getcrtime_at(int fd, const char *name, int flags, usec_t *ret);