cache/engines/fs.c
author Tero Marttila <terom@fixme.fi>
Sat, 09 Aug 2008 01:00:18 +0300
changeset 34 f3ab8656b6a0
parent 33 b750e8865127
child 36 b4023990811e
permissions -rw-r--r--
update cache.h to reflect changes, empty cache entries are illegal, and unknown-size is returned as 0
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     1
#define _GNU_SOURCE
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     2
#include <sys/types.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     3
#include <unistd.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     4
#include <sys/mman.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     5
#include <sys/stat.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     6
#include <stdlib.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     7
#include <sys/param.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     8
#include <stdio.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
     9
#include <fcntl.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    10
#include <errno.h>
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    11
#include <assert.h>
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    12
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    13
#include "../../cache.h"
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    14
#include "../cache.h"
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    15
#include "../engine.h"
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    16
#include "../op.h"
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    17
#include "../../common.h"
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    18
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    19
#define FS_PAGE_SIZE (4096)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    20
#define FS_INITIAL_SIZE (1 * FS_PAGE_SIZE)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    21
#define FS_PAGE_GROW_FACTOR (2)
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    22
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    23
struct cache_engine_fs {
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    24
    struct cache_engine base;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    25
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    26
    // custom stuff
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    27
    const char *cache_dir;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    28
};
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    29
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    30
struct cache_fs {
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    31
    struct cache base;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    32
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    33
    // custom stuff?
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    34
};
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    35
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    36
struct cache_op_fs {
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    37
    struct cache_op base;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    38
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    39
    // custom
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    40
    int fd;
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    41
    
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    42
    /*
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    43
     * Either contains the final size of the cache entry, or zero.
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    44
     */
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    45
    off_t size;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    46
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    47
    /*
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    48
     * Contains the size of the currently mmap'd region
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    49
     */
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    50
    off_t mmap_size;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    51
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    52
    void *mmap;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    53
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    54
    off_t write_offset;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    55
};
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    56
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    57
/*
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    58
 * if new_size is equal to op->mmap_size, nothing will be changed
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    59
 */
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    60
static int _fs_mmap (struct cache_op_fs *op, size_t new_size) {
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    61
    off_t old_size = op->mmap_size;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    62
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    63
    assert(new_size > 0);
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    64
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    65
    if (new_size != old_size) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    66
        // calc new size
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    67
        op->mmap_size = new_size;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    68
        
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    69
        // and ftruncate
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    70
        if (ftruncate(op->fd, op->mmap_size))
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    71
            PERROR("ftruncate");
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    72
    }
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    73
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    74
    if (op->mmap) {
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    75
        assert(old_size > 0);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    76
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    77
        // XXX: test
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    78
        if ((op->mmap = mremap(op->mmap, old_size, op->mmap_size, MREMAP_MAYMOVE)) == MAP_FAILED)
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    79
            PERROR("mremap");
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    80
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    81
    } else {
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
    82
        if ((op->mmap = mmap(NULL, op->mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, op->fd, 0)) == MAP_FAILED)
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    83
            PERROR("mmap");
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    84
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    85
    }
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    86
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    87
    return 0;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    88
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    89
error:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    90
    return -1;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    91
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
    92
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    93
/*
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    94
 * reuturn a pointer to a static buf containing the path to the key in the given op
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    95
 */
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    96
static const char *_fs_path (struct cache_engine_fs *engine, struct cache_op_fs *op) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    97
    static char path[PATH_MAX];
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    98
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    99
    struct cache_key *key = op->base.key;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   100
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   101
    // construct the path to the appropriate file
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   102
    if (snprintf(path, PATH_MAX, "%s/%*s", engine->cache_dir, (int) key->length - 1, key->buf) >= PATH_MAX)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   103
        ERROR("path too long: %s/%*s", engine->cache_dir, (int) key->length - 1, key->buf);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   104
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   105
    return path;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   106
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   107
error:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   108
    return NULL;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   109
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   110
 
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   111
/*
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   112
 * Grow the file if needed so that it fits the given amount of bytes
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   113
 */
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   114
static int _fs_grow (struct cache_op_fs *op, size_t new_size_hint) {
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   115
    if (op->mmap_size >= new_size_hint)
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   116
        return 0;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   117
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   118
    // XXX: need some math.ceil
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   119
    size_t new_size = ((op->mmap_size / FS_PAGE_SIZE) + 1) * FS_PAGE_SIZE;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   120
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   121
    while (new_size < new_size_hint) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   122
        new_size *= FS_PAGE_GROW_FACTOR;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   123
    }
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   124
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   125
    return _fs_mmap(op, new_size);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   126
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   127
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   128
static int _fs_do_init (struct cache_engine *engine, struct cache **cache_ptr) {
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   129
    struct cache_engine_fs *ctx = (struct cache_engine_fs *) engine;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   130
    struct cache_fs *cache = NULL;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   131
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   132
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   133
    if ((cache = calloc(1, sizeof(*cache))) == NULL)
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   134
        ERROR("calloc");
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   135
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   136
    if (cache_init(&cache->base, &ctx->base))
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   137
        goto error;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   138
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   139
    *cache_ptr = &cache->base;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   140
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   141
    return 0;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   142
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   143
error:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   144
    free(cache);
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   145
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   146
    return -1;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   147
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   148
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   149
static int _fs_do_op_start (struct cache *cache, struct cache_op **op_ptr, struct cache_key *key) {
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   150
    struct cache_engine_fs *engine = (struct cache_engine_fs *) cache->engine;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   151
    struct cache_op_fs *op = NULL;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   152
    struct stat stat_info;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   153
    int found = 0;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   154
    const char *path;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   155
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   156
    // allocate it
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   157
    if ((op = calloc(1, sizeof(*op))) == NULL)
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   158
        ERROR("calloc");
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   159
    
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   160
    // init it
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   161
    if (cache_op_init(&op->base, cache, key))
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   162
        goto error;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   163
    
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   164
    // mark it as being in the lookup state...
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   165
    op->base.state = OP_STATE_LOOKUP;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   166
    
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   167
    // fetch the path
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   168
    if ((path = _fs_path(engine, op)) == NULL)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   169
        goto error;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   170
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   171
    // stat
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   172
    if (stat(path, &stat_info)) {
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   173
        if (errno == ENOENT) {
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   174
            found = 0;
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   175
            // op->size remains 0
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   176
        } else
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   177
            PERROR("stat: %s", path);
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   178
    } else {
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   179
        found = 1;
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   180
        op->size = stat_info.st_size;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   181
    }
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   182
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   183
    // indicate that the key was found/not found
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   184
    if (cache_op_lookup_done(&op->base, found))
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   185
        goto error;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   186
    
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   187
    *op_ptr = &op->base;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   188
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   189
    // done!
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   190
    return 0;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   191
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   192
error:
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   193
    free(op);
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   194
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   195
    return -1;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   196
   
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   197
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   198
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   199
int _fs_do_op_available (struct cache_op *op_base, size_t *size, size_t *offset) {
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   200
    struct cache_op_fs *op = (struct cache_op_fs *) op_base;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   201
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   202
    // this is easy, it's only known if a HIT, or after op_done, so it's the same as...
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   203
    *size = op->size;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   204
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   205
    // op->size if nonzero, write_offset otherwise
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   206
    *offset = op->size ? op->size : op->write_offset;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   207
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   208
    return 0;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   209
}
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   210
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   211
int _fs_do_op_begin_write (struct cache_op *op_base, size_t size_hint) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   212
    struct cache_op_fs *op = (struct cache_op_fs *) op_base;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   213
    struct cache_engine_fs *engine = (struct cache_engine_fs *) op->base.cache->engine;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   214
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   215
    const char *path;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   216
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   217
    assert(size_hint >= 0);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   218
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   219
    // should be unknown (0) for HITs that we then write to
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   220
    assert(op->size == 0);
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   221
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   222
    // fetch the path
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   223
    if ((path = _fs_path(engine, op)) == NULL)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   224
        goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   225
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   226
    // create the appropriate file for read-write, exclusively
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   227
    if ((op->fd = open(path, O_CREAT | O_RDWR | O_EXCL, 0644)) == -1)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   228
        PERROR("open: %s", path);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   229
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   230
    // ftruncate, and then mmap
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   231
    if (_fs_mmap(op, size_hint ? size_hint : FS_INITIAL_SIZE))
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   232
        goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   233
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   234
    // great
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   235
    if (cache_op_write_ready(&op->base))
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   236
        goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   237
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   238
    // done
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   239
    return 0;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   240
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   241
error:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   242
    return -1;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   243
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   244
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   245
int _fs_do_op_push (struct cache_op *op_base, int fd, size_t *size_ptr) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   246
    struct cache_op_fs *op = (struct cache_op_fs *) op_base;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   247
    struct cache_engine_fs *engine = (struct cache_engine_fs *) op->base.cache->engine;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   248
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   249
    size_t ret, size;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   250
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   251
    assert(op->fd > 0);
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   252
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   253
    // must have called begin_write first...
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   254
    assert(op->mmap_size > 0);
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   255
    assert(op->mmap != NULL);
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   256
    assert(op->write_offset <= op->mmap_size);
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   257
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   258
    // default size if none specified
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   259
    if (*size_ptr == 0)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   260
        size = FS_INITIAL_SIZE;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   261
    else
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   262
        size = *size_ptr;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   263
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   264
    // grow the file if needed
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   265
    if (_fs_grow(op, op->write_offset + size))
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   266
        goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   267
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   268
    // read the data into the mmap'd region
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   269
    if ((ret = read(fd, op->mmap + op->write_offset, size)) == -1)
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   270
        // XXX: EAGAIN
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   271
        PERROR("read");
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   272
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   273
    // move the write offset along
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   274
    op->write_offset += ret;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   275
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   276
    // return something
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   277
    *size_ptr = ret;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   278
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   279
    // notify newly available data
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   280
    if (cache_op_data_available(&op->base))
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   281
        goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   282
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   283
    // great
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   284
    return 0;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   285
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   286
error:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   287
    return -1;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   288
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   289
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   290
int _fs_do_op_done (struct cache_op *op_base) {
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   291
    struct cache_op_fs *op = (struct cache_op_fs *) op_base;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   292
    struct cache_engine_fs *engine = (struct cache_engine_fs *) op->base.cache->engine;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   293
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   294
    assert(op->fd > 0);
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   295
    assert(op->mmap != NULL);
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   296
    assert(op->write_offset <= op->mmap_size);
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   297
    
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   298
    // empty cache entries are illegal
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   299
    assert(op->write_offset > 0);
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   300
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   301
    // final size is now known
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   302
    op->size = op->write_offset;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   303
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   304
    // truncate to match data length
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   305
    if (_fs_mmap(op, op->size))
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   306
        goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   307
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   308
    // notify that data is complete
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   309
    if (cache_op_write_done(&op->base))
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   310
        goto error;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   311
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   312
    // great
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   313
    return 0;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   314
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   315
error:
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   316
    return -1;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   317
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   318
}
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   319
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   320
int _fs_do_op_close (struct cache_op *op_base) {
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   321
    struct cache_op_fs *op = (struct cache_op_fs *) op_base;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   322
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   323
    // unmap
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   324
    if (op->mmap)
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   325
        if (munmap(op->mmap, op->mmap_size))
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   326
            PWARNING("munmap");
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   327
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   328
    // close
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   329
    if (op->fd > 0)
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   330
        if (close(op->fd))
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   331
            PWARNING("close");
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   332
    
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   333
    // XXX: delete if op->write_offset == 0?
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   334
    
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   335
    // free
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   336
    free(op);
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   337
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   338
    return 0;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   339
}
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   340
struct cache_engine *cache_engine_fs (const char *cache_dir) {
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   341
    struct cache_engine_fs *ctx = NULL;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   342
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   343
    if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   344
        ERROR("calloc");
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   345
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   346
    ctx->cache_dir = cache_dir;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   347
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   348
    // set up the fn table
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   349
    ctx->base.fn_init = &_fs_do_init;
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   350
    ctx->base.fn_op_start = &_fs_do_op_start;
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   351
    ctx->base.fn_op_available = &_fs_do_op_available;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   352
    ctx->base.fn_op_begin_write = &_fs_do_op_begin_write;
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   353
    ctx->base.fn_op_push = &_fs_do_op_push;
33
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   354
    ctx->base.fn_op_done = &_fs_do_op_done;
b750e8865127 working write cycle support
Tero Marttila <terom@fixme.fi>
parents: 31
diff changeset
   355
    ctx->base.fn_op_close = &_fs_do_op_close;
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   356
    
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   357
    return &ctx->base;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   358
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   359
error:
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   360
    free(ctx);
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   361
31
12d5361e7472 req/write/push implemented
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   362
    return NULL;
30
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   363
}
33e464fd6773 my hg working dir managed to break itself somehow... my attempt to fix that, and add some cache code :)
terom@cl-543.hel-01.fi.sixxs.net
parents:
diff changeset
   364