forked from dayne/zunkfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk-db-sqlite.c
140 lines (112 loc) · 3.15 KB
/
chunk-db-sqlite.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sqlite3.h>
#include "zunkfs.h"
#include "chunk-db.h"
#include "utils.h"
#include "mutex.h"
/*
* CREATE TABLE chunk (
* hash CHAR(20) PRIMARY KEY UNIQUE,
* data BLOB
* );
*/
struct db_info {
sqlite3 *db;
struct mutex mutex;
};
#define lock_db(db) lock(&(db)->mutex)
#define unlock_db(db) unlock(&(db)->mutex)
static int write_chunk_sqlite(const unsigned char *chunk,
const unsigned char *digest, void *db_info_ptr)
{
static const char sql[] =
"INSERT OR IGNORE INTO chunk(hash, data) VALUES(?,?)";
struct db_info *db_info = db_info_ptr;
sqlite3_stmt *stmt;
int err;
lock_db(db_info);
err = sqlite3_prepare(db_info->db, sql, -1, &stmt, 0);
if (err != SQLITE_OK) {
ERROR("sqlite3_prepare failed: %s\n",
sqlite3_errmsg(db_info->db));
unlock_db(db_info);
return -EIO;
}
sqlite3_bind_text(stmt, 1, digest_string(digest), -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 2, chunk, CHUNK_SIZE, SQLITE_STATIC);
err = sqlite3_step(stmt);
assert(err != SQLITE_ROW);
if (sqlite3_finalize(stmt) != SQLITE_OK) {
ERROR("sqlite3_finalize failed: %s\n",
sqlite3_errmsg(db_info->db));
unlock_db(db_info);
return -EIO;
}
unlock_db(db_info);
return CHUNK_SIZE;
}
static int read_chunk_sqlite(unsigned char *chunk, const unsigned char *digest,
void *db_info_ptr)
{
static const char sql[] = "SELECT data FROM chunk WHERE hash = ?";
struct db_info *db_info = db_info_ptr;
sqlite3_stmt *stmt;
int err, ret = -EIO;
lock_db(db_info);
err = sqlite3_prepare(db_info->db, sql, -1, &stmt, 0);
if (err != SQLITE_OK) {
ERROR("sqlite3_prepare failed: %d\n",
sqlite3_errmsg(db_info->db));
unlock_db(db_info);
return -EIO;
}
TRACE("%s\n", digest_string(digest));
sqlite3_bind_text(stmt, 1, digest_string(digest), -1, SQLITE_STATIC);
err = sqlite3_step(stmt);
if (err != SQLITE_ROW) {
ERROR("sqlite3_step failed: %s\n",
sqlite3_errmsg(db_info->db));
} else if (sqlite3_column_bytes(stmt, 0) != CHUNK_SIZE) {
ERROR("sqlite3 query returned %d bytes instead of %d.\n",
sqlite3_column_bytes(stmt, 0), CHUNK_SIZE);
} else {
TRACE("sqlite3 query got chunk.\n");
memcpy(chunk, sqlite3_column_blob(stmt, 0), CHUNK_SIZE);
ret = CHUNK_SIZE;
}
sqlite3_finalize(stmt);
unlock_db(db_info);
return ret;
}
static struct chunk_db *sqlite_chunkdb_ctor(int mode, const char *spec)
{
struct chunk_db *cdb;
struct db_info *db_info;
int err;
if (strncmp(spec, "sqlite:", 7))
return NULL;
cdb = malloc(sizeof(struct chunk_db) + sizeof(struct db_info));
if (!cdb)
return ERR_PTR(ENOMEM);
db_info = cdb->db_info = (void *)(cdb + 1);
init_mutex(&db_info->mutex);
err = sqlite3_open(spec + 7, &db_info->db);
if (err != SQLITE_OK) {
fprintf(stderr, "Can't open SQLite database '%s': %s\n",
spec + 7, sqlite3_errmsg(db_info->db));
sqlite3_close(db_info->db);
free(cdb);
return ERR_PTR(EINVAL);
}
cdb->read_chunk = read_chunk_sqlite;
cdb->write_chunk = (mode == CHUNKDB_RW) ? write_chunk_sqlite : NULL;
return cdb;
}
static void __attribute__((constructor)) init_sqlite_chunkdb(void)
{
register_chunkdb(sqlite_chunkdb_ctor);
}