s3-winbindd_ads: prevent negative GM/ cache entries due to broken connections
[Samba/gebeck_regimport.git] / source3 / lib / dbwrap.c
blob73c2761a1b3d069cf482a31757e9c150fe1f00a0
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;
45 /**
46 * open a database
48 struct db_context *db_open(TALLOC_CTX *mem_ctx,
49 const char *name,
50 int hash_size, int tdb_flags,
51 int open_flags, mode_t mode)
53 struct db_context *result = NULL;
54 #ifdef CLUSTER_SUPPORT
55 const char *sockname = lp_ctdbd_socket();
56 #endif
58 #ifdef CLUSTER_SUPPORT
59 if(!sockname || !*sockname) {
60 sockname = CTDB_PATH;
63 if (lp_clustering()) {
64 const char *partname;
66 if (!socket_exist(sockname)) {
67 DEBUG(1, ("ctdb socket does not exist - is ctdb not "
68 "running?\n"));
69 return NULL;
72 /* ctdb only wants the file part of the name */
73 partname = strrchr(name, '/');
74 if (partname) {
75 partname++;
76 } else {
77 partname = name;
79 /* allow ctdb for individual databases to be disabled */
80 if (lp_parm_bool(-1, "ctdb", partname, True)) {
81 result = db_open_ctdb(mem_ctx, partname, hash_size,
82 tdb_flags, open_flags, mode);
83 if (result == NULL) {
84 DEBUG(0,("failed to attach to ctdb %s\n",
85 partname));
86 if (errno == 0) {
87 errno = EIO;
89 return NULL;
94 #endif
96 if (result == NULL) {
97 result = db_open_tdb(mem_ctx, name, hash_size,
98 tdb_flags, open_flags, mode);
101 if ((result != NULL) && (result->fetch == NULL)) {
102 result->fetch = dbwrap_fallback_fetch;
105 return result;
108 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
110 struct db_record *rec;
111 NTSTATUS status;
113 rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
114 if (rec == NULL) {
115 return NT_STATUS_NO_MEMORY;
117 status = rec->delete_rec(rec);
118 TALLOC_FREE(rec);
119 return status;
122 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
123 TDB_DATA data, int flags)
125 struct db_record *rec;
126 NTSTATUS status;
128 rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
129 if (rec == NULL) {
130 return NT_STATUS_NO_MEMORY;
133 status = rec->store(rec, data, flags);
134 TALLOC_FREE(rec);
135 return status;
138 TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
139 const char *key)
141 TDB_DATA result;
143 if (db->fetch(db, mem_ctx, string_term_tdb_data(key), &result) == -1) {
144 return make_tdb_data(NULL, 0);
147 return result;