forked from dayne/zunkfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk-db-mem.c
91 lines (70 loc) · 1.67 KB
/
chunk-db-mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "utils.h"
#include "mutex.h"
#include "chunk-db.h"
#include "zunkfs.h"
struct chunk {
unsigned char digest[CHUNK_DIGEST_LEN];
unsigned char data[CHUNK_SIZE];
struct chunk *next;
};
static struct chunk *head_chunk = NULL;
static DECLARE_MUTEX(cdb_mutex);
static int mem_read_chunk(unsigned char *chunk, const unsigned char *digest,
void *db_info)
{
struct chunk *cp;
int ret = 0;
lock(&cdb_mutex);
for (cp = head_chunk; cp; cp = cp->next) {
if (!memcmp(digest, cp->digest, CHUNK_DIGEST_LEN)) {
memcpy(chunk, cp->data, CHUNK_SIZE);
ret = CHUNK_SIZE;
break;
}
}
unlock(&cdb_mutex);
return ret;
}
static int mem_write_chunk(const unsigned char *chunk,
const unsigned char *digest, void *db_info)
{
struct chunk *cp, **cpp;
int ret = 0;
lock(&cdb_mutex);
for (cpp = &head_chunk; (cp = *cpp); cpp = &cp->next)
if (!memcmp(digest, cp->digest, CHUNK_DIGEST_LEN))
goto found;
ret = -ENOMEM;
cp = malloc(sizeof(struct chunk));
if (!cp)
goto out;
memcpy(cp->digest, digest, CHUNK_DIGEST_LEN);
memcpy(cp->data, chunk, CHUNK_SIZE);
cp->next = NULL;
*cpp = cp;
found:
ret = CHUNK_SIZE;
out:
unlock(&cdb_mutex);
return ret;
}
static struct chunk_db *mem_chunkdb_ctor(int mode, const char *spec)
{
struct chunk_db *cdb;
if (strncmp(spec, "mem:", 4) || mode != CHUNKDB_RW)
return NULL;
cdb = malloc(sizeof(struct chunk_db));
if (!cdb)
panic("Failed to allocate chunk_db.\n");
cdb->read_chunk = mem_read_chunk;
cdb->write_chunk = mem_write_chunk;
return cdb;
}
static void __attribute__((constructor)) init_chunkdb_mem(void)
{
register_chunkdb(mem_chunkdb_ctor);
}