libsmbconf: Convert smbconf_delete_share() to sbcErr.
[Samba.git] / source3 / lib / dbwrap.c
blob4e7346c2c4f73289ab9d0f0d3c0c385094741958
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 #include "dbwrap.h"
24 #ifdef CLUSTER_SUPPORT
25 #include "ctdb_private.h"
26 #endif
28 * Fall back using fetch_locked if no genuine fetch operation is provided
31 static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
32 TDB_DATA key, TDB_DATA *data)
34 struct db_record *rec;
36 if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
37 return -1;
40 data->dsize = rec->value.dsize;
41 data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
42 TALLOC_FREE(rec);
43 return 0;
47 * Fall back using fetch if no genuine parse operation is provided
50 static int dbwrap_fallback_parse_record(struct db_context *db, TDB_DATA key,
51 int (*parser)(TDB_DATA key,
52 TDB_DATA data,
53 void *private_data),
54 void *private_data)
56 TDB_DATA data;
57 int res;
59 res = db->fetch(db, talloc_tos(), key, &data);
60 if (res != 0) {
61 return res;
64 res = parser(key, data, private_data);
65 TALLOC_FREE(data.dptr);
66 return res;
69 bool db_is_local(const char *name)
71 #ifdef CLUSTER_SUPPORT
72 const char *sockname = lp_ctdbd_socket();
74 if(!sockname || !*sockname) {
75 sockname = CTDB_PATH;
78 if (lp_clustering() && socket_exist(sockname)) {
79 const char *partname;
80 /* ctdb only wants the file part of the name */
81 partname = strrchr(name, '/');
82 if (partname) {
83 partname++;
84 } else {
85 partname = name;
87 /* allow ctdb for individual databases to be disabled */
88 if (lp_parm_bool(-1, "ctdb", partname, True)) {
89 return false;
92 #endif
93 return true;
96 /**
97 * open a database
99 struct db_context *db_open(TALLOC_CTX *mem_ctx,
100 const char *name,
101 int hash_size, int tdb_flags,
102 int open_flags, mode_t mode)
104 struct db_context *result = NULL;
105 #ifdef CLUSTER_SUPPORT
106 const char *sockname = lp_ctdbd_socket();
108 if(!sockname || !*sockname) {
109 sockname = CTDB_PATH;
112 if (lp_clustering()) {
113 const char *partname;
115 if (!socket_exist(sockname)) {
116 DEBUG(1, ("ctdb socket does not exist - is ctdb not "
117 "running?\n"));
118 return NULL;
121 /* ctdb only wants the file part of the name */
122 partname = strrchr(name, '/');
123 if (partname) {
124 partname++;
125 } else {
126 partname = name;
128 /* allow ctdb for individual databases to be disabled */
129 if (lp_parm_bool(-1, "ctdb", partname, True)) {
130 result = db_open_ctdb(mem_ctx, partname, hash_size,
131 tdb_flags, open_flags, mode);
132 if (result == NULL) {
133 DEBUG(0,("failed to attach to ctdb %s\n",
134 partname));
135 if (errno == 0) {
136 errno = EIO;
138 return NULL;
143 #endif
145 if (result == NULL) {
146 result = db_open_tdb(mem_ctx, name, hash_size,
147 tdb_flags, open_flags, mode);
150 if ((result != NULL) && (result->fetch == NULL)) {
151 result->fetch = dbwrap_fallback_fetch;
153 if ((result != NULL) && (result->parse_record == NULL)) {
154 result->parse_record = dbwrap_fallback_parse_record;
157 return result;
160 NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
162 struct db_record *rec;
163 NTSTATUS status;
165 rec = db->fetch_locked(db, talloc_tos(), key);
166 if (rec == NULL) {
167 return NT_STATUS_NO_MEMORY;
169 status = rec->delete_rec(rec);
170 TALLOC_FREE(rec);
171 return status;
174 NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
175 TDB_DATA data, int flags)
177 struct db_record *rec;
178 NTSTATUS status;
180 rec = db->fetch_locked(db, talloc_tos(), key);
181 if (rec == NULL) {
182 return NT_STATUS_NO_MEMORY;
185 status = rec->store(rec, data, flags);
186 TALLOC_FREE(rec);
187 return status;
190 TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
191 TDB_DATA key)
193 TDB_DATA result;
195 if (db->fetch(db, mem_ctx, key, &result) == -1) {
196 return make_tdb_data(NULL, 0);
199 return result;
202 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
204 return dbwrap_delete(db, string_term_tdb_data(key));
207 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
208 TDB_DATA data, int flags)
210 return dbwrap_store(db, string_term_tdb_data(key), data, flags);
213 TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
214 const char *key)
216 return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key));