id128-util: add id128_from_string_not_null()

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-08-29 17:06:47 +03:00
parent f4e518e812
commit 7c9de5d85d
3 changed files with 27 additions and 0 deletions
+17
View File
@@ -13,6 +13,23 @@
#include "string-util.h"
#include "sync-util.h"
int id128_from_string_nonzero(const char *s, sd_id128_t *ret) {
sd_id128_t t;
int r;
assert(ret);
r = sd_id128_from_string(ASSERT_PTR(s), &t);
if (r < 0)
return r;
if (sd_id128_is_null(t))
return -ENXIO;
*ret = t;
return 0;
}
bool id128_is_valid(const char *s) {
size_t l;
+2
View File
@@ -21,6 +21,8 @@ typedef enum Id128Flag {
ID128_REFUSE_NULL = 1 << 3, /* Refuse all zero ID with -ENOMEDIUM. */
} Id128Flag;
int id128_from_string_nonzero(const char *s, sd_id128_t *ret);
int id128_read_fd(int fd, 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) {
+8
View File
@@ -20,6 +20,7 @@
#define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
#define STR_WALDI "0102030405060708090a0b0c0d0e0f10"
#define UUID_WALDI "01020304-0506-0708-090a-0b0c0d0e0f10"
#define STR_NULL "00000000000000000000000000000000"
TEST(id128) {
sd_id128_t id, id2;
@@ -75,6 +76,13 @@ TEST(id128) {
assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0);
assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0);
assert_se(id128_from_string_nonzero(STR_WALDI, &id) == 0);
assert_se(id128_from_string_nonzero(STR_NULL, &id) == -ENXIO);
assert_se(id128_from_string_nonzero("01020304-0506-0708-090a-0b0c0d0e0f101", &id) < 0);
assert_se(id128_from_string_nonzero("01020304-0506-0708-090a-0b0c0d0e0f10-", &id) < 0);
assert_se(id128_from_string_nonzero("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0);
assert_se(id128_from_string_nonzero("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0);
assert_se(id128_is_valid(STR_WALDI));
assert_se(id128_is_valid(UUID_WALDI));
assert_se(!id128_is_valid(""));