r12608: Remove some unused #include lines.
[metze/samba/wip.git] / source4 / lib / ldb / ldb_tdb / ldb_tdb_wrap.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "includes.h"
26 #include "ldb/include/ldb.h"
27 #include "ldb/ldb_tdb/ldb_tdb.h"
28
29 /*
30   the purpose of this code is to work around the braindead posix locking
31   rules, to allow us to have a ldb open more than once while allowing 
32   locking to work
33 */
34
35 struct ltdb_wrap {
36         struct ltdb_wrap *next, *prev;
37         struct tdb_context *tdb;
38         dev_t device;
39         ino_t inode;
40 };
41
42 static struct ltdb_wrap *tdb_list;
43
44 /* destroy the last connection to a tdb */
45 static int ltdb_wrap_destructor(void *ctx)
46 {
47         struct ltdb_wrap *w = talloc_get_type(ctx, struct ltdb_wrap);
48         tdb_close(w->tdb);
49         if (w->next) {
50                 w->next->prev = w->prev;
51         }
52         if (w->prev) {
53                 w->prev->next = w->next;
54         }
55         if (w == tdb_list) {
56                 tdb_list = w->next;
57         }
58         return 0;
59 }                                
60
61 /*
62   wrapped connection to a tdb database. The caller should _not_ free
63   this as it is not a talloc structure (as tdb does not use talloc
64   yet). It will auto-close when the caller frees the mem_ctx that is
65   passed to this call
66  */
67 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
68                                    const char *path, int hash_size, int tdb_flags,
69                                    int open_flags, mode_t mode)
70 {
71         struct ltdb_wrap *w;
72         struct stat st;
73
74         if (stat(path, &st) == 0) {
75                 for (w=tdb_list;w;w=w->next) {
76                         if (st.st_dev == w->device && st.st_ino == w->inode) {
77                                 talloc_reference(mem_ctx, w);
78                                 return w->tdb;
79                         }
80                 }
81         }
82
83         w = talloc(mem_ctx, struct ltdb_wrap);
84         if (w == NULL) {
85                 return NULL;
86         }
87
88         w->tdb = tdb_open(path, hash_size, tdb_flags, open_flags, mode);
89         if (w->tdb == NULL) {
90                 talloc_free(w);
91                 return NULL;
92         }
93
94         if (fstat(tdb_fd(w->tdb), &st) != 0) {
95                 tdb_close(w->tdb);
96                 talloc_free(w);
97                 return NULL;
98         }
99
100         w->device = st.st_dev;
101         w->inode  = st.st_ino;
102
103         talloc_set_destructor(w, ltdb_wrap_destructor);
104
105         w->next = tdb_list;
106         w->prev = NULL;
107         if (tdb_list) {
108                 tdb_list->prev = w;
109         }
110         tdb_list = w;
111         
112         return w->tdb;
113 }
114