[core] use pread() to skip lseek()

master
Glenn Strauss 2 years ago
parent 60a1224cd0
commit 042622c8c1

@ -1362,7 +1362,9 @@ chunkqueue_small_resp_optim (chunkqueue * const restrict cq)
const int fd = filec->file.fd;
if (fd < 0) return; /*(require that file already be open)*/
off_t offset = filec->offset;
#ifndef HAVE_PREAD
if (-1 == lseek(fd, offset, SEEK_SET)) return;
#endif
/* Note: there should be no size change in chunkqueue,
* so cq->bytes_in and cq->bytes_out should not be modified */
@ -1378,9 +1380,16 @@ chunkqueue_small_resp_optim (chunkqueue * const restrict cq)
char * const ptr = b->ptr + buffer_clen(b);
ssize_t rd;
#ifdef HAVE_PREAD
const off_t foff = offset;
#endif
offset = 0; /*(reuse offset var for offset into mem buffer)*/
do {
#ifdef HAVE_PREAD
rd =pread(fd, ptr+offset, (size_t)len, foff+offset);
#else
rd = read(fd, ptr+offset, (size_t)len);
#endif
} while (rd > 0 ? (offset += rd, len -= rd) : errno == EINTR);
/*(contents of chunkqueue kept valid even if error reading from file)*/
if (len)
@ -1425,14 +1434,20 @@ chunkqueue_peek_data (chunkqueue * const cq,
if (0 == len)
break;
#ifndef HAVE_PREAD
if (-1 == lseek(c->file.fd, offset, SEEK_SET)) {
log_perror(errh, __FILE__, __LINE__, "lseek(\"%s\")",
c->mem->ptr);
return -1;
}
#endif
ssize_t rd;
do {
#ifdef HAVE_PREAD
rd =pread(c->file.fd, data_in + *dlen, (size_t)len, offset);
#else
rd = read(c->file.fd, data_in + *dlen, (size_t)len);
#endif
} while (-1 == rd && errno == EINTR);
if (rd <= 0) { /* -1 error; 0 EOF (unexpected) */
log_perror(errh, __FILE__, __LINE__, "read(\"%s\")",

@ -71,12 +71,21 @@ static int http_chunk_append_read_fd_range(request_st * const r, const buffer *
if (r->resp_send_chunked)
http_chunk_len_append(cq, (uintmax_t)len);
#ifndef HAVE_PREAD
if (-1 == lseek(fd, offset, SEEK_SET)) return -1;
#endif
buffer * const b = chunkqueue_append_buffer_open_sz(cq, len+2+1);
ssize_t rd;
#ifdef HAVE_PREAD
const off_t foff = offset;
#endif
offset = 0;
do {
#ifdef HAVE_PREAD
rd =pread(fd, b->ptr+offset, (size_t)(len-offset), foff+offset);
#else
rd = read(fd, b->ptr+offset, (size_t)(len-offset));
#endif
} while (rd > 0 ? (offset += rd) != len : errno == EINTR);
buffer_commit(b, offset);

@ -163,11 +163,16 @@ static int network_write_file_chunk_no_mmap(const int fd, chunkqueue * const cq,
if (toSend > (off_t)sizeof(buf)) toSend = (off_t)sizeof(buf);
#ifndef HAVE_PREAD
if (-1 == lseek(c->file.fd, offset, SEEK_SET)) {
log_perror(errh, __FILE__, __LINE__, "lseek");
return -1;
}
if ((toSend = read(c->file.fd, buf, toSend)) <= 0) {
toSend = read(c->file.fd, buf, toSend);
#else
toSend =pread(c->file.fd, buf, toSend, offset);
#endif
if (toSend <= 0) {
log_perror(errh, __FILE__, __LINE__, "read");/* err or unexpected EOF */
return -1;
}

Loading…
Cancel
Save