From 59b903f11cc9ec8780ae333997f274d3ccfe9e01 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Mon, 28 Apr 2025 11:26:22 -0700 Subject: [PATCH] Tolerate short reads from pread() --- main.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index b419f9970..fc1475a7b 100644 --- a/main.cpp +++ b/main.cpp @@ -430,9 +430,20 @@ void *run_sort(void *v) { std::string s; s.resize(end - start); - if (pread(a->indexfd, (void *) s.c_str(), end - start, start) != end - start) { - fprintf(stderr, "pread(index): %s\n", strerror(errno)); - exit(EXIT_READ); + const char *dest = s.c_str(); + ssize_t off = start; + ssize_t count = end - start; + + while (count > 0) { + ssize_t n = pread(a->indexfd, (void *) dest, count, off); + if (n < 0) { + fprintf(stderr, "pread(index): %s\n", strerror(errno)); + exit(EXIT_READ); + } + + dest += n; + off += n; + count -= n; } qsort((void *) s.c_str(), (end - start) / a->bytes, a->bytes, indexcmp);