1/* 2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23#define STANDALONE 1 24/* 25 * Database functions 26 * Copyright (C) Andrew Tridgell 1999 27 * 28 * Redistribution and use in source and binary forms are permitted 29 * provided that the above copyright notice and this paragraph are 30 * duplicated in all such forms AND provided that this software or 31 * any derived work is only used as part of the PPP daemon (pppd) 32 * and related utilities. 33 * The name of the author may not be used to endorse or promote products 34 * derived from this software without specific prior written permission. 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 36 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 37 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 38 * 39 * Note: this software is also available under the Gnu Public License 40 * version 2 or later. 41 */ 42 43typedef unsigned tdb_len; 44typedef unsigned tdb_off; 45 46#define TDB_MAGIC_FOOD "TDB file\n" 47 48/* this is stored at the front of every database */ 49struct tdb_header { 50 char magic_food[32]; /* for /etc/magic */ 51 unsigned version; /* version of the code */ 52 unsigned hash_size; /* number of hash entries */ 53}; 54 55typedef struct { 56 char *dptr; 57 size_t dsize; 58} TDB_DATA; 59 60/* this is the context structure that is returned from a db open */ 61typedef struct { 62 char *name; /* the name of the database */ 63 void *map_ptr; /* where it is currently mapped */ 64 int fd; /* open file descriptor for the database */ 65 tdb_len map_size; /* how much space has been mapped */ 66 int read_only; /* opened read-only */ 67 int *locked; /* set if we have a chain locked */ 68 int ecode; /* error code for last tdb error */ 69 struct tdb_header header; /* a cached copy of the header */ 70} TDB_CONTEXT; 71 72/* flags to tdb_store() */ 73#define TDB_REPLACE 1 74#define TDB_INSERT 2 75 76/* flags for tdb_open() */ 77#define TDB_CLEAR_IF_FIRST 1 78 79/* error codes */ 80enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, 81 TDB_ERR_OOM, TDB_ERR_EXISTS}; 82 83#if STANDALONE 84TDB_CONTEXT *tdb_open(char *name, int hash_size, int tdb_flags, 85 int open_flags, mode_t mode); 86char *tdb_error(TDB_CONTEXT *tdb); 87int tdb_writelock(TDB_CONTEXT *tdb); 88int tdb_writeunlock(TDB_CONTEXT *tdb); 89TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key); 90int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key); 91int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag); 92int tdb_close(TDB_CONTEXT *tdb); 93TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb); 94TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key); 95int tdb_traverse(TDB_CONTEXT *tdb, 96 int (*fn)(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state), 97 void *state); 98int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key); 99#endif 100