buildtools: Rename perl vendorarch configure option.
[Samba.git] / source3 / lib / dbwrap / dbwrap_open.c
blob3c8756b0bcef978ac898a7536fe65a4dc7427dc3
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper
5 Copyright (C) Volker Lendecke 2005-2007
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.
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.
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/>.
21 #include "includes.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_private.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "dbwrap/dbwrap_tdb.h"
26 #include "dbwrap/dbwrap_ctdb.h"
27 #include "lib/param/param.h"
28 #include "util_tdb.h"
29 #ifdef CLUSTER_SUPPORT
30 #include "ctdb_private.h"
31 #endif
33 bool db_is_local(const char *name)
35 #ifdef CLUSTER_SUPPORT
36 const char *sockname = lp_ctdbd_socket();
38 if (lp_clustering() && socket_exist(sockname)) {
39 const char *partname;
40 /* ctdb only wants the file part of the name */
41 partname = strrchr(name, '/');
42 if (partname) {
43 partname++;
44 } else {
45 partname = name;
47 /* allow ctdb for individual databases to be disabled */
48 if (lp_parm_bool(-1, "ctdb", partname, True)) {
49 return false;
52 #endif
53 return true;
56 /**
57 * open a database
59 struct db_context *db_open(TALLOC_CTX *mem_ctx,
60 const char *name,
61 int hash_size, int tdb_flags,
62 int open_flags, mode_t mode,
63 enum dbwrap_lock_order lock_order,
64 uint64_t dbwrap_flags)
66 struct db_context *result = NULL;
67 #ifdef CLUSTER_SUPPORT
68 const char *sockname;
69 #endif
71 if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) {
72 errno = EINVAL;
73 return NULL;
76 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
77 const char *base;
78 bool try_readonly = false;
80 base = strrchr_m(name, '/');
81 if (base != NULL) {
82 base += 1;
83 } else {
84 base = name;
87 if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
88 try_readonly = true;
91 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
92 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
94 if (try_readonly) {
95 dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
96 } else {
97 dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
101 #ifdef CLUSTER_SUPPORT
102 sockname = lp_ctdbd_socket();
104 if (lp_clustering()) {
105 const char *partname;
107 if (!socket_exist(sockname)) {
108 DEBUG(1, ("ctdb socket does not exist - is ctdb not "
109 "running?\n"));
110 return NULL;
113 /* ctdb only wants the file part of the name */
114 partname = strrchr(name, '/');
115 if (partname) {
116 partname++;
117 } else {
118 partname = name;
120 /* allow ctdb for individual databases to be disabled */
121 if (lp_parm_bool(-1, "ctdb", partname, True)) {
122 result = db_open_ctdb(mem_ctx, partname, hash_size,
123 tdb_flags, open_flags, mode,
124 lock_order, dbwrap_flags);
125 if (result == NULL) {
126 DEBUG(0,("failed to attach to ctdb %s\n",
127 partname));
128 if (errno == 0) {
129 errno = EIO;
131 return NULL;
136 #endif
138 if (result == NULL) {
139 struct loadparm_context *lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
140 result = dbwrap_local_open(mem_ctx, lp_ctx, name, hash_size,
141 tdb_flags, open_flags, mode,
142 lock_order, dbwrap_flags);
143 talloc_unlink(mem_ctx, lp_ctx);
145 return result;