diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2015-03-09 21:23:53 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2015-03-09 22:10:54 -0400 |
commit | a6dcc7e5924f9c27d3e9c6560a448b02ec28b65f (patch) | |
tree | 694aa3c7b5b2f4e61fb8c25d5bd8d1f023cf511e /src/libsystemd/sd-id128/sd-id128.c | |
parent | ad7bcf526d5ec54838bc9411a0e09a293845a015 (diff) |
Introduce loop_read_exact helper
Usually when using loop_read(), we want to read the full buffer.
Add a helper that mirrors loop_write(), and returns 0 when full buffer
was read, and an error otherwise.
Use -ENODATA for the short read, to distinguish it from a read error.
Diffstat (limited to 'src/libsystemd/sd-id128/sd-id128.c')
-rw-r--r-- | src/libsystemd/sd-id128/sd-id128.c | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c index c876f6e381..f0ffedc38b 100644 --- a/src/libsystemd/sd-id128/sd-id128.c +++ b/src/libsystemd/sd-id128/sd-id128.c @@ -108,9 +108,9 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { static thread_local bool saved_machine_id_valid = false; _cleanup_close_ int fd = -1; char buf[33]; - ssize_t k; unsigned j; sd_id128_t t; + int r; assert_return(ret, -EINVAL); @@ -123,13 +123,9 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { if (fd < 0) return -errno; - k = loop_read(fd, buf, 33, false); - if (k < 0) - return (int) k; - - if (k != 33) - return -EIO; - + r = loop_read_exact(fd, buf, 33, false); + if (r < 0) + return r; if (buf[32] !='\n') return -EIO; @@ -157,10 +153,10 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) { static thread_local bool saved_boot_id_valid = false; _cleanup_close_ int fd = -1; char buf[36]; - ssize_t k; unsigned j; sd_id128_t t; char *p; + int r; assert_return(ret, -EINVAL); @@ -173,22 +169,19 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) { if (fd < 0) return -errno; - k = loop_read(fd, buf, 36, false); - if (k < 0) - return (int) k; - - if (k != 36) - return -EIO; + r = loop_read_exact(fd, buf, 36, false); + if (r < 0) + return r; for (j = 0, p = buf; j < 16; j++) { int a, b; - if (p >= buf + k - 1) + if (p >= buf + 35) return -EIO; if (*p == '-') { p++; - if (p >= buf + k - 1) + if (p >= buf + 35) return -EIO; } |