2 Unix SMB/Netbios implementation.
4 Samba database functions
5 Copyright (C) Andrew Tridgell 1999
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #define TDB_VERSION (0x26011967 + 1)
38 #define TDB_MAGIC (0x26011999U)
39 #define TDB_FREE_MAGIC (~TDB_MAGIC)
41 #define MIN_REC_SIZE (2*sizeof(struct list_struct) + TDB_ALIGN)
42 #define DEFAULT_HASH_SIZE 128
43 #define TDB_PAGE_SIZE 0x2000
44 #define TDB_LEN_MULTIPLIER 10
45 #define FREELIST_TOP (sizeof(struct tdb_header))
53 #define LIST_LOCK_BASE 1024
55 #define BUCKET(hash) ((hash) % tdb->header.hash_size)
57 /* the body of the database is made of one list_struct for the free space
58 plus a separate data list for each hash value */
60 tdb_len rec_len; /* total byte length of record */
61 tdb_off next; /* offset of the next record in the list */
62 tdb_len key_len; /* byte length of key */
63 tdb_len data_len; /* byte length of data */
64 unsigned full_hash; /* the full 32 bit hash of the key */
65 unsigned magic; /* try to catch errors */
67 the following union is implied
78 /* a null data record - useful for error returns */
79 static TDB_DATA null_data;
81 /* a byte range locking function - return 0 on success
82 this functions locks/unlocks 1 byte at the specified offset */
83 static int tdb_brlock(TDB_CONTEXT *tdb, tdb_off offset,
84 int set, int rw_type, int lck_type)
91 if (tdb->read_only) return -1;
93 fl.l_type = set==LOCK_SET?rw_type:F_UNLCK;
94 fl.l_whence = SEEK_SET;
99 if (fcntl(tdb->fd, lck_type, &fl) != 0) {
101 if (lck_type == F_SETLKW) {
102 printf("lock %d failed at %d (%s)\n",
103 set, offset, strerror(errno));
106 tdb->ecode = TDB_ERR_LOCK;
113 /* lock a list in the database. list -1 is the alloc list */
114 static int tdb_lock(TDB_CONTEXT *tdb, int list)
116 if (list < -1 || list >= (int)tdb->header.hash_size) {
118 printf("bad list %d\n", list);
122 if (tdb->locked[list+1] == 0) {
123 if (tdb_brlock(tdb, LIST_LOCK_BASE + 4*list, LOCK_SET,
124 F_WRLCK, F_SETLKW) != 0) {
128 tdb->locked[list+1]++;
132 /* unlock the database. */
133 static int tdb_unlock(TDB_CONTEXT *tdb, int list)
135 if (list < -1 || list >= (int)tdb->header.hash_size) {
137 printf("bad unlock list %d\n", list);
142 if (tdb->locked[list+1] == 0) {
144 printf("not locked %d\n", list);
146 tdb->ecode = TDB_ERR_LOCK;
149 if (tdb->locked[list+1] == 1) {
150 if (tdb_brlock(tdb, LIST_LOCK_BASE + 4*list, LOCK_CLEAR,
151 F_WRLCK, F_SETLKW) != 0) {
155 tdb->locked[list+1]--;
159 /* the hash algorithm - turn a key into an integer
160 This is based on the hash agorithm from gdbm */
161 static unsigned tdb_hash(TDB_DATA *key)
163 unsigned value; /* Used to compute the hash value. */
164 unsigned i; /* Used to cycle through random values. */
166 /* Set the initial value from the key size. */
167 value = 0x238F13AF * key->dsize;
168 for (i=0; i < key->dsize; i++) {
169 value = (value + (key->dptr[i] << (i*5 % 24)));
172 value = (1103515243 * value + 12345);
177 /* find the top of the hash chain for an open database */
178 static tdb_off tdb_hash_top(TDB_CONTEXT *tdb, unsigned hash)
182 ret = FREELIST_TOP + (hash+1)*sizeof(tdb_off);
187 /* check for an out of bounds access - if it is out of bounds then
188 see if the database has been expanded by someone else and expand
190 static int tdb_oob(TDB_CONTEXT *tdb, tdb_off offset)
193 if (offset <= tdb->map_size) return 0;
196 if (st.st_size <= (ssize_t)offset) {
197 tdb->ecode = TDB_ERR_IO;
203 munmap(tdb->map_ptr, tdb->map_size);
208 tdb->map_size = st.st_size;
210 tdb->map_ptr = (void *)mmap(NULL, tdb->map_size,
211 tdb->read_only?PROT_READ:PROT_READ|PROT_WRITE,
212 MAP_SHARED | MAP_FILE, tdb->fd, 0);
218 /* write a lump of data at a specified offset */
219 static int tdb_write(TDB_CONTEXT *tdb, tdb_off offset, char *buf, tdb_len len)
221 if (tdb_oob(tdb, offset + len) != 0) {
222 /* oops - trying to write beyond the end of the database! */
227 memcpy(offset + (char *)tdb->map_ptr, buf, len);
229 if (lseek(tdb->fd, offset, SEEK_SET) != offset ||
230 write(tdb->fd, buf, len) != (ssize_t)len) {
231 tdb->ecode = TDB_ERR_IO;
238 /* read a lump of data at a specified offset */
239 static int tdb_read(TDB_CONTEXT *tdb, tdb_off offset, char *buf, tdb_len len)
241 if (tdb_oob(tdb, offset + len) != 0) {
242 /* oops - trying to read beyond the end of the database! */
247 memcpy(buf, offset + (char *)tdb->map_ptr, len);
249 if (lseek(tdb->fd, offset, SEEK_SET) != offset ||
250 read(tdb->fd, buf, len) != (ssize_t)len) {
251 tdb->ecode = TDB_ERR_IO;
259 /* read a lump of data, allocating the space for it */
260 static char *tdb_alloc_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_len len)
264 buf = (char *)malloc(len);
267 tdb->ecode = TDB_ERR_OOM;
271 if (tdb_read(tdb, offset, buf, len) == -1) {
279 /* convenience routine for writing a record */
280 static int rec_write(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
282 return tdb_write(tdb, offset, (char *)rec, sizeof(*rec));
285 /* convenience routine for writing a tdb_off */
286 static int ofs_write(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
288 return tdb_write(tdb, offset, (char *)d, sizeof(*d));
291 /* read a tdb_off from the store */
292 static int ofs_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
294 return tdb_read(tdb, offset, (char *)d, sizeof(*d));
297 /* read a record and check for simple errors */
298 static int rec_read(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
300 if (tdb_read(tdb, offset, (char *)rec, sizeof(*rec)) == -1) return -1;
301 if (rec->magic != TDB_MAGIC) {
303 printf("bad magic 0x%08x at offset %d\n",
306 tdb->ecode = TDB_ERR_CORRUPT;
309 if (tdb_oob(tdb, rec->next) != 0) {
315 /* expand the database at least length bytes by expanding the
316 underlying file and doing the mmap again if necessary */
317 static int tdb_expand(TDB_CONTEXT *tdb, tdb_off length)
319 struct list_struct rec;
325 /* make sure we know about any previous expansions by another
327 tdb_oob(tdb,tdb->map_size + 1);
329 /* always make room for at least 10 more records */
330 length *= TDB_LEN_MULTIPLIER;
332 /* and round the database up to a multiple of TDB_PAGE_SIZE */
333 length = ((tdb->map_size + length + TDB_PAGE_SIZE) & ~(TDB_PAGE_SIZE - 1)) - tdb->map_size;
335 /* expand the file itself */
336 lseek(tdb->fd, tdb->map_size + length - 1, SEEK_SET);
337 if (write(tdb->fd, &b, 1) != 1) goto fail;
339 /* form a new freelist record */
340 offset = FREELIST_TOP;
341 rec.rec_len = length - sizeof(rec);
342 rec.magic = TDB_FREE_MAGIC;
343 if (ofs_read(tdb, offset, &rec.next) == -1) {
349 munmap(tdb->map_ptr, tdb->map_size);
354 tdb->map_size += length;
357 if (rec_write(tdb, tdb->map_size - length, &rec) == -1) {
361 /* link it into the free list */
362 ptr = tdb->map_size - length;
363 if (ofs_write(tdb, offset, &ptr) == -1) goto fail;
366 tdb->map_ptr = (void *)mmap(NULL, tdb->map_size,
367 PROT_READ|PROT_WRITE,
368 MAP_SHARED | MAP_FILE, tdb->fd, 0);
378 /* allocate some space from the free list. The offset returned points
379 to a unconnected list_struct within the database with room for at
380 least length bytes of total data
382 0 is returned if the space could not be allocated
384 static tdb_off tdb_allocate(TDB_CONTEXT *tdb, tdb_len length)
386 tdb_off offset, rec_ptr, last_ptr;
387 struct list_struct rec, lastrec, newrec;
393 offset = FREELIST_TOP;
395 /* read in the freelist top */
396 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
400 /* keep looking until we find a freelist record that is big
403 if (tdb_read(tdb, rec_ptr, (char *)&rec, sizeof(rec)) == -1) {
407 if (rec.magic != TDB_FREE_MAGIC) {
409 printf("bad magic 0x%08x in free list\n", rec.magic);
414 if (rec.rec_len >= length) {
415 /* found it - now possibly split it up */
416 if (rec.rec_len > length + MIN_REC_SIZE) {
417 length = (length + TDB_ALIGN) & ~(TDB_ALIGN-1);
419 newrec.rec_len = rec.rec_len - (sizeof(rec) + length);
420 newrec.next = rec.next;
421 newrec.magic = TDB_FREE_MAGIC;
423 rec.rec_len = length;
424 rec.next = rec_ptr + sizeof(rec) + rec.rec_len;
426 if (rec_write(tdb, rec.next, &newrec) == -1) {
430 if (rec_write(tdb, rec_ptr, &rec) == -1) {
435 /* remove it from the list */
437 offset = FREELIST_TOP;
439 if (ofs_write(tdb, offset, &rec.next) == -1) {
443 lastrec.next = rec.next;
444 if (rec_write(tdb, last_ptr, &lastrec) == -1) {
449 /* all done - return the new record offset */
454 /* move to the next record */
460 /* we didn't find enough space. See if we can expand the
461 database and if we can then try again */
462 if (tdb_expand(tdb, length + sizeof(rec)) == 0) goto again;
466 printf("tdb_allocate failed for size %u\n", length);
472 /* initialise a new database with a specified hash size */
473 static int tdb_new_database(TDB_CONTEXT *tdb, int hash_size)
475 struct tdb_header header;
480 /* create the header */
481 memset(&header, 0, sizeof(header));
482 memcpy(header.magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
483 header.version = TDB_VERSION;
484 header.hash_size = hash_size;
485 lseek(tdb->fd, 0, SEEK_SET);
486 ftruncate(tdb->fd, 0);
488 if (write(tdb->fd, &header, sizeof(header)) != sizeof(header)) {
489 tdb->ecode = TDB_ERR_IO;
493 /* the freelist and hash pointers */
495 memset(buf, 0, sizeof(buf));
496 for (i=0;(hash_size+1)-i >= 16; i += 16) {
497 if (write(tdb->fd, buf, sizeof(buf)) != sizeof(buf)) {
498 tdb->ecode = TDB_ERR_IO;
502 for (;i<hash_size+1; i++) {
503 if (write(tdb->fd, buf, sizeof(tdb_off)) != sizeof(tdb_off)) {
504 tdb->ecode = TDB_ERR_IO;
510 printf("initialised database of hash_size %u\n",
516 /* Returns 0 on fail. On success, return offset of record, and fills
518 static tdb_off tdb_find(TDB_CONTEXT *tdb, TDB_DATA key, unsigned int hash,
519 struct list_struct *rec)
521 tdb_off offset, rec_ptr;
523 /* find the top of the hash chain */
524 offset = tdb_hash_top(tdb, hash);
526 /* read in the hash top */
527 if (ofs_read(tdb, offset, &rec_ptr) == -1)
530 /* keep looking until we find the right record */
532 if (rec_read(tdb, rec_ptr, rec) == -1)
535 if (hash == rec->full_hash && key.dsize == rec->key_len) {
537 /* a very likely hit - read the key */
538 k = tdb_alloc_read(tdb, rec_ptr + sizeof(*rec),
544 if (memcmp(key.dptr, k, key.dsize) == 0) {
551 /* move to the next record */
558 return an error string for the last tdb error
560 char *tdb_error(TDB_CONTEXT *tdb)
564 enum TDB_ERROR ecode;
567 {TDB_SUCCESS, "Success"},
568 {TDB_ERR_CORRUPT, "Corrupt database"},
569 {TDB_ERR_IO, "IO Error"},
570 {TDB_ERR_LOCK, "Locking error"},
571 {TDB_ERR_OOM, "Out of memory"},
572 {TDB_ERR_EXISTS, "Record exists"},
574 for (i=0;emap[i].estring;i++) {
575 if (tdb->ecode == emap[i].ecode) return emap[i].estring;
577 return "Invalid error code";
581 /* update an entry in place - this only works if the new data size
582 is <= the old data size and the key exists.
585 int tdb_update(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf)
588 struct list_struct rec;
592 /* find which hash bucket it is in */
593 hash = tdb_hash(&key);
595 tdb_lock(tdb, BUCKET(hash));
596 rec_ptr = tdb_find(tdb, key, hash, &rec);
601 /* must be long enough */
602 if (rec.rec_len < key.dsize + dbuf.dsize)
605 if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
606 dbuf.dptr, dbuf.dsize) == -1)
609 if (dbuf.dsize != rec.data_len) {
611 rec.data_len = dbuf.dsize;
612 ret = rec_write(tdb, rec_ptr, &rec);
617 tdb_unlock(tdb, BUCKET(hash));
621 /* find an entry in the database given a key */
622 TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key)
626 struct list_struct rec;
627 TDB_DATA ret = null_data;
629 /* find which hash bucket it is in */
630 hash = tdb_hash(&key);
632 tdb_lock(tdb, BUCKET(hash));
633 rec_ptr = tdb_find(tdb, key, hash, &rec);
636 ret.dptr = tdb_alloc_read(tdb,
637 rec_ptr + sizeof(rec) + rec.key_len,
639 ret.dsize = rec.data_len;
642 tdb_unlock(tdb, BUCKET(hash));
646 /* check if an entry in the database exists
648 note that 1 is returned if the key is found and 0 is returned if not found
649 this doesn't match the conventions in the rest of this module, but is
652 int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key)
656 struct list_struct rec;
658 /* find which hash bucket it is in */
659 hash = tdb_hash(&key);
661 tdb_lock(tdb, BUCKET(hash));
662 rec_ptr = tdb_find(tdb, key, hash, &rec);
663 tdb_unlock(tdb, BUCKET(hash));
668 /* traverse the entire database - calling fn(tdb, key, data) on each element.
669 return -1 on error or the record count traversed
670 if fn is NULL then it is not called
671 a non-zero return value from fn() indicates that the traversal should stop
673 int tdb_traverse(TDB_CONTEXT *tdb, int (*fn)(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf))
677 tdb_off offset, rec_ptr;
678 struct list_struct rec;
682 /* loop over all hash chains */
683 for (h = 0; h < tdb->header.hash_size; h++) {
684 tdb_lock(tdb, BUCKET(h));
686 /* read in the hash top */
687 offset = tdb_hash_top(tdb, h);
688 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
692 /* traverse all records for this hash */
694 if (rec_read(tdb, rec_ptr, &rec) == -1) {
698 /* now read the full record */
699 data = tdb_alloc_read(tdb, rec_ptr + sizeof(rec),
700 rec.key_len + rec.data_len);
706 key.dsize = rec.key_len;
707 dbuf.dptr = data + rec.key_len;
708 dbuf.dsize = rec.data_len;
711 if (fn && fn(tdb, key, dbuf) != 0) {
712 /* they want us to stop traversing */
714 tdb_unlock(tdb, BUCKET(h));
721 /* move to the next record */
724 tdb_unlock(tdb, BUCKET(h));
727 /* return the number traversed */
731 tdb_unlock(tdb, BUCKET(h));
736 /* find the first entry in the database and return its key */
737 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb)
739 tdb_off offset, rec_ptr;
740 struct list_struct rec;
744 /* look for a non-empty hash chain */
745 for (hash = 0, rec_ptr = 0;
746 hash < tdb->header.hash_size;
748 /* find the top of the hash chain */
749 offset = tdb_hash_top(tdb, hash);
751 tdb_lock(tdb, BUCKET(hash));
753 /* read in the hash top */
754 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
760 tdb_unlock(tdb, BUCKET(hash));
763 if (rec_ptr == 0) return null_data;
765 /* we've found a non-empty chain, now read the record */
766 if (rec_read(tdb, rec_ptr, &rec) == -1) {
770 /* allocate and read the key space */
771 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec), rec.key_len);
772 ret.dsize = rec.key_len;
773 tdb_unlock(tdb, BUCKET(hash));
777 tdb_unlock(tdb, BUCKET(hash));
781 /* find the next entry in the database, returning its key */
782 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key)
784 unsigned hash, hbucket;
785 tdb_off rec_ptr, offset;
786 struct list_struct rec;
789 /* find which hash bucket it is in */
790 hash = tdb_hash(&key);
791 hbucket = BUCKET(hash);
793 tdb_lock(tdb, hbucket);
794 rec_ptr = tdb_find(tdb, key, hash, &rec);
796 /* we want the next record after this one */
800 /* not found or last in hash: look for next non-empty hash chain */
801 while (rec_ptr == 0) {
802 tdb_unlock(tdb, hbucket);
804 if (++hbucket >= tdb->header.hash_size - 1)
807 offset = tdb_hash_top(tdb, hbucket);
808 tdb_lock(tdb, hbucket);
809 /* read in the hash top */
810 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
811 tdb_unlock(tdb, hbucket);
816 /* Read the record. */
817 if (rec_read(tdb, rec_ptr, &rec) == 0) {
818 tdb_unlock(tdb, hbucket);
821 /* allocate and read the key */
822 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec), rec.key_len);
823 ret.dsize = rec.key_len;
824 tdb_unlock(tdb, hbucket);
829 /* delete an entry in the database given a key */
830 int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key)
833 tdb_off offset, rec_ptr, last_ptr;
834 struct list_struct rec, lastrec;
837 /* find which hash bucket it is in */
838 hash = tdb_hash(&key);
840 tdb_lock(tdb, BUCKET(hash));
842 /* find the top of the hash chain */
843 offset = tdb_hash_top(tdb, hash);
845 /* read in the hash top */
846 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
852 /* keep looking until we find the right record */
854 if (rec_read(tdb, rec_ptr, &rec) == -1) {
858 if (hash == rec.full_hash && key.dsize == rec.key_len) {
859 /* a very likely hit - read the record and full key */
860 data = tdb_alloc_read(tdb, rec_ptr + sizeof(rec),
866 if (memcmp(key.dptr, data, key.dsize) == 0) {
867 /* a definite match - delete it */
869 offset = tdb_hash_top(tdb, hash);
870 if (ofs_write(tdb, offset, &rec.next) == -1) {
874 lastrec.next = rec.next;
875 if (rec_write(tdb, last_ptr, &lastrec) == -1) {
879 tdb_unlock(tdb, BUCKET(hash));
881 /* and recover the space */
882 offset = FREELIST_TOP;
883 if (ofs_read(tdb, offset, &rec.next) == -1) {
886 rec.magic = TDB_FREE_MAGIC;
887 if (rec_write(tdb, rec_ptr, &rec) == -1) {
890 if (ofs_write(tdb, offset, &rec_ptr) == -1) {
894 /* yipee - all done */
905 /* move to the next record */
912 if (data) free(data);
913 tdb_unlock(tdb, BUCKET(hash));
917 if (data) free(data);
923 /* store an element in the database, replacing any existing element
926 return 0 on success, -1 on failure
928 int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
930 struct list_struct rec;
932 tdb_off rec_ptr, offset;
935 /* find which hash bucket it is in */
936 hash = tdb_hash(&key);
938 /* check for it existing */
939 if (flag == TDB_INSERT && tdb_exists(tdb, key)) {
940 tdb->ecode = TDB_ERR_EXISTS;
944 /* first try in-place update */
945 if (flag != TDB_INSERT && tdb_update(tdb, key, dbuf) == 0) {
949 rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize);
954 tdb_lock(tdb, BUCKET(hash));
956 /* delete any existing record - if it doesn't exist we don't care */
957 if (flag != TDB_INSERT) {
958 tdb_delete(tdb, key);
961 /* read the newly created record */
962 if (tdb_read(tdb, rec_ptr, (char *)&rec, sizeof(rec)) == -1) {
966 if (rec.magic != TDB_FREE_MAGIC) goto fail;
968 /* find the top of the hash chain */
969 offset = tdb_hash_top(tdb, hash);
971 /* read in the hash top diretcly into our next pointer */
972 if (ofs_read(tdb, offset, &rec.next) == -1) {
976 rec.key_len = key.dsize;
977 rec.data_len = dbuf.dsize;
978 rec.full_hash = hash;
979 rec.magic = TDB_MAGIC;
981 p = (char *)malloc(sizeof(rec) + key.dsize + dbuf.dsize);
983 tdb->ecode = TDB_ERR_OOM;
987 memcpy(p, &rec, sizeof(rec));
988 memcpy(p+sizeof(rec), key.dptr, key.dsize);
989 memcpy(p+sizeof(rec)+key.dsize, dbuf.dptr, dbuf.dsize);
991 if (tdb_write(tdb, rec_ptr, p, sizeof(rec)+key.dsize+dbuf.dsize) == -1)
997 /* and point the top of the hash chain at it */
998 if (ofs_write(tdb, offset, &rec_ptr) == -1) goto fail;
1000 tdb_unlock(tdb, BUCKET(hash));
1005 printf("store failed for hash 0x%08x in bucket %u\n", hash, BUCKET(hash));
1008 tdb_unlock(tdb, BUCKET(hash));
1013 /* open the database, creating it if necessary
1015 The open_flags and mode are passed straight to the open call on the database
1016 file. A flags value of O_WRONLY is invalid
1018 The hash size is advisory, use zero for a default value.
1020 return is NULL on error
1022 TDB_CONTEXT *tdb_open(char *name, int hash_size, int tdb_flags,
1023 int open_flags, mode_t mode)
1025 TDB_CONTEXT tdb, *ret;
1032 if ((open_flags & O_ACCMODE) == O_WRONLY) {
1036 if (hash_size == 0) hash_size = DEFAULT_HASH_SIZE;
1038 memset(&tdb, 0, sizeof(tdb));
1040 tdb.read_only = ((open_flags & O_ACCMODE) == O_RDONLY);
1042 tdb.fd = open(name, open_flags, mode);
1047 /* ensure there is only one process initialising at once */
1048 tdb_brlock(&tdb, GLOBAL_LOCK, LOCK_SET, F_WRLCK, F_SETLKW);
1050 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
1051 /* we need to zero the database if we are the only
1053 if (tdb_brlock(&tdb, ACTIVE_LOCK, LOCK_SET, F_WRLCK, F_SETLK) == 0) {
1054 ftruncate(tdb.fd, 0);
1055 tdb_brlock(&tdb, ACTIVE_LOCK, LOCK_CLEAR, F_WRLCK, F_SETLK);
1059 /* leave this lock in place */
1060 tdb_brlock(&tdb, ACTIVE_LOCK, LOCK_SET, F_RDLCK, F_SETLKW);
1062 if (read(tdb.fd, &tdb.header, sizeof(tdb.header)) != sizeof(tdb.header) ||
1063 strcmp(tdb.header.magic_food, TDB_MAGIC_FOOD) != 0 ||
1064 tdb.header.version != TDB_VERSION) {
1065 /* its not a valid database - possibly initialise it */
1066 if (!(open_flags & O_CREAT)) {
1069 if (tdb_new_database(&tdb, hash_size) == -1) goto fail;
1071 lseek(tdb.fd, 0, SEEK_SET);
1072 if (read(tdb.fd, &tdb.header, sizeof(tdb.header)) != sizeof(tdb.header)) goto fail;
1077 /* map the database and fill in the return structure */
1078 tdb.name = (char *)strdup(name);
1079 tdb.locked = (int *)calloc(tdb.header.hash_size+1,
1080 sizeof(tdb.locked[0]));
1084 tdb.map_size = st.st_size;
1086 tdb.map_ptr = (void *)mmap(NULL, st.st_size,
1087 tdb.read_only? PROT_READ : PROT_READ|PROT_WRITE,
1088 MAP_SHARED | MAP_FILE, tdb.fd, 0);
1091 ret = (TDB_CONTEXT *)malloc(sizeof(tdb));
1092 if (!ret) goto fail;
1097 printf("mapped database of hash_size %u map_size=%u\n",
1098 hash_size, tdb.map_size);
1101 tdb_brlock(&tdb, GLOBAL_LOCK, LOCK_CLEAR, F_WRLCK, F_SETLKW);
1105 if (tdb.name) free(tdb.name);
1106 if (tdb.fd != -1) close(tdb.fd);
1107 if (tdb.map_ptr) munmap(tdb.map_ptr, tdb.map_size);
1112 /* close a database */
1113 int tdb_close(TDB_CONTEXT *tdb)
1115 if (!tdb) return -1;
1117 if (tdb->name) free(tdb->name);
1118 if (tdb->fd != -1) close(tdb->fd);
1119 if (tdb->map_ptr) munmap(tdb->map_ptr, tdb->map_size);
1120 if (tdb->locked) free(tdb->locked);
1122 memset(tdb, 0, sizeof(*tdb));
1128 /* lock the database. If we already have it locked then don't do anything */
1129 int tdb_writelock(TDB_CONTEXT *tdb)
1131 return tdb_lock(tdb, -1);
1134 /* unlock the database. */
1135 int tdb_writeunlock(TDB_CONTEXT *tdb)
1137 return tdb_unlock(tdb, -1);
1140 /* lock one hash chain. This is meant to be used to reduce locking
1141 contention - it cannot guarantee how many records will be locked */
1142 int tdb_lockchain(TDB_CONTEXT *tdb, TDB_DATA key)
1144 return tdb_lock(tdb, BUCKET(tdb_hash(&key)));
1148 /* unlock one hash chain */
1149 int tdb_unlockchain(TDB_CONTEXT *tdb, TDB_DATA key)
1151 return tdb_unlock(tdb, BUCKET(tdb_hash(&key)));