Merge CTDB-related fixes from samba-ctdb 3.0 branch (http://samba.org/~tridge/3_0...
[samba.git] / source3 / lib / dbwrap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Database interface wrapper
4    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
5
6    Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
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 #include "includes.h"
23 #ifdef CLUSTER_SUPPORT
24 #include "ctdb_private.h"
25 #endif
26 /*
27  * Fall back using fetch_locked if no genuine fetch operation is provided
28  */
29
30 static int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
31                                  TDB_DATA key, TDB_DATA *data)
32 {
33         struct db_record *rec;
34
35         if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
36                 return -1;
37         }
38
39         data->dsize = rec->value.dsize;
40         data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
41         TALLOC_FREE(rec);
42         return 0;
43 }
44
45 struct db_context *db_open(TALLOC_CTX *mem_ctx,
46                            const char *name,
47                            int hash_size, int tdb_flags,
48                            int open_flags, mode_t mode)
49 {
50         struct db_context *result = NULL;
51 #ifdef CLUSTER_SUPPORT
52         const char *sockname = lp_ctdbd_socket();
53 #endif
54
55 #ifdef CLUSTER_SUPPORT
56         if(!sockname || !*sockname) {
57                 sockname = CTDB_PATH;
58         }
59
60         if (lp_clustering() && socket_exist(sockname)) {
61                 const char *partname;
62                 /* ctdb only wants the file part of the name */
63                 partname = strrchr(name, '/');
64                 if (partname) {
65                         partname++;
66                 } else {
67                         partname = name;
68                 }
69                 /* allow ctdb for individual databases to be disabled */
70                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
71                         result = db_open_ctdb(mem_ctx, partname, hash_size,
72                                               tdb_flags, open_flags, mode);
73                         if (result == NULL) {
74                                 DEBUG(0,("failed to attach to ctdb %s\n",
75                                          partname));
76                                 smb_panic("failed to attach to a ctdb "
77                                           "database");
78                         }
79                 }
80         }
81
82 #endif
83
84         if (result == NULL) {
85                 result = db_open_tdb(mem_ctx, name, hash_size,
86                                      tdb_flags, open_flags, mode);
87         }
88
89         if ((result != NULL) && (result->fetch == NULL)) {
90                 result->fetch = dbwrap_fallback_fetch;
91         }
92
93         return result;
94 }
95
96 NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
97 {
98         struct db_record *rec;
99         NTSTATUS status;
100
101         rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
102         if (rec == NULL) {
103                 return NT_STATUS_NO_MEMORY;
104         }
105         status = rec->delete_rec(rec);
106         TALLOC_FREE(rec);
107         return status;
108 }
109
110 NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
111                                TDB_DATA data, int flags)
112 {
113         struct db_record *rec;
114         NTSTATUS status;
115
116         rec = db->fetch_locked(db, talloc_tos(), string_term_tdb_data(key));
117         if (rec == NULL) {
118                 return NT_STATUS_NO_MEMORY;
119         }
120
121         status = rec->store(rec, data, flags);
122         TALLOC_FREE(rec);
123         return status;
124 }
125
126 TDB_DATA dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
127                                const char *key)
128 {
129         TDB_DATA result;
130
131         if (db->fetch(db, mem_ctx, string_term_tdb_data(key), &result) == -1) {
132                 return make_tdb_data(NULL, 0);
133         }
134
135         return result;
136 }