s3: Lift the smbd_messaging_context from print_queue_purge
[Samba/gbeck.git] / source3 / lib / dbwrap.c
blob8b3db8940aab13545a3e7fa8833cd03fd6313d17
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
6 Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
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.
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.
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/>.
22 #include "includes.h"
23 #ifdef CLUSTER_SUPPORT
24 #include "ctdb_private.h"
25 #endif
27 * Fall back using fetch_locked if no genuine fetch operation is provided
30 static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
31 TDB_DATA key, TDB_DATA *data)
33 struct db_record *rec;
35 if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
36 return -1;
39 data->dsize = rec->value.dsize;
40 data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
41 TALLOC_FREE(rec);
42 return 0;
46 * Fall back using fetch if no genuine parse operation is provided
49 static int dbwrap_fallback_parse_record(struct db_context *db, TDB_DATA key,
50 int (*parser)(TDB_DATA key,
51 TDB_DATA data,
52 void *private_data),
53 void *private_data)
55 TDB_DATA data;
56 int res;
58 res = db->fetch(db, talloc_tos(), key, &data);
59 if (res != 0) {
60 return res;
63 res = parser(key, data, private_data);
64 TALLOC_FREE(data.dptr);
65 return res;
68 bool db_is_local(const char *name)
70 #ifdef CLUSTER_SUPPORT
71 const char *sockname = lp_ctdbd_socket();
73 if(!sockname || !*sockname) {
74 sockname = CTDB_PATH;
77 if (lp_clustering() && socket_exist(sockname)) {
78 const char *partname;
79 /* ctdb only wants the file part of the name */
80 partname = strrchr(name, '/');
81 if (partname) {
82 partname++;
83 } else {
84 partname = name;
86 /* allow ctdb for individual databases to be disabled */
87 if (lp_parm_bool(-1, "ctdb", partname, True)) {
88 return false;
91 #endif
92 return true;
95 /**
96 * open a database
98 struct db_context *db_open(TALLOC_CTX *mem_ctx,
99 const char *name,
100 int hash_size, int tdb_flags,
101 int open_flags, mode_t mode)
103 struct db_context *result = NULL;
104 #ifdef CLUSTER_SUPPORT
105 const char *sockname = lp_ctdbd_socket();
107 if(!sockname || !*sockname) {
108 sockname = CTDB_PATH;
111 if (lp_clustering()) {
112 const char *partname;
114 if (!socket_exist(sockname)) {
115 DEBUG(1, ("ctdb socket does not exist - is ctdb not "
116 "running?\n"));
117 return NULL;
120 /* ctdb only wants the file part of the name */
121 partname = strrchr(name, '/');
122 if (partname) {
123 partname++;
124 } else {
125 partname = name;
127 /* allow ctdb for individual databases to be disabled */
128 if (lp_parm_bool(-1, "ctdb", partname, True)) {
129 result = db_open_ctdb(mem_ctx, partname, hash_size,
130 tdb_flags, open_flags, mode);
131 if (result == NULL) {
132 DEBUG(0,("failed to attach to ctdb %s\n",
133 partname));
134 if (errno == 0) {
135 errno = EIO;
137 return NULL;
142 #endif
144 if (result == NULL) {
145 result = db_open_tdb(mem_ctx, name, hash_size,
146 tdb_flags, open_flags, mode);
149 if ((result != NULL) && (result->fetch == NULL)) {
150 result->fetch = dbwrap_fallback_fetch;
152 if ((result != NULL) && (result->parse_record == NULL)) {
153 result->parse_record = dbwrap_fallback_parse_record;
156 return result;
159 NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
161 struct db_record *rec;
162 NTSTATUS status;
164 rec = db->fetch_locked(db, talloc_tos(), key);
165 if (rec == NULL) {
166 return NT_STATUS_NO_MEMORY;
168 status = rec->delete_rec(rec);
169 TALLOC_FREE(rec);
170 return status;
173 NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
174 TDB_DATA data, int flags)
176 struct db_record *rec;
177 NTSTATUS status;
179 rec = db->fetch_locked(db, talloc_tos(), key);
180 if (rec == NULL) {
181 return NT_STATUS_NO_MEMORY;
184 status = rec->store(rec, data, flags);
185 TALLOC_FREE(rec);
186 return status;
189 TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
190 TDB_DATA key)
192 TDB_DATA result;
194 if (db->fetch(db, mem_ctx, key, &result) == -1) {
195 return make_tdb_data(NULL, 0);
198 return result;
201 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
203 return dbwrap_delete(db, string_term_tdb_data(key));
206 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
207 TDB_DATA data, int flags)
209 return dbwrap_store(db, string_term_tdb_data(key), data, flags);
212 TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
213 const char *key)
215 return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key));