s3:dbwrap: move all .c and .h files of dbwrap to lib/dbwrap/
[mat/samba.git] / source3 / lib / dbwrap / dbwrap_open.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Database interface wrapper
4
5    Copyright (C) Volker Lendecke 2005-2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_private.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "util_tdb.h"
26 #ifdef CLUSTER_SUPPORT
27 #include "ctdb_private.h"
28 #endif
29
30 bool db_is_local(const char *name)
31 {
32 #ifdef CLUSTER_SUPPORT
33         const char *sockname = lp_ctdbd_socket();
34
35         if(!sockname || !*sockname) {
36                 sockname = CTDB_PATH;
37         }
38
39         if (lp_clustering() && socket_exist(sockname)) {
40                 const char *partname;
41                 /* ctdb only wants the file part of the name */
42                 partname = strrchr(name, '/');
43                 if (partname) {
44                         partname++;
45                 } else {
46                         partname = name;
47                 }
48                 /* allow ctdb for individual databases to be disabled */
49                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
50                         return false;
51                 }
52         }
53 #endif
54         return true;
55 }
56
57 /**
58  * open a database
59  */
60 struct db_context *db_open(TALLOC_CTX *mem_ctx,
61                            const char *name,
62                            int hash_size, int tdb_flags,
63                            int open_flags, mode_t mode)
64 {
65         struct db_context *result = NULL;
66 #ifdef CLUSTER_SUPPORT
67         const char *sockname = lp_ctdbd_socket();
68
69         if(!sockname || !*sockname) {
70                 sockname = CTDB_PATH;
71         }
72
73         if (lp_clustering()) {
74                 const char *partname;
75
76                 if (!socket_exist(sockname)) {
77                         DEBUG(1, ("ctdb socket does not exist - is ctdb not "
78                                   "running?\n"));
79                         return NULL;
80                 }
81
82                 /* ctdb only wants the file part of the name */
83                 partname = strrchr(name, '/');
84                 if (partname) {
85                         partname++;
86                 } else {
87                         partname = name;
88                 }
89                 /* allow ctdb for individual databases to be disabled */
90                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
91                         result = db_open_ctdb(mem_ctx, partname, hash_size,
92                                               tdb_flags, open_flags, mode);
93                         if (result == NULL) {
94                                 DEBUG(0,("failed to attach to ctdb %s\n",
95                                          partname));
96                                 if (errno == 0) {
97                                         errno = EIO;
98                                 }
99                                 return NULL;
100                         }
101                 }
102         }
103
104 #endif
105
106         if (result == NULL) {
107                 result = db_open_tdb(mem_ctx, name, hash_size,
108                                      tdb_flags, open_flags, mode);
109         }
110
111         if ((result != NULL) && (result->fetch == NULL)) {
112                 result->fetch = dbwrap_fallback_fetch;
113         }
114         if ((result != NULL) && (result->parse_record == NULL)) {
115                 result->parse_record = dbwrap_fallback_parse_record;
116         }
117
118         return result;
119 }