2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Andrew Tridgell 1999-2005
7 Copyright (C) Paul `Rusty' Russell 2000
8 Copyright (C) Jeremy Allison 2000-2003
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 2 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "tdb_private.h"
33 /* Returns 0 on fail. On success, return offset of record, and fills
35 static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, u32 hash,
36 struct list_struct *r)
40 /* read in the hash top */
41 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
44 /* keep looking until we find the right record */
46 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
49 if (!TDB_DEAD(r) && hash==r->full_hash && key.dsize==r->key_len) {
51 /* a very likely hit - read the key */
52 k = tdb_alloc_read(tdb, rec_ptr + sizeof(*r),
57 if (memcmp(key.dptr, k, key.dsize) == 0) {
65 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
68 /* As tdb_find, but if you succeed, keep the lock */
69 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash, int locktype,
70 struct list_struct *rec)
74 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
76 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
77 tdb_unlock(tdb, BUCKET(hash), locktype);
82 /* update an entry in place - this only works if the new data size
83 is <= the old data size and the key exists.
86 static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash, TDB_DATA dbuf)
88 struct list_struct rec;
92 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
95 /* must be long enough key, data and tailer */
96 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
97 tdb->ecode = TDB_SUCCESS; /* Not really an error */
101 if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
102 dbuf.dptr, dbuf.dsize) == -1)
105 if (dbuf.dsize != rec.data_len) {
107 rec.data_len = dbuf.dsize;
108 return tdb_rec_write(tdb, rec_ptr, &rec);
114 /* find an entry in the database given a key */
115 /* If an entry doesn't exist tdb_err will be set to
116 * TDB_ERR_NOEXIST. If a key has no data attached
117 * then the TDB_DATA will have zero length but
120 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
123 struct list_struct rec;
127 /* find which hash bucket it is in */
128 hash = tdb->hash_fn(&key);
129 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
132 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
134 ret.dsize = rec.data_len;
135 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
139 /* check if an entry in the database exists
141 note that 1 is returned if the key is found and 0 is returned if not found
142 this doesn't match the conventions in the rest of this module, but is
145 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash)
147 struct list_struct rec;
149 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
151 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
155 int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
157 u32 hash = tdb->hash_fn(&key);
158 return tdb_exists_hash(tdb, key, hash);
161 /* actually delete an entry in the database given the offset */
162 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct*rec)
164 tdb_off_t last_ptr, i;
165 struct list_struct lastrec;
167 if (tdb->read_only) return -1;
169 if (tdb_write_lock_record(tdb, rec_ptr) == -1) {
170 /* Someone traversing here: mark it as dead */
171 rec->magic = TDB_DEAD_MAGIC;
172 return tdb_rec_write(tdb, rec_ptr, rec);
174 if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
177 /* find previous record in hash chain */
178 if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
180 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
181 if (tdb_rec_read(tdb, i, &lastrec) == -1)
184 /* unlink it: next ptr is at start of record. */
186 last_ptr = TDB_HASH_TOP(rec->full_hash);
187 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
190 /* recover the space */
191 if (tdb_free(tdb, rec_ptr, rec) == -1)
196 /* delete an entry in the database given a key */
197 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash)
200 struct list_struct rec;
203 if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK, &rec)))
205 ret = tdb_do_delete(tdb, rec_ptr, &rec);
206 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
207 TDB_LOG((tdb, 0, "tdb_delete: WARNING tdb_unlock failed!\n"));
211 int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
213 u32 hash = tdb->hash_fn(&key);
214 return tdb_delete_hash(tdb, key, hash);
217 /* store an element in the database, replacing any existing element
220 return 0 on success, -1 on failure
222 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
224 struct list_struct rec;
230 if (tdb->read_only) {
231 tdb->ecode = TDB_ERR_RDONLY;
235 /* find which hash bucket it is in */
236 hash = tdb->hash_fn(&key);
237 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
240 /* check for it existing, on insert. */
241 if (flag == TDB_INSERT) {
242 if (tdb_exists_hash(tdb, key, hash)) {
243 tdb->ecode = TDB_ERR_EXISTS;
247 /* first try in-place update, on modify or replace. */
248 if (tdb_update_hash(tdb, key, hash, dbuf) == 0)
250 if (tdb->ecode == TDB_ERR_NOEXIST &&
251 flag == TDB_MODIFY) {
252 /* if the record doesn't exist and we are in TDB_MODIFY mode then
253 we should fail the store */
257 /* reset the error code potentially set by the tdb_update() */
258 tdb->ecode = TDB_SUCCESS;
260 /* delete any existing record - if it doesn't exist we don't
261 care. Doing this first reduces fragmentation, and avoids
262 coalescing with `allocated' block before it's updated. */
263 if (flag != TDB_INSERT)
264 tdb_delete_hash(tdb, key, hash);
266 /* Copy key+value *before* allocating free space in case malloc
267 fails and we are left with a dead spot in the tdb. */
269 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
270 tdb->ecode = TDB_ERR_OOM;
274 memcpy(p, key.dptr, key.dsize);
276 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
278 /* we have to allocate some space */
279 if (!(rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec)))
282 /* Read hash top into next ptr */
283 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
286 rec.key_len = key.dsize;
287 rec.data_len = dbuf.dsize;
288 rec.full_hash = hash;
289 rec.magic = TDB_MAGIC;
291 /* write out and point the top of the hash chain at it */
292 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
293 || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
294 || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
295 /* Need to tdb_unallocate() here */
300 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
308 /* Append to an entry. Create if not exist. */
309 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
315 /* find which hash bucket it is in */
316 hash = tdb->hash_fn(&key);
317 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
320 dbuf = tdb_fetch(tdb, key);
322 if (dbuf.dptr == NULL) {
323 dbuf.dptr = malloc(new_dbuf.dsize);
325 dbuf.dptr = realloc(dbuf.dptr, dbuf.dsize + new_dbuf.dsize);
328 if (dbuf.dptr == NULL) {
329 tdb->ecode = TDB_ERR_OOM;
333 memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
334 dbuf.dsize += new_dbuf.dsize;
336 ret = tdb_store(tdb, key, dbuf, 0);
339 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
340 SAFE_FREE(dbuf.dptr);
346 return the name of the current tdb file
347 useful for external logging functions
349 const char *tdb_name(struct tdb_context *tdb)
355 return the underlying file descriptor being used by tdb, or -1
356 useful for external routines that want to check the device/inode
359 int tdb_fd(struct tdb_context *tdb)
365 return the current logging function
366 useful for external tdb routines that wish to log tdb errors
368 tdb_log_func tdb_log_fn(struct tdb_context *tdb)