From: Yu Watanabe Date: Fri, 31 Mar 2023 07:12:39 +0000 (+0900) Subject: sd-id128: introduce id128_write_at() X-Git-Tag: v254-rc1~819^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c227c46b8b6fefe2604ca110b2e2c4e4f78c2c9d;p=thirdparty%2Fsystemd.git sd-id128: introduce id128_write_at() --- diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c index 94094ed208a..3fa1585aa3a 100644 --- a/src/libsystemd/sd-id128/id128-util.c +++ b/src/libsystemd/sd-id128/id128-util.c @@ -145,14 +145,15 @@ int id128_write_fd(int fd, Id128Flag f, sd_id128_t id) { return 0; } -int id128_write(const char *path, Id128Flag f, sd_id128_t id) { +int id128_write_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t id) { _cleanup_close_ int fd = -EBADF; + assert(dir_fd >= 0 || dir_fd == AT_FDCWD); assert(path); - fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444); + fd = xopenat(dir_fd, path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444); if (fd < 0) - return -errno; + return fd; return id128_write_fd(fd, f, id); } diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h index 7bab3e83031..fde58a52284 100644 --- a/src/libsystemd/sd-id128/id128-util.h +++ b/src/libsystemd/sd-id128/id128-util.h @@ -26,7 +26,10 @@ static inline int id128_read(const char *path, Id128Flag f, sd_id128_t *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); +int id128_write_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t id); +static inline int id128_write(const char *path, Id128Flag f, sd_id128_t id) { + return id128_write_at(AT_FDCWD, path, f, id); +} void id128_hash_func(const sd_id128_t *p, struct siphash *state); int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) _pure_;