2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/**
|
|
|
|
* the network chunk-API
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "chunk.h"
|
2017-03-28 04:04:31 +00:00
|
|
|
#include "fdevent.h"
|
2015-02-08 19:10:36 +00:00
|
|
|
#include "log.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2015-08-22 16:00:59 +00:00
|
|
|
#include "sys-mmap.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-07-28 07:57:52 +00:00
|
|
|
/* default 1MB, upper limit 128MB */
|
|
|
|
#define DEFAULT_TEMPFILE_SIZE (1 * 1024 * 1024)
|
|
|
|
#define MAX_TEMPFILE_SIZE (128 * 1024 * 1024)
|
|
|
|
|
2018-11-12 13:56:02 +00:00
|
|
|
static size_t chunk_buf_sz = 4096;
|
2018-11-07 04:35:09 +00:00
|
|
|
static chunk *chunks;
|
|
|
|
static chunk *chunk_buffers;
|
2016-07-28 07:57:52 +00:00
|
|
|
static array *chunkqueue_default_tempdirs = NULL;
|
|
|
|
static unsigned int chunkqueue_default_tempfile_size = DEFAULT_TEMPFILE_SIZE;
|
|
|
|
|
2018-11-18 04:50:12 +00:00
|
|
|
void chunkqueue_set_chunk_size (size_t sz)
|
|
|
|
{
|
|
|
|
chunk_buf_sz = sz > 0 ? ((sz + 1023) & ~1023uL) : 4096;
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:01:58 +00:00
|
|
|
void chunkqueue_set_tempdirs_default_reset (void)
|
|
|
|
{
|
|
|
|
chunkqueue_default_tempdirs = NULL;
|
|
|
|
chunkqueue_default_tempfile_size = DEFAULT_TEMPFILE_SIZE;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue *chunkqueue_init(void) {
|
|
|
|
chunkqueue *cq;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cq = calloc(1, sizeof(*cq));
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != cq);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cq->first = NULL;
|
|
|
|
cq->last = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-07-28 07:57:52 +00:00
|
|
|
cq->tempdirs = chunkqueue_default_tempdirs;
|
|
|
|
cq->upload_temp_file_size = chunkqueue_default_tempfile_size;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return cq;
|
|
|
|
}
|
|
|
|
|
2018-11-12 13:56:02 +00:00
|
|
|
static chunk *chunk_init(size_t sz) {
|
2005-02-20 14:27:00 +00:00
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = calloc(1, sizeof(*c));
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
c->type = MEM_CHUNK;
|
2005-09-14 08:00:33 +00:00
|
|
|
c->mem = buffer_init();
|
2015-02-08 12:37:10 +00:00
|
|
|
c->file.start = c->file.length = c->file.mmap.offset = 0;
|
2005-09-14 08:00:33 +00:00
|
|
|
c->file.fd = -1;
|
|
|
|
c->file.mmap.start = MAP_FAILED;
|
2015-02-08 12:37:10 +00:00
|
|
|
c->file.mmap.length = 0;
|
|
|
|
c->file.is_temp = 0;
|
|
|
|
c->offset = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
c->next = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-11-12 13:56:02 +00:00
|
|
|
buffer_string_prepare_copy(c->mem, sz-1);
|
2018-11-07 04:35:09 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:23:40 +00:00
|
|
|
static void chunk_reset_file_chunk(chunk *c) {
|
2018-11-07 04:35:09 +00:00
|
|
|
if (c->file.is_temp && !buffer_string_is_empty(c->mem)) {
|
|
|
|
unlink(c->mem->ptr);
|
2005-09-26 08:53:58 +00:00
|
|
|
}
|
2005-09-14 08:00:33 +00:00
|
|
|
if (c->file.fd != -1) {
|
|
|
|
close(c->file.fd);
|
|
|
|
c->file.fd = -1;
|
|
|
|
}
|
|
|
|
if (MAP_FAILED != c->file.mmap.start) {
|
|
|
|
munmap(c->file.mmap.start, c->file.mmap.length);
|
|
|
|
c->file.mmap.start = MAP_FAILED;
|
2005-09-08 10:00:32 +00:00
|
|
|
}
|
2015-02-08 12:37:10 +00:00
|
|
|
c->file.start = c->file.length = c->file.mmap.offset = 0;
|
|
|
|
c->file.mmap.length = 0;
|
|
|
|
c->file.is_temp = 0;
|
2018-11-07 04:35:09 +00:00
|
|
|
c->type = MEM_CHUNK;
|
2018-11-21 05:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void chunk_reset(chunk *c) {
|
|
|
|
if (c->type == FILE_CHUNK) chunk_reset_file_chunk(c);
|
|
|
|
|
2018-11-23 05:37:38 +00:00
|
|
|
buffer_clear(c->mem);
|
2015-02-08 12:37:10 +00:00
|
|
|
c->offset = 0;
|
2005-09-08 10:00:32 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
static void chunk_free(chunk *c) {
|
2018-11-21 05:23:40 +00:00
|
|
|
if (c->type == FILE_CHUNK) chunk_reset_file_chunk(c);
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_free(c->mem);
|
|
|
|
free(c);
|
|
|
|
}
|
2005-09-08 10:00:32 +00:00
|
|
|
|
2018-11-07 04:35:09 +00:00
|
|
|
buffer * chunk_buffer_acquire(void) {
|
|
|
|
chunk *c;
|
|
|
|
buffer *b;
|
|
|
|
if (chunks) {
|
|
|
|
c = chunks;
|
|
|
|
chunks = c->next;
|
|
|
|
}
|
|
|
|
else {
|
2018-11-12 13:56:02 +00:00
|
|
|
c = chunk_init(chunk_buf_sz);
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
c->next = chunk_buffers;
|
|
|
|
chunk_buffers = c;
|
|
|
|
b = c->mem;
|
|
|
|
c->mem = NULL;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void chunk_buffer_release(buffer *b) {
|
|
|
|
if (NULL == b) return;
|
2018-11-12 13:56:02 +00:00
|
|
|
if (b->size >= chunk_buf_sz && chunk_buffers) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk *c = chunk_buffers;
|
|
|
|
chunk_buffers = c->next;
|
|
|
|
c->mem = b;
|
|
|
|
c->next = chunks;
|
|
|
|
chunks = c;
|
2018-11-23 05:37:38 +00:00
|
|
|
buffer_clear(b);
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer_free(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static chunk * chunk_acquire(void) {
|
|
|
|
if (chunks) {
|
|
|
|
chunk *c = chunks;
|
|
|
|
chunks = c->next;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
else {
|
2018-11-12 13:56:02 +00:00
|
|
|
return chunk_init(chunk_buf_sz);
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chunk_release(chunk *c) {
|
2018-11-12 13:56:02 +00:00
|
|
|
if (c->mem->size >= chunk_buf_sz) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk_reset(c);
|
|
|
|
c->next = chunks;
|
|
|
|
chunks = c;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chunk_free(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void chunkqueue_chunk_pool_clear(void)
|
|
|
|
{
|
|
|
|
for (chunk *next, *c = chunks; c; c = next) {
|
|
|
|
next = c->next;
|
|
|
|
chunk_free(c);
|
|
|
|
}
|
|
|
|
chunks = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void chunkqueue_chunk_pool_free(void)
|
|
|
|
{
|
|
|
|
chunkqueue_chunk_pool_clear();
|
|
|
|
for (chunk *next, *c = chunk_buffers; c; c = next) {
|
|
|
|
next = c->next;
|
|
|
|
c->mem = buffer_init(); /*(chunk_reset() expects c->mem != NULL)*/
|
|
|
|
chunk_free(c);
|
|
|
|
}
|
|
|
|
chunk_buffers = NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-22 17:04:02 +00:00
|
|
|
static off_t chunk_remaining_length(const chunk *c) {
|
|
|
|
off_t len = 0;
|
|
|
|
switch (c->type) {
|
|
|
|
case MEM_CHUNK:
|
|
|
|
len = buffer_string_length(c->mem);
|
|
|
|
break;
|
|
|
|
case FILE_CHUNK:
|
|
|
|
len = c->file.length;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
force_assert(c->type == MEM_CHUNK || c->type == FILE_CHUNK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
force_assert(c->offset <= len);
|
|
|
|
return len - c->offset;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
void chunkqueue_free(chunkqueue *cq) {
|
|
|
|
chunk *c, *pc;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (NULL == cq) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (c = cq->first; c; ) {
|
|
|
|
pc = c;
|
|
|
|
c = c->next;
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk_release(pc);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(cq);
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
static void chunkqueue_prepend_chunk(chunkqueue *cq, chunk *c) {
|
2005-02-20 14:27:00 +00:00
|
|
|
c->next = cq->first;
|
|
|
|
cq->first = c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (NULL == cq->last) {
|
2005-02-20 14:27:00 +00:00
|
|
|
cq->last = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
static void chunkqueue_append_chunk(chunkqueue *cq, chunk *c) {
|
2015-12-19 08:28:39 +00:00
|
|
|
c->next = NULL;
|
2005-02-20 14:27:00 +00:00
|
|
|
if (cq->last) {
|
|
|
|
cq->last->next = c;
|
|
|
|
}
|
|
|
|
cq->last = c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (NULL == cq->first) {
|
2005-02-20 14:27:00 +00:00
|
|
|
cq->first = c;
|
|
|
|
}
|
2018-10-28 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static chunk * chunkqueue_prepend_mem_chunk(chunkqueue *cq) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk *c = chunk_acquire();
|
2018-10-28 14:00:03 +00:00
|
|
|
chunkqueue_prepend_chunk(cq, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static chunk * chunkqueue_append_mem_chunk(chunkqueue *cq) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk *c = chunk_acquire();
|
2018-10-28 14:00:03 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static chunk * chunkqueue_append_file_chunk(chunkqueue *cq, buffer *fn, off_t offset, off_t len) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk *c = chunk_acquire();
|
2018-10-28 14:00:03 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
|
|
|
c->type = FILE_CHUNK;
|
|
|
|
c->file.start = offset;
|
|
|
|
c->file.length = len;
|
|
|
|
cq->bytes_in += len;
|
2018-11-07 04:35:09 +00:00
|
|
|
buffer_copy_buffer(c->mem, fn);
|
2018-10-28 14:00:03 +00:00
|
|
|
return c;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void chunkqueue_reset(chunkqueue *cq) {
|
2015-02-08 12:37:10 +00:00
|
|
|
chunk *cur = cq->first;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
cq->first = cq->last = NULL;
|
|
|
|
|
|
|
|
while (NULL != cur) {
|
|
|
|
chunk *next = cur->next;
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk_release(cur);
|
2015-02-08 12:37:10 +00:00
|
|
|
cur = next;
|
2005-09-08 10:00:32 +00:00
|
|
|
}
|
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
cq->bytes_in = 0;
|
|
|
|
cq->bytes_out = 0;
|
2016-04-08 04:39:50 +00:00
|
|
|
cq->tempdir_idx = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
void chunkqueue_append_file_fd(chunkqueue *cq, buffer *fn, int fd, off_t offset, off_t len) {
|
2018-10-28 14:00:03 +00:00
|
|
|
if (len > 0) {
|
|
|
|
(chunkqueue_append_file_chunk(cq, fn, offset, len))->file.fd = fd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
close(fd);
|
|
|
|
}
|
2016-03-30 10:39:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void chunkqueue_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len) {
|
2018-10-28 14:00:03 +00:00
|
|
|
if (len > 0) {
|
|
|
|
chunkqueue_append_file_chunk(cq, fn, offset, len);
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 02:35:50 +00:00
|
|
|
|
|
|
|
static int chunkqueue_append_mem_extend_chunk(chunkqueue *cq, const char *mem, size_t len) {
|
|
|
|
chunk *c = cq->last;
|
|
|
|
if (0 == len) return 1;
|
|
|
|
if (c != NULL && c->type == MEM_CHUNK
|
2018-11-12 13:56:02 +00:00
|
|
|
&& buffer_string_space(c->mem) >= len) {
|
2018-10-27 02:35:50 +00:00
|
|
|
buffer_append_string_len(c->mem, mem, len);
|
|
|
|
cq->bytes_in += len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void chunkqueue_append_buffer(chunkqueue *cq, buffer *mem) {
|
2005-02-20 14:27:00 +00:00
|
|
|
chunk *c;
|
2018-10-27 02:35:50 +00:00
|
|
|
size_t len = buffer_string_length(mem);
|
|
|
|
if (len < 256 && chunkqueue_append_mem_extend_chunk(cq, mem->ptr, len)) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-10-28 14:00:03 +00:00
|
|
|
c = chunkqueue_append_mem_chunk(cq);
|
|
|
|
cq->bytes_in += len;
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_move(c->mem, mem);
|
2006-09-21 08:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void chunkqueue_append_mem(chunkqueue *cq, const char * mem, size_t len) {
|
2005-02-20 14:27:00 +00:00
|
|
|
chunk *c;
|
2018-11-12 13:56:02 +00:00
|
|
|
if (len < chunk_buf_sz && chunkqueue_append_mem_extend_chunk(cq, mem, len))
|
|
|
|
return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-10-28 14:00:03 +00:00
|
|
|
c = chunkqueue_append_mem_chunk(cq);
|
|
|
|
cq->bytes_in += len;
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_string_len(c->mem, mem, len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:33:18 +00:00
|
|
|
|
2018-12-13 01:00:07 +00:00
|
|
|
void chunkqueue_append_mem_min(chunkqueue *cq, const char * mem, size_t len) {
|
|
|
|
chunk *c;
|
|
|
|
if (len < chunk_buf_sz && chunkqueue_append_mem_extend_chunk(cq, mem, len))
|
|
|
|
return;
|
|
|
|
|
|
|
|
c = chunk_init(len+1);
|
|
|
|
chunkqueue_append_chunk(cq, c);
|
|
|
|
cq->bytes_in += len;
|
|
|
|
buffer_copy_string_len(c->mem, mem, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-21 19:33:18 +00:00
|
|
|
void chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src) {
|
|
|
|
if (src == NULL || NULL == src->first) return;
|
|
|
|
|
|
|
|
if (NULL == cq->first) {
|
|
|
|
cq->first = src->first;
|
|
|
|
} else {
|
|
|
|
cq->last->next = src->first;
|
|
|
|
}
|
|
|
|
cq->last = src->last;
|
|
|
|
cq->bytes_in += (src->bytes_in - src->bytes_out);
|
|
|
|
|
|
|
|
src->first = NULL;
|
|
|
|
src->last = NULL;
|
|
|
|
src->bytes_out = src->bytes_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 13:56:02 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static void chunkqueue_buffer_open_resize(chunk *c, size_t sz) {
|
|
|
|
chunk * const n = chunk_init((sz + 4095) & ~4095uL);
|
|
|
|
buffer * const b = c->mem;
|
|
|
|
c->mem = n->mem;
|
|
|
|
n->mem = b;
|
|
|
|
chunk_release(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 05:29:55 +00:00
|
|
|
buffer * chunkqueue_prepend_buffer_open_sz(chunkqueue *cq, size_t sz) {
|
|
|
|
chunk * const c = chunkqueue_prepend_mem_chunk(cq);
|
2018-11-12 13:56:02 +00:00
|
|
|
if (buffer_string_space(c->mem) < sz) {
|
|
|
|
chunkqueue_buffer_open_resize(c, sz);
|
2018-11-12 05:29:55 +00:00
|
|
|
}
|
2018-11-12 13:56:02 +00:00
|
|
|
return c->mem;
|
2018-11-12 05:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-27 02:03:31 +00:00
|
|
|
buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq) {
|
2018-10-28 14:00:03 +00:00
|
|
|
chunk *c = chunkqueue_prepend_mem_chunk(cq);
|
2018-10-27 02:03:31 +00:00
|
|
|
return c->mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void chunkqueue_prepend_buffer_commit(chunkqueue *cq) {
|
|
|
|
cq->bytes_in += buffer_string_length(cq->first->mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 05:29:55 +00:00
|
|
|
buffer * chunkqueue_append_buffer_open_sz(chunkqueue *cq, size_t sz) {
|
|
|
|
chunk * const c = chunkqueue_append_mem_chunk(cq);
|
2018-11-12 13:56:02 +00:00
|
|
|
if (buffer_string_space(c->mem) < sz) {
|
|
|
|
chunkqueue_buffer_open_resize(c, sz);
|
2018-11-12 05:29:55 +00:00
|
|
|
}
|
2018-11-12 13:56:02 +00:00
|
|
|
return c->mem;
|
2018-11-12 05:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-27 18:01:58 +00:00
|
|
|
buffer * chunkqueue_append_buffer_open(chunkqueue *cq) {
|
2018-10-28 14:00:03 +00:00
|
|
|
chunk *c = chunkqueue_append_mem_chunk(cq);
|
2018-10-27 18:01:58 +00:00
|
|
|
return c->mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void chunkqueue_append_buffer_commit(chunkqueue *cq) {
|
|
|
|
cq->bytes_in += buffer_string_length(cq->last->mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-26 04:16:46 +00:00
|
|
|
static void chunkqueue_remove_empty_chunks(chunkqueue *cq);
|
|
|
|
|
|
|
|
|
2018-11-13 02:51:25 +00:00
|
|
|
char * chunkqueue_get_memory(chunkqueue *cq, size_t *len) {
|
|
|
|
size_t sz = *len ? *len : (chunk_buf_sz >> 1);
|
2015-02-08 19:10:36 +00:00
|
|
|
buffer *b;
|
2018-11-13 02:51:25 +00:00
|
|
|
chunk *c = cq->last;
|
|
|
|
if (NULL != c && MEM_CHUNK == c->type) {
|
2015-02-08 19:10:36 +00:00
|
|
|
/* return pointer into existing buffer if large enough */
|
2018-11-13 02:51:25 +00:00
|
|
|
size_t avail = buffer_string_space(c->mem);
|
|
|
|
if (avail >= sz) {
|
|
|
|
*len = avail;
|
|
|
|
b = c->mem;
|
|
|
|
return b->ptr + buffer_string_length(b);
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
/* allocate new chunk */
|
2018-11-13 02:51:25 +00:00
|
|
|
b = chunkqueue_append_buffer_open_sz(cq, sz);
|
2015-02-08 19:10:36 +00:00
|
|
|
*len = buffer_string_space(b);
|
2018-11-13 02:51:25 +00:00
|
|
|
return b->ptr;
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void chunkqueue_use_memory(chunkqueue *cq, size_t len) {
|
|
|
|
buffer *b;
|
|
|
|
|
|
|
|
force_assert(NULL != cq);
|
|
|
|
force_assert(NULL != cq->last && MEM_CHUNK == cq->last->type);
|
|
|
|
b = cq->last->mem;
|
|
|
|
|
|
|
|
if (len > 0) {
|
2015-02-08 19:10:44 +00:00
|
|
|
buffer_commit(b, len);
|
2015-08-22 17:04:02 +00:00
|
|
|
cq->bytes_in += len;
|
2015-02-08 19:10:36 +00:00
|
|
|
} else if (buffer_string_is_empty(b)) {
|
2019-02-26 04:16:46 +00:00
|
|
|
/* scan chunkqueue to remove empty last chunk
|
|
|
|
* (generally not expecting a deep queue) */
|
|
|
|
chunkqueue_remove_empty_chunks(cq);
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 07:57:52 +00:00
|
|
|
void chunkqueue_set_tempdirs_default (array *tempdirs, unsigned int upload_temp_file_size) {
|
|
|
|
chunkqueue_default_tempdirs = tempdirs;
|
|
|
|
chunkqueue_default_tempfile_size
|
|
|
|
= (0 == upload_temp_file_size) ? DEFAULT_TEMPFILE_SIZE
|
|
|
|
: (upload_temp_file_size > MAX_TEMPFILE_SIZE) ? MAX_TEMPFILE_SIZE
|
|
|
|
: upload_temp_file_size;
|
|
|
|
}
|
2016-04-08 04:39:50 +00:00
|
|
|
|
2016-07-28 07:57:52 +00:00
|
|
|
#if 0
|
2015-11-07 12:51:14 +00:00
|
|
|
void chunkqueue_set_tempdirs(chunkqueue *cq, array *tempdirs, unsigned int upload_temp_file_size) {
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != cq);
|
2005-11-01 07:50:08 +00:00
|
|
|
cq->tempdirs = tempdirs;
|
2016-04-08 04:39:50 +00:00
|
|
|
cq->upload_temp_file_size
|
|
|
|
= (0 == upload_temp_file_size) ? DEFAULT_TEMPFILE_SIZE
|
|
|
|
: (upload_temp_file_size > MAX_TEMPFILE_SIZE) ? MAX_TEMPFILE_SIZE
|
|
|
|
: upload_temp_file_size;
|
|
|
|
cq->tempdir_idx = 0;
|
2005-11-01 07:50:08 +00:00
|
|
|
}
|
2016-07-28 07:57:52 +00:00
|
|
|
#endif
|
2005-11-01 07:50:08 +00:00
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
void chunkqueue_steal(chunkqueue *dest, chunkqueue *src, off_t len) {
|
|
|
|
while (len > 0) {
|
|
|
|
chunk *c = src->first;
|
|
|
|
off_t clen = 0, use;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
if (NULL == c) break;
|
2005-09-26 08:53:58 +00:00
|
|
|
|
2015-08-22 17:04:02 +00:00
|
|
|
clen = chunk_remaining_length(c);
|
2015-02-08 19:10:36 +00:00
|
|
|
if (0 == clen) {
|
|
|
|
/* drop empty chunk */
|
|
|
|
src->first = c->next;
|
|
|
|
if (c == src->last) src->last = NULL;
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk_release(c);
|
2015-02-08 19:10:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-22 17:04:02 +00:00
|
|
|
use = len >= clen ? clen : len;
|
|
|
|
len -= use;
|
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
if (use == clen) {
|
|
|
|
/* move complete chunk */
|
|
|
|
src->first = c->next;
|
|
|
|
if (c == src->last) src->last = NULL;
|
|
|
|
|
|
|
|
chunkqueue_append_chunk(dest, c);
|
2018-10-28 14:00:03 +00:00
|
|
|
dest->bytes_in += use;
|
2015-08-22 17:04:02 +00:00
|
|
|
} else {
|
|
|
|
/* partial chunk with length "use" */
|
|
|
|
|
|
|
|
switch (c->type) {
|
|
|
|
case MEM_CHUNK:
|
|
|
|
chunkqueue_append_mem(dest, c->mem->ptr + c->offset, use);
|
|
|
|
break;
|
|
|
|
case FILE_CHUNK:
|
|
|
|
/* tempfile flag is in "last" chunk after the split */
|
2018-11-07 04:35:09 +00:00
|
|
|
chunkqueue_append_file(dest, c->mem, c->file.start + c->offset, use);
|
2015-08-22 17:04:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2015-08-22 17:04:02 +00:00
|
|
|
c->offset += use;
|
|
|
|
force_assert(0 == len);
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 17:04:02 +00:00
|
|
|
src->bytes_out += use;
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 16:32:47 +00:00
|
|
|
static chunk *chunkqueue_get_append_tempfile(server *srv, chunkqueue *cq) {
|
2015-02-08 19:10:36 +00:00
|
|
|
chunk *c;
|
|
|
|
buffer *template = buffer_init_string("/var/tmp/lighttpd-upload-XXXXXX");
|
2016-06-21 07:39:17 +00:00
|
|
|
int fd = -1;
|
2005-11-01 07:50:08 +00:00
|
|
|
|
2005-11-02 12:38:53 +00:00
|
|
|
if (cq->tempdirs && cq->tempdirs->used) {
|
2005-11-01 07:50:08 +00:00
|
|
|
/* we have several tempdirs, only if all of them fail we jump out */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-04-08 04:39:50 +00:00
|
|
|
for (errno = EIO; cq->tempdir_idx < cq->tempdirs->used; ++cq->tempdir_idx) {
|
|
|
|
data_string *ds = (data_string *)cq->tempdirs->data[cq->tempdir_idx];
|
2005-11-01 07:50:08 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(template, ds->value);
|
2018-11-28 07:12:41 +00:00
|
|
|
buffer_append_path_len(template, CONST_STR_LEN("lighttpd-upload-XXXXXX"));
|
2019-03-16 05:38:16 +00:00
|
|
|
if (-1 != (fd = fdevent_mkstemp_append(template->ptr))) break;
|
2005-11-01 07:50:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-16 05:38:16 +00:00
|
|
|
fd = fdevent_mkstemp_append(template->ptr);
|
2005-10-31 08:14:02 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 07:57:19 +00:00
|
|
|
if (fd < 0) {
|
2017-03-25 16:32:47 +00:00
|
|
|
/* (report only the last error to mkstemp()
|
|
|
|
* if multiple temp dirs attempted) */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbs",
|
|
|
|
"opening temp-file failed:",
|
|
|
|
template, strerror(errno));
|
2015-02-08 19:10:36 +00:00
|
|
|
buffer_free(template);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:00:03 +00:00
|
|
|
c = chunkqueue_append_file_chunk(cq, template, 0, 0);
|
2015-02-08 19:10:36 +00:00
|
|
|
c->file.fd = fd;
|
|
|
|
c->file.is_temp = 1;
|
2005-11-01 07:50:08 +00:00
|
|
|
|
|
|
|
buffer_free(template);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-26 08:53:58 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-05-25 20:45:09 +00:00
|
|
|
int chunkqueue_append_mem_to_tempfile(server *srv, chunkqueue *dest, const char *mem, size_t len) {
|
2016-04-08 04:39:50 +00:00
|
|
|
chunk *dst_c;
|
2015-02-08 19:10:36 +00:00
|
|
|
ssize_t written;
|
|
|
|
|
2016-04-08 04:39:50 +00:00
|
|
|
do {
|
|
|
|
/*
|
|
|
|
* if the last chunk is
|
|
|
|
* - smaller than dest->upload_temp_file_size
|
|
|
|
* - not read yet (offset == 0)
|
|
|
|
* -> append to it (so it might actually become larger than dest->upload_temp_file_size)
|
|
|
|
* otherwise
|
|
|
|
* -> create a new chunk
|
|
|
|
*
|
|
|
|
* */
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2016-04-08 04:39:50 +00:00
|
|
|
dst_c = dest->last;
|
|
|
|
if (NULL != dst_c
|
|
|
|
&& FILE_CHUNK == dst_c->type
|
|
|
|
&& dst_c->file.is_temp
|
2016-06-23 07:57:19 +00:00
|
|
|
&& dst_c->file.fd >= 0
|
2016-04-08 04:39:50 +00:00
|
|
|
&& 0 == dst_c->offset) {
|
|
|
|
/* ok, take the last chunk for our job */
|
|
|
|
|
|
|
|
if (dst_c->file.length >= (off_t)dest->upload_temp_file_size) {
|
|
|
|
/* the chunk is too large now, close it */
|
2016-05-11 02:17:22 +00:00
|
|
|
int rc = close(dst_c->file.fd);
|
2015-02-08 19:10:36 +00:00
|
|
|
dst_c->file.fd = -1;
|
2016-05-11 02:17:22 +00:00
|
|
|
if (0 != rc) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbss",
|
2018-11-07 04:35:09 +00:00
|
|
|
"close() temp-file", dst_c->mem, "failed:",
|
2016-05-11 02:17:22 +00:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-21 07:39:17 +00:00
|
|
|
dst_c = NULL;
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
2016-04-08 04:39:50 +00:00
|
|
|
} else {
|
|
|
|
dst_c = NULL;
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 16:32:47 +00:00
|
|
|
if (NULL == dst_c && NULL == (dst_c = chunkqueue_get_append_tempfile(srv, dest))) {
|
2016-04-08 04:39:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-08-12 19:39:12 +00:00
|
|
|
#ifdef __COVERITY__
|
|
|
|
if (dst_c->file.fd < 0) return -1;
|
|
|
|
#endif
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2016-09-23 09:46:01 +00:00
|
|
|
/* (dst_c->file.fd >= 0) */
|
|
|
|
/* coverity[negative_returns : FALSE] */
|
2016-04-08 04:39:50 +00:00
|
|
|
written = write(dst_c->file.fd, mem, len);
|
|
|
|
|
|
|
|
if ((size_t) written == len) {
|
|
|
|
dst_c->file.length += len;
|
|
|
|
dest->bytes_in += len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else if (written >= 0) {
|
|
|
|
/*(assume EINTR if partial write and retry write();
|
|
|
|
* retry write() might fail with ENOSPC if no more space on volume)*/
|
|
|
|
dest->bytes_in += written;
|
|
|
|
mem += written;
|
|
|
|
len -= (size_t)written;
|
|
|
|
dst_c->file.length += (size_t)written;
|
|
|
|
/* continue; retry */
|
|
|
|
} else if (errno == EINTR) {
|
|
|
|
/* continue; retry */
|
|
|
|
} else {
|
|
|
|
int retry = (errno == ENOSPC && dest->tempdirs && ++dest->tempdir_idx < dest->tempdirs->used);
|
|
|
|
if (!retry) {
|
|
|
|
|