1 #ifndef _CHUNK_H_ 2 #define _CHUNK_H_ 3 #include "first.h" 4 5 #ifdef _AIX /*(AIX might #define mmap mmap64)*/ 6 #include "sys-mmap.h" 7 #endif 8 9 #include "buffer.h" 10 #include "array.h" 11 12 typedef struct chunk { 13 struct chunk *next; 14 enum { MEM_CHUNK, FILE_CHUNK } type; 15 16 buffer *mem; /* either the storage of the mem-chunk or the name of the file */ 17 18 /* the size of the chunk is either: 19 * - mem-chunk: buffer_string_length(chunk::mem) 20 * - file-chunk: chunk::file.length 21 */ 22 off_t offset; /* octets sent from this chunk */ 23 24 struct { 25 /* filechunk */ 26 off_t start; /* starting offset in the file */ 27 off_t length; /* octets to send from the starting offset */ 28 29 int fd; 30 int is_temp; /* file is temporary and will be deleted if on cleanup */ 31 struct { 32 char *start; /* the start pointer of the mmap'ed area */ 33 size_t length; /* size of the mmap'ed area */ 34 off_t offset; /* start is <n> octet away from the start of the file */ 35 } mmap; 36 } file; 37 } chunk; 38 39 typedef struct { 40 chunk *first; 41 chunk *last; 42 43 off_t bytes_in, bytes_out; 44 45 array *tempdirs; 46 off_t upload_temp_file_size; 47 unsigned int tempdir_idx; 48 } chunkqueue; 49 50 buffer * chunk_buffer_acquire(void); 51 void chunk_buffer_release(buffer *b); 52 53 void chunkqueue_chunk_pool_clear(void); 54 void chunkqueue_chunk_pool_free(void); 55 56 chunkqueue *chunkqueue_init(void); 57 void chunkqueue_set_chunk_size (size_t sz); 58 void chunkqueue_set_tempdirs_default_reset (void); 59 void chunkqueue_set_tempdirs_default (array *tempdirs, off_t upload_temp_file_size); 60 void chunkqueue_set_tempdirs(chunkqueue *cq, array *tempdirs, off_t upload_temp_file_size); 61 void chunkqueue_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len); /* copies "fn" */ 62 void chunkqueue_append_file_fd(chunkqueue *cq, buffer *fn, int fd, off_t offset, off_t len); /* copies "fn" */ 63 void chunkqueue_append_mem(chunkqueue *cq, const char *mem, size_t len); /* copies memory */ 64 void chunkqueue_append_mem_min(chunkqueue *cq, const char * mem, size_t len); /* copies memory */ 65 void chunkqueue_append_buffer(chunkqueue *cq, buffer *mem); /* may reset "mem" */ 66 void chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src); 67 68 buffer * chunkqueue_prepend_buffer_open_sz(chunkqueue *cq, size_t sz); 69 buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq); 70 void chunkqueue_prepend_buffer_commit(chunkqueue *cq); 71 buffer * chunkqueue_append_buffer_open_sz(chunkqueue *cq, size_t sz); 72 buffer * chunkqueue_append_buffer_open(chunkqueue *cq); 73 void chunkqueue_append_buffer_commit(chunkqueue *cq); 74 75 struct server; /*(declaration)*/ 76 int chunkqueue_append_mem_to_tempfile(struct server *srv, chunkqueue *cq, const char *mem, size_t len); 77 78 /* functions to handle buffers to read into: */ 79 /* obtain/reserve memory in chunkqueue at least len (input) size, 80 * return pointer to memory with len (output) available for use 81 * modifying the chunkqueue invalidates the memory area. 82 * should always be followed by chunkqueue_get_memory(), 83 * even if nothing was read. 84 * pass 0 in len for mem at least half of chunk_buf_sz 85 */ 86 char * chunkqueue_get_memory(chunkqueue *cq, size_t *len); 87 /* commit len bytes of mem obtained from chunkqueue_get_memory() */ 88 void chunkqueue_use_memory(chunkqueue *cq, size_t len); 89 90 /* mark first "len" bytes as written (incrementing chunk offsets) 91 * and remove finished chunks 92 */ 93 void chunkqueue_mark_written(chunkqueue *cq, off_t len); 94 95 void chunkqueue_remove_finished_chunks(chunkqueue *cq); 96 97 void chunkqueue_steal(chunkqueue *dest, chunkqueue *src, off_t len); 98 struct server; 99 int chunkqueue_steal_with_tempfiles(struct server *srv, chunkqueue *dest, chunkqueue *src, off_t len); 100 101 int chunkqueue_open_file_chunk(struct server *srv, chunkqueue *cq); 102 103 off_t chunkqueue_length(chunkqueue *cq); 104 void chunkqueue_free(chunkqueue *cq); 105 void chunkqueue_reset(chunkqueue *cq); 106 107 static inline int chunkqueue_is_empty(const chunkqueue *cq); 108 static inline int chunkqueue_is_empty(const chunkqueue *cq) { 109 return NULL == cq->first; 110 } 111 112 #endif 113