]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-id128: introduce id128_write_at()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 31 Mar 2023 07:12:39 +0000 (16:12 +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 94094ed208a93eb2fca6af193b54b2405eefb227..3fa1585aa3a2f5aca624b03a40437c36e033f606 100644 (file)
@@ -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);
 }
index 7bab3e83031522079d19f22f5b2a3d5a40679f3a..fde58a52284f63acbf01c2ac6acd5a5a6d7c7b49 100644 (file)
@@ -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_;