r12733: Merge ldap/ldb controls into main tree
[bbaumbach/samba-autobuild/.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/include/ldb_private.h"
28 #include "ldb/ldb_tdb/ldb_tdb.h"
29
30 /*
31   the purpose of this code is to work around the braindead posix locking
32   rules, to allow us to have a ldb open more than once while allowing 
33   locking to work
34 */
35
36 struct ltdb_wrap {
37         struct ltdb_wrap *next, *prev;
38         struct tdb_context *tdb;
39         dev_t device;
40         ino_t inode;
41 };
42
43 static struct ltdb_wrap *tdb_list;
44
45 /* destroy the last connection to a tdb */
46 static int ltdb_wrap_destructor(void *ctx)
47 {
48         struct ltdb_wrap *w = talloc_get_type(ctx, struct ltdb_wrap);
49         tdb_close(w->tdb);
50         if (w->next) {
51                 w->next->prev = w->prev;
52         }
53         if (w->prev) {
54                 w->prev->next = w->next;
55         }
56         if (w == tdb_list) {
57                 tdb_list = w->next;
58         }
59         return 0;
60 }                                
61
62 /*
63   wrapped connection to a tdb database. The caller should _not_ free
64   this as it is not a talloc structure (as tdb does not use talloc
65   yet). It will auto-close when the caller frees the mem_ctx that is
66   passed to this call
67  */
68 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
69                                    const char *path, int hash_size, int tdb_flags,
70                                    int open_flags, mode_t mode)
71 {
72         struct ltdb_wrap *w;
73         struct stat st;
74
75         if (stat(path, &st) == 0) {
76                 for (w=tdb_list;w;w=w->next) {
77                         if (st.st_dev == w->device && st.st_ino == w->inode) {
78                                 talloc_reference(mem_ctx, w);
79                                 return w->tdb;
80                         }
81                 }
82         }
83
84         w = talloc(mem_ctx, struct ltdb_wrap);
85         if (w == NULL) {
86                 return NULL;
87         }
88
89         w->tdb = tdb_open(path, hash_size, tdb_flags, open_flags, mode);
90         if (w->tdb == NULL) {
91                 talloc_free(w);
92                 return NULL;
93         }
94
95         if (fstat(tdb_fd(w->tdb), &st) != 0) {
96                 tdb_close(w->tdb);
97                 talloc_free(w);
98                 return NULL;
99         }
100
101         w->device = st.st_dev;
102         w->inode  = st.st_ino;
103
104         talloc_set_destructor(w, ltdb_wrap_destructor);
105
106         w->next = tdb_list;
107         w->prev = NULL;
108         if (tdb_list) {
109                 tdb_list->prev = w;
110         }
111         tdb_list = w;
112         
113         return w->tdb;
114 }
115