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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2005-09-14 08:00:33 +00:00
|
|
|
#include <sys/mman.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "chunk.h"
|
|
|
|
|
|
|
|
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));
|
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
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cq->unused = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return cq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static chunk *chunk_init(void) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = calloc(1, sizeof(*c));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
c->mem = buffer_init();
|
|
|
|
c->file.name = buffer_init();
|
|
|
|
c->file.fd = -1;
|
|
|
|
c->file.mmap.start = MAP_FAILED;
|
2005-02-20 14:27:00 +00:00
|
|
|
c->next = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chunk_free(chunk *c) {
|
|
|
|
if (!c) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_free(c->mem);
|
|
|
|
buffer_free(c->file.name);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
|
2005-09-08 10:00:32 +00:00
|
|
|
static void chunk_reset(chunk *c) {
|
|
|
|
if (!c) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_reset(c->mem);
|
2005-09-26 08:53:58 +00:00
|
|
|
|
|
|
|
if (c->file.is_temp && !buffer_is_empty(c->file.name)) {
|
|
|
|
unlink(c->file.name->ptr);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_reset(c->file.name);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
void chunkqueue_free(chunkqueue *cq) {
|
|
|
|
chunk *c, *pc;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!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;
|
|
|
|
chunk_free(pc);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (c = cq->unused; c; ) {
|
|
|
|
pc = c;
|
|
|
|
c = c->next;
|
|
|
|
chunk_free(pc);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(cq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static chunk *chunkqueue_get_unused_chunk(chunkqueue *cq) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* check if we have a unused chunk */
|
|
|
|
if (!cq->unused) {
|
|
|
|
c = chunk_init();
|
|
|
|
} else {
|
|
|
|
/* take the first element from the list (a stack) */
|
|
|
|
c = cq->unused;
|
|
|
|
cq->unused = c->next;
|
|
|
|
c->next = NULL;
|
2005-09-08 10:00:32 +00:00
|
|
|
cq->unused_chunks--;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chunkqueue_prepend_chunk(chunkqueue *cq, chunk *c) {
|
|
|
|
c->next = cq->first;
|
|
|
|
cq->first = c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (cq->last == NULL) {
|
|
|
|
cq->last = c;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chunkqueue_append_chunk(chunkqueue *cq, chunk *c) {
|
|
|
|
if (cq->last) {
|
|
|
|
cq->last->next = c;
|
|
|
|
}
|
|
|
|
cq->last = c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (cq->first == NULL) {
|
|
|
|
cq->first = c;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void chunkqueue_reset(chunkqueue *cq) {
|
2005-09-08 10:00:32 +00:00
|
|
|
chunk *c;
|
2005-02-20 14:27:00 +00:00
|
|
|
/* move everything to the unused queue */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/* mark all read written */
|
2005-09-08 10:00:32 +00:00
|
|
|
for (c = cq->first; c; c = c->next) {
|
|
|
|
switch(c->type) {
|
|
|
|
case MEM_CHUNK:
|
2005-09-14 08:00:33 +00:00
|
|
|
c->offset = c->mem->used - 1;
|
2005-09-08 10:00:32 +00:00
|
|
|
break;
|
|
|
|
case FILE_CHUNK:
|
2005-09-14 08:00:33 +00:00
|
|
|
c->offset = c->file.length;
|
2005-09-08 10:00:32 +00:00
|
|
|
break;
|
2006-10-04 13:26:23 +00:00
|
|
|
default:
|
2005-09-08 10:00:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkqueue_remove_finished_chunks(cq);
|
2005-09-14 08:00:33 +00:00
|
|
|
cq->bytes_in = 0;
|
|
|
|
cq->bytes_out = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int chunkqueue_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (len == 0) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c->type = FILE_CHUNK;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_copy_string_buffer(c->file.name, fn);
|
2005-10-24 11:36:22 +00:00
|
|
|
c->file.start = offset;
|
2005-09-14 08:00:33 +00:00
|
|
|
c->file.length = len;
|
2005-02-20 14:27:00 +00:00
|
|
|
c->offset = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int chunkqueue_append_buffer(chunkqueue *cq, buffer *mem) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (mem->used == 0) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
|
|
|
c->type = MEM_CHUNK;
|
|
|
|
c->offset = 0;
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_copy_string_buffer(c->mem, mem);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:03:40 +00:00
|
|
|
int chunkqueue_append_buffer_weak(chunkqueue *cq, buffer *mem) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-21 08:03:40 +00:00
|
|
|
if (mem->used == 0) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-21 08:03:40 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
|
|
|
c->type = MEM_CHUNK;
|
|
|
|
c->offset = 0;
|
|
|
|
if (c->mem) buffer_free(c->mem);
|
|
|
|
c->mem = mem;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-21 08:03:40 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-21 08:03:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
int chunkqueue_prepend_buffer(chunkqueue *cq, buffer *mem) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (mem->used == 0) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
|
|
|
c->type = MEM_CHUNK;
|
|
|
|
c->offset = 0;
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_copy_string_buffer(c->mem, mem);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_prepend_chunk(cq, c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:03:40 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
int chunkqueue_append_mem(chunkqueue *cq, const char * mem, size_t len) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (len == 0) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
|
|
|
c->type = MEM_CHUNK;
|
|
|
|
c->offset = 0;
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_copy_string_len(c->mem, mem, len - 1);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer * chunkqueue_get_prepend_buffer(chunkqueue *cq) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c->type = MEM_CHUNK;
|
|
|
|
c->offset = 0;
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_reset(c->mem);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_prepend_chunk(cq, c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
return c->mem;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer *chunkqueue_get_append_buffer(chunkqueue *cq) {
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c->type = MEM_CHUNK;
|
|
|
|
c->offset = 0;
|
2005-09-14 08:00:33 +00:00
|
|
|
buffer_reset(c->mem);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_append_chunk(cq, c);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-14 08:00:33 +00:00
|
|
|
return c->mem;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2005-11-01 07:50:08 +00:00
|
|
|
int chunkqueue_set_tempdirs(chunkqueue *cq, array *tempdirs) {
|
|
|
|
if (!cq) return -1;
|
|
|
|
|
|
|
|
cq->tempdirs = tempdirs;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-26 08:53:58 +00:00
|
|
|
chunk *chunkqueue_get_append_tempfile(chunkqueue *cq) {
|
|
|
|
chunk *c;
|
2005-11-01 07:50:08 +00:00
|
|
|
buffer *template = buffer_init_string("/var/tmp/lighttpd-upload-XXXXXX");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-26 08:53:58 +00:00
|
|
|
c = chunkqueue_get_unused_chunk(cq);
|
|
|
|
|
|
|
|
c->type = FILE_CHUNK;
|
|
|
|
c->offset = 0;
|
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
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* we have several tempdirs, only if all of them fail we jump out */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-01 07:50:08 +00:00
|
|
|
for (i = 0; i < cq->tempdirs->used; i++) {
|
|
|
|
data_string *ds = (data_string *)cq->tempdirs->data[i];
|
|
|
|
|
|
|
|
buffer_copy_string_buffer(template, ds->value);
|
|
|
|
BUFFER_APPEND_SLASH(template);
|
|
|
|
BUFFER_APPEND_STRING_CONST(template, "lighttpd-upload-XXXXXX");
|
|
|
|
|
|
|
|
if (-1 != (c->file.fd = mkstemp(template->ptr))) {
|
|
|
|
/* only trigger the unlink if we created the temp-file successfully */
|
|
|
|
c->file.is_temp = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (-1 != (c->file.fd = mkstemp(template->ptr))) {
|
|
|
|
/* only trigger the unlink if we created the temp-file successfully */
|
|
|
|
c->file.is_temp = 1;
|
|
|
|
}
|
2005-10-31 08:14:02 +00:00
|
|
|
}
|
|
|
|
|
2005-11-01 07:50:08 +00:00
|
|
|
buffer_copy_string_buffer(c->file.name, template);
|
2005-10-31 08:14:02 +00:00
|
|
|
c->file.length = 0;
|
2005-09-26 08:53:58 +00:00
|
|
|
|
|
|
|
chunkqueue_append_chunk(cq, c);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
off_t chunkqueue_length(chunkqueue *cq) {
|
|
|
|
off_t len = 0;
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (c = cq->first; c; c = c->next) {
|
|
|
|
switch (c->type) {
|
|
|
|
case MEM_CHUNK:
|
2005-09-14 08:00:33 +00:00
|
|
|
len += c->mem->used ? c->mem->used - 1 : 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case FILE_CHUNK:
|
2005-09-14 08:00:33 +00:00
|
|
|
len += c->file.length;
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t chunkqueue_written(chunkqueue *cq) {
|
|
|
|
off_t len = 0;
|
|
|
|
chunk *c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (c = cq->first; c; c = c->next) {
|
|
|
|
switch (c->type) {
|
|
|
|
case MEM_CHUNK:
|
|
|
|
case FILE_CHUNK:
|
|
|
|
len += c->offset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int chunkqueue_is_empty(chunkqueue *cq) {
|
|
|
|
return cq->first ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2005-09-08 10:00:32 +00:00
|
|
|
int chunkqueue_remove_finished_chunks(chunkqueue *cq) {
|
|
|
|
chunk *c;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2005-09-08 10:00:32 +00:00
|
|
|
for (c = cq->first; c; c = cq->first) {
|
|
|
|
int is_finished = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2005-09-08 10:00:32 +00:00
|
|
|
switch (c->type) {
|
|
|
|
case MEM_CHUNK:
|
2006-09-18 16:41:43 +00:00
|
|
|
if (c->mem->used == 0 || (c->offset == (off_t)c->mem->used - 1)) is_finished = 1;
|
2005-09-08 10:00:32 +00:00
|
|
|
break;
|
|
|
|
case FILE_CHUNK:
|
2006-10-04 13:26:23 +00:00
|
|
|
if (c->offset == c->file.length) is_finished = 1;
|
2005-09-08 10:00:32 +00:00
|
|
|
break;
|
2006-10-04 13:26:23 +00:00
|
|
|
default:
|
2005-09-08 10:00:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2005-09-08 10:00:32 +00:00
|
|
|
if (!is_finished) break;
|
|
|
|
|
|
|
|
chunk_reset(c);
|
|
|
|
|
|
|
|
cq->first = c->next;
|
|
|
|
if (c == cq->last) cq->last = NULL;
|
|
|
|
|
|
|
|
/* keep at max 4 chunks in the 'unused'-cache */
|
|
|
|
if (cq->unused_chunks > 4) {
|
|
|
|
chunk_free(c);
|
|
|
|
} else {
|
|
|
|
c->next = cq->unused;
|
|
|
|
cq->unused = c;
|
|
|
|
cq->unused_chunks++;
|
|
|
|
}
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|