From: Harald Seiler Date: Sun, 6 Sep 2020 19:23:36 +0000 (+0200) Subject: id128: add format which treats "uninitialized" like an empty id X-Git-Tag: v247-rc1~44^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8085114828c3b07406298f0fa89d368413978e20;p=thirdparty%2Fsystemd.git id128: add format which treats "uninitialized" like an empty id Add a new ID128_PLAIN_OR_UNINIT format which treats the string "uninitialized" like the file was empty and return -ENOMEDIUM. This format should be used when reading an /etc/machine-id file from an image that is not currently running. --- diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c index 335f22b9208..ebbfb2d32eb 100644 --- a/src/libsystemd/sd-id128/id128-util.c +++ b/src/libsystemd/sd-id128/id128-util.c @@ -10,6 +10,7 @@ #include "id128-util.h" #include "io-util.h" #include "stdio-util.h" +#include "string-util.h" char *id128_to_uuid_string(sd_id128_t id, char s[static ID128_UUID_STRING_MAX]) { unsigned n, k = 0; @@ -97,6 +98,11 @@ int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) { switch (l) { + case 13: + case 14: + /* Treat an "uninitialized" id file like an empty one */ + return f == ID128_PLAIN_OR_UNINIT && strneq(buffer, "uninitialized\n", l) ? -ENOMEDIUM : -EINVAL; + case 33: /* plain UUID with trailing newline */ if (buffer[32] != '\n') return -EINVAL; @@ -115,7 +121,7 @@ int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) { _fallthrough_; case 36: /* RFC UUID without trailing newline */ - if (f == ID128_PLAIN) + if (IN_SET(f, ID128_PLAIN, ID128_PLAIN_OR_UNINIT)) return -EINVAL; buffer[36] = 0; diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h index 1901bf119ff..1453c00f2f1 100644 --- a/src/libsystemd/sd-id128/id128-util.h +++ b/src/libsystemd/sd-id128/id128-util.h @@ -17,6 +17,10 @@ bool id128_is_valid(const char *s) _pure_; typedef enum Id128Format { ID128_ANY, ID128_PLAIN, /* formatted as 32 hex chars as-is */ + ID128_PLAIN_OR_UNINIT, /* formatted as 32 hex chars as-is; allow special "uninitialized" + * value when reading from file (id128_read() and id128_read_fd()). + * + * This format should be used when reading a machine-id file. */ ID128_UUID, /* formatted as 36 character uuid string */ _ID128_FORMAT_MAX, } Id128Format;