util: util_ntdb ntdb_fetch_int32/ntdb_store_int32 and ntdb_add_int32_atomic
[ddiss/samba.git] / lib / util / util_ntdb.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    tdb utility functions
5
6    Copyright (C) Rusty Russell 2012
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef _____LIB_UTIL_UTIL_NTDB_H__
23 #define _____LIB_UTIL_UTIL_NTDB_H__
24 #include <ntdb.h>
25 #include <talloc.h>
26 #include "libcli/util/ntstatus.h"
27
28 struct loadparm_context;
29 union ntdb_attribute;
30
31 /* You only need this on databases with NTDB_CLEAR_IF_FIRST */
32 int ntdb_reopen(struct ntdb_context *ntdb);
33
34 /* You only need to do this if you have NTDB_CLEAR_IF_FIRST databases, and
35  * the parent will go away before this child. */
36 int ntdb_reopen_all(void);
37
38 /*
39  * This is like TDB_CLEAR_IF_FIRST, for use with ntdb_new.
40  *
41  * It's a bad idea for new code.
42  */
43 #define NTDB_CLEAR_IF_FIRST 1048576
44
45 /***************************************************************
46  Open an NTDB using talloc: it will be allocated off the context, and
47  all NTDB_DATA.dptr are allocated as children of the ntdb context.
48  Sets up a logging function for you, and uses lp_ctx to decide whether
49  to disable mmap.
50
51  Any extra ntdb attributes can be handed through attr; usually it's
52  NULL, ntdb_new provides logging and allocator attributes.
53
54  The destructor for the struct ntdb_context will do ntdb_close()
55  for you.
56 ****************************************************************/
57 struct ntdb_context *ntdb_new(TALLOC_CTX *ctx,
58                               const char *name, int ntdb_flags,
59                               int open_flags, mode_t mode,
60                               union ntdb_attribute *attr,
61                               struct loadparm_context *lp_ctx);
62
63 /****************************************************************************
64  Lock a chain by string.
65 ****************************************************************************/
66 enum NTDB_ERROR ntdb_lock_bystring(struct ntdb_context *ntdb,
67                                    const char *keyval);
68
69 /****************************************************************************
70  Unlock a chain by string.
71 ****************************************************************************/
72 void ntdb_unlock_bystring(struct ntdb_context *ntdb, const char *keyval);
73
74 /****************************************************************************
75  Delete an entry using a null terminated string key.
76 ****************************************************************************/
77 enum NTDB_ERROR ntdb_delete_bystring(struct ntdb_context *ntdb,
78                                      const char *keystr);
79
80 /****************************************************************************
81  Store a buffer by a null terminated string key.  Return 0 on success, -ve
82  on failure.
83 ****************************************************************************/
84 enum NTDB_ERROR ntdb_store_bystring(struct ntdb_context *ntdb,
85                                     const char *keystr,
86                                     NTDB_DATA data, int nflags);
87
88 /****************************************************************************
89  Fetch a buffer using a null terminated string key.  Don't forget to call
90  free() on the result dptr (unless the ntdb is from ntdb_new, in which case
91  it will be a child of ntdb).
92 ****************************************************************************/
93 enum NTDB_ERROR ntdb_fetch_bystring(struct ntdb_context *ntdb,
94                                     const char *keystr,
95                                     NTDB_DATA *data);
96
97
98 /****************************************************************************
99  Fetch a int32_t value by a string key.  *val is int32_t in native byte order.
100  ntdb must have been created with ntdb_new() (as it uses talloc_free).
101 ****************************************************************************/
102 enum NTDB_ERROR ntdb_fetch_int32(struct ntdb_context *ntdb,
103                                  const char *keystr, int32_t *val);
104
105 /****************************************************************************
106  Store a int32_t value by a string key.  val is int32_t in native byte order.
107 ****************************************************************************/
108 enum NTDB_ERROR ntdb_store_int32(struct ntdb_context *ntdb,
109                                  const char *keystr, int32_t val);
110
111
112 /****************************************************************************
113  Atomic integer add; reads the old value into *oldval (if found), then stores
114  *oldval + addval back for next time.  Uses chainlock to do this atomically.
115
116  Thus the first time this is ever called, oldval will be unchanged.
117 ****************************************************************************/
118 enum NTDB_ERROR ntdb_add_int32_atomic(struct ntdb_context *ntdb,
119                                       const char *keystr,
120                                       int32_t *oldval, int32_t addval);
121
122 /****************************************************************************
123  Turn a nul-terminated string into an NTDB_DATA.
124 ****************************************************************************/
125 static inline NTDB_DATA string_term_ntdb_data(const char *string)
126 {
127         return ntdb_mkdata(string, string ? strlen(string) + 1 : 0);
128 }
129
130
131 /****************************************************************************
132  Return an NTSTATUS from a NTDB_ERROR
133 ****************************************************************************/
134 NTSTATUS map_nt_error_from_ntdb(enum NTDB_ERROR err);
135 #endif /* _____LIB_UTIL_UTIL_NTDB_H__ */