2 #include <ccan/asprintf/asprintf.h>
5 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
9 struct tdb_used_record *rec,
12 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
15 ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
16 keylen + dataroom, h);
17 if (ecode == TDB_SUCCESS) {
18 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
23 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
25 struct tdb_data key, struct tdb_data dbuf,
26 tdb_off_t old_off, tdb_len_t old_room,
32 /* Allocate a new record. */
33 new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
35 if (TDB_OFF_IS_ERR(new_off)) {
39 /* We didn't like the existing one: remove it. */
41 add_stat(tdb, frees, 1);
42 ecode = add_free_record(tdb, old_off,
43 sizeof(struct tdb_used_record)
44 + key.dsize + old_room);
45 if (ecode == TDB_SUCCESS)
46 ecode = replace_in_hash(tdb, h, new_off);
48 ecode = add_to_hash(tdb, h, new_off);
50 if (ecode != TDB_SUCCESS) {
54 new_off += sizeof(struct tdb_used_record);
55 ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
56 if (ecode != TDB_SUCCESS) {
61 ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
62 if (ecode != TDB_SUCCESS) {
66 /* FIXME: tdb_increment_seqnum(tdb); */
70 static enum TDB_ERROR update_data(struct tdb_context *tdb,
77 ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
78 if (ecode == TDB_SUCCESS && extra) {
79 /* Put a zero in; future versions may append other data. */
80 ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
85 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
86 struct tdb_data key, struct tdb_data dbuf, int flag)
90 tdb_len_t old_room = 0;
91 struct tdb_used_record rec;
94 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
95 if (TDB_OFF_IS_ERR(off)) {
99 /* Now we have lock on this hash bucket. */
100 if (flag == TDB_INSERT) {
102 ecode = TDB_ERR_EXISTS;
107 old_room = rec_data_length(&rec)
108 + rec_extra_padding(&rec);
109 if (old_room >= dbuf.dsize) {
110 /* Can modify in-place. Easy! */
111 ecode = update_rec_hdr(tdb, off,
112 key.dsize, dbuf.dsize,
114 if (ecode != TDB_SUCCESS) {
117 ecode = update_data(tdb,
120 old_room - dbuf.dsize);
121 if (ecode != TDB_SUCCESS) {
124 tdb_unlock_hashes(tdb, h.hlock_start,
125 h.hlock_range, F_WRLCK);
129 if (flag == TDB_MODIFY) {
130 /* if the record doesn't exist and we
131 are in TDB_MODIFY mode then we should fail
133 ecode = TDB_ERR_NOEXIST;
139 /* If we didn't use the old record, this implies we're growing. */
140 ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
142 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
146 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
147 struct tdb_data key, struct tdb_data dbuf)
151 struct tdb_used_record rec;
152 tdb_len_t old_room = 0, old_dlen;
153 unsigned char *newdata;
154 struct tdb_data new_dbuf;
155 enum TDB_ERROR ecode;
157 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
158 if (TDB_OFF_IS_ERR(off)) {
163 old_dlen = rec_data_length(&rec);
164 old_room = old_dlen + rec_extra_padding(&rec);
166 /* Fast path: can append in place. */
167 if (rec_extra_padding(&rec) >= dbuf.dsize) {
168 ecode = update_rec_hdr(tdb, off, key.dsize,
169 old_dlen + dbuf.dsize, &rec,
171 if (ecode != TDB_SUCCESS) {
175 off += sizeof(rec) + key.dsize + old_dlen;
176 ecode = update_data(tdb, off, dbuf,
177 rec_extra_padding(&rec));
182 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
184 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
186 " failed to allocate %zu bytes",
187 (size_t)(key.dsize + old_dlen
191 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
193 if (ecode != TDB_SUCCESS) {
194 goto out_free_newdata;
196 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
197 new_dbuf.dptr = newdata;
198 new_dbuf.dsize = old_dlen + dbuf.dsize;
204 /* If they're using tdb_append(), it implies they're growing record. */
205 ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
210 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
214 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
215 struct tdb_data *data)
218 struct tdb_used_record rec;
220 enum TDB_ERROR ecode;
222 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
223 if (TDB_OFF_IS_ERR(off)) {
228 ecode = TDB_ERR_NOEXIST;
230 data->dsize = rec_data_length(&rec);
231 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
233 if (TDB_PTR_IS_ERR(data->dptr)) {
234 ecode = TDB_PTR_ERR(data->dptr);
239 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
243 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key)
246 struct tdb_used_record rec;
249 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
250 if (TDB_OFF_IS_ERR(off)) {
253 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
255 return off ? true : false;
258 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
261 struct tdb_used_record rec;
263 enum TDB_ERROR ecode;
265 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
266 if (TDB_OFF_IS_ERR(off)) {
271 ecode = TDB_ERR_NOEXIST;
275 ecode = delete_from_hash(tdb, &h);
276 if (ecode != TDB_SUCCESS) {
280 /* Free the deleted entry. */
281 add_stat(tdb, frees, 1);
282 ecode = add_free_record(tdb, off,
283 sizeof(struct tdb_used_record)
284 + rec_key_length(&rec)
285 + rec_data_length(&rec)
286 + rec_extra_padding(&rec));
289 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
293 unsigned int tdb_get_flags(struct tdb_context *tdb)
298 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
300 if (tdb->flags & TDB_INTERNAL) {
301 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
302 "tdb_add_flag: internal db");
307 tdb->flags |= TDB_NOLOCK;
310 tdb->flags |= TDB_NOMMAP;
311 tdb_munmap(tdb->file);
314 tdb->flags |= TDB_NOSYNC;
317 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
318 "tdb_add_flag: Unknown flag %u", flag);
322 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
324 if (tdb->flags & TDB_INTERNAL) {
325 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
326 "tdb_remove_flag: internal db");
331 tdb->flags &= ~TDB_NOLOCK;
334 tdb->flags &= ~TDB_NOMMAP;
338 tdb->flags &= ~TDB_NOSYNC;
341 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
342 "tdb_remove_flag: Unknown flag %u", flag);
346 const char *tdb_errorstr(enum TDB_ERROR ecode)
348 /* Gcc warns if you miss a case in the switch, so use that. */
350 case TDB_SUCCESS: return "Success";
351 case TDB_ERR_CORRUPT: return "Corrupt database";
352 case TDB_ERR_IO: return "IO Error";
353 case TDB_ERR_LOCK: return "Locking error";
354 case TDB_ERR_OOM: return "Out of memory";
355 case TDB_ERR_EXISTS: return "Record exists";
356 case TDB_ERR_EINVAL: return "Invalid parameter";
357 case TDB_ERR_NOEXIST: return "Record does not exist";
358 case TDB_ERR_RDONLY: return "write not permitted";
360 return "Invalid error code";
363 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
364 enum TDB_ERROR ecode,
365 enum tdb_log_level level,
366 const char *fmt, ...)
371 /* tdb_open paths care about errno, so save it. */
372 int saved_errno = errno;
378 len = vasprintf(&message, fmt, ap);
382 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
383 "out of memory formatting message:");
384 tdb->logfn(tdb, level, tdb->log_private, fmt);
386 tdb->logfn(tdb, level, tdb->log_private, message);
393 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
395 enum TDB_ERROR (*parse)(TDB_DATA key,
401 struct tdb_used_record rec;
404 enum TDB_ERROR ecode;
406 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
407 if (TDB_OFF_IS_ERR(off)) {
412 ecode = TDB_ERR_NOEXIST;
414 data.dsize = rec_data_length(&rec);
415 data.dptr = (void *)tdb_access_read(tdb,
419 if (TDB_PTR_IS_ERR(data.dptr)) {
420 ecode = TDB_PTR_ERR(data.dptr);
422 ecode = parse(key, data, p);
423 tdb_access_release(tdb, data.dptr);
427 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
431 const char *tdb_name(const struct tdb_context *tdb)
436 int tdb_fd(const struct tdb_context *tdb)
438 return tdb->file->fd;