diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-06 18:36:29 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-06 18:36:29 +0000 |
| commit | 82fdee91fad190d4d4c7354d707b7c72fb877493 (patch) | |
| tree | 0247f2db777e7b7fd08f30ce3f61448bc9746556 | |
| parent | 58b17c98fdef1ded74c1028329cf88753157f4b7 (diff) | |
util/nvmutil: clean up file handling
inline check_read_or_die
Signed-off-by: Leah Rowe <leah@libreboot.org>
| -rw-r--r-- | util/nvmutil/nvmutil.c | 49 |
1 files changed, 17 insertions, 32 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 457f6d00..39302fdc 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -30,8 +30,6 @@ static uint8_t hextonum(char); static uint8_t rhex(void); static void read_file_PERFECTLY_or_die(int, void *, size_t, off_t, const char *, const char *); -static int check_read_or_die(const char *, - ssize_t, size_t, int, const char *); static int write_mac_part(int); static void cmd_dump(void); static void print_mac_address(int); @@ -504,46 +502,33 @@ static void read_file_PERFECTLY_or_die(int fd, void *buf, size_t len, off_t off, const char *path, const char *op) { - int retry; ssize_t rval; + int retry; for (retry = 0; retry < MAX_RETRY_READ; retry++) { - if (op == NULL) - rval = read(fd, buf, len); - else + if (op) rval = pread(fd, buf, len, off); + else + rval = read(fd, buf, len); - if (check_read_or_die(path, rval, len, retry, - op ? op : "read")) + if (rval == (ssize_t)len) { + errno = 0; return; - } + } - err(EINTR, "%s: max retries exceeded on file: %s", - op ? op : "read", path); -} + if (rval != -1) + err(ECANCELED, + "Short %s, %zd bytes, on file: %s", + op ? op : "read", rval, path); -static int -check_read_or_die(const char *rpath, ssize_t rval, size_t rsize, - int retry, const char *readtype) -{ - if (rval == (ssize_t)rsize) { - errno = 0; - return 1; /* Successful read */ + if (errno != EINTR) + err(ECANCELED, + "Could not %s file: '%s'", + op ? op : "read", path); } - if (rval != -1) - err(ECANCELED, "Short %s, %zd bytes, on file: %s", - readtype, rval, rpath); - if (errno != EINTR) - err(ECANCELED, "Could not %s file: '%s'", readtype, rpath); - if (retry == MAX_RETRY_READ - 1) - err(EINTR, "%s: max retries exceeded on file: %s", - readtype, rpath); - - /* - * Bad read, with errno EINTR (syscall interrupted). - */ - return 0; + err(EINTR, "%s: max retries exceeded on file: %s", + op ? op : "read", path); } static int |
