]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-id128: introduce id128_read_at()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 31 Mar 2023 07:10:36 +0000 (16:10 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 4 Apr 2023 02:46:18 +0000 (11:46 +0900)
src/libsystemd/sd-id128/id128-util.c
src/libsystemd/sd-id128/id128-util.h

index 0d77b04735c2c561e5e8a5d25525e3cb6ccb2026..94094ed208a93eb2fca6af193b54b2405eefb227 100644 (file)
@@ -5,6 +5,7 @@
 #include <unistd.h>
 
 #include "fd-util.h"
+#include "fs-util.h"
 #include "hexdecoct.h"
 #include "id128-util.h"
 #include "io-util.h"
@@ -101,14 +102,15 @@ int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret) {
         return r == -EINVAL ? -EUCLEAN : r;
 }
 
-int id128_read(const char *path, Id128Flag f, sd_id128_t *ret) {
+int id128_read_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t *ret) {
         _cleanup_close_ int fd = -EBADF;
 
+        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
         assert(path);
 
-        fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
+        fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY, 0);
         if (fd < 0)
-                return -errno;
+                return fd;
 
         return id128_read_fd(fd, f, ret);
 }
index 834fa4cd75f2492353a37f9a62eaa9949e3a615d..7bab3e83031522079d19f22f5b2a3d5a40679f3a 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 #pragma once
 
+#include <fcntl.h>
 #include <stdbool.h>
 
 #include "sd-id128.h"
@@ -19,7 +20,10 @@ typedef enum Id128Flag {
 } Id128Flag;
 
 int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret);
-int id128_read(const char *path, Id128Flag f, sd_id128_t *ret);
+int id128_read_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t *ret);
+static inline int id128_read(const char *path, Id128Flag f, sd_id128_t *ret) {
+        return id128_read_at(AT_FDCWD, path, f, ret);
+}
 
 int id128_write_fd(int fd, Id128Flag f, sd_id128_t id);
 int id128_write(const char *path, Id128Flag f, sd_id128_t id);