wafsamba: use additional xml catalog file (bug #9512)
[Samba/gebeck_regimport.git] / lib / dbwrap / dbwrap_file.c
blob50e43b7baef56fd15a34364d1dc7ecbf601af6c6
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface using a file per record
4 Copyright (C) Volker Lendecke 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "dbwrap/dbwrap.h"
22 #include "dbwrap/dbwrap_file.h"
23 #include "dbwrap/dbwrap_private.h"
24 #include "lib/tdb_wrap/tdb_wrap.h"
26 struct db_file_ctx {
27 const char *dirname;
29 /* We only support one locked record at a time -- everything else
30 * would lead to a potential deadlock anyway! */
31 struct db_record *locked_record;
34 struct db_locked_file {
35 int fd;
36 uint8_t hash;
37 const char *name;
38 const char *path;
39 struct db_file_ctx *parent;
42 /* Copy from statcache.c... */
44 static uint32_t fsh(const uint8_t *p, int len)
46 uint32_t n = 0;
47 int i;
48 for (i=0; i<len; i++) {
49 n = ((n << 5) + n) ^ (uint32_t)(p[i]);
51 return n;
54 static int db_locked_file_destr(struct db_locked_file *data)
56 if (data->parent != NULL) {
57 data->parent->locked_record = NULL;
60 if (close(data->fd) != 0) {
61 DEBUG(3, ("close failed: %s\n", strerror(errno)));
62 return -1;
65 return 0;
68 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag);
69 static NTSTATUS db_file_delete(struct db_record *rec);
71 static struct db_record *db_file_fetch_locked(struct db_context *db,
72 TALLOC_CTX *mem_ctx,
73 TDB_DATA key)
75 struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
76 struct db_file_ctx);
77 struct db_record *result;
78 struct db_locked_file *file;
79 struct flock fl;
80 SMB_STRUCT_STAT statbuf;
81 int ret;
83 SMB_ASSERT(ctx->locked_record == NULL);
85 again:
86 if (!(result = talloc(mem_ctx, struct db_record))) {
87 DEBUG(0, ("talloc failed\n"));
88 return NULL;
91 if (!(file = talloc(result, struct db_locked_file))) {
92 DEBUG(0, ("talloc failed\n"));
93 TALLOC_FREE(result);
94 return NULL;
97 result->private_data = file;
98 result->store = db_file_store;
99 result->delete_rec = db_file_delete;
101 result->key.dsize = key.dsize;
102 result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr,
103 key.dsize);
104 if (result->key.dptr == NULL) {
105 DEBUG(0, ("talloc failed\n"));
106 TALLOC_FREE(result);
107 return NULL;
110 /* Cut to 8 bits */
111 file->hash = fsh(key.dptr, key.dsize);
112 file->name = hex_encode_talloc(file, (unsigned char *)key.dptr, key.dsize);
113 if (file->name == NULL) {
114 DEBUG(0, ("hex_encode failed\n"));
115 TALLOC_FREE(result);
116 return NULL;
119 file->path = talloc_asprintf(file, "%s/%2.2X/%s", ctx->dirname,
120 file->hash, file->name);
121 if (file->path == NULL) {
122 DEBUG(0, ("talloc_asprintf failed\n"));
123 TALLOC_FREE(result);
124 return NULL;
127 become_root();
128 file->fd = open(file->path, O_RDWR|O_CREAT, 0644);
129 unbecome_root();
131 if (file->fd < 0) {
132 DEBUG(3, ("Could not open/create %s: %s\n",
133 file->path, strerror(errno)));
134 TALLOC_FREE(result);
135 return NULL;
138 talloc_set_destructor(file, db_locked_file_destr);
140 fl.l_type = F_WRLCK;
141 fl.l_whence = SEEK_SET;
142 fl.l_start = 0;
143 fl.l_len = 1;
144 fl.l_pid = 0;
146 do {
147 ret = fcntl(file->fd, F_SETLKW, &fl);
148 } while ((ret == -1) && (errno == EINTR));
150 if (ret == -1) {
151 DEBUG(3, ("Could not get lock on %s: %s\n",
152 file->path, strerror(errno)));
153 TALLOC_FREE(result);
154 return NULL;
157 if (sys_fstat(file->fd, &statbuf, false) != 0) {
158 DEBUG(3, ("Could not fstat %s: %s\n",
159 file->path, strerror(errno)));
160 TALLOC_FREE(result);
161 return NULL;
164 if (statbuf.st_ex_nlink == 0) {
165 /* Someone has deleted it under the lock, retry */
166 TALLOC_FREE(result);
167 goto again;
170 result->value.dsize = 0;
171 result->value.dptr = NULL;
173 if (statbuf.st_ex_size != 0) {
174 NTSTATUS status;
176 result->value.dsize = statbuf.st_ex_size;
177 result->value.dptr = talloc_array(result, uint8_t,
178 statbuf.st_ex_size);
179 if (result->value.dptr == NULL) {
180 DEBUG(1, ("talloc failed\n"));
181 TALLOC_FREE(result);
182 return NULL;
185 status = read_data(file->fd, (char *)result->value.dptr,
186 result->value.dsize);
187 if (!NT_STATUS_IS_OK(status)) {
188 DEBUG(3, ("read_data failed: %s\n",
189 nt_errstr(status)));
190 TALLOC_FREE(result);
191 return NULL;
195 ctx->locked_record = result;
196 file->parent = (struct db_file_ctx *)talloc_reference(file, ctx);
198 return result;
201 static NTSTATUS db_file_store_root(int fd, TDB_DATA data)
203 if (lseek(fd, 0, SEEK_SET) != 0) {
204 DEBUG(0, ("lseek failed: %s\n", strerror(errno)));
205 return map_nt_error_from_unix(errno);
208 if (write_data(fd, (char *)data.dptr, data.dsize) != data.dsize) {
209 DEBUG(3, ("write_data failed: %s\n", strerror(errno)));
210 return map_nt_error_from_unix(errno);
213 if (ftruncate(fd, data.dsize) != 0) {
214 DEBUG(3, ("ftruncate failed: %s\n", strerror(errno)));
215 return map_nt_error_from_unix(errno);
218 return NT_STATUS_OK;
221 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag)
223 struct db_locked_file *file =
224 talloc_get_type_abort(rec->private_data,
225 struct db_locked_file);
226 NTSTATUS status;
228 become_root();
229 status = db_file_store_root(file->fd, data);
230 unbecome_root();
232 return status;
235 static NTSTATUS db_file_delete(struct db_record *rec)
237 struct db_locked_file *file =
238 talloc_get_type_abort(rec->private_data,
239 struct db_locked_file);
240 int res;
242 become_root();
243 res = unlink(file->path);
244 unbecome_root();
246 if (res == -1) {
247 DEBUG(3, ("unlink(%s) failed: %s\n", file->path,
248 strerror(errno)));
249 return map_nt_error_from_unix(errno);
252 return NT_STATUS_OK;
255 static int db_file_traverse(struct db_context *db,
256 int (*fn)(struct db_record *rec,
257 void *private_data),
258 void *private_data)
260 struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
261 struct db_file_ctx);
262 TALLOC_CTX *mem_ctx = talloc_init("traversal %s\n", ctx->dirname);
264 int i;
265 int count = 0;
267 for (i=0; i<256; i++) {
268 const char *dirname = talloc_asprintf(mem_ctx, "%s/%2.2X",
269 ctx->dirname, i);
270 DIR *dir;
271 struct dirent *dirent;
273 if (dirname == NULL) {
274 DEBUG(0, ("talloc failed\n"));
275 TALLOC_FREE(mem_ctx);
276 return -1;
279 dir = opendir(dirname);
280 if (dir == NULL) {
281 DEBUG(3, ("Could not open dir %s: %s\n", dirname,
282 strerror(errno)));
283 TALLOC_FREE(mem_ctx);
284 return -1;
287 while ((dirent = readdir(dir)) != NULL) {
288 DATA_BLOB keyblob;
289 TDB_DATA key;
290 struct db_record *rec;
292 if ((dirent->d_name[0] == '.') &&
293 ((dirent->d_name[1] == '\0') ||
294 ((dirent->d_name[1] == '.') &&
295 (dirent->d_name[2] == '\0')))) {
296 continue;
299 keyblob = strhex_to_data_blob(mem_ctx, dirent->d_name);
300 if (keyblob.data == NULL) {
301 DEBUG(5, ("strhex_to_data_blob failed\n"));
302 continue;
305 key.dptr = keyblob.data;
306 key.dsize = keyblob.length;
308 if ((ctx->locked_record != NULL) &&
309 (key.dsize == ctx->locked_record->key.dsize) &&
310 (memcmp(key.dptr, ctx->locked_record->key.dptr,
311 key.dsize) == 0)) {
312 count += 1;
313 if (fn(ctx->locked_record,
314 private_data) != 0) {
315 TALLOC_FREE(mem_ctx);
316 closedir(dir);
317 return count;
321 rec = db_file_fetch_locked(db, mem_ctx, key);
322 if (rec == NULL) {
323 /* Someone might have deleted it */
324 continue;
327 if (rec->value.dptr == NULL) {
328 TALLOC_FREE(rec);
329 continue;
332 count += 1;
334 if (fn(rec, private_data) != 0) {
335 TALLOC_FREE(mem_ctx);
336 closedir(dir);
337 return count;
339 TALLOC_FREE(rec);
342 closedir(dir);
345 TALLOC_FREE(mem_ctx);
346 return count;
349 struct db_context *db_open_file(TALLOC_CTX *mem_ctx,
350 const char *name,
351 int tdb_flags,
352 int open_flags, mode_t mode)
354 struct db_context *result = NULL;
355 struct db_file_ctx *ctx;
357 if (!(result = talloc_zero(mem_ctx, struct db_context))) {
358 DEBUG(0, ("talloc failed\n"));
359 return NULL;
362 if (!(ctx = talloc(result, struct db_file_ctx))) {
363 DEBUG(0, ("talloc failed\n"));
364 TALLOC_FREE(result);
365 return NULL;
368 result->private_data = ctx;
369 result->fetch_locked = db_file_fetch_locked;
370 result->try_fetch_locked = NULL;
371 result->traverse = db_file_traverse;
372 result->traverse_read = db_file_traverse;
373 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
374 result->name = name;
375 result->hash_size = 0;
377 ctx->locked_record = NULL;
378 if (!(ctx->dirname = talloc_strdup(ctx, name))) {
379 DEBUG(0, ("talloc failed\n"));
380 TALLOC_FREE(result);
381 return NULL;
384 if (open_flags & O_CREAT) {
385 int ret, i;
387 mode |= (mode & S_IRUSR) ? S_IXUSR : 0;
388 mode |= (mode & S_IRGRP) ? S_IXGRP : 0;
389 mode |= (mode & S_IROTH) ? S_IXOTH : 0;
391 ret = mkdir(name, mode);
392 if ((ret != 0) && (errno != EEXIST)) {
393 DEBUG(5, ("mkdir(%s,%o) failed: %s\n", name, mode,
394 strerror(errno)));
395 TALLOC_FREE(result);
396 return NULL;
399 for (i=0; i<256; i++) {
400 char *path;
401 path = talloc_asprintf(result, "%s/%2.2X", name, i);
402 if (path == NULL) {
403 DEBUG(0, ("asprintf failed\n"));
404 TALLOC_FREE(result);
405 return NULL;
407 ret = mkdir(path, mode);
408 if ((ret != 0) && (errno != EEXIST)) {
409 DEBUG(5, ("mkdir(%s,%o) failed: %s\n", path,
410 mode, strerror(errno)));
411 TALLOC_FREE(result);
412 return NULL;
414 TALLOC_FREE(path);
418 return result;