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)
|
|
|
|
|
2019-11-29 23:18:52 +00:00
|
|
|
static size_t chunk_buf_sz = 8192;
|
2019-04-18 21:02:42 +00:00
|
|
|
static chunk *chunks, *chunks_oversized;
|
2018-11-07 04:35:09 +00:00
|
|
|
static chunk *chunk_buffers;
|
2020-12-24 04:14:47 +00:00
|
|
|
static int chunks_oversized_n;
|
2019-11-16 01:26:54 +00:00
|
|
|
static const array *chunkqueue_default_tempdirs = NULL;
|
2019-03-16 05:39:59 +00:00
|
|
|
static off_t chunkqueue_default_tempfile_size = DEFAULT_TEMPFILE_SIZE;
|
2016-07-28 07:57:52 +00:00
|
|
|
|
2018-11-18 04:50:12 +00:00
|
|
|
void chunkqueue_set_chunk_size (size_t sz)
|
|
|
|
{
|
2020-12-24 04:14:47 +00:00
|
|
|
size_t x = 1024;
|
|
|
|
while (x < sz && x < (1u << 30)) x <<= 1;
|
|
|
|
chunk_buf_sz = sz > 0 ? x : 8192;
|
2018-11-18 04:50:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 02:01:58 +00:00
|
|
|
void chunkqueue_set_tempdirs_default_reset (void)
|
|
|
|
{
|
2020-12-24 04:14:47 +00:00
|
|
|
chunk_buf_sz = 8192;
|
2017-01-24 02:01:58 +00:00
|
|
|
chunkqueue_default_tempdirs = NULL;
|
|
|
|
chunkqueue_default_tempfile_size = DEFAULT_TEMPFILE_SIZE;
|
|
|
|
}
|
|
|
|
|
2019-05-12 07:51:58 +00:00
|
|
|
/* chunk buffer (c->mem) is never NULL; specialize routines from buffer.h */
|
2020-01-22 02:01:05 +00:00
|
|
|
__attribute_pure__
|
2019-05-12 07:51:58 +00:00
|
|
|
static inline size_t chunk_buffer_string_length(const buffer *b) {
|
|
|
|
return 0 != b->used ? b->used - 1 : 0;
|
|
|
|
}
|
2020-01-22 02:01:05 +00:00
|
|
|
__attribute_pure__
|
2019-05-12 07:51:58 +00:00
|
|
|
static inline int chunk_buffer_string_is_empty(const buffer *b) {
|
|
|
|
return b->used < 2;
|
|
|
|
}
|
2020-01-22 02:01:05 +00:00
|
|
|
__attribute_pure__
|
2019-05-12 07:51:58 +00:00
|
|
|
static inline size_t chunk_buffer_string_space(const buffer *b) {
|
|
|
|
return b->size ? b->size - (b->used | (0 == b->used)) : 0;
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue *chunkqueue_init(chunkqueue *cq) {
|
|
|
|
/* (if caller passes non-NULL cq, it must be 0-init) */
|
|
|
|
if (NULL == cq) {
|
|
|
|
cq = calloc(1, sizeof(*cq));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2018-11-12 13:56:02 +00:00
|
|
|
static chunk *chunk_init(size_t sz) {
|
2020-10-14 17:08:38 +00:00
|
|
|
chunk * const restrict 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
|
|
|
|
2020-10-14 17:08:38 +00:00
|
|
|
#if 0 /*(zeroed by calloc())*/
|
2015-02-08 12:37:10 +00:00
|
|
|
c->type = MEM_CHUNK;
|
2020-10-14 17:08:38 +00:00
|
|
|
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
|
2005-09-14 08:00:33 +00:00
|
|
|
c->file.fd = -1;
|
|
|
|
c->file.mmap.start = MAP_FAILED;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-10-14 17:08:38 +00:00
|
|
|
c->mem = buffer_init();
|
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) {
|
2020-10-14 17:08:38 +00:00
|
|
|
if (c->file.is_temp) {
|
|
|
|
c->file.is_temp = 0;
|
|
|
|
if (!chunk_buffer_string_is_empty(c->mem))
|
|
|
|
unlink(c->mem->ptr);
|
2005-09-26 08:53:58 +00:00
|
|
|
}
|
2020-10-20 01:37:38 +00:00
|
|
|
if (c->file.refchg) {
|
|
|
|
c->file.refchg(c->file.ref, -1);
|
|
|
|
c->file.refchg = 0; /* NULL fn ptr */
|
|
|
|
c->file.ref = NULL;
|
|
|
|
}
|
|
|
|
else if (c->file.fd != -1) {
|
2005-09-14 08:00:33 +00:00
|
|
|
close(c->file.fd);
|
|
|
|
}
|
|
|
|
if (MAP_FAILED != c->file.mmap.start) {
|
|
|
|
munmap(c->file.mmap.start, c->file.mmap.length);
|
|
|
|
c->file.mmap.start = MAP_FAILED;
|
2020-10-14 17:08:38 +00:00
|
|
|
c->file.mmap.length = c->file.mmap.offset = 0;
|
2005-09-08 10:00:32 +00:00
|
|
|
}
|
2020-12-09 10:02:43 +00:00
|
|
|
c->file.fd = -1;
|
2020-10-14 17:08:38 +00:00
|
|
|
c->file.length = 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
|
|
|
|
2020-12-24 04:14:47 +00:00
|
|
|
static chunk * chunk_pop_oversized(size_t sz) {
|
|
|
|
/* future: might have buckets of certain sizes, up to socket buf sizes */
|
|
|
|
if (chunks_oversized && chunks_oversized->mem->size >= sz) {
|
|
|
|
--chunks_oversized_n;
|
|
|
|
chunk *c = chunks_oversized;
|
|
|
|
chunks_oversized = c->next;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chunk_push_oversized(chunk * const c, const size_t sz) {
|
|
|
|
if (chunks_oversized_n < 64 && chunk_buf_sz >= 4096) {
|
|
|
|
++chunks_oversized_n;
|
|
|
|
chunk **co = &chunks_oversized;
|
|
|
|
while (*co && sz < (*co)->mem->size) co = &(*co)->next;
|
|
|
|
c->next = *co;
|
|
|
|
*co = c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
chunk_free(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_returns_nonnull__
|
|
|
|
static buffer * chunk_buffer_acquire_sz(size_t sz) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk *c;
|
|
|
|
buffer *b;
|
2020-12-24 04:14:47 +00:00
|
|
|
if (sz <= chunk_buf_sz) {
|
|
|
|
if (chunks) {
|
|
|
|
c = chunks;
|
|
|
|
chunks = c->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c = chunk_init(chunk_buf_sz);
|
|
|
|
/* future: might choose to pop from chunks_oversized, if available
|
|
|
|
* (even if larger than sz) rather than allocating new chunk
|
|
|
|
* (and if doing so, might replace chunks_oversized_n) */
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-12-24 04:14:47 +00:00
|
|
|
/*(round up to nearest chunk_buf_sz)*/
|
|
|
|
sz = (sz + (chunk_buf_sz-1)) & ~(chunk_buf_sz-1);
|
|
|
|
c = chunk_pop_oversized(sz);
|
|
|
|
if (NULL == c)
|
|
|
|
c = chunk_init(sz);
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
c->next = chunk_buffers;
|
|
|
|
chunk_buffers = c;
|
|
|
|
b = c->mem;
|
|
|
|
c->mem = NULL;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2020-12-24 04:14:47 +00:00
|
|
|
buffer * chunk_buffer_acquire(void) {
|
|
|
|
return chunk_buffer_acquire_sz(chunk_buf_sz);
|
|
|
|
}
|
|
|
|
|
2018-11-07 04:35:09 +00:00
|
|
|
void chunk_buffer_release(buffer *b) {
|
|
|
|
if (NULL == b) return;
|
2020-12-24 04:14:47 +00:00
|
|
|
if (chunk_buffers) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk *c = chunk_buffers;
|
|
|
|
chunk_buffers = c->next;
|
|
|
|
c->mem = b;
|
2018-11-23 05:37:38 +00:00
|
|
|
buffer_clear(b);
|
2020-12-24 04:14:47 +00:00
|
|
|
if (b->size == chunk_buf_sz) {
|
|
|
|
c->next = chunks;
|
|
|
|
chunks = c;
|
|
|
|
}
|
|
|
|
else if (b->size > chunk_buf_sz)
|
|
|
|
chunk_push_oversized(c, b->size);
|
|
|
|
else
|
|
|
|
chunk_free(c);
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer_free(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 04:14:47 +00:00
|
|
|
size_t chunk_buffer_prepare_append(buffer * const b, size_t sz) {
|
|
|
|
if (sz > chunk_buffer_string_space(b)) {
|
|
|
|
sz += b->used ? b->used : 1;
|
|
|
|
buffer * const cb = chunk_buffer_acquire_sz(sz);
|
|
|
|
/* swap buffer contents and copy original b->ptr into larger b->ptr */
|
|
|
|
/*(this does more than buffer_move())*/
|
|
|
|
buffer tb = *b;
|
|
|
|
*b = *cb;
|
|
|
|
*cb = tb;
|
|
|
|
if ((b->used = tb.used))
|
|
|
|
memcpy(b->ptr, tb.ptr, tb.used);
|
|
|
|
chunk_buffer_release(cb);
|
|
|
|
}
|
|
|
|
return chunk_buffer_string_space(b);
|
|
|
|
}
|
|
|
|
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2019-04-18 21:02:42 +00:00
|
|
|
static chunk * chunk_acquire(size_t sz) {
|
|
|
|
if (sz <= chunk_buf_sz) {
|
|
|
|
if (chunks) {
|
|
|
|
chunk *c = chunks;
|
|
|
|
chunks = c->next;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
sz = chunk_buf_sz;
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-12-24 04:14:47 +00:00
|
|
|
/*(round up to nearest chunk_buf_sz)*/
|
|
|
|
sz = (sz + (chunk_buf_sz-1)) & ~(chunk_buf_sz-1);
|
|
|
|
chunk *c = chunk_pop_oversized(sz);
|
|
|
|
if (c) return c;
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
2019-04-18 21:02:42 +00:00
|
|
|
|
|
|
|
return chunk_init(sz);
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void chunk_release(chunk *c) {
|
2019-04-18 21:02:42 +00:00
|
|
|
const size_t sz = c->mem->size;
|
|
|
|
if (sz == chunk_buf_sz) {
|
2018-11-07 04:35:09 +00:00
|
|
|
chunk_reset(c);
|
|
|
|
c->next = chunks;
|
|
|
|
chunks = c;
|
|
|
|
}
|
2019-04-18 21:02:42 +00:00
|
|
|
else if (sz > chunk_buf_sz) {
|
|
|
|
chunk_reset(c);
|
2020-12-24 04:14:47 +00:00
|
|
|
chunk_push_oversized(c, sz);
|
2019-04-18 21:02:42 +00:00
|
|
|
}
|
2018-11-07 04:35:09 +00:00
|
|
|
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;
|
2019-04-18 21:02:42 +00:00
|
|
|
for (chunk *next, *c = chunks_oversized; c; c = next) {
|
|
|
|
next = c->next;
|
|
|
|
chunk_free(c);
|
|
|
|
}
|
|
|
|
chunks_oversized = NULL;
|
2020-12-24 04:14:47 +00:00
|
|
|
chunks_oversized_n = 0;
|
2018-11-07 04:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-22 02:01:05 +00:00
|
|
|
__attribute_pure__
|
2015-08-22 17:04:02 +00:00
|
|
|
static off_t chunk_remaining_length(const chunk *c) {
|
2019-05-12 04:18:03 +00:00
|
|
|
/* MEM_CHUNK or FILE_CHUNK */
|
|
|
|
return (c->type == MEM_CHUNK
|
2019-05-12 07:51:58 +00:00
|
|
|
? (off_t)chunk_buffer_string_length(c->mem)
|
2019-05-12 04:18:03 +00:00
|
|
|
: c->file.length)
|
|
|
|
- c->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chunkqueue_release_chunks(chunkqueue *cq) {
|
|
|
|
cq->last = NULL;
|
|
|
|
for (chunk *c; (c = cq->first); ) {
|
|
|
|
cq->first = c->next;
|
|
|
|
chunk_release(c);
|
|
|
|
}
|
2015-08-22 17:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
void chunkqueue_free(chunkqueue *cq) {
|
2019-05-12 04:18:03 +00:00
|
|
|
if (NULL == cq) return;
|
|
|
|
chunkqueue_release_chunks(cq);
|
|
|
|
free(cq);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
static void chunkqueue_prepend_chunk(chunkqueue * const restrict cq, chunk * const restrict c) {
|
2019-05-12 04:18:03 +00:00
|
|
|
if (NULL == (c->next = cq->first)) cq->last = c;
|
|
|
|
cq->first = c;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
static void chunkqueue_append_chunk(chunkqueue * const restrict cq, chunk * const restrict c) {
|
2019-05-12 04:18:03 +00:00
|
|
|
c->next = NULL;
|
|
|
|
*(cq->last ? &cq->last->next : &cq->first) = c;
|
|
|
|
cq->last = c;
|
2018-10-28 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2019-04-18 21:02:42 +00:00
|
|
|
static chunk * chunkqueue_prepend_mem_chunk(chunkqueue *cq, size_t sz) {
|
|
|
|
chunk *c = chunk_acquire(sz);
|
2018-10-28 14:00:03 +00:00
|
|
|
chunkqueue_prepend_chunk(cq, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2019-04-18 21:02:42 +00:00
|
|
|
static chunk * chunkqueue_append_mem_chunk(chunkqueue *cq, size_t sz) {
|
|
|
|
chunk *c = chunk_acquire(sz);
|
2018-10-28 14:00:03 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2020-01-18 03:18:30 +00:00
|
|
|
static chunk * chunkqueue_append_file_chunk(chunkqueue * const restrict cq, const buffer * const restrict fn, off_t offset, off_t len) {
|
2019-04-18 21:02:42 +00:00
|
|
|
chunk *c = chunk_acquire(buffer_string_length(fn)+1);
|
2018-10-28 14:00:03 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
|
|
|
c->type = FILE_CHUNK;
|
2020-10-14 17:08:38 +00:00
|
|
|
c->offset = offset;
|
|
|
|
c->file.length = offset + len;
|
2018-10-28 14:00:03 +00:00
|
|
|
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) {
|
2019-05-12 04:18:03 +00:00
|
|
|
chunkqueue_release_chunks(cq);
|
|
|
|
cq->bytes_in = 0;
|
|
|
|
cq->bytes_out = 0;
|
|
|
|
cq->tempdir_idx = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_append_file_fd(chunkqueue * const restrict cq, const buffer * const restrict 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
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_append_file(chunkqueue * const restrict cq, const buffer * const restrict 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
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
static int chunkqueue_append_mem_extend_chunk(chunkqueue * const restrict cq, const char * const restrict mem, size_t len) {
|
2018-10-27 02:35:50 +00:00
|
|
|
chunk *c = cq->last;
|
|
|
|
if (0 == len) return 1;
|
|
|
|
if (c != NULL && c->type == MEM_CHUNK
|
2019-05-12 07:51:58 +00:00
|
|
|
&& chunk_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_append_buffer(chunkqueue * const restrict cq, buffer * const restrict 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
|
|
|
|
2019-04-18 21:02:42 +00:00
|
|
|
c = chunkqueue_append_mem_chunk(cq, chunk_buf_sz);
|
2018-10-28 14:00:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_append_mem(chunkqueue * const restrict cq, const char * const restrict 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
|
|
|
|
2019-04-18 21:02:42 +00:00
|
|
|
c = chunkqueue_append_mem_chunk(cq, len+1);
|
2018-10-28 14:00:03 +00:00
|
|
|
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
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_append_mem_min(chunkqueue * const restrict cq, const char * const restrict mem, size_t len) {
|
2018-12-13 01:00:07 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_append_chunkqueue(chunkqueue * const restrict cq, chunkqueue * const restrict src) {
|
2016-06-21 19:33:18 +00:00
|
|
|
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;
|
2020-09-08 05:15:25 +00:00
|
|
|
cq->bytes_in += chunkqueue_length(src);
|
2016-06-21 19:33:18 +00:00
|
|
|
|
|
|
|
src->first = NULL;
|
|
|
|
src->last = NULL;
|
|
|
|
src->bytes_out = src->bytes_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 05:29:55 +00:00
|
|
|
buffer * chunkqueue_prepend_buffer_open_sz(chunkqueue *cq, size_t sz) {
|
2019-04-18 21:02:42 +00:00
|
|
|
chunk * const c = chunkqueue_prepend_mem_chunk(cq, sz);
|
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) {
|
2019-04-18 21:02:42 +00:00
|
|
|
return chunkqueue_prepend_buffer_open_sz(cq, chunk_buf_sz);
|
2018-10-27 02:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void chunkqueue_prepend_buffer_commit(chunkqueue *cq) {
|
2019-05-12 07:51:58 +00:00
|
|
|
cq->bytes_in += chunk_buffer_string_length(cq->first->mem);
|
2018-10-27 02:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 05:29:55 +00:00
|
|
|
buffer * chunkqueue_append_buffer_open_sz(chunkqueue *cq, size_t sz) {
|
2019-04-18 21:02:42 +00:00
|
|
|
chunk * const c = chunkqueue_append_mem_chunk(cq, sz);
|
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) {
|
2019-04-18 21:02:42 +00:00
|
|
|
return chunkqueue_append_buffer_open_sz(cq, chunk_buf_sz);
|
2018-10-27 18:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void chunkqueue_append_buffer_commit(chunkqueue *cq) {
|
2019-05-12 07:51:58 +00:00
|
|
|
cq->bytes_in += chunk_buffer_string_length(cq->last->mem);
|
2018-10-27 18:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-26 04:16:46 +00:00
|
|
|
static void chunkqueue_remove_empty_chunks(chunkqueue *cq);
|
|
|
|
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
char * chunkqueue_get_memory(chunkqueue * const restrict cq, size_t * const restrict len) {
|
2018-11-13 02:51:25 +00:00
|
|
|
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 */
|
2019-05-12 07:51:58 +00:00
|
|
|
size_t avail = chunk_buffer_string_space(c->mem);
|
2018-11-13 02:51:25 +00:00
|
|
|
if (avail >= sz) {
|
|
|
|
*len = avail;
|
|
|
|
b = c->mem;
|
2019-05-12 07:51:58 +00:00
|
|
|
return b->ptr + chunk_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);
|
2019-05-12 07:51:58 +00:00
|
|
|
*len = chunk_buffer_string_space(b);
|
2018-11-13 02:51:25 +00:00
|
|
|
return b->ptr;
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_use_memory(chunkqueue * const restrict cq, chunk *ckpt, size_t len) {
|
2019-11-29 23:17:22 +00:00
|
|
|
buffer *b = cq->last->mem;
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2019-11-29 23:17:22 +00:00
|
|
|
if (len > 0) {
|
|
|
|
buffer_commit(b, len);
|
|
|
|
cq->bytes_in += len;
|
|
|
|
if (cq->last == ckpt || NULL == ckpt || MEM_CHUNK != ckpt->type
|
|
|
|
|| len > chunk_buffer_string_space(ckpt->mem)) return;
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2019-11-29 23:17:22 +00:00
|
|
|
buffer_append_string_buffer(ckpt->mem, b);
|
|
|
|
}
|
|
|
|
else if (!chunk_buffer_string_is_empty(b)) { /*(cq->last == ckpt)*/
|
|
|
|
return; /* last chunk is not empty */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove empty last chunk */
|
|
|
|
chunk_release(cq->last);
|
|
|
|
cq->last = ckpt;
|
|
|
|
*(ckpt ? &ckpt->next : &cq->first) = NULL;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 04:43:36 +00:00
|
|
|
void chunkqueue_update_file(chunkqueue * const restrict cq, chunk *c, off_t len) {
|
|
|
|
/*assert(c->type == FILE_CHUNK);*/
|
|
|
|
c->file.length += len;
|
|
|
|
cq->bytes_in += len;
|
|
|
|
}
|
|
|
|
|
2019-11-16 01:26:54 +00:00
|
|
|
void chunkqueue_set_tempdirs_default (const array *tempdirs, off_t upload_temp_file_size) {
|
2016-07-28 07:57:52 +00:00
|
|
|
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
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_set_tempdirs(chunkqueue * const restrict cq, const array * const restrict tempdirs, off_t 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
|
|
|
}
|
|
|
|
|
2020-10-20 01:37:38 +00:00
|
|
|
static void chunkqueue_steal_partial_file_chunk(chunkqueue * const restrict dest, const chunk * const restrict c, const off_t len) {
|
|
|
|
chunkqueue_append_file(dest, c->mem, c->offset, len);
|
|
|
|
if (c->file.fd >= 0) {
|
|
|
|
chunk * const d = dest->last;
|
|
|
|
if (c->file.refchg) {
|
2020-10-20 20:53:21 +00:00
|
|
|
d->file.fd = c->file.fd;
|
2020-10-20 01:37:38 +00:00
|
|
|
d->file.ref = c->file.ref;
|
|
|
|
d->file.refchg = c->file.refchg;
|
|
|
|
d->file.refchg(d->file.ref, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
d->file.fd = fdevent_dup_cloexec(c->file.fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void chunkqueue_steal(chunkqueue * const restrict dest, chunkqueue * const restrict src, off_t len) {
|
2015-02-08 19:10:36 +00:00
|
|
|
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 */
|
2020-10-20 01:37:38 +00:00
|
|
|
chunkqueue_steal_partial_file_chunk(dest, c, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
static chunk *chunkqueue_get_append_tempfile(chunkqueue * const restrict cq, log_error_st * const restrict errh) {
|
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
|
|
|
|
2019-10-13 08:59:57 +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) */
|
2019-11-25 06:54:08 +00:00
|
|
|
log_perror(errh, __FILE__, __LINE__,
|
|
|
|
"opening temp-file failed: %s", template->ptr);
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
int chunkqueue_append_mem_to_tempfile(chunkqueue * const restrict dest, const char * restrict mem, size_t len, log_error_st * const restrict errh) {
|
2016-04-08 04:39:50 +00:00
|
|
|
chunk *dst_c;
|
2015-02-08 19:10:36 +00:00
|
|
|
ssize_t written;
|
|
|
|
|
|