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-- |
31 | 1 |
#define _GNU_SOURCE |
2 |
#include <sys/types.h> |
|
3 |
#include <unistd.h> |
|
4 |
#include <sys/mman.h> |
|
5 |
#include <sys/stat.h> |
|
6 |
#include <stdlib.h> |
|
7 |
#include <sys/param.h> |
|
8 |
#include <stdio.h> |
|
9 |
#include <fcntl.h> |
|
10 |
#include <errno.h> |
|
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 | 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 | 19 |
#define FS_PAGE_SIZE (4096) |
20 |
#define FS_INITIAL_SIZE (1 * FS_PAGE_SIZE) |
|
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 | 41 |
|
42 |
/* |
|
43 |
* Either contains the final size of the cache entry, or zero. |
|
44 |
*/ |
|
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 | 47 |
/* |
48 |
* Contains the size of the currently mmap'd region |
|
49 |
*/ |
|
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 | 53 |
|
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 | 57 |
/* |
33 | 58 |
* if new_size is equal to op->mmap_size, nothing will be changed |
31 | 59 |
*/ |
60 |
static int _fs_mmap (struct cache_op_fs *op, size_t new_size) { |
|
33 | 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 | 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 | 65 |
if (new_size != old_size) { |
66 |
// calc new size |
|
33 | 67 |
op->mmap_size = new_size; |
31 | 68 |
|
69 |
// and ftruncate |
|
33 | 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 | 75 |
assert(old_size > 0); |
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 | 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 | 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 | 93 |
/* |
94 |
* reuturn a pointer to a static buf containing the path to the key in the given op |
|
95 |
*/ |
|
96 |
static const char *_fs_path (struct cache_engine_fs *engine, struct cache_op_fs *op) { |
|
97 |
static char path[PATH_MAX]; |
|
98 |
||
99 |
struct cache_key *key = op->base.key; |
|
100 |
||
101 |
// construct the path to the appropriate file |
|
102 |
if (snprintf(path, PATH_MAX, "%s/%*s", engine->cache_dir, (int) key->length - 1, key->buf) >= PATH_MAX) |
|
103 |
ERROR("path too long: %s/%*s", engine->cache_dir, (int) key->length - 1, key->buf); |
|
104 |
||
105 |
return path; |
|
106 |
||
107 |
error: |
|
108 |
return NULL; |
|
109 |
} |
|
110 |
||
111 |
/* |
|
112 |
* Grow the file if needed so that it fits the given amount of bytes |
|
113 |
*/ |
|
114 |
static int _fs_grow (struct cache_op_fs *op, size_t new_size_hint) { |
|
33 | 115 |
if (op->mmap_size >= new_size_hint) |
31 | 116 |
return 0; |
117 |
||
118 |
// XXX: need some math.ceil |
|
33 | 119 |
size_t new_size = ((op->mmap_size / FS_PAGE_SIZE) + 1) * FS_PAGE_SIZE; |
31 | 120 |
|
121 |
while (new_size < new_size_hint) { |
|
122 |
new_size *= FS_PAGE_GROW_FACTOR; |
|
123 |
} |
|
124 |
||
125 |
return _fs_mmap(op, new_size); |
|
126 |
} |
|
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 | 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 | 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 | 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 | 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 | 167 |
// fetch the path |
168 |
if ((path = _fs_path(engine, op)) == NULL) |
|
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 | 171 |
// stat |
172 |
if (stat(path, &stat_info)) { |
|
33 | 173 |
if (errno == ENOENT) { |
31 | 174 |
found = 0; |
33 | 175 |
// op->size remains 0 |
176 |
} else |
|
31 | 177 |
PERROR("stat: %s", path); |
33 | 178 |
} else { |
31 | 179 |
found = 1; |
33 | 180 |
op->size = stat_info.st_size; |
181 |
} |
|
31 | 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 | 187 |
*op_ptr = &op->base; |
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 | 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 | 199 |
int _fs_do_op_available (struct cache_op *op_base, size_t *size, size_t *offset) { |
200 |
struct cache_op_fs *op = (struct cache_op_fs *) op_base; |
|
201 |
||
202 |
// this is easy, it's only known if a HIT, or after op_done, so it's the same as... |
|
203 |
*size = op->size; |
|
204 |
||
205 |
// op->size if nonzero, write_offset otherwise |
|
206 |
*offset = op->size ? op->size : op->write_offset; |
|
207 |
||
208 |
return 0; |
|
209 |
} |
|
210 |
||
31 | 211 |
int _fs_do_op_begin_write (struct cache_op *op_base, size_t size_hint) { |
212 |
struct cache_op_fs *op = (struct cache_op_fs *) op_base; |
|
213 |
struct cache_engine_fs *engine = (struct cache_engine_fs *) op->base.cache->engine; |
|
214 |
||
215 |
const char *path; |
|
216 |
||
217 |
assert(size_hint >= 0); |
|
218 |
||
33 | 219 |
// should be unknown (0) for HITs that we then write to |
220 |
assert(op->size == 0); |
|
31 | 221 |
|
222 |
// fetch the path |
|
223 |
if ((path = _fs_path(engine, op)) == NULL) |
|
224 |
goto error; |
|
225 |
||
226 |
// create the appropriate file for read-write, exclusively |
|
227 |
if ((op->fd = open(path, O_CREAT | O_RDWR | O_EXCL, 0644)) == -1) |
|
228 |
PERROR("open: %s", path); |
|
229 |
||
230 |
// ftruncate, and then mmap |
|
231 |
if (_fs_mmap(op, size_hint ? size_hint : FS_INITIAL_SIZE)) |
|
232 |
goto error; |
|
233 |
||
234 |
// great |
|
235 |
if (cache_op_write_ready(&op->base)) |
|
236 |
goto error; |
|
237 |
||
238 |
// done |
|
239 |
return 0; |
|
240 |
||
241 |
error: |
|
242 |
return -1; |
|
243 |
} |
|
244 |
||
245 |
int _fs_do_op_push (struct cache_op *op_base, int fd, size_t *size_ptr) { |
|
246 |
struct cache_op_fs *op = (struct cache_op_fs *) op_base; |
|
247 |
struct cache_engine_fs *engine = (struct cache_engine_fs *) op->base.cache->engine; |
|
248 |
||
249 |
size_t ret, size; |
|
250 |
||
251 |
assert(op->fd > 0); |
|
33 | 252 |
|
253 |
// must have called begin_write first... |
|
254 |
assert(op->mmap_size > 0); |
|
31 | 255 |
assert(op->mmap != NULL); |
33 | 256 |
assert(op->write_offset <= op->mmap_size); |
31 | 257 |
|
258 |
// default size if none specified |
|
259 |
if (*size_ptr == 0) |
|
260 |
size = FS_INITIAL_SIZE; |
|
261 |
else |
|
262 |
size = *size_ptr; |
|
263 |
||
264 |
// grow the file if needed |
|
265 |
if (_fs_grow(op, op->write_offset + size)) |
|
266 |
goto error; |
|
267 |
||
268 |
// read the data into the mmap'd region |
|
269 |
if ((ret = read(fd, op->mmap + op->write_offset, size)) == -1) |
|
270 |
// XXX: EAGAIN |
|
271 |
PERROR("read"); |
|
272 |
||
273 |
// move the write offset along |
|
274 |
op->write_offset += ret; |
|
275 |
||
276 |
// return something |
|
277 |
*size_ptr = ret; |
|
278 |
||
279 |
// notify newly available data |
|
280 |
if (cache_op_data_available(&op->base)) |
|
281 |
goto error; |
|
282 |
||
283 |
// great |
|
284 |
return 0; |
|
285 |
||
286 |
error: |
|
287 |
return -1; |
|
288 |
} |
|
289 |
||
290 |
int _fs_do_op_done (struct cache_op *op_base) { |
|
291 |
struct cache_op_fs *op = (struct cache_op_fs *) op_base; |
|
292 |
struct cache_engine_fs *engine = (struct cache_engine_fs *) op->base.cache->engine; |
|
293 |
||
294 |
assert(op->fd > 0); |
|
295 |
assert(op->mmap != NULL); |
|
33 | 296 |
assert(op->write_offset <= op->mmap_size); |
297 |
||
298 |
// empty cache entries are illegal |
|
299 |
assert(op->write_offset > 0); |
|
300 |
||
301 |
// final size is now known |
|
302 |
op->size = op->write_offset; |
|
31 | 303 |
|
304 |
// truncate to match data length |
|
33 | 305 |
if (_fs_mmap(op, op->size)) |
31 | 306 |
goto error; |
307 |
||
308 |
// notify that data is complete |
|
309 |
if (cache_op_write_done(&op->base)) |
|
310 |
goto error; |
|
311 |
||
312 |
// great |
|
313 |
return 0; |
|
314 |
||
315 |
error: |
|
316 |
return -1; |
|
317 |
||
318 |
} |
|
319 |
||
33 | 320 |
int _fs_do_op_close (struct cache_op *op_base) { |
321 |
struct cache_op_fs *op = (struct cache_op_fs *) op_base; |
|
322 |
||
323 |
// unmap |
|
324 |
if (op->mmap) |
|
325 |
if (munmap(op->mmap, op->mmap_size)) |
|
326 |
PWARNING("munmap"); |
|
327 |
||
328 |
// close |
|
329 |
if (op->fd > 0) |
|
330 |
if (close(op->fd)) |
|
331 |
PWARNING("close"); |
|
332 |
||
333 |
// XXX: delete if op->write_offset == 0? |
|
334 |
||
335 |
// free |
|
336 |
free(op); |
|
337 |
||
338 |
return 0; |
|
339 |
} |
|
31 | 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 | 351 |
ctx->base.fn_op_available = &_fs_do_op_available; |
31 | 352 |
ctx->base.fn_op_begin_write = &_fs_do_op_begin_write; |
353 |
ctx->base.fn_op_push = &_fs_do_op_push; |
|
33 | 354 |
ctx->base.fn_op_done = &_fs_do_op_done; |
355 |
ctx->base.fn_op_close = &_fs_do_op_close; |
|
31 | 356 |
|
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 | 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 |