llama: handle short reads in direct I/O path (#18504)
This commit is contained in:
+11
-4
@@ -240,9 +240,10 @@ struct llama_file::impl {
|
|||||||
throw std::runtime_error("unexpectedly reached end of file");
|
throw std::runtime_error("unexpectedly reached end of file");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bool successful = false;
|
size_t bytes_read = 0;
|
||||||
while (!successful) {
|
while (bytes_read < len) {
|
||||||
off_t ret = read(fd, ptr, len);
|
const size_t to_read = len - bytes_read;
|
||||||
|
ssize_t ret = ::read(fd, reinterpret_cast<char *>(ptr) + bytes_read, to_read);
|
||||||
|
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
@@ -251,10 +252,16 @@ struct llama_file::impl {
|
|||||||
throw std::runtime_error(format("read error: %s", strerror(errno)));
|
throw std::runtime_error(format("read error: %s", strerror(errno)));
|
||||||
}
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
|
// EOF: allow if this read was only pulling alignment padding past file end
|
||||||
|
off_t pos = lseek(fd, 0, SEEK_CUR);
|
||||||
|
if (pos != -1 && (size_t) pos == size) {
|
||||||
|
std::memset(reinterpret_cast<char *>(ptr) + bytes_read, 0, len - bytes_read);
|
||||||
|
return;
|
||||||
|
}
|
||||||
throw std::runtime_error("unexpectedly reached end of file");
|
throw std::runtime_error("unexpectedly reached end of file");
|
||||||
}
|
}
|
||||||
|
|
||||||
successful = true;
|
bytes_read += (size_t) ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user