Since we do not want to modify the current position in file, use pread
instead of read + lseek. Removes one lseek call per module, which for
depmod on Arch Linux means 6143 calls.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/189
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
return NULL;
}
- sz = read_str_safe(file->fd, buf, sizeof(buf));
- lseek(file->fd, 0, SEEK_SET);
+ sz = pread_str_safe(file->fd, buf, sizeof(buf), 0);
if (sz != (sizeof(buf) - 1)) {
if (sz < 0)
errno = -sz;
/* read-like and fread-like functions */
/* ************************************************************************ */
+ssize_t pread_str_safe(int fd, char *buf, size_t buflen, off_t off)
+{
+ size_t todo = buflen - 1;
+ size_t done = 0;
+
+ assert_cc(EAGAIN == EWOULDBLOCK);
+
+ do {
+ ssize_t r = pread(fd, buf + done, todo, off + done);
+
+ if (r == 0)
+ break;
+ else if (r > 0) {
+ todo -= r;
+ done += r;
+ } else {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+ else
+ return -errno;
+ }
+ } while (todo > 0);
+
+ buf[done] = '\0';
+ return done;
+}
+
ssize_t read_str_safe(int fd, char *buf, size_t buflen)
{
size_t todo = buflen - 1;
/* read-like and fread-like functions */
/* ************************************************************************ */
+_must_check_ _nonnull_(2) ssize_t pread_str_safe(int fd, char *buf, size_t buflen,
+ off_t off);
_must_check_ _nonnull_(2) ssize_t read_str_safe(int fd, char *buf, size_t buflen);
_nonnull_(2) ssize_t write_str_safe(int fd, const char *buf, size_t buflen);
_must_check_ _nonnull_(2) int read_str_long(int fd, long *value, int base);