From ef84329a81d9082e23aafc2f1a06599d7b3611f5 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Fri, 27 Jan 2023 14:17:30 +0000 Subject: util/nvmutil: optimise rhex() for speed don't constantly open/close the file: /dev/urandom only read 12 bytes at a time because of this change, the readFromFile() function now only handles gbe files --- util/nvmutil/nvmutil.c | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) (limited to 'util') diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index ef89876b..e2dafec7 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -32,8 +32,8 @@ #include #include -ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags, - size_t size); +ssize_t readGbeFile(int *fd, uint8_t *buf, const char *path, int flags, + size_t nr); void cmd_setmac(const char *strMac); uint8_t hextonum(char chs); uint8_t rhex(void); @@ -119,7 +119,7 @@ main(int argc, char *argv[]) if ((strMac == NULL) && (cmd == NULL)) errno = EINVAL; - else if (readFromFile(&fd, buf, FILENAME, flags, SIZE_8KB) != SIZE_8KB) + else if (readGbeFile(&fd, buf, FILENAME, flags, SIZE_8KB) != SIZE_8KB) goto nvmutil_exit; if (errno == 0) { @@ -141,31 +141,29 @@ nvmutil_exit: } ssize_t -readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size) +readGbeFile(int *fd, uint8_t *buf, const char *path, int flags, size_t nr) { struct stat st; if (opendir(path) != NULL) { errno = EISDIR; return -1; - } else if (((*fd) = open(path, flags)) == -1) { + } + if (((*fd) = open(path, flags)) == -1) { + return -1; + } + if (fstat((*fd), &st) == -1) + return -1; + if ((st.st_size != SIZE_8KB)) { + fprintf(stderr, "%s: Bad file size\n", path); + errno = ECANCELED; return -1; - } else if (size == SIZE_8KB) { - if (fstat((*fd), &st) == -1) - return -1; - if ((st.st_size != SIZE_8KB) && strcmp(path, "/dev/urandom")) { - fprintf(stderr, "Bad file size\n"); - errno = ECANCELED; - return -1; - } } - if (errno == ENOTDIR) errno = 0; - else if (errno != 0) + if (errno != 0) return -1; - - return read((*fd), buf, size); + return read((*fd), buf, nr); } void @@ -251,23 +249,17 @@ rhex(void) { static int rfd = -1; static uint8_t *rbuf = NULL; - static size_t rindex = BUFSIZ; - int bsize = BUFSIZ; + static size_t rindex = 12; - if (rindex == bsize) { + if (rindex == 12) { rindex = 0; if (rbuf == NULL) - if ((rbuf = (uint8_t *) malloc(bsize)) == NULL) + if ((rbuf = (uint8_t *) malloc(BUFSIZ)) == NULL) err(errno, NULL); - if (rfd != -1) { - if (close(rfd)) + if (rfd == -1) + if ((rfd = open("/dev/urandom", O_RDONLY)) == -1) err(errno, "/dev/urandom"); - rfd = -1; - } - if (readFromFile(&rfd, rbuf, "/dev/urandom", O_RDONLY, bsize) - != bsize) - err(errno, "/dev/urandom"); - if (errno != 0) + if (read(rfd, rbuf, 12) == -1) err(errno, "/dev/urandom"); } -- cgit v1.2.1