s3:dbwrap: move db_open() to a file dbwrap_open.c of its own.
[ira/wip.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 #include "dbwrap.h"
24 #include "dbwrap/dbwrap_private.h"
25 #include "util_tdb.h"
26 #ifdef CLUSTER_SUPPORT
27 #include "ctdb_private.h"
28 #endif
29
30
31 /*
32  * Fall back using fetch_locked if no genuine fetch operation is provided
33  */
34
35 int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
36                           TDB_DATA key, TDB_DATA *data)
37 {
38         struct db_record *rec;
39
40         if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
41                 return -1;
42         }
43
44         data->dsize = rec->value.dsize;
45         data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
46         TALLOC_FREE(rec);
47         return 0;
48 }
49
50 /*
51  * Fall back using fetch if no genuine parse operation is provided
52  */
53
54 int dbwrap_fallback_parse_record(struct db_context *db, TDB_DATA key,
55                                  int (*parser)(TDB_DATA key,
56                                                TDB_DATA data,
57                                                void *private_data),
58                                  void *private_data)
59 {
60         TDB_DATA data;
61         int res;
62
63         res = db->fetch(db, talloc_tos(), key, &data);
64         if (res != 0) {
65                 return res;
66         }
67
68         res = parser(key, data, private_data);
69         TALLOC_FREE(data.dptr);
70         return res;
71 }
72
73 bool db_is_local(const char *name)
74 {
75 #ifdef CLUSTER_SUPPORT
76         const char *sockname = lp_ctdbd_socket();
77
78         if(!sockname || !*sockname) {
79                 sockname = CTDB_PATH;
80         }
81
82         if (lp_clustering() && socket_exist(sockname)) {
83                 const char *partname;
84                 /* ctdb only wants the file part of the name */
85                 partname = strrchr(name, '/');
86                 if (partname) {
87                         partname++;
88                 } else {
89                         partname = name;
90                 }
91                 /* allow ctdb for individual databases to be disabled */
92                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
93                         return false;
94                 }
95         }
96 #endif
97         return true;
98 }