diff options
Diffstat (limited to 'src/storage-mmap.c')
-rw-r--r-- | src/storage-mmap.c | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/storage-mmap.c b/src/storage-mmap.c index f5e73ab..6fa4565 100644 --- a/src/storage-mmap.c +++ b/src/storage-mmap.c @@ -36,9 +36,13 @@ static int write_header(int fd, struct tsnif_storage_opts *opts) static int map_header(struct tsnif_storage_handle *h) { void *ptr; + int flags = PROT_READ; + + if (h->opts->flags & TSNIF_STORAGE_OPT_WRITE) + flags = PROT_WRITE; ptr = mmap(NULL, sysconf(_SC_PAGESIZE), - PROT_WRITE | PROT_READ, + flags, MAP_SHARED, h->fd, 0); if (MAP_FAILED == ptr) return -1; @@ -51,7 +55,7 @@ int tsnif_storage_init(struct tsnif_storage_handle *h, struct tsnif_storage_opts *opts, char *name) { int fd, create, err; - int flags = O_RDWR; + int flags = O_RDONLY; if (!opts) return -1; @@ -65,9 +69,9 @@ int tsnif_storage_init(struct tsnif_storage_handle *h, create = (opts->flags & TSNIF_STORAGE_OPT_WRITE); if (create) - flags |= O_CREAT | O_TRUNC; + flags |= O_CREAT | O_TRUNC | O_RDWR; - fd = open(name, flags, S_IRUSR | S_IWUSR); + fd = open(name, flags); if (fd < 0) { TSNIF_DEBUG("open failed for '%s': %s\n", name, strerror(errno)); @@ -165,7 +169,8 @@ static int map_chunk(struct tsnif_storage_handle *h, { void *ptr; off_t offset = 0; - int err; + int err, flags = PROT_READ; + TSNIF_DEBUG("what %d\n", what); @@ -231,10 +236,12 @@ static int map_chunk(struct tsnif_storage_handle *h, err = file_trunc(h, offset + h->opts->chunk_size); if (err) return err; + + flags = PROT_WRITE; } ptr = mmap(NULL, h->opts->chunk_size, - PROT_WRITE | PROT_READ, + flags, MAP_SHARED, h->fd, offset); if (MAP_FAILED == ptr) return -1; @@ -356,10 +363,13 @@ static int store_rec(struct tsnif_storage_handle *h, TSNIF_DEBUG("chunk data %p\n", chunk->current_data); TSNIF_DEBUG("chunk index %p\n", chunk->current_index); + TSNIF_DEBUG("time %ld\n", rec->time.tv_sec); mrec = chunk->current_data; - mrec->len = rec->len; + mrec->len = rec->len; mrec->flags = rec->flags; + mrec->time = rec->time; + mrec->ws = rec->ws; memcpy(mrec->data, rec->ptr, rec->len); #define RECLEN (sizeof(struct tsnif_storage_rec_mmap) + rec->len) @@ -414,9 +424,11 @@ static int read_rec(struct tsnif_storage_handle *h, offset = *(chunk->current_index - chunk->read_index); mrec = (void*) header + offset; - rec->ptr = mrec->data; - rec->len = mrec->len; + rec->ptr = mrec->data; + rec->len = mrec->len; rec->flags = mrec->flags; + rec->time = mrec->time; + rec->ws = mrec->ws; return 0; } |