dbwrap: Use tdb_null in db_ctdb_delete
[Samba/gbeck.git] / lib / dbwrap / dbwrap_local_open.c
blobc2eb7b73eeb0c1d1d63be5bc87455ba4deb1f13d
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper: local open code.
5 Copyright (C) Rusty Russell 2012
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_tdb.h"
24 #include "dbwrap/dbwrap_ntdb.h"
25 #include "tdb.h"
26 #ifndef DISABLE_NTDB
27 #include "lib/util/util_ntdb.h"
28 #endif
29 #include "lib/param/param.h"
30 #include "system/filesys.h"
31 #include "ccan/str/str.h"
33 #ifndef DISABLE_NTDB
34 struct flag_map {
35 int tdb_flag;
36 int ntdb_flag;
39 static const struct flag_map tdb_ntdb_flags[] = {
40 { TDB_CLEAR_IF_FIRST, NTDB_CLEAR_IF_FIRST },
41 { TDB_INTERNAL, NTDB_INTERNAL },
42 { TDB_NOLOCK, NTDB_NOLOCK },
43 { TDB_NOMMAP, NTDB_NOMMAP },
44 { TDB_CONVERT, NTDB_CONVERT },
45 { TDB_NOSYNC, NTDB_NOSYNC },
46 { TDB_SEQNUM, NTDB_SEQNUM },
47 { TDB_VOLATILE, 0 },
48 { TDB_ALLOW_NESTING, NTDB_ALLOW_NESTING },
49 { TDB_DISALLOW_NESTING, 0 },
50 { TDB_INCOMPATIBLE_HASH, 0 }
53 static int tdb_flags_to_ntdb_flags(int tdb_flags)
55 unsigned int i;
56 int ntdb_flags = 0;
58 /* TDB allows nesting unless told not to. */
59 if (!(tdb_flags & TDB_DISALLOW_NESTING))
60 ntdb_flags |= NTDB_ALLOW_NESTING;
62 for (i = 0; i < sizeof(tdb_ntdb_flags)/sizeof(tdb_ntdb_flags[0]); i++) {
63 if (tdb_flags & tdb_ntdb_flags[i].tdb_flag) {
64 tdb_flags &= ~tdb_ntdb_flags[i].tdb_flag;
65 ntdb_flags |= tdb_ntdb_flags[i].ntdb_flag;
69 SMB_ASSERT(tdb_flags == 0);
70 return ntdb_flags;
73 struct trav_data {
74 struct db_context *ntdb;
75 NTSTATUS status;
78 static int write_to_ntdb(struct db_record *rec, void *_tdata)
80 struct trav_data *tdata = _tdata;
81 TDB_DATA key, value;
83 key = dbwrap_record_get_key(rec);
84 value = dbwrap_record_get_value(rec);
86 tdata->status = dbwrap_store(tdata->ntdb, key, value, TDB_INSERT);
87 if (!NT_STATUS_IS_OK(tdata->status)) {
88 return 1;
90 return 0;
93 static bool tdb_to_ntdb(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
94 const char *tdbname, const char *ntdbname)
96 struct db_context *ntdb, *tdb;
97 char *bakname;
98 const char *tdbbase, *bakbase;
99 struct trav_data tdata;
100 struct stat st;
102 /* We need permissions from the tdb file. */
103 if (stat(tdbname, &st) == -1) {
104 DEBUG(0, ("tdb_to_ntdb: fstat %s failed: %s\n",
105 tdbname, strerror(errno)));
106 return false;
108 tdb = db_open_tdb(ctx, lp_ctx, tdbname, 0,
109 TDB_DEFAULT, O_RDONLY, 0, 0);
110 if (!tdb) {
111 DEBUG(0, ("tdb_to_ntdb: could not open %s: %s\n",
112 tdbname, strerror(errno)));
113 return false;
115 ntdb = db_open_ntdb(ctx, lp_ctx, ntdbname, dbwrap_hash_size(tdb),
116 TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
117 st.st_mode & 0777, 0);
118 if (!ntdb) {
119 DEBUG(0, ("tdb_to_ntdb: could not create %s: %s\n",
120 ntdbname, strerror(errno)));
121 return false;
123 bakname = talloc_asprintf(ctx, "%s.bak", tdbname);
124 if (!bakname) {
125 DEBUG(0, ("tdb_to_ntdb: could not allocate\n"));
126 return false;
129 tdata.status = NT_STATUS_OK;
130 tdata.ntdb = ntdb;
131 if (!NT_STATUS_IS_OK(dbwrap_traverse_read(tdb, write_to_ntdb, &tdata,
132 NULL))) {
133 return false;
135 if (!NT_STATUS_IS_OK(tdata.status)) {
136 return false;
139 if (rename(tdbname, bakname) != 0) {
140 DEBUG(0, ("tdb_to_ntdb: could not rename %s to %s\n",
141 tdbname, bakname));
142 unlink(ntdbname);
143 return false;
146 /* Make sure it's never accidentally used. */
147 symlink("This is now in an NTDB", tdbname);
149 /* Make message a bit shorter by using basenames. */
150 tdbbase = strrchr(tdbname, '/');
151 if (!tdbbase)
152 tdbbase = tdbname;
153 bakbase = strrchr(bakname, '/');
154 if (!bakbase)
155 bakbase = bakname;
156 DEBUG(1, ("Upgraded %s from %s (which moved to %s)\n",
157 ntdbname, tdbbase, bakbase));
158 return true;
160 #endif /* !DISABLE_NTDB */
162 struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
163 struct loadparm_context *lp_ctx,
164 const char *name,
165 int hash_size, int tdb_flags,
166 int open_flags, mode_t mode,
167 enum dbwrap_lock_order lock_order)
169 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
170 const char *ntdbname, *tdbname;
171 struct db_context *db = NULL;
173 /* Get both .ntdb and .tdb variants of the name. */
174 if (!name) {
175 tdbname = ntdbname = "unnamed database";
176 } else if (strends(name, ".tdb")) {
177 tdbname = name;
178 ntdbname = talloc_asprintf(tmp_ctx,
179 "%.*s.ntdb",
180 (int)strlen(name) - 4, name);
181 } else if (strends(name, ".ntdb")) {
182 ntdbname = name;
183 tdbname = talloc_asprintf(tmp_ctx,
184 "%.*s.tdb",
185 (int)strlen(name) - 5, name);
186 } else {
187 DEBUG(1, ("WARNING: database '%s' does not end in .[n]tdb:"
188 " treating it as a TDB file!\n", name));
189 ntdbname = talloc_strdup(tmp_ctx, name);
190 tdbname = name;
193 if (ntdbname == NULL || tdbname == NULL) {
194 DEBUG(0, ("talloc failed\n"));
195 goto out;
198 if (name == ntdbname) {
199 #ifdef DISABLE_NTDB
200 DEBUG(1, ("WARNING: no ntdb support to open '%s'\n", name));
201 #else
202 int ntdb_flags = tdb_flags_to_ntdb_flags(tdb_flags);
204 /* For non-internal databases, we upgrade on demand. */
205 if (!(tdb_flags & TDB_INTERNAL)) {
206 if (!file_exist(ntdbname) && file_exist(tdbname)) {
207 if (!tdb_to_ntdb(tmp_ctx, lp_ctx,
208 tdbname, ntdbname)) {
209 goto out;
213 db = db_open_ntdb(mem_ctx, lp_ctx, ntdbname, hash_size,
214 ntdb_flags, open_flags, mode, lock_order);
215 #endif
216 } else {
217 if (!streq(ntdbname, tdbname) && file_exist(ntdbname)) {
218 DEBUG(0, ("Refusing to open '%s' when '%s' exists\n",
219 tdbname, ntdbname));
220 goto out;
222 db = db_open_tdb(mem_ctx, lp_ctx, tdbname, hash_size,
223 tdb_flags, open_flags, mode,
224 lock_order);
226 out:
227 talloc_free(tmp_ctx);
228 return db;