r22761: This introduces lib/conn_tdb.c with two main functions: connections_traverse
[mat/samba.git] / source3 / lib / conn_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Low-level connections.tdb access functions
4    Copyright (C) Volker Lendecke 2007
5    
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 2 of the License, or
9    (at your option) any later version.
10    
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.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 TDB_CONTEXT *conn_tdb_ctx(BOOL rw)
24 {
25         static TDB_CONTEXT *tdb;
26
27         if (tdb != NULL) {
28                 return tdb;
29         }
30
31         if (rw) {
32                 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
33                                    TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
34                                    O_RDWR | O_CREAT, 0644);
35         }
36         else {
37                 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
38                                    TDB_DEFAULT, O_RDONLY, 0);
39         }
40
41         if (tdb == NULL) {
42                 DEBUG(0, ("Could not open connections.tdb: %s\n",
43                           strerror(errno)));
44         }
45
46         return tdb;
47 }
48
49 struct conn_traverse_state {
50         int (*fn)(TDB_CONTEXT *tdb,
51                   const struct connections_key *key,
52                   const struct connections_data *data,
53                   void *private_data);
54         void *private_data;
55 };
56
57 static int conn_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key,
58                             TDB_DATA data, void *private_data)
59 {
60         struct conn_traverse_state *state =
61                 (struct conn_traverse_state *)private_data;
62
63         if ((key.dsize != sizeof(struct connections_key))
64             || (data.dsize != sizeof(struct connections_data))) {
65                 return 0;
66         }
67
68         return state->fn(
69                 tdb, (const struct connections_key *)key.dptr,
70                 (const struct connections_data *)data.dptr,
71                 state->private_data);
72 }
73
74 int connections_traverse(int (*fn)(TDB_CONTEXT *tdb, TDB_DATA key,
75                                    TDB_DATA data, void *private_data),
76                          void *private_data)
77 {
78         TDB_CONTEXT *tdb = conn_tdb_ctx(True);
79
80         if (tdb == NULL) {
81                 DEBUG(5, ("Could not open connections.tdb r/w, trying r/o\n"));
82                 tdb = conn_tdb_ctx(False);
83         }
84
85         if (tdb == NULL) {
86                 return -1;
87         }
88
89         return tdb_traverse(tdb, fn, private_data);
90 }
91
92 int connections_forall(int (*fn)(TDB_CONTEXT *tdb,
93                                  const struct connections_key *key,
94                                  const struct connections_data *data,
95                                  void *private_data),
96                        void *private_data)
97 {
98         struct conn_traverse_state state;
99
100         state.fn = fn;
101         state.private_data = private_data;
102
103         return connections_traverse(conn_traverse_fn, (void *)&state);
104 }
105
106 BOOL connections_init(BOOL rw)
107 {
108         return (conn_tdb_ctx(rw) != NULL);
109 }