[core] consolidate duplicated read-to-close code
[lighttpd.git] / src / chunk.c
blobfd431a226a0e30ab390e8a84c1e08a9ebef83399
1 #include "first.h"
3 /**
4 * the network chunk-API
7 */
9 #include "chunk.h"
10 #include "base.h"
11 #include "log.h"
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include "sys-mmap.h"
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <unistd.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <string.h>
25 /* default 1MB, upper limit 128MB */
26 #define DEFAULT_TEMPFILE_SIZE (1 * 1024 * 1024)
27 #define MAX_TEMPFILE_SIZE (128 * 1024 * 1024)
29 static array *chunkqueue_default_tempdirs = NULL;
30 static unsigned int chunkqueue_default_tempfile_size = DEFAULT_TEMPFILE_SIZE;
32 chunkqueue *chunkqueue_init(void) {
33 chunkqueue *cq;
35 cq = calloc(1, sizeof(*cq));
36 force_assert(NULL != cq);
38 cq->first = NULL;
39 cq->last = NULL;
41 cq->unused = NULL;
43 cq->tempdirs = chunkqueue_default_tempdirs;
44 cq->upload_temp_file_size = chunkqueue_default_tempfile_size;
46 return cq;
49 static chunk *chunk_init(void) {
50 chunk *c;
52 c = calloc(1, sizeof(*c));
53 force_assert(NULL != c);
55 c->type = MEM_CHUNK;
56 c->mem = buffer_init();
57 c->file.name = buffer_init();
58 c->file.start = c->file.length = c->file.mmap.offset = 0;
59 c->file.fd = -1;
60 c->file.mmap.start = MAP_FAILED;
61 c->file.mmap.length = 0;
62 c->file.is_temp = 0;
63 c->offset = 0;
64 c->next = NULL;
66 return c;
69 static void chunk_reset(chunk *c) {
70 if (NULL == c) return;
72 c->type = MEM_CHUNK;
74 buffer_reset(c->mem);
76 if (c->file.is_temp && !buffer_string_is_empty(c->file.name)) {
77 unlink(c->file.name->ptr);
80 buffer_reset(c->file.name);
82 if (c->file.fd != -1) {
83 close(c->file.fd);
84 c->file.fd = -1;
86 if (MAP_FAILED != c->file.mmap.start) {
87 munmap(c->file.mmap.start, c->file.mmap.length);
88 c->file.mmap.start = MAP_FAILED;
90 c->file.start = c->file.length = c->file.mmap.offset = 0;
91 c->file.mmap.length = 0;
92 c->file.is_temp = 0;
93 c->offset = 0;
94 c->next = NULL;
97 static void chunk_free(chunk *c) {
98 if (NULL == c) return;
100 chunk_reset(c);
102 buffer_free(c->mem);
103 buffer_free(c->file.name);
105 free(c);
108 static off_t chunk_remaining_length(const chunk *c) {
109 off_t len = 0;
110 switch (c->type) {
111 case MEM_CHUNK:
112 len = buffer_string_length(c->mem);
113 break;
114 case FILE_CHUNK:
115 len = c->file.length;
116 break;
117 default:
118 force_assert(c->type == MEM_CHUNK || c->type == FILE_CHUNK);
119 break;
121 force_assert(c->offset <= len);
122 return len - c->offset;
125 void chunkqueue_free(chunkqueue *cq) {
126 chunk *c, *pc;
128 if (NULL == cq) return;
130 for (c = cq->first; c; ) {
131 pc = c;
132 c = c->next;
133 chunk_free(pc);
136 for (c = cq->unused; c; ) {
137 pc = c;
138 c = c->next;
139 chunk_free(pc);
142 free(cq);
145 static void chunkqueue_push_unused_chunk(chunkqueue *cq, chunk *c) {
146 force_assert(NULL != cq && NULL != c);
148 /* keep at max 4 chunks in the 'unused'-cache */
149 if (cq->unused_chunks > 4) {
150 chunk_free(c);
151 } else {
152 chunk_reset(c);
153 c->next = cq->unused;
154 cq->unused = c;
155 cq->unused_chunks++;
159 static chunk *chunkqueue_get_unused_chunk(chunkqueue *cq) {
160 chunk *c;
162 force_assert(NULL != cq);
164 /* check if we have a unused chunk */
165 if (0 == cq->unused) {
166 c = chunk_init();
167 } else {
168 /* take the first element from the list (a stack) */
169 c = cq->unused;
170 cq->unused = c->next;
171 c->next = NULL;
172 cq->unused_chunks--;
175 return c;
178 static void chunkqueue_prepend_chunk(chunkqueue *cq, chunk *c) {
179 c->next = cq->first;
180 cq->first = c;
182 if (NULL == cq->last) {
183 cq->last = c;
185 cq->bytes_in += chunk_remaining_length(c);
188 static void chunkqueue_append_chunk(chunkqueue *cq, chunk *c) {
189 c->next = NULL;
190 if (cq->last) {
191 cq->last->next = c;
193 cq->last = c;
195 if (NULL == cq->first) {
196 cq->first = c;
198 cq->bytes_in += chunk_remaining_length(c);
201 void chunkqueue_reset(chunkqueue *cq) {
202 chunk *cur = cq->first;
204 cq->first = cq->last = NULL;
206 while (NULL != cur) {
207 chunk *next = cur->next;
208 chunkqueue_push_unused_chunk(cq, cur);
209 cur = next;
212 cq->bytes_in = 0;
213 cq->bytes_out = 0;
214 cq->tempdir_idx = 0;
217 void chunkqueue_append_file_fd(chunkqueue *cq, buffer *fn, int fd, off_t offset, off_t len) {
218 chunk *c;
220 if (0 == len) {
221 close(fd);
222 return;
225 c = chunkqueue_get_unused_chunk(cq);
227 c->type = FILE_CHUNK;
229 buffer_copy_buffer(c->file.name, fn);
230 c->file.start = offset;
231 c->file.length = len;
232 c->file.fd = fd;
233 c->offset = 0;
235 chunkqueue_append_chunk(cq, c);
238 void chunkqueue_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len) {
239 chunk *c;
241 if (0 == len) return;
243 c = chunkqueue_get_unused_chunk(cq);
245 c->type = FILE_CHUNK;
247 buffer_copy_buffer(c->file.name, fn);
248 c->file.start = offset;
249 c->file.length = len;
250 c->offset = 0;
252 chunkqueue_append_chunk(cq, c);
255 void chunkqueue_append_buffer(chunkqueue *cq, buffer *mem) {
256 chunk *c;
258 if (buffer_string_is_empty(mem)) return;
260 c = chunkqueue_get_unused_chunk(cq);
261 c->type = MEM_CHUNK;
262 force_assert(NULL != c->mem);
263 buffer_move(c->mem, mem);
265 chunkqueue_append_chunk(cq, c);
268 void chunkqueue_prepend_buffer(chunkqueue *cq, buffer *mem) {
269 chunk *c;
271 if (buffer_string_is_empty(mem)) return;
273 c = chunkqueue_get_unused_chunk(cq);
274 c->type = MEM_CHUNK;
275 force_assert(NULL != c->mem);
276 buffer_move(c->mem, mem);
278 chunkqueue_prepend_chunk(cq, c);
282 void chunkqueue_append_mem(chunkqueue *cq, const char * mem, size_t len) {
283 chunk *c;
285 if (0 == len) return;
287 c = chunkqueue_get_unused_chunk(cq);
288 c->type = MEM_CHUNK;
289 buffer_copy_string_len(c->mem, mem, len);
291 chunkqueue_append_chunk(cq, c);
295 void chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src) {
296 if (src == NULL || NULL == src->first) return;
298 if (NULL == cq->first) {
299 cq->first = src->first;
300 } else {
301 cq->last->next = src->first;
303 cq->last = src->last;
304 cq->bytes_in += (src->bytes_in - src->bytes_out);
306 src->first = NULL;
307 src->last = NULL;
308 src->bytes_out = src->bytes_in;
312 void chunkqueue_get_memory(chunkqueue *cq, char **mem, size_t *len, size_t min_size, size_t alloc_size) {
313 static const size_t REALLOC_MAX_SIZE = 256;
314 chunk *c;
315 buffer *b;
316 char *dummy_mem;
317 size_t dummy_len;
319 force_assert(NULL != cq);
320 if (NULL == mem) mem = &dummy_mem;
321 if (NULL == len) len = &dummy_len;
323 /* default values: */
324 if (0 == min_size) min_size = 1024;
325 if (0 == alloc_size) alloc_size = 4096;
326 if (alloc_size < min_size) alloc_size = min_size;
328 if (NULL != cq->last && MEM_CHUNK == cq->last->type) {
329 size_t have;
331 b = cq->last->mem;
332 have = buffer_string_space(b);
334 /* unused buffer: allocate space */
335 if (buffer_string_is_empty(b)) {
336 buffer_string_prepare_copy(b, alloc_size);
337 have = buffer_string_space(b);
339 /* if buffer is really small just make it bigger */
340 else if (have < min_size && b->size <= REALLOC_MAX_SIZE) {
341 size_t cur_len = buffer_string_length(b);
342 size_t new_size = cur_len + min_size, append;
343 if (new_size < alloc_size) new_size = alloc_size;
345 append = new_size - cur_len;
346 if (append >= min_size) {
347 buffer_string_prepare_append(b, append);
348 have = buffer_string_space(b);
352 /* return pointer into existing buffer if large enough */
353 if (have >= min_size) {
354 *mem = b->ptr + buffer_string_length(b);
355 *len = have;
356 return;
360 /* allocate new chunk */
361 c = chunkqueue_get_unused_chunk(cq);
362 c->type = MEM_CHUNK;
363 chunkqueue_append_chunk(cq, c);
365 b = c->mem;
366 buffer_string_prepare_append(b, alloc_size);
368 *mem = b->ptr + buffer_string_length(b);
369 *len = buffer_string_space(b);
372 void chunkqueue_use_memory(chunkqueue *cq, size_t len) {
373 buffer *b;
375 force_assert(NULL != cq);
376 force_assert(NULL != cq->last && MEM_CHUNK == cq->last->type);
377 b = cq->last->mem;
379 if (len > 0) {
380 buffer_commit(b, len);
381 cq->bytes_in += len;
382 } else if (buffer_string_is_empty(b)) {
383 /* unused buffer: can't remove chunk easily from
384 * end of list, so just reset the buffer
386 buffer_reset(b);
390 void chunkqueue_set_tempdirs_default (array *tempdirs, unsigned int upload_temp_file_size) {
391 chunkqueue_default_tempdirs = tempdirs;
392 chunkqueue_default_tempfile_size
393 = (0 == upload_temp_file_size) ? DEFAULT_TEMPFILE_SIZE
394 : (upload_temp_file_size > MAX_TEMPFILE_SIZE) ? MAX_TEMPFILE_SIZE
395 : upload_temp_file_size;
398 #if 0
399 void chunkqueue_set_tempdirs(chunkqueue *cq, array *tempdirs, unsigned int upload_temp_file_size) {
400 force_assert(NULL != cq);
401 cq->tempdirs = tempdirs;
402 cq->upload_temp_file_size
403 = (0 == upload_temp_file_size) ? DEFAULT_TEMPFILE_SIZE
404 : (upload_temp_file_size > MAX_TEMPFILE_SIZE) ? MAX_TEMPFILE_SIZE
405 : upload_temp_file_size;
406 cq->tempdir_idx = 0;
408 #endif
410 void chunkqueue_steal(chunkqueue *dest, chunkqueue *src, off_t len) {
411 while (len > 0) {
412 chunk *c = src->first;
413 off_t clen = 0, use;
415 if (NULL == c) break;
417 clen = chunk_remaining_length(c);
418 if (0 == clen) {
419 /* drop empty chunk */
420 src->first = c->next;
421 if (c == src->last) src->last = NULL;
422 chunkqueue_push_unused_chunk(src, c);
423 continue;
426 use = len >= clen ? clen : len;
427 len -= use;
429 if (use == clen) {
430 /* move complete chunk */
431 src->first = c->next;
432 if (c == src->last) src->last = NULL;
434 chunkqueue_append_chunk(dest, c);
435 } else {
436 /* partial chunk with length "use" */
438 switch (c->type) {
439 case MEM_CHUNK:
440 chunkqueue_append_mem(dest, c->mem->ptr + c->offset, use);
441 break;
442 case FILE_CHUNK:
443 /* tempfile flag is in "last" chunk after the split */
444 chunkqueue_append_file(dest, c->file.name, c->file.start + c->offset, use);
445 break;
448 c->offset += use;
449 force_assert(0 == len);
452 src->bytes_out += use;
456 static chunk *chunkqueue_get_append_tempfile(chunkqueue *cq) {
457 chunk *c;
458 buffer *template = buffer_init_string("/var/tmp/lighttpd-upload-XXXXXX");
459 int fd = -1;
461 if (cq->tempdirs && cq->tempdirs->used) {
462 /* we have several tempdirs, only if all of them fail we jump out */
464 for (errno = EIO; cq->tempdir_idx < cq->tempdirs->used; ++cq->tempdir_idx) {
465 data_string *ds = (data_string *)cq->tempdirs->data[cq->tempdir_idx];
467 buffer_copy_buffer(template, ds->value);
468 buffer_append_slash(template);
469 buffer_append_string_len(template, CONST_STR_LEN("lighttpd-upload-XXXXXX"));
471 /* coverity[secure_temp : FALSE] */
472 if (-1 != (fd = mkstemp(template->ptr))) break;
474 } else {
475 /* coverity[secure_temp : FALSE] */
476 fd = mkstemp(template->ptr);
479 if (fd < 0) {
480 buffer_free(template);
481 return NULL;
484 if (0 != fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_APPEND)) {
485 close(fd);
486 buffer_free(template);
487 return NULL;
489 fd_close_on_exec(fd);
491 c = chunkqueue_get_unused_chunk(cq);
492 c->type = FILE_CHUNK;
493 c->file.fd = fd;
494 c->file.is_temp = 1;
495 buffer_copy_buffer(c->file.name, template);
496 c->file.length = 0;
498 chunkqueue_append_chunk(cq, c);
500 buffer_free(template);
502 return c;
505 static void chunkqueue_remove_empty_chunks(chunkqueue *cq);
507 int chunkqueue_append_mem_to_tempfile(server *srv, chunkqueue *dest, const char *mem, size_t len) {
508 chunk *dst_c;
509 ssize_t written;
511 do {
513 * if the last chunk is
514 * - smaller than dest->upload_temp_file_size
515 * - not read yet (offset == 0)
516 * -> append to it (so it might actually become larger than dest->upload_temp_file_size)
517 * otherwise
518 * -> create a new chunk
520 * */
522 dst_c = dest->last;
523 if (NULL != dst_c
524 && FILE_CHUNK == dst_c->type
525 && dst_c->file.is_temp
526 && dst_c->file.fd >= 0
527 && 0 == dst_c->offset) {
528 /* ok, take the last chunk for our job */
530 if (dst_c->file.length >= (off_t)dest->upload_temp_file_size) {
531 /* the chunk is too large now, close it */
532 int rc = close(dst_c->file.fd);
533 dst_c->file.fd = -1;
534 if (0 != rc) {
535 log_error_write(srv, __FILE__, __LINE__, "sbss",
536 "close() temp-file", dst_c->file.name, "failed:",
537 strerror(errno));
538 return -1;
540 dst_c = NULL;
542 } else {
543 dst_c = NULL;
546 if (NULL == dst_c && NULL == (dst_c = chunkqueue_get_append_tempfile(dest))) {
547 /* we don't have file to write to,
548 * EACCES might be one reason.
550 * Instead of sending 500 we send 413 and say the request is too large
553 log_error_write(srv, __FILE__, __LINE__, "ss",
554 "opening temp-file failed:", strerror(errno));
556 return -1;
559 /* (dst_c->file.fd >= 0) */
560 /* coverity[negative_returns : FALSE] */
561 written = write(dst_c->file.fd, mem, len);
563 if ((size_t) written == len) {
564 dst_c->file.length += len;
565 dest->bytes_in += len;
567 return 0;
568 } else if (written >= 0) {
569 /*(assume EINTR if partial write and retry write();
570 * retry write() might fail with ENOSPC if no more space on volume)*/
571 dest->bytes_in += written;
572 mem += written;
573 len -= (size_t)written;
574 dst_c->file.length += (size_t)written;
575 /* continue; retry */
576 } else if (errno == EINTR) {
577 /* continue; retry */
578 } else {
579 int retry = (errno == ENOSPC && dest->tempdirs && ++dest->tempdir_idx < dest->tempdirs->used);
580 if (!retry) {
581 log_error_write(srv, __FILE__, __LINE__, "sbs",
582 "write() temp-file", dst_c->file.name, "failed:",
583 strerror(errno));
586 if (0 == chunk_remaining_length(dst_c)) {
587 /*(remove empty chunk and unlink tempfile)*/
588 chunkqueue_remove_empty_chunks(dest);
589 } else {/*(close tempfile; avoid later attempts to append)*/
590 int rc = close(dst_c->file.fd);
591 dst_c->file.fd = -1;
592 if (0 != rc) {
593 log_error_write(srv, __FILE__, __LINE__, "sbss",
594 "close() temp-file", dst_c->file.name, "failed:",
595 strerror(errno));
596 return -1;
599 if (!retry) break; /* return -1; */
601 /* continue; retry */
604 } while (dst_c);
606 return -1;
609 int chunkqueue_steal_with_tempfiles(server *srv, chunkqueue *dest, chunkqueue *src, off_t len) {
610 while (len > 0) {
611 chunk *c = src->first;
612 off_t clen = 0, use;
614 if (NULL == c) break;
616 clen = chunk_remaining_length(c);
617 if (0 == clen) {
618 /* drop empty chunk */
619 src->first = c->next;
620 if (c == src->last) src->last = NULL;
621 chunkqueue_push_unused_chunk(src, c);
622 continue;
625 use = (len >= clen) ? clen : len;
626 len -= use;
628 switch (c->type) {
629 case FILE_CHUNK:
630 if (use == clen) {
631 /* move complete chunk */
632 src->first = c->next;
633 if (c == src->last) src->last = NULL;
634 chunkqueue_append_chunk(dest, c);
635 } else {
636 /* partial chunk with length "use" */
637 /* tempfile flag is in "last" chunk after the split */
638 chunkqueue_append_file(dest, c->file.name, c->file.start + c->offset, use);
640 c->offset += use;
641 force_assert(0 == len);
643 break;
645 case MEM_CHUNK:
646 /* store "use" bytes from memory chunk in tempfile */
647 if (0 != chunkqueue_append_mem_to_tempfile(srv, dest, c->mem->ptr + c->offset, use)) {
648 return -1;
651 if (use == clen) {
652 /* finished chunk */
653 src->first = c->next;
654 if (c == src->last) src->last = NULL;
655 chunkqueue_push_unused_chunk(src, c);
656 } else {
657 /* partial chunk */
658 c->offset += use;
659 force_assert(0 == len);
661 break;
664 src->bytes_out += use;
667 return 0;
670 off_t chunkqueue_length(chunkqueue *cq) {
671 off_t len = 0;
672 chunk *c;
674 for (c = cq->first; c; c = c->next) {
675 len += chunk_remaining_length(c);
678 return len;
681 int chunkqueue_is_empty(chunkqueue *cq) {
682 return NULL == cq->first;
685 void chunkqueue_mark_written(chunkqueue *cq, off_t len) {
686 off_t written = len;
687 chunk *c;
688 force_assert(len >= 0);
690 for (c = cq->first; NULL != c; c = cq->first) {
691 off_t c_len = chunk_remaining_length(c);
693 if (0 == written && 0 != c_len) break; /* no more finished chunks */
695 if (written >= c_len) { /* chunk got finished */
696 c->offset += c_len;
697 written -= c_len;
699 cq->first = c->next;
700 if (c == cq->last) cq->last = NULL;
702 chunkqueue_push_unused_chunk(cq, c);
703 } else { /* partial chunk */
704 c->offset += written;
705 written = 0;
706 break; /* chunk not finished */
710 force_assert(0 == written);
711 cq->bytes_out += len;
714 void chunkqueue_remove_finished_chunks(chunkqueue *cq) {
715 chunk *c;
717 for (c = cq->first; c; c = cq->first) {
718 if (0 != chunk_remaining_length(c)) break; /* not finished yet */
720 cq->first = c->next;
721 if (c == cq->last) cq->last = NULL;
723 chunkqueue_push_unused_chunk(cq, c);
727 static void chunkqueue_remove_empty_chunks(chunkqueue *cq) {
728 chunk *c;
729 chunkqueue_remove_finished_chunks(cq);
730 if (chunkqueue_is_empty(cq)) return;
732 for (c = cq->first; c->next; c = c->next) {
733 if (0 == chunk_remaining_length(c->next)) {
734 chunk *empty = c->next;
735 c->next = empty->next;
736 if (empty == cq->last) cq->last = c;
738 chunkqueue_push_unused_chunk(cq, empty);