[multiple] remove chunk file.start member

c->offset is now offset into file
c->file.length is end of octets to send (end pos + 1)
c->file.length - c->offset is num of octets to send
This commit is contained in:
Glenn Strauss 2020-10-14 13:08:38 -04:00
parent d865d8c330
commit 3f1a12e5fb
6 changed files with 45 additions and 49 deletions

View File

@ -74,29 +74,31 @@ chunkqueue *chunkqueue_init(chunkqueue *cq) {
__attribute_returns_nonnull__
static chunk *chunk_init(size_t sz) {
chunk *c;
c = calloc(1, sizeof(*c));
chunk * const restrict c = calloc(1, sizeof(*c));
force_assert(NULL != c);
#if 0 /*(zeroed by calloc())*/
c->type = MEM_CHUNK;
c->mem = buffer_init();
c->file.start = c->file.length = c->file.mmap.offset = 0;
c->next = NULL;
c->offset = 0;
c->file.length = 0;
c->file.mmap.length = c->file.mmap.offset = 0;
c->file.is_temp = 0;
#endif
c->file.fd = -1;
c->file.mmap.start = MAP_FAILED;
c->file.mmap.length = 0;
c->file.is_temp = 0;
c->offset = 0;
c->next = NULL;
c->mem = buffer_init();
buffer_string_prepare_copy(c->mem, sz-1);
return c;
}
static void chunk_reset_file_chunk(chunk *c) {
if (c->file.is_temp && !chunk_buffer_string_is_empty(c->mem)) {
unlink(c->mem->ptr);
if (c->file.is_temp) {
c->file.is_temp = 0;
if (!chunk_buffer_string_is_empty(c->mem))
unlink(c->mem->ptr);
}
if (c->file.fd != -1) {
close(c->file.fd);
@ -105,10 +107,9 @@ static void chunk_reset_file_chunk(chunk *c) {
if (MAP_FAILED != c->file.mmap.start) {
munmap(c->file.mmap.start, c->file.mmap.length);
c->file.mmap.start = MAP_FAILED;
c->file.mmap.length = c->file.mmap.offset = 0;
}
c->file.start = c->file.length = c->file.mmap.offset = 0;
c->file.mmap.length = 0;
c->file.is_temp = 0;
c->file.length = 0;
c->type = MEM_CHUNK;
}
@ -277,8 +278,8 @@ static chunk * chunkqueue_append_file_chunk(chunkqueue * const restrict cq, cons
chunk *c = chunk_acquire(buffer_string_length(fn)+1);
chunkqueue_append_chunk(cq, c);
c->type = FILE_CHUNK;
c->file.start = offset;
c->file.length = len;
c->offset = offset;
c->file.length = offset + len;
cq->bytes_in += len;
buffer_copy_buffer(c->mem, fn);
return c;
@ -506,7 +507,7 @@ void chunkqueue_steal(chunkqueue * const restrict dest, chunkqueue * const restr
break;
case FILE_CHUNK:
/* tempfile flag is in "last" chunk after the split */
chunkqueue_append_file(dest, c->mem, c->file.start + c->offset, use);
chunkqueue_append_file(dest, c->mem, c->offset, use);
if (c->file.fd >= 0)
dest->last->file.fd = fdevent_dup_cloexec(c->file.fd);
break;
@ -680,7 +681,7 @@ int chunkqueue_steal_with_tempfiles(chunkqueue * const restrict dest, chunkqueue
} else {
/* partial chunk with length "use" */
/* tempfile flag is in "last" chunk after the split */
chunkqueue_append_file(dest, c->mem, c->file.start + c->offset, use);
chunkqueue_append_file(dest, c->mem, c->offset, use);
if (c->file.fd >= 0)
dest->last->file.fd = fdevent_dup_cloexec(c->file.fd);
@ -814,7 +815,7 @@ static int chunk_open_file_chunk(chunk * const restrict c, log_error_st * const
force_assert(FILE_CHUNK == c->type);
force_assert(c->offset >= 0 && c->offset <= c->file.length);
offset = c->file.start + c->offset;
offset = c->offset;
toSend = c->file.length - c->offset;
if (-1 == c->file.fd) {
@ -864,7 +865,7 @@ 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->file.start + filec->offset;
off_t offset = filec->offset;
if (0 != offset && -1 == lseek(fd, offset, SEEK_SET)) return;
/* Note: there should be no size change in chunkqueue,
@ -920,7 +921,7 @@ chunkqueue_peek_data (chunkqueue * const cq,
case FILE_CHUNK:
if (c->file.fd >= 0 || 0 == chunk_open_file_chunk(c, errh)) {
off_t offset = c->file.start + c->offset;
off_t offset = c->offset;
off_t toSend = c->file.length - c->offset;
if (toSend > (off_t)space)
toSend = (off_t)space;

View File

@ -22,15 +22,14 @@ typedef struct chunk {
buffer *mem; /* either the storage of the mem-chunk or the name of the file */
/* the size of the chunk is either:
* - mem-chunk: buffer_string_length(chunk::mem)
* - file-chunk: chunk::file.length
* - mem-chunk: buffer_string_length(chunk::mem) - c->offset
* - file-chunk: chunk::file.length - c->offset
*/
off_t offset; /* octets sent from this chunk */
off_t offset;
struct {
/* filechunk */
off_t start; /* starting offset in the file */
off_t length; /* octets to send from the starting offset */
off_t length; /* end pos + 1 in file (octets to send: file.length - c->offset) */
int fd;
int is_temp; /* file is temporary and will be deleted if on cleanup */

View File

@ -555,7 +555,7 @@ static ssize_t cgi_write_file_chunk_mmap(request_st * const r, int fd, chunkqueu
force_assert(FILE_CHUNK == c->type);
force_assert(c->offset >= 0 && c->offset <= c->file.length);
offset = c->file.start + c->offset;
offset = c->offset;
toSend = c->file.length - c->offset;
if (0 == toSend) {
@ -570,7 +570,7 @@ static ssize_t cgi_write_file_chunk_mmap(request_st * const r, int fd, chunkqueu
#else
size_t mmap_offset, mmap_avail;
char *data = NULL;
off_t file_end = c->file.start + c->file.length; /* offset to file end in this chunk */
off_t file_end = c->file.length; /* offset to file end in this chunk */
/*(simplified from chunk.c:chunkqueue_open_file_chunk())*/
if (-1 == c->file.fd) {

View File

@ -1053,7 +1053,7 @@ static int mod_deflate_file_chunk(request_st * const r, handler_ctx * const hctx
off_t toSend = -1;
char *start;
#ifdef USE_MMAP
off_t we_want_to_mmap = 2 MByte;
const off_t we_want_to_mmap = 2 MByte; /* must be power-of-2 */
off_t we_want_to_send = st_size;
volatile int mapped = 0;/* quiet warning: might be clobbered by 'longjmp' */
#else
@ -1067,7 +1067,7 @@ static int mod_deflate_file_chunk(request_st * const r, handler_ctx * const hctx
}
}
abs_offset = c->file.start + c->offset;
abs_offset = c->offset;
#ifdef USE_MMAP
/* mmap the buffer
@ -1106,15 +1106,10 @@ static int mod_deflate_file_chunk(request_st * const r, handler_ctx * const hctx
c->file.mmap.offset += we_want_to_mmap;
} else {
/* in case the range-offset is after the first mmap()ed area we skip the area */
c->file.mmap.offset = 0;
while (c->file.mmap.offset + we_want_to_mmap < c->file.start) {
c->file.mmap.offset += we_want_to_mmap;
}
c->file.mmap.offset = c->offset & ~(we_want_to_mmap-1);
}
/* length is rel, c->offset too, assume there is no limit at the mmap-boundaries */
to_mmap = (c->file.start + c->file.length) - c->file.mmap.offset;
to_mmap = c->file.length - c->file.mmap.offset;
if (to_mmap > we_want_to_mmap) to_mmap = we_want_to_mmap;
/* we have more to send than we can mmap() at once */
if (we_want_to_send > to_mmap) we_want_to_send = to_mmap;
@ -1530,7 +1525,7 @@ REQUEST_FUNC(mod_deflate_handle_response_start) {
&& r->resp_body_finished
&& r->write_queue.first == r->write_queue.last
&& r->write_queue.first->type == FILE_CHUNK
&& r->write_queue.first->file.start == 0
&& r->write_queue.first->offset == 0
&& !r->write_queue.first->file.is_temp
&& !light_btst(r->resp_htags, HTTP_HEADER_RANGE)) {
tb = mod_deflate_cache_file_name(r, p->conf.cache_dir, vb);

View File

@ -3418,7 +3418,7 @@ webdav_mmap_file_chunk (chunk * const c)
* be able to be re-opened if it was a tmpfile that was unlinked */
/*assert(c->type == FILE_CHUNK);*/
if (MAP_FAILED != c->file.mmap.start)
return c->file.mmap.start + c->offset;
return c->file.mmap.start + c->offset - c->file.mmap.offset;
if (webdav_open_chunk_file_rd(c) < 0)
return NULL;
@ -3432,7 +3432,7 @@ webdav_mmap_file_chunk (chunk * const c)
close(c->file.fd);
c->file.fd = -1;
c->file.mmap.length = c->file.length;
return c->file.mmap.start + c->offset;
return c->file.mmap.start + c->offset - c->file.mmap.offset;
}
@ -3466,7 +3466,7 @@ webdav_parse_chunkqueue (request_st * const r,
}
else if (c->type == FILE_CHUNK) {
xmlstr = webdav_mmap_file_chunk(c);
/*xmlstr = c->file.mmap.start + c->offset;*/
/*xmlstr = c->file.mmap.start + c->offset - c->file.mmap.offset;*/
if (NULL != xmlstr) {
weHave = c->file.length - c->offset;
}
@ -3487,7 +3487,7 @@ webdav_parse_chunkqueue (request_st * const r,
}
ssize_t rd = -1;
do {
if (-1 ==lseek(c->file.fd,c->file.start+c->offset,SEEK_SET))
if (-1 == lseek(c->file.fd, c->offset, SEEK_SET))
break;
off_t len = c->file.length - c->offset;
if (len > (off_t)sizeof(buf)) len = (off_t)sizeof(buf);
@ -4149,7 +4149,8 @@ mod_webdav_write_cq_first_chunk (request_st * const r, chunkqueue * const cq,
case FILE_CHUNK:
if (NULL != webdav_mmap_file_chunk(c)) {
do {
wr = write(fd, c->file.mmap.start+c->offset,
wr = write(fd,
c->file.mmap.start + c->offset - c->file.mmap.offset,
c->file.length - c->offset);
} while (-1 == wr && errno == EINTR);
break;
@ -4170,7 +4171,7 @@ mod_webdav_write_cq_first_chunk (request_st * const r, chunkqueue * const cq,
ssize_t rd = -1;
char buf[16384];
do {
if (-1 == lseek(c->file.fd, c->file.start+c->offset, SEEK_SET))
if (-1 == lseek(c->file.fd, c->offset, SEEK_SET))
break;
off_t len = c->file.length - c->offset;
if (len > (off_t)sizeof(buf)) len = (off_t)sizeof(buf);
@ -4437,7 +4438,7 @@ mod_webdav_put_linkat_rename (request_st * const r,
webdav_response_etag(r, &st);
}
chunkqueue_mark_written(cq, c->file.length);
chunkqueue_mark_written(cq, c->file.length); /*(c->offset == 0)*/
return 1;
}

View File

@ -148,7 +148,7 @@ static int network_write_file_chunk_no_mmap(int fd, chunkqueue *cq, off_t *p_max
force_assert(c->offset >= 0 && c->offset <= c->file.length);
offset = c->file.start + c->offset;
offset = c->offset;
toSend = c->file.length - c->offset;
if (toSend > *p_max_bytes) toSend = *p_max_bytes;
@ -223,10 +223,10 @@ static int network_write_file_chunk_mmap(int fd, chunkqueue *cq, off_t *p_max_by
force_assert(c->offset >= 0 && c->offset <= c->file.length);
offset = c->file.start + c->offset;
offset = c->offset;
toSend = c->file.length - c->offset;
if (toSend > *p_max_bytes) toSend = *p_max_bytes;
file_end = c->file.start + c->file.length; /*file end offset in this chunk*/
file_end = c->file.length; /*file end offset in this chunk*/
if (0 == toSend) {
chunkqueue_remove_finished_chunks(cq);
@ -443,7 +443,7 @@ static int network_write_file_chunk_sendfile(int fd, chunkqueue *cq, off_t *p_ma
force_assert(c->offset >= 0 && c->offset <= c->file.length);
offset = c->file.start + c->offset;
offset = c->offset;
toSend = c->file.length - c->offset;
if (toSend > *p_max_bytes) toSend = *p_max_bytes;