~ubuntu-branches/ubuntu/wily/ruby-ferret/wily

« back to all changes in this revision

Viewing changes to ext/ram_store.c

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Terceiro
  • Date: 2011-07-28 00:02:49 UTC
  • Revision ID: james.westby@ubuntu.com-20110728000249-v0443y69ftcpxwi6
Tags: upstream-0.11.6
ImportĀ upstreamĀ versionĀ 0.11.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "store.h"
 
2
#include <string.h>
 
3
 
 
4
extern Store *store_new();
 
5
extern void store_destroy(Store *store);
 
6
extern OutStream *os_new();
 
7
extern InStream *is_new();
 
8
extern int file_is_lock(char *filename);
 
9
 
 
10
static RAMFile *rf_new(const char *name)
 
11
{
 
12
    RAMFile *rf = ALLOC(RAMFile);
 
13
    rf->buffers = ALLOC(uchar *);
 
14
    rf->buffers[0] = ALLOC_N(uchar, BUFFER_SIZE);
 
15
    rf->name = estrdup(name);
 
16
    rf->len = 0;
 
17
    rf->bufcnt = 1;
 
18
    rf->ref_cnt = 1;
 
19
    return rf;
 
20
}
 
21
 
 
22
static void rf_extend_if_necessary(RAMFile *rf, int buf_num)
 
23
{
 
24
    while (rf->bufcnt <= buf_num) {
 
25
        REALLOC_N(rf->buffers, uchar *, (rf->bufcnt + 1));
 
26
        rf->buffers[rf->bufcnt++] = ALLOC_N(uchar, BUFFER_SIZE);
 
27
    }
 
28
}
 
29
 
 
30
static void rf_close(void *p)
 
31
{
 
32
    int i;
 
33
    RAMFile *rf = (RAMFile *)p;
 
34
    if (rf->ref_cnt > 0) {
 
35
        return;
 
36
    }
 
37
    free(rf->name);
 
38
    for (i = 0; i < rf->bufcnt; i++) {
 
39
        free(rf->buffers[i]);
 
40
    }
 
41
    free(rf->buffers);
 
42
    free(rf);
 
43
}
 
44
 
 
45
static void ram_touch(Store *store, char *filename)
 
46
{
 
47
    if (h_get(store->dir.ht, filename) == NULL) {
 
48
        h_set(store->dir.ht, filename, rf_new(filename));
 
49
    }
 
50
}
 
51
 
 
52
static int ram_exists(Store *store, char *filename)
 
53
{
 
54
    if (h_get(store->dir.ht, filename) != NULL) {
 
55
        return true;
 
56
    }
 
57
    else {
 
58
        return false;
 
59
    }
 
60
}
 
61
 
 
62
static int ram_remove(Store *store, char *filename)
 
63
{
 
64
    RAMFile *rf = h_rem(store->dir.ht, filename, false);
 
65
    if (rf != NULL) {
 
66
        DEREF(rf);
 
67
        rf_close(rf);
 
68
        return true;
 
69
    }
 
70
    else {
 
71
        return false;
 
72
    }
 
73
}
 
74
 
 
75
static void ram_rename(Store *store, char *from, char *to)
 
76
{
 
77
    RAMFile *rf = (RAMFile *)h_rem(store->dir.ht, from, false);
 
78
    RAMFile *tmp;
 
79
 
 
80
    if (rf == NULL) {
 
81
        RAISE(IO_ERROR, "couldn't rename \"%s\" to \"%s\". \"%s\""
 
82
              " doesn't exist", from, to, from);
 
83
    }
 
84
 
 
85
    free(rf->name);
 
86
 
 
87
    rf->name = estrdup(to);
 
88
 
 
89
    /* clean up the file we are overwriting */
 
90
    tmp = (RAMFile *)h_get(store->dir.ht, to);
 
91
    if (tmp != NULL) {
 
92
        DEREF(tmp);
 
93
    }
 
94
 
 
95
    h_set(store->dir.ht, rf->name, rf);
 
96
}
 
97
 
 
98
static int ram_count(Store *store)
 
99
{
 
100
    return store->dir.ht->size;
 
101
}
 
102
 
 
103
static void ram_each(Store *store,
 
104
                     void (*func)(char *fname, void *arg), void *arg)
 
105
{
 
106
    HashTable *ht = store->dir.ht;
 
107
    int i;
 
108
    for (i = 0; i <= ht->mask; i++) {
 
109
        RAMFile *rf = (RAMFile *)ht->table[i].value;
 
110
        if (rf) {
 
111
            if (strncmp(rf->name, LOCK_PREFIX, strlen(LOCK_PREFIX)) == 0) {
 
112
                continue;
 
113
            }
 
114
            func(rf->name, arg);
 
115
        }
 
116
    }
 
117
}
 
118
 
 
119
static void ram_close_i(Store *store)
 
120
{
 
121
    HashTable *ht = store->dir.ht;
 
122
    int i;
 
123
    for (i = 0; i <= ht->mask; i++) {
 
124
        RAMFile *rf = (RAMFile *)ht->table[i].value;
 
125
        if (rf) {
 
126
            DEREF(rf);
 
127
        }
 
128
    }
 
129
    h_destroy(store->dir.ht);
 
130
    store_destroy(store);
 
131
}
 
132
 
 
133
/*
 
134
 * Be sure to keep the locks
 
135
 */
 
136
static void ram_clear(Store *store)
 
137
{
 
138
    int i;
 
139
    HashTable *ht = store->dir.ht;
 
140
    for (i = 0; i <= ht->mask; i++) {
 
141
        RAMFile *rf = (RAMFile *)ht->table[i].value;
 
142
        if (rf && !file_is_lock(rf->name)) {
 
143
            DEREF(rf);
 
144
            h_del(ht, rf->name);
 
145
        }
 
146
    }
 
147
}
 
148
 
 
149
static void ram_clear_locks(Store *store)
 
150
{
 
151
    int i;
 
152
    HashTable *ht = store->dir.ht;
 
153
    for (i = 0; i <= ht->mask; i++) {
 
154
        RAMFile *rf = (RAMFile *)ht->table[i].value;
 
155
        if (rf && file_is_lock(rf->name)) {
 
156
            DEREF(rf);
 
157
            h_del(ht, rf->name);
 
158
        }
 
159
    }
 
160
}
 
161
 
 
162
static void ram_clear_all(Store *store)
 
163
{
 
164
    int i;
 
165
    HashTable *ht = store->dir.ht;
 
166
    for (i = 0; i <= ht->mask; i++) {
 
167
        RAMFile *rf = (RAMFile *)ht->table[i].value;
 
168
        if (rf) {
 
169
            DEREF(rf);
 
170
            h_del(ht, rf->name);
 
171
        }
 
172
    }
 
173
}
 
174
 
 
175
static off_t ram_length(Store *store, char *filename)
 
176
{
 
177
    RAMFile *rf = (RAMFile *)h_get(store->dir.ht, filename);
 
178
    if (rf != NULL) {
 
179
        return rf->len;
 
180
    }
 
181
    else {
 
182
        return 0;
 
183
    }
 
184
}
 
185
 
 
186
off_t ramo_length(OutStream *os)
 
187
{
 
188
    return os->file.rf->len;
 
189
}
 
190
 
 
191
static void ramo_flush_i(OutStream *os, uchar *src, int len)
 
192
{
 
193
    uchar *buffer;
 
194
    RAMFile *rf = os->file.rf;
 
195
    int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
 
196
    int src_offset;
 
197
    off_t pointer = os->pointer;
 
198
 
 
199
    buffer_number = (int)(pointer / BUFFER_SIZE);
 
200
    buffer_offset = pointer % BUFFER_SIZE;
 
201
    bytes_in_buffer = BUFFER_SIZE - buffer_offset;
 
202
    bytes_to_copy = bytes_in_buffer < len ? bytes_in_buffer : len;
 
203
 
 
204
    rf_extend_if_necessary(rf, buffer_number);
 
205
 
 
206
    buffer = rf->buffers[buffer_number];
 
207
    memcpy(buffer + buffer_offset, src, bytes_to_copy);
 
208
 
 
209
    if (bytes_to_copy < len) {
 
210
        src_offset = bytes_to_copy;
 
211
        bytes_to_copy = len - bytes_to_copy;
 
212
        buffer_number += 1;
 
213
        rf_extend_if_necessary(rf, buffer_number);
 
214
        buffer = rf->buffers[buffer_number];
 
215
 
 
216
        memcpy(buffer, src + src_offset, bytes_to_copy);
 
217
    }
 
218
    os->pointer += len;
 
219
 
 
220
    if (os->pointer > rf->len) {
 
221
        rf->len = os->pointer;
 
222
    }
 
223
}
 
224
 
 
225
static void ramo_seek_i(OutStream *os, off_t pos)
 
226
{
 
227
    os->pointer = pos;
 
228
}
 
229
 
 
230
void ramo_reset(OutStream *os)
 
231
{
 
232
    os_seek(os, 0);
 
233
    os->file.rf->len = 0;
 
234
}
 
235
 
 
236
static void ramo_close_i(OutStream *os)
 
237
{
 
238
    RAMFile *rf = os->file.rf;
 
239
    DEREF(rf);
 
240
    rf_close(rf);
 
241
}
 
242
 
 
243
void ramo_write_to(OutStream *os, OutStream *other_o)
 
244
{
 
245
    int i, len;
 
246
    RAMFile *rf = os->file.rf;
 
247
    int last_buffer_number;
 
248
    int last_buffer_offset;
 
249
 
 
250
    os_flush(os);
 
251
    last_buffer_number = (int) (rf->len / BUFFER_SIZE);
 
252
    last_buffer_offset = rf->len % BUFFER_SIZE;
 
253
    for (i = 0; i <= last_buffer_number; i++) {
 
254
        len = (i == last_buffer_number ? last_buffer_offset : BUFFER_SIZE);
 
255
        os_write_bytes(other_o, rf->buffers[i], len);
 
256
    }
 
257
}
 
258
 
 
259
const struct OutStreamMethods RAM_OUT_STREAM_METHODS = {
 
260
    ramo_flush_i,
 
261
    ramo_seek_i,
 
262
    ramo_close_i
 
263
};
 
264
 
 
265
OutStream *ram_new_buffer()
 
266
{
 
267
    RAMFile *rf = rf_new("");
 
268
    OutStream *os = os_new();
 
269
 
 
270
    DEREF(rf);
 
271
    os->file.rf = rf;
 
272
    os->pointer = 0;
 
273
    os->m = &RAM_OUT_STREAM_METHODS;
 
274
    return os;
 
275
}
 
276
 
 
277
void ram_destroy_buffer(OutStream *os)
 
278
{
 
279
    rf_close(os->file.rf);
 
280
    free(os);
 
281
}
 
282
 
 
283
static OutStream *ram_new_output(Store *store, const char *filename)
 
284
{
 
285
    RAMFile *rf = (RAMFile *)h_get(store->dir.ht, filename);
 
286
    OutStream *os = os_new();
 
287
 
 
288
    if (rf == NULL) {
 
289
        rf = rf_new(filename);
 
290
        h_set(store->dir.ht, rf->name, rf);
 
291
    }
 
292
    REF(rf);
 
293
    os->pointer = 0;
 
294
    os->file.rf = rf;
 
295
    os->m = &RAM_OUT_STREAM_METHODS;
 
296
    return os;
 
297
}
 
298
 
 
299
static void rami_read_i(InStream *is, uchar *b, int len)
 
300
{
 
301
    RAMFile *rf = is->file.rf;
 
302
 
 
303
    int offset = 0;
 
304
    int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
 
305
    int remainder = len;
 
306
    off_t start = is->d.pointer;
 
307
    uchar *buffer;
 
308
 
 
309
    while (remainder > 0) {
 
310
        buffer_number = (int) (start / BUFFER_SIZE);
 
311
        buffer_offset = start % BUFFER_SIZE;
 
312
        bytes_in_buffer = BUFFER_SIZE - buffer_offset;
 
313
 
 
314
        if (bytes_in_buffer >= remainder) {
 
315
            bytes_to_copy = remainder;
 
316
        }
 
317
        else {
 
318
            bytes_to_copy = bytes_in_buffer;
 
319
        }
 
320
        buffer = rf->buffers[buffer_number];
 
321
        memcpy(b + offset, buffer + buffer_offset, bytes_to_copy);
 
322
        offset += bytes_to_copy;
 
323
        start += bytes_to_copy;
 
324
        remainder -= bytes_to_copy;
 
325
    }
 
326
 
 
327
    is->d.pointer += len;
 
328
}
 
329
 
 
330
static off_t rami_length_i(InStream *is)
 
331
{
 
332
    return is->file.rf->len;
 
333
}
 
334
 
 
335
static void rami_seek_i(InStream *is, off_t pos)
 
336
{
 
337
    is->d.pointer = pos;
 
338
}
 
339
 
 
340
static void rami_close_i(InStream *is)
 
341
{
 
342
    RAMFile *rf = is->file.rf;
 
343
    DEREF(rf);
 
344
    rf_close(rf);
 
345
}
 
346
 
 
347
static const struct InStreamMethods RAM_IN_STREAM_METHODS = {
 
348
    rami_read_i,
 
349
    rami_seek_i,
 
350
    rami_length_i,
 
351
    rami_close_i
 
352
};
 
353
 
 
354
static InStream *ram_open_input(Store *store, const char *filename)
 
355
{
 
356
    RAMFile *rf = (RAMFile *)h_get(store->dir.ht, filename);
 
357
    InStream *is = NULL;
 
358
 
 
359
    if (rf == NULL) {
 
360
        RAISE(FILE_NOT_FOUND_ERROR,
 
361
              "tried to open \"%s\" but it doesn't exist", filename);
 
362
    }
 
363
    REF(rf);
 
364
    is = is_new();
 
365
    is->file.rf = rf;
 
366
    is->d.pointer = 0;
 
367
    is->m = &RAM_IN_STREAM_METHODS;
 
368
 
 
369
    return is;
 
370
}
 
371
 
 
372
#define LOCK_OBTAIN_TIMEOUT 5
 
373
 
 
374
static int ram_lock_obtain(Lock *lock)
 
375
{
 
376
    int ret = true;
 
377
    if (ram_exists(lock->store, lock->name))
 
378
        ret = false;
 
379
    ram_touch(lock->store, lock->name);
 
380
    return ret;
 
381
}
 
382
 
 
383
static int ram_lock_is_locked(Lock *lock)
 
384
{
 
385
    return ram_exists(lock->store, lock->name);
 
386
}
 
387
 
 
388
static void ram_lock_release(Lock *lock)
 
389
{
 
390
    ram_remove(lock->store, lock->name);
 
391
}
 
392
 
 
393
static Lock *ram_open_lock_i(Store *store, char *lockname)
 
394
{
 
395
    Lock *lock = ALLOC(Lock);
 
396
    char lname[100];
 
397
    snprintf(lname, 100, "%s%s.lck", LOCK_PREFIX, lockname);
 
398
    lock->name = estrdup(lname);
 
399
    lock->store = store;
 
400
    lock->obtain = &ram_lock_obtain;
 
401
    lock->release = &ram_lock_release;
 
402
    lock->is_locked = &ram_lock_is_locked;
 
403
    return lock;
 
404
}
 
405
 
 
406
static void ram_close_lock_i(Lock *lock)
 
407
{
 
408
    free(lock->name);
 
409
    free(lock);
 
410
}
 
411
 
 
412
 
 
413
Store *open_ram_store()
 
414
{
 
415
    Store *new_store = store_new();
 
416
 
 
417
    new_store->dir.ht       = h_new_str(NULL, rf_close);
 
418
    new_store->touch        = &ram_touch;
 
419
    new_store->exists       = &ram_exists;
 
420
    new_store->remove       = &ram_remove;
 
421
    new_store->rename       = &ram_rename;
 
422
    new_store->count        = &ram_count;
 
423
    new_store->clear        = &ram_clear;
 
424
    new_store->clear_all    = &ram_clear_all;
 
425
    new_store->clear_locks  = &ram_clear_locks;
 
426
    new_store->length       = &ram_length;
 
427
    new_store->each         = &ram_each;
 
428
    new_store->new_output   = &ram_new_output;
 
429
    new_store->open_input   = &ram_open_input;
 
430
    new_store->open_lock_i  = &ram_open_lock_i;
 
431
    new_store->close_lock_i = &ram_close_lock_i;
 
432
    new_store->close_i      = &ram_close_i;
 
433
    return new_store;
 
434
}
 
435
 
 
436
struct CopyFileArg
 
437
{
 
438
    Store *to_store, *from_store;
 
439
};
 
440
 
 
441
static void copy_files(char *fname, void *arg)
 
442
{
 
443
    struct CopyFileArg *cfa = (struct CopyFileArg *)arg;
 
444
    OutStream *os = cfa->to_store->new_output(cfa->to_store, fname);
 
445
    InStream *is = cfa->from_store->open_input(cfa->from_store, fname);
 
446
    int len = (int)is_length(is);
 
447
    uchar *buffer = ALLOC_N(uchar, len + 1);
 
448
 
 
449
    is_read_bytes(is, buffer, len);
 
450
    os_write_bytes(os, buffer, len);
 
451
 
 
452
    is_close(is);
 
453
    os_close(os);
 
454
    free(buffer);
 
455
}
 
456
 
 
457
Store *open_ram_store_and_copy(Store *from_store, bool close_dir)
 
458
{
 
459
    Store *store = open_ram_store();
 
460
    struct CopyFileArg cfa;
 
461
    cfa.to_store = store;
 
462
    cfa.from_store = from_store;
 
463
 
 
464
    from_store->each(from_store, &copy_files, &cfa);
 
465
 
 
466
    if (close_dir) {
 
467
        store_deref(from_store);
 
468
    }
 
469
 
 
470
    return store;
 
471
}