tdb.h 5.4 KB

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