| 1 | #ifndef _CHUNK_H_ |
|---|
| 2 | #define _CHUNK_H_ |
|---|
| 3 | |
|---|
| 4 | #include "buffer.h" |
|---|
| 5 | #include "array.h" |
|---|
| 6 | |
|---|
| 7 | typedef struct chunk { |
|---|
| 8 | enum { UNUSED_CHUNK, MEM_CHUNK, FILE_CHUNK } type; |
|---|
| 9 | |
|---|
| 10 | buffer *mem; /* either the storage of the mem-chunk or the read-ahead buffer */ |
|---|
| 11 | |
|---|
| 12 | struct { |
|---|
| 13 | /* filechunk */ |
|---|
| 14 | buffer *name; /* name of the file */ |
|---|
| 15 | off_t start; /* starting offset in the file */ |
|---|
| 16 | off_t length; /* octets to send from the starting offset */ |
|---|
| 17 | |
|---|
| 18 | int fd; |
|---|
| 19 | struct { |
|---|
| 20 | char *start; /* the start pointer of the mmap'ed area */ |
|---|
| 21 | size_t length; /* size of the mmap'ed area */ |
|---|
| 22 | off_t offset; /* start is <n> octets away from the start of the file */ |
|---|
| 23 | } mmap; |
|---|
| 24 | |
|---|
| 25 | int is_temp; /* file is temporary and will be deleted on cleanup */ |
|---|
| 26 | |
|---|
| 27 | struct { |
|---|
| 28 | int fd; |
|---|
| 29 | off_t length; |
|---|
| 30 | off_t offset; |
|---|
| 31 | } copy; |
|---|
| 32 | } file; |
|---|
| 33 | |
|---|
| 34 | off_t offset; /* octets sent from this chunk |
|---|
| 35 | the size of the chunk is either |
|---|
| 36 | - mem-chunk: mem->used - 1 |
|---|
| 37 | - file-chunk: file.length |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | struct { |
|---|
| 41 | off_t written; |
|---|
| 42 | int ret_val; |
|---|
| 43 | } async; |
|---|
| 44 | |
|---|
| 45 | struct chunk *next; |
|---|
| 46 | } chunk; |
|---|
| 47 | |
|---|
| 48 | typedef struct { |
|---|
| 49 | chunk *first; |
|---|
| 50 | chunk *last; |
|---|
| 51 | |
|---|
| 52 | array *tempdirs; |
|---|
| 53 | |
|---|
| 54 | int is_closed; /* the input to this CQ is closed */ |
|---|
| 55 | |
|---|
| 56 | off_t bytes_in, bytes_out; |
|---|
| 57 | } chunkqueue; |
|---|
| 58 | |
|---|
| 59 | LI_API chunkqueue* chunkqueue_init(void); |
|---|
| 60 | LI_API int chunkqueue_set_tempdirs(chunkqueue *c, array *tempdirs); |
|---|
| 61 | LI_API int chunkqueue_append_file(chunkqueue *c, buffer *fn, off_t offset, off_t len); |
|---|
| 62 | LI_API int chunkqueue_append_mem(chunkqueue *c, const char *mem, size_t len); |
|---|
| 63 | LI_API int chunkqueue_append_buffer(chunkqueue *c, buffer *mem); |
|---|
| 64 | LI_API int chunkqueue_prepend_buffer(chunkqueue *c, buffer *mem); |
|---|
| 65 | |
|---|
| 66 | LI_API buffer * chunkqueue_get_append_buffer(chunkqueue *c); |
|---|
| 67 | LI_API buffer * chunkqueue_get_prepend_buffer(chunkqueue *c); |
|---|
| 68 | LI_API chunk * chunkqueue_get_append_tempfile(chunkqueue *cq); |
|---|
| 69 | LI_API int chunkqueue_steal_tempfile(chunkqueue *cq, chunk *in); |
|---|
| 70 | LI_API off_t chunkqueue_steal_chunk(chunkqueue *cq, chunk *c); |
|---|
| 71 | LI_API off_t chunkqueue_steal_chunks_len(chunkqueue *cq, chunk *c, off_t max_len); |
|---|
| 72 | LI_API off_t chunkqueue_steal_all_chunks(chunkqueue *cq, chunkqueue *in); |
|---|
| 73 | LI_API off_t chunkqueue_skip(chunkqueue *cq, off_t skip); |
|---|
| 74 | LI_API void chunkqueue_remove_empty_last_chunk(chunkqueue *cq); |
|---|
| 75 | |
|---|
| 76 | LI_API int chunkqueue_remove_finished_chunks(chunkqueue *cq); |
|---|
| 77 | |
|---|
| 78 | LI_API off_t chunkqueue_length(chunkqueue *c); |
|---|
| 79 | LI_API off_t chunkqueue_written(chunkqueue *c); |
|---|
| 80 | LI_API void chunkqueue_free(chunkqueue *c); |
|---|
| 81 | LI_API void chunkqueue_reset(chunkqueue *c); |
|---|
| 82 | |
|---|
| 83 | LI_API int chunkqueue_is_empty(chunkqueue *c); |
|---|
| 84 | |
|---|
| 85 | LI_API void chunkqueue_print(chunkqueue *cq); |
|---|
| 86 | |
|---|
| 87 | LI_API int chunk_is_done(chunk *c); |
|---|
| 88 | LI_API void chunk_set_done(chunk *c); |
|---|
| 89 | |
|---|
| 90 | #endif |
|---|