source: trunk/packages/xen-common/xen-common/tools/xenstore/tdb.h @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

File size: 5.1 KB
Line 
1#ifndef __TDB_H__
2#define __TDB_H__
3
4/*
5   Unix SMB/CIFS implementation.
6
7   trivial database library
8
9   Copyright (C) Andrew Tridgell 1999-2004
10   
11     ** NOTE! The following LGPL license applies to the tdb
12     ** library. This does NOT imply that all of Samba is released
13     ** under the LGPL
14   
15   This library is free software; you can redistribute it and/or
16   modify it under the terms of the GNU Lesser General Public
17   License as published by the Free Software Foundation; either
18   version 2 of the License, or (at your option) any later version.
19
20   This library is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   Lesser General Public License for more details.
24
25   You should have received a copy of the GNU Lesser General Public
26   License along with this library; if not, write to the Free Software
27   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28*/
29
30#ifdef  __cplusplus
31extern "C" {
32#endif
33
34
35/* flags to tdb_store() */
36#define TDB_REPLACE 1
37#define TDB_INSERT 2
38#define TDB_MODIFY 3
39
40/* flags for tdb_open() */
41#define TDB_DEFAULT 0 /* just a readability place holder */
42#define TDB_CLEAR_IF_FIRST 1
43#define TDB_INTERNAL 2 /* don't store on disk */
44#define TDB_NOLOCK   4 /* don't do any locking */
45#define TDB_NOMMAP   8 /* don't use mmap */
46#define TDB_CONVERT 16 /* convert endian (internal use) */
47#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
48
49#define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
50
51/* error codes */
52enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, 
53                TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
54                TDB_ERR_NOEXIST};
55
56#ifndef uint32_t
57#define uint32_t unsigned
58#endif
59
60typedef struct TDB_DATA {
61        char *dptr;
62        size_t dsize;
63} TDB_DATA;
64
65typedef uint32_t tdb_len;
66typedef uint32_t tdb_off;
67
68/* this is stored at the front of every database */
69struct tdb_header {
70        char magic_food[32]; /* for /etc/magic */
71        uint32_t version; /* version of the code */
72        uint32_t hash_size; /* number of hash entries */
73        tdb_off rwlocks;
74        tdb_off reserved[31];
75};
76
77struct tdb_lock_type {
78        uint32_t count;
79        uint32_t ltype;
80};
81
82struct tdb_traverse_lock {
83        struct tdb_traverse_lock *next;
84        uint32_t off;
85        uint32_t hash;
86};
87
88#ifndef PRINTF_ATTRIBUTE
89#define PRINTF_ATTRIBUTE(a,b)
90#endif
91
92/* this is the context structure that is returned from a db open */
93typedef struct tdb_context {
94        char *name; /* the name of the database */
95        void *map_ptr; /* where it is currently mapped */
96        int fd; /* open file descriptor for the database */
97        tdb_len map_size; /* how much space has been mapped */
98        int read_only; /* opened read-only */
99        struct tdb_lock_type *locked; /* array of chain locks */
100        enum TDB_ERROR ecode; /* error code for last tdb error */
101        struct tdb_header header; /* a cached copy of the header */
102        uint32_t flags; /* the flags passed to tdb_open */
103        struct tdb_traverse_lock travlocks; /* current traversal locks */
104        struct tdb_context *next; /* all tdbs to avoid multiple opens */
105        dev_t device;   /* uniquely identifies this tdb */
106        ino_t inode;    /* uniquely identifies this tdb */
107        void (*log_fn)(struct tdb_context *tdb, int level, const char *, ...) PRINTF_ATTRIBUTE(3,4); /* logging function */
108        uint32_t (*hash_fn)(TDB_DATA *key);
109        int open_flags; /* flags used in the open - needed by reopen */
110} TDB_CONTEXT;
111
112typedef int (*tdb_traverse_func)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *);
113typedef void (*tdb_log_func)(TDB_CONTEXT *, int , const char *, ...);
114typedef uint32_t (*tdb_hash_func)(TDB_DATA *key);
115
116TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
117                      int open_flags, mode_t mode);
118TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
119                         int open_flags, mode_t mode,
120                         tdb_log_func log_fn,
121                         tdb_hash_func hash_fn);
122
123int tdb_reopen(TDB_CONTEXT *tdb);
124int tdb_reopen_all(void);
125void tdb_logging_function(TDB_CONTEXT *tdb, tdb_log_func);
126enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb);
127const char *tdb_errorstr(TDB_CONTEXT *tdb);
128TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key);
129int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key);
130int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
131int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf);
132int tdb_close(TDB_CONTEXT *tdb);
133TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb);
134TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key);
135int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *);
136int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key);
137int tdb_lockall(TDB_CONTEXT *tdb);
138void tdb_unlockall(TDB_CONTEXT *tdb);
139
140/* Low level locking functions: use with care */
141int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key);
142int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key);
143int tdb_chainlock_read(TDB_CONTEXT *tdb, TDB_DATA key);
144int tdb_chainunlock_read(TDB_CONTEXT *tdb, TDB_DATA key);
145TDB_CONTEXT *tdb_copy(TDB_CONTEXT *tdb, const char *outfile);
146
147/* Debug functions. Not used in production. */
148void tdb_dump_all(TDB_CONTEXT *tdb);
149int tdb_printfreelist(TDB_CONTEXT *tdb);
150
151extern TDB_DATA tdb_null;
152
153#ifdef  __cplusplus
154}
155#endif
156
157#endif /* tdb.h */
Note: See TracBrowser for help on using the repository browser.