tdb.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 3 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, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #ifndef _SAMBA_BUILD_
  25. /* For mode_t */
  26. #include <sys/types.h>
  27. /* For sig_atomic_t. */
  28. #include <signal.h>
  29. #endif
  30. /* flags to tdb_store() */
  31. #define TDB_REPLACE 1 /* Unused */
  32. #define TDB_INSERT 2 /* Don't overwrite an existing entry */
  33. #define TDB_MODIFY 3 /* Don't create an existing entry */
  34. /* flags for tdb_open() */
  35. #define TDB_DEFAULT 0 /* just a readability place holder */
  36. #define TDB_CLEAR_IF_FIRST 1
  37. #define TDB_INTERNAL 2 /* don't store on disk */
  38. #define TDB_NOLOCK 4 /* don't do any locking */
  39. #define TDB_NOMMAP 8 /* don't use mmap */
  40. #define TDB_CONVERT 16 /* convert endian (internal use) */
  41. #define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
  42. #define TDB_NOSYNC 64 /* don't use synchronous transactions */
  43. #define TDB_SEQNUM 128 /* maintain a sequence number */
  44. #define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
  45. #define TDB_NO_NESTING 512 /* Dont allow transaction nesting */
  46. #define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
  47. /* error codes */
  48. enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
  49. TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
  50. TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY};
  51. /* debugging uses one of the following levels */
  52. enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
  53. TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
  54. typedef struct TDB_DATA {
  55. unsigned char *dptr;
  56. size_t dsize;
  57. } TDB_DATA;
  58. #ifndef PRINTF_ATTRIBUTE
  59. #if (__GNUC__ >= 3)
  60. /** Use gcc attribute to check printf fns. a1 is the 1-based index of
  61. * the parameter containing the format, and a2 the index of the first
  62. * argument. Note that some gcc 2.x versions don't handle this
  63. * properly **/
  64. #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
  65. #else
  66. #define PRINTF_ATTRIBUTE(a1, a2)
  67. #endif
  68. #endif
  69. /* this is the context structure that is returned from a db open */
  70. typedef struct tdb_context TDB_CONTEXT;
  71. typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
  72. typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
  73. typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
  74. struct tdb_logging_context {
  75. tdb_log_func log_fn;
  76. void *log_private;
  77. };
  78. struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
  79. int open_flags, mode_t mode);
  80. struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
  81. int open_flags, mode_t mode,
  82. const struct tdb_logging_context *log_ctx,
  83. tdb_hash_func hash_fn);
  84. void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
  85. int tdb_reopen(struct tdb_context *tdb);
  86. int tdb_reopen_all(int parent_longlived);
  87. void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
  88. enum TDB_ERROR tdb_error(struct tdb_context *tdb);
  89. const char *tdb_errorstr(struct tdb_context *tdb);
  90. TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
  91. int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
  92. int (*parser)(TDB_DATA key, TDB_DATA data,
  93. void *private_data),
  94. void *private_data);
  95. int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
  96. int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
  97. int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
  98. int tdb_close(struct tdb_context *tdb);
  99. TDB_DATA tdb_firstkey(struct tdb_context *tdb);
  100. TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
  101. int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
  102. int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *);
  103. int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
  104. int tdb_lockall(struct tdb_context *tdb);
  105. int tdb_lockall_nonblock(struct tdb_context *tdb);
  106. int tdb_unlockall(struct tdb_context *tdb);
  107. int tdb_lockall_read(struct tdb_context *tdb);
  108. int tdb_lockall_read_nonblock(struct tdb_context *tdb);
  109. int tdb_unlockall_read(struct tdb_context *tdb);
  110. int tdb_lockall_mark(struct tdb_context *tdb);
  111. int tdb_lockall_unmark(struct tdb_context *tdb);
  112. const char *tdb_name(struct tdb_context *tdb);
  113. int tdb_fd(struct tdb_context *tdb);
  114. tdb_log_func tdb_log_fn(struct tdb_context *tdb);
  115. void *tdb_get_logging_private(struct tdb_context *tdb);
  116. int tdb_transaction_start(struct tdb_context *tdb);
  117. int tdb_transaction_commit(struct tdb_context *tdb);
  118. int tdb_transaction_cancel(struct tdb_context *tdb);
  119. int tdb_transaction_recover(struct tdb_context *tdb);
  120. int tdb_get_seqnum(struct tdb_context *tdb);
  121. int tdb_hash_size(struct tdb_context *tdb);
  122. size_t tdb_map_size(struct tdb_context *tdb);
  123. int tdb_get_flags(struct tdb_context *tdb);
  124. void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
  125. void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
  126. void tdb_enable_seqnum(struct tdb_context *tdb);
  127. void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
  128. /* Low level locking functions: use with care */
  129. int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
  130. int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
  131. int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
  132. int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
  133. int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
  134. int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
  135. int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
  136. void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *sigptr);
  137. /* Debug functions. Not used in production. */
  138. void tdb_dump_all(struct tdb_context *tdb);
  139. int tdb_printfreelist(struct tdb_context *tdb);
  140. int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
  141. int tdb_wipe_all(struct tdb_context *tdb);
  142. int tdb_freelist_size(struct tdb_context *tdb);
  143. extern TDB_DATA tdb_null;
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif /* tdb.h */