-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdb.c
210 lines (180 loc) · 4.07 KB
/
bdb.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <db.h>
#include "bdb.h"
/* This file try to manage Berkeley DB access in the same way
* that postfix:
* - Detects file change
* - read only access
* - GET access only
*/
static int _bdb_reopen(struct bdb *bdb)
{
int err;
if (bdb->db != NULL) {
bdb->db->close(bdb->db, 0);
bdb->db = NULL;
}
if (db_create(&bdb->db, NULL, 0) != 0) {
bdb->db = NULL;
bdb->status = BDB_ERROR;
return 0;
}
err = bdb->db->open(bdb->db, NULL, bdb->file, NULL, DB_UNKNOWN, DB_RDONLY, 0);
if (err != 0) {
bdb->db->close(bdb->db, 0);
bdb->db = NULL;
switch (err) {
case ENOENT:
bdb->status = BDB_NOT_EXISTS;
break;
case DB_OLD_VERSION:
#ifdef DB_CHKSUM_FAIL
case DB_CHKSUM_FAIL:
#endif
bdb->status = BDB_FORMAT;
break;
default:
bdb->status = BDB_ERROR;
}
return 0;
}
return 1;
}
void bdb_close(struct bdb *bdb)
{
if (bdb->db != NULL) {
bdb->db->close(bdb->db, 0);
}
free(bdb->file);
free(bdb);
}
struct bdb *bdb_open(const char *filename)
{
struct bdb *bdb;
bdb = calloc(1, sizeof(*bdb));
if (bdb == NULL) {
return NULL;
}
bdb->file = strdup(filename);
if (bdb->file == NULL) {
free(bdb);
return NULL;
}
return bdb;
}
/* return: NULL: no match; freeable null terminated string: match */
char *bdb_get(struct bdb *bdb, const char *key)
{
int fd;
int status;
char *ret;
DBT db_key;
DBT db_value;
struct stat file_open;
struct stat file_name;
/* check file change */
if (stat(bdb->file, &file_name) < 0) {
/* No access */
if (bdb->db != NULL) {
bdb->db->close(bdb->db, 0);
bdb->db = NULL;
bdb->mtime = -1;
}
bdb->status = BDB_NOT_EXISTS;
return NULL;
}
if (bdb->db == NULL) {
/* open file if it is not opened */
if (_bdb_reopen(bdb) == 0) {
return NULL;
}
/* Get dict FD */
if (bdb->db->fd(bdb->db, &fd) != 0) {
bdb->status = BDB_ERROR;
return NULL;
}
/* check stat */
if (fstat(fd, &file_open) < 0) {
bdb->db->close(bdb->db, 0);
bdb->db = NULL;
bdb->status = BDB_ERROR;
return NULL;
}
bdb->mtime = file_open.st_mtime;
} else {
/* Get dict FD */
if (bdb->db->fd(bdb->db, &fd) != 0) {
bdb->status = BDB_ERROR;
return NULL;
}
/* Reopen */
if (bdb->mtime != file_name.st_mtime) {
if (_bdb_reopen(bdb) == 0) {
return NULL;
}
/* Get dict FD */
if (bdb->db->fd(bdb->db, &fd) != 0) {
bdb->status = BDB_ERROR;
return NULL;
}
}
}
/* Acquire a shared lock. */
while ((status = flock(fd, LOCK_SH)) < 0 && errno == EINTR)
usleep(100000); /* 100 000 microseconds = 100 milliseconds = 0.1 second */
/* Perform lookup according with end 0 detection */
switch (bdb->has_zero) {
case 0: /* I don't known */
case 1: /* Have zero terminated keys. */
memset(&db_key, 0, sizeof(db_key));
memset(&db_value, 0, sizeof(db_value));
db_key.data = (char *)key;
db_key.size = strlen(key) + 1;
status = bdb->db->get(bdb->db, NULL, &db_key, &db_value, 0);
if (status == 0) {
bdb->has_zero = 1;
} else if (status != DB_NOTFOUND) {
bdb->status = BDB_TEMP_ERROR;
}
if (bdb->has_zero == 1) {
break;
}
case 2: /* Doesn't Have zero terminated keys. */
memset(&db_key, 0, sizeof(db_key));
memset(&db_value, 0, sizeof(db_value));
db_key.data = (char *)key;
db_key.size = strlen(key);
status = bdb->db->get(bdb->db, NULL, &db_key, &db_value, 0);
if (status == 0) {
bdb->has_zero = 2;
} else if (status != DB_NOTFOUND) {
bdb->status = BDB_TEMP_ERROR;
}
}
/* in success case, duplicate data. If strdup fail, we return NULL
* as the lookup doesn't match. Don't care about the zero terminated
* values. strndup stops on the first encountered 0, or the max length
*/
ret = NULL;
if (status == 0) {
ret = strndup(db_value.data, db_value.size);
bdb->status = BDB_OK;
} else if (status == DB_NOTFOUND) {
bdb->status = BDB_OK;
}
/* Release lock */
flock(fd, LOCK_UN);
/* Return value */
return ret;
}
int bdb_status(struct bdb *bdb)
{
return bdb->status;
}