tdb2.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef CCAN_TDB2_H
  2. #define CCAN_TDB2_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 O_* flags. */
  28. #include <sys/stat.h>
  29. /* For sig_atomic_t. */
  30. #include <signal.h>
  31. /* For uint64_t */
  32. #include <stdint.h>
  33. #endif
  34. /* flags to tdb_store() */
  35. #define TDB_REPLACE 1 /* Unused */
  36. #define TDB_INSERT 2 /* Don't overwrite an existing entry */
  37. #define TDB_MODIFY 3 /* Don't create an existing entry */
  38. /* flags for tdb_open() */
  39. #define TDB_DEFAULT 0 /* just a readability place holder */
  40. #define TDB_CLEAR_IF_FIRST 1
  41. #define TDB_INTERNAL 2 /* don't store on disk */
  42. #define TDB_NOLOCK 4 /* don't do any locking */
  43. #define TDB_NOMMAP 8 /* don't use mmap */
  44. #define TDB_CONVERT 16 /* convert endian */
  45. #define TDB_NOSYNC 64 /* don't use synchronous transactions */
  46. #define TDB_SEQNUM 128 /* maintain a sequence number */
  47. #define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
  48. #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
  49. #define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
  50. /* error codes */
  51. enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
  52. TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
  53. TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
  54. TDB_ERR_NESTING};
  55. /* debugging uses one of the following levels */
  56. enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
  57. TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
  58. typedef struct tdb_data {
  59. unsigned char *dptr;
  60. size_t dsize;
  61. } TDB_DATA;
  62. #ifndef PRINTF_ATTRIBUTE
  63. #if (__GNUC__ >= 3)
  64. /** Use gcc attribute to check printf fns. a1 is the 1-based index of
  65. * the parameter containing the format, and a2 the index of the first
  66. * argument. Note that some gcc 2.x versions don't handle this
  67. * properly **/
  68. #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
  69. #else
  70. #define PRINTF_ATTRIBUTE(a1, a2)
  71. #endif
  72. #endif
  73. struct tdb_context;
  74. /* FIXME: Make typesafe */
  75. typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *priv, const char *, ...) PRINTF_ATTRIBUTE(4, 5);
  76. typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed,
  77. void *priv);
  78. enum tdb_attribute_type {
  79. TDB_ATTRIBUTE_LOG = 0,
  80. TDB_ATTRIBUTE_HASH = 1
  81. };
  82. struct tdb_attribute_base {
  83. enum tdb_attribute_type attr;
  84. union tdb_attribute *next;
  85. };
  86. struct tdb_attribute_log {
  87. struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
  88. tdb_logfn_t log_fn;
  89. void *log_private;
  90. };
  91. struct tdb_attribute_hash {
  92. struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
  93. tdb_hashfn_t hash_fn;
  94. void *hash_private;
  95. };
  96. union tdb_attribute {
  97. struct tdb_attribute_base base;
  98. struct tdb_attribute_log log;
  99. struct tdb_attribute_hash hash;
  100. };
  101. struct tdb_context *tdb_open(const char *name, int tdb_flags,
  102. int open_flags, mode_t mode,
  103. union tdb_attribute *attributes);
  104. struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key);
  105. int tdb_delete(struct tdb_context *tdb, struct tdb_data key);
  106. int tdb_store(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf, int flag);
  107. int tdb_close(struct tdb_context *tdb);
  108. int tdb_check(struct tdb_context *tdb,
  109. int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
  110. void *private_data);
  111. enum TDB_ERROR tdb_error(struct tdb_context *tdb);
  112. extern struct tdb_data tdb_null;
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif /* tdb2.h */