s3:net_util: add some const to sockaddr_storage
[Samba/gebeck_regimport.git] / lib / tdb2 / tdb.c
blob753ccb0c8b2ee68a570bbdf027606a49299f22c9
1 #include "private.h"
2 #ifndef _SAMBA_BUILD_
3 #include <ccan/asprintf/asprintf.h>
4 #include <stdarg.h>
5 #endif
7 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
8 tdb_off_t off,
9 tdb_len_t keylen,
10 tdb_len_t datalen,
11 struct tdb_used_record *rec,
12 uint64_t h)
14 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
15 enum TDB_ERROR ecode;
17 ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
18 keylen + dataroom, h);
19 if (ecode == TDB_SUCCESS) {
20 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
22 return ecode;
25 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
26 struct hash_info *h,
27 struct tdb_data key, struct tdb_data dbuf,
28 tdb_off_t old_off, tdb_len_t old_room,
29 bool growing)
31 tdb_off_t new_off;
32 enum TDB_ERROR ecode;
34 /* Allocate a new record. */
35 new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
36 growing);
37 if (TDB_OFF_IS_ERR(new_off)) {
38 return new_off;
41 /* We didn't like the existing one: remove it. */
42 if (old_off) {
43 tdb->stats.frees++;
44 ecode = add_free_record(tdb, old_off,
45 sizeof(struct tdb_used_record)
46 + key.dsize + old_room,
47 TDB_LOCK_WAIT, true);
48 if (ecode == TDB_SUCCESS)
49 ecode = replace_in_hash(tdb, h, new_off);
50 } else {
51 ecode = add_to_hash(tdb, h, new_off);
53 if (ecode != TDB_SUCCESS) {
54 return ecode;
57 new_off += sizeof(struct tdb_used_record);
58 ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
59 if (ecode != TDB_SUCCESS) {
60 return ecode;
63 new_off += key.dsize;
64 ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
65 if (ecode != TDB_SUCCESS) {
66 return ecode;
69 if (tdb->flags & TDB_SEQNUM)
70 tdb_inc_seqnum(tdb);
72 return TDB_SUCCESS;
75 static enum TDB_ERROR update_data(struct tdb_context *tdb,
76 tdb_off_t off,
77 struct tdb_data dbuf,
78 tdb_len_t extra)
80 enum TDB_ERROR ecode;
82 ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
83 if (ecode == TDB_SUCCESS && extra) {
84 /* Put a zero in; future versions may append other data. */
85 ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
87 if (tdb->flags & TDB_SEQNUM)
88 tdb_inc_seqnum(tdb);
90 return ecode;
93 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
94 struct tdb_data key, struct tdb_data dbuf, int flag)
96 struct hash_info h;
97 tdb_off_t off;
98 tdb_len_t old_room = 0;
99 struct tdb_used_record rec;
100 enum TDB_ERROR ecode;
102 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
103 if (TDB_OFF_IS_ERR(off)) {
104 return tdb->last_error = off;
107 /* Now we have lock on this hash bucket. */
108 if (flag == TDB_INSERT) {
109 if (off) {
110 ecode = TDB_ERR_EXISTS;
111 goto out;
113 } else {
114 if (off) {
115 old_room = rec_data_length(&rec)
116 + rec_extra_padding(&rec);
117 if (old_room >= dbuf.dsize) {
118 /* Can modify in-place. Easy! */
119 ecode = update_rec_hdr(tdb, off,
120 key.dsize, dbuf.dsize,
121 &rec, h.h);
122 if (ecode != TDB_SUCCESS) {
123 goto out;
125 ecode = update_data(tdb,
126 off + sizeof(rec)
127 + key.dsize, dbuf,
128 old_room - dbuf.dsize);
129 if (ecode != TDB_SUCCESS) {
130 goto out;
132 tdb_unlock_hashes(tdb, h.hlock_start,
133 h.hlock_range, F_WRLCK);
134 return tdb->last_error = TDB_SUCCESS;
136 } else {
137 if (flag == TDB_MODIFY) {
138 /* if the record doesn't exist and we
139 are in TDB_MODIFY mode then we should fail
140 the store */
141 ecode = TDB_ERR_NOEXIST;
142 goto out;
147 /* If we didn't use the old record, this implies we're growing. */
148 ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
149 out:
150 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
151 return tdb->last_error = ecode;
154 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
155 struct tdb_data key, struct tdb_data dbuf)
157 struct hash_info h;
158 tdb_off_t off;
159 struct tdb_used_record rec;
160 tdb_len_t old_room = 0, old_dlen;
161 unsigned char *newdata;
162 struct tdb_data new_dbuf;
163 enum TDB_ERROR ecode;
165 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
166 if (TDB_OFF_IS_ERR(off)) {
167 return tdb->last_error = off;
170 if (off) {
171 old_dlen = rec_data_length(&rec);
172 old_room = old_dlen + rec_extra_padding(&rec);
174 /* Fast path: can append in place. */
175 if (rec_extra_padding(&rec) >= dbuf.dsize) {
176 ecode = update_rec_hdr(tdb, off, key.dsize,
177 old_dlen + dbuf.dsize, &rec,
178 h.h);
179 if (ecode != TDB_SUCCESS) {
180 goto out;
183 off += sizeof(rec) + key.dsize + old_dlen;
184 ecode = update_data(tdb, off, dbuf,
185 rec_extra_padding(&rec));
186 goto out;
189 /* Slow path. */
190 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
191 if (!newdata) {
192 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
193 "tdb_append:"
194 " failed to allocate %zu bytes",
195 (size_t)(key.dsize + old_dlen
196 + dbuf.dsize));
197 goto out;
199 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
200 newdata, old_dlen);
201 if (ecode != TDB_SUCCESS) {
202 goto out_free_newdata;
204 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
205 new_dbuf.dptr = newdata;
206 new_dbuf.dsize = old_dlen + dbuf.dsize;
207 } else {
208 newdata = NULL;
209 new_dbuf = dbuf;
212 /* If they're using tdb_append(), it implies they're growing record. */
213 ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
215 out_free_newdata:
216 free(newdata);
217 out:
218 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
219 return tdb->last_error = ecode;
222 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
223 struct tdb_data *data)
225 tdb_off_t off;
226 struct tdb_used_record rec;
227 struct hash_info h;
228 enum TDB_ERROR ecode;
230 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
231 if (TDB_OFF_IS_ERR(off)) {
232 return tdb->last_error = off;
235 if (!off) {
236 ecode = TDB_ERR_NOEXIST;
237 } else {
238 data->dsize = rec_data_length(&rec);
239 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
240 data->dsize);
241 if (TDB_PTR_IS_ERR(data->dptr)) {
242 ecode = TDB_PTR_ERR(data->dptr);
243 } else
244 ecode = TDB_SUCCESS;
247 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
248 return tdb->last_error = ecode;
251 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key)
253 tdb_off_t off;
254 struct tdb_used_record rec;
255 struct hash_info h;
257 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
258 if (TDB_OFF_IS_ERR(off)) {
259 tdb->last_error = off;
260 return false;
262 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
264 tdb->last_error = TDB_SUCCESS;
265 return off ? true : false;
268 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
270 tdb_off_t off;
271 struct tdb_used_record rec;
272 struct hash_info h;
273 enum TDB_ERROR ecode;
275 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
276 if (TDB_OFF_IS_ERR(off)) {
277 return tdb->last_error = off;
280 if (!off) {
281 ecode = TDB_ERR_NOEXIST;
282 goto unlock;
285 ecode = delete_from_hash(tdb, &h);
286 if (ecode != TDB_SUCCESS) {
287 goto unlock;
290 /* Free the deleted entry. */
291 tdb->stats.frees++;
292 ecode = add_free_record(tdb, off,
293 sizeof(struct tdb_used_record)
294 + rec_key_length(&rec)
295 + rec_data_length(&rec)
296 + rec_extra_padding(&rec),
297 TDB_LOCK_WAIT, true);
299 if (tdb->flags & TDB_SEQNUM)
300 tdb_inc_seqnum(tdb);
302 unlock:
303 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
304 return tdb->last_error = ecode;
307 unsigned int tdb_get_flags(struct tdb_context *tdb)
309 return tdb->flags;
312 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
314 if (tdb->flags & TDB_INTERNAL) {
315 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
316 TDB_LOG_USE_ERROR,
317 "tdb_add_flag: internal db");
318 return;
320 switch (flag) {
321 case TDB_NOLOCK:
322 tdb->flags |= TDB_NOLOCK;
323 break;
324 case TDB_NOMMAP:
325 tdb->flags |= TDB_NOMMAP;
326 tdb_munmap(tdb->file);
327 break;
328 case TDB_NOSYNC:
329 tdb->flags |= TDB_NOSYNC;
330 break;
331 case TDB_SEQNUM:
332 tdb->flags |= TDB_SEQNUM;
333 break;
334 case TDB_ALLOW_NESTING:
335 tdb->flags |= TDB_ALLOW_NESTING;
336 break;
337 default:
338 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
339 TDB_LOG_USE_ERROR,
340 "tdb_add_flag: Unknown flag %u",
341 flag);
345 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
347 if (tdb->flags & TDB_INTERNAL) {
348 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
349 TDB_LOG_USE_ERROR,
350 "tdb_remove_flag: internal db");
351 return;
353 switch (flag) {
354 case TDB_NOLOCK:
355 tdb->flags &= ~TDB_NOLOCK;
356 break;
357 case TDB_NOMMAP:
358 tdb->flags &= ~TDB_NOMMAP;
359 tdb_mmap(tdb);
360 break;
361 case TDB_NOSYNC:
362 tdb->flags &= ~TDB_NOSYNC;
363 break;
364 case TDB_SEQNUM:
365 tdb->flags &= ~TDB_SEQNUM;
366 break;
367 case TDB_ALLOW_NESTING:
368 tdb->flags &= ~TDB_ALLOW_NESTING;
369 break;
370 default:
371 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
372 TDB_LOG_USE_ERROR,
373 "tdb_remove_flag: Unknown flag %u",
374 flag);
378 const char *tdb_errorstr(enum TDB_ERROR ecode)
380 /* Gcc warns if you miss a case in the switch, so use that. */
381 switch (ecode) {
382 case TDB_SUCCESS: return "Success";
383 case TDB_ERR_CORRUPT: return "Corrupt database";
384 case TDB_ERR_IO: return "IO Error";
385 case TDB_ERR_LOCK: return "Locking error";
386 case TDB_ERR_OOM: return "Out of memory";
387 case TDB_ERR_EXISTS: return "Record exists";
388 case TDB_ERR_EINVAL: return "Invalid parameter";
389 case TDB_ERR_NOEXIST: return "Record does not exist";
390 case TDB_ERR_RDONLY: return "write not permitted";
392 return "Invalid error code";
395 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
397 return tdb->last_error;
400 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
401 enum TDB_ERROR ecode,
402 enum tdb_log_level level,
403 const char *fmt, ...)
405 char *message;
406 va_list ap;
407 size_t len;
408 /* tdb_open paths care about errno, so save it. */
409 int saved_errno = errno;
411 if (!tdb->log_fn)
412 return ecode;
414 va_start(ap, fmt);
415 len = vasprintf(&message, fmt, ap);
416 va_end(ap);
418 if (len < 0) {
419 tdb->log_fn(tdb, TDB_LOG_ERROR,
420 "out of memory formatting message:", tdb->log_data);
421 tdb->log_fn(tdb, level, fmt, tdb->log_data);
422 } else {
423 tdb->log_fn(tdb, level, message, tdb->log_data);
424 free(message);
426 errno = saved_errno;
427 return ecode;
430 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
431 TDB_DATA key,
432 enum TDB_ERROR (*parse)(TDB_DATA k,
433 TDB_DATA d,
434 void *data),
435 void *data)
437 tdb_off_t off;
438 struct tdb_used_record rec;
439 struct hash_info h;
440 enum TDB_ERROR ecode;
442 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
443 if (TDB_OFF_IS_ERR(off)) {
444 return tdb->last_error = off;
447 if (!off) {
448 ecode = TDB_ERR_NOEXIST;
449 } else {
450 const void *dptr;
451 dptr = tdb_access_read(tdb, off + sizeof(rec) + key.dsize,
452 rec_data_length(&rec), false);
453 if (TDB_PTR_IS_ERR(dptr)) {
454 ecode = TDB_PTR_ERR(dptr);
455 } else {
456 TDB_DATA d = tdb_mkdata(dptr, rec_data_length(&rec));
458 ecode = parse(key, d, data);
459 tdb_access_release(tdb, dptr);
463 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
464 return tdb->last_error = ecode;
467 const char *tdb_name(const struct tdb_context *tdb)
469 return tdb->name;
472 int64_t tdb_get_seqnum(struct tdb_context *tdb)
474 tdb_off_t off = tdb_read_off(tdb, offsetof(struct tdb_header, seqnum));
475 if (TDB_OFF_IS_ERR(off))
476 tdb->last_error = off;
477 else
478 tdb->last_error = TDB_SUCCESS;
479 return off;
483 int tdb_fd(const struct tdb_context *tdb)
485 return tdb->file->fd;