]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-id128: fold do_sync flag into Id128FormatFlag 25646/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 8 Dec 2022 20:37:12 +0000 (05:37 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 12 Dec 2022 13:07:48 +0000 (22:07 +0900)
src/libsystemd/sd-id128/id128-util.c
src/libsystemd/sd-id128/id128-util.h
src/nspawn/nspawn.c
src/shared/machine-id-setup.c
src/test/test-id128.c

index 2cf884898107cece7664881a4af31e9615cce5a0..3395ba2e58762067f806485e626489c126846471 100644 (file)
@@ -109,7 +109,7 @@ int id128_read(const char *p, Id128FormatFlag f, sd_id128_t *ret) {
         return id128_read_fd(fd, f, ret);
 }
 
-int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id, bool do_sync) {
+int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id) {
         char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
         size_t sz;
         int r;
@@ -130,7 +130,7 @@ int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id, bool do_sync) {
         if (r < 0)
                 return r;
 
-        if (do_sync) {
+        if (FLAGS_SET(f, ID128_SYNC_ON_WRITE)) {
                 r = fsync_full(fd);
                 if (r < 0)
                         return r;
@@ -139,14 +139,14 @@ int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id, bool do_sync) {
         return 0;
 }
 
-int id128_write(const char *p, Id128FormatFlag f, sd_id128_t id, bool do_sync) {
+int id128_write(const char *p, Id128FormatFlag f, sd_id128_t id) {
         _cleanup_close_ int fd = -1;
 
         fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444);
         if (fd < 0)
                 return -errno;
 
-        return id128_write_fd(fd, f, id, do_sync);
+        return id128_write_fd(fd, f, id);
 }
 
 void id128_hash_func(const sd_id128_t *p, struct siphash *state) {
index bb237cb6cc7f213bcceb82a3ace57fe5024a50ec..4b91a16bd163f21c58a160b2a72d2df96b72e5a1 100644 (file)
@@ -14,13 +14,15 @@ typedef enum Id128FormatFlag {
         ID128_FORMAT_PLAIN = 1 << 0,  /* formatted as 32 hex chars as-is */
         ID128_FORMAT_UUID  = 1 << 1,  /* formatted as 36 character uuid string */
         ID128_FORMAT_ANY   = ID128_FORMAT_PLAIN | ID128_FORMAT_UUID,
+
+        ID128_SYNC_ON_WRITE = 1 << 2, /* Sync the file after write. Used only when writing an ID. */
 } Id128FormatFlag;
 
 int id128_read_fd(int fd, Id128FormatFlag f, sd_id128_t *ret);
 int id128_read(const char *p, Id128FormatFlag f, sd_id128_t *ret);
 
-int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id, bool do_sync);
-int id128_write(const char *p, Id128FormatFlag f, sd_id128_t id, bool do_sync);
+int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id);
+int id128_write(const char *p, Id128FormatFlag f, sd_id128_t 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_;
index 0618ab23130ce855ab73b20ca236707d98599265..96611058fe9cc0cf5eb39e0438b3f62a918962cd 100644 (file)
@@ -2196,7 +2196,7 @@ static int setup_boot_id(void) {
         if (r < 0)
                 return log_error_errno(r, "Failed to generate random boot id: %m");
 
-        r = id128_write(path, ID128_FORMAT_UUID, rnd, false);
+        r = id128_write(path, ID128_FORMAT_UUID, rnd);
         if (r < 0)
                 return log_error_errno(r, "Failed to write boot id: %m");
 
index 132e372e07af3deab0b6ab19b03932a910ba5915..2e33a23cb92176f31342c8e2bbfa3831e5c17c6c 100644 (file)
@@ -151,7 +151,7 @@ int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_
                         if (r < 0)
                                 return log_error_errno(r, "Failed to sync %s: %m", etc_machine_id);
                 } else {
-                        r = id128_write_fd(fd, ID128_FORMAT_PLAIN, machine_id, true);
+                        r = id128_write_fd(fd, ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, machine_id);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to write %s: %m", etc_machine_id);
                         else
@@ -167,7 +167,7 @@ int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_
         run_machine_id = prefix_roota(root, "/run/machine-id");
 
         RUN_WITH_UMASK(0022)
-                r = id128_write(run_machine_id, ID128_FORMAT_PLAIN, machine_id, false);
+                r = id128_write(run_machine_id, ID128_FORMAT_PLAIN, machine_id);
         if (r < 0) {
                 (void) unlink(run_machine_id);
                 return log_error_errno(r, "Cannot write %s: %m", run_machine_id);
@@ -260,7 +260,7 @@ int machine_id_commit(const char *root) {
                 return r;
 
         /* Update a persistent version of etc_machine_id */
-        r = id128_write(etc_machine_id, ID128_FORMAT_PLAIN, id, true);
+        r = id128_write(etc_machine_id, ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, id);
         if (r < 0)
                 return log_error_errno(r, "Cannot write %s. This is mandatory to get a persistent machine ID: %m", etc_machine_id);
 
index dccf3b7fb9192c4fc9619079e1de57824f3408f8..6de0cec4264084d30cb771c29b19922579622b3a 100644 (file)
@@ -86,7 +86,7 @@ TEST(id128) {
 
         /* First, write as UUID */
         assert_se(sd_id128_randomize(&id) >= 0);
-        assert_se(id128_write_fd(fd, ID128_FORMAT_UUID, id, false) >= 0);
+        assert_se(id128_write_fd(fd, ID128_FORMAT_UUID, id) >= 0);
 
         assert_se(lseek(fd, 0, SEEK_SET) == 0);
         assert_se(id128_read_fd(fd, ID128_FORMAT_PLAIN, &id2) == -EINVAL);
@@ -104,7 +104,7 @@ TEST(id128) {
         assert_se(ftruncate(fd, 0) >= 0);
 
         assert_se(sd_id128_randomize(&id) >= 0);
-        assert_se(id128_write_fd(fd, ID128_FORMAT_PLAIN, id, false) >= 0);
+        assert_se(id128_write_fd(fd, ID128_FORMAT_PLAIN, id) >= 0);
 
         assert_se(lseek(fd, 0, SEEK_SET) == 0);
         assert_se(id128_read_fd(fd, ID128_FORMAT_UUID, &id2) == -EINVAL);