1 /****************************************************************************
2  * Copyright (c) 2006-2011,2013 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                        2006-on                 *
31  ****************************************************************************/
32 
33 #include <curses.priv.h>
34 #include <tic.h>
35 #include <hashed_db.h>
36 
37 #if USE_HASHED_DB
38 
39 MODULE_ID("$Id: hashed_db.c,v 1.17 2013/12/15 00:33:01 tom Exp $")
40 
41 #if HASHED_DB_API >= 2
42 static DBC *cursor;
43 #endif
44 
45 typedef struct _myconn {
46     struct _myconn *next;
47     DB *db;
48     char *path;
49     bool modify;
50 } MYCONN;
51 
52 static MYCONN *connections;
53 
54 static void
55 cleanup(void)
56 {
57     while (connections != 0) {
58 	_nc_db_close(connections->db);
59     }
60 }
61 
62 static DB *
63 find_connection(const char *path, bool modify)
64 {
65     DB *result = 0;
66     MYCONN *p;
67 
68     for (p = connections; p != 0; p = p->next) {
69 	if (!strcmp(p->path, path) && p->modify == modify) {
70 	    result = p->db;
71 	    break;
72 	}
73     }
74 
75     return result;
76 }
77 
78 static void
79 drop_connection(DB * db)
80 {
81     MYCONN *p, *q;
82 
83     for (p = connections, q = 0; p != 0; q = p, p = p->next) {
84 	if (p->db == db) {
85 	    if (q != 0)
86 		q->next = p->next;
87 	    else
88 		connections = p->next;
89 	    free(p->path);
90 	    free(p);
91 	    break;
92 	}
93     }
94 }
95 
96 static void
97 make_connection(DB * db, const char *path, bool modify)
98 {
99     MYCONN *p = typeCalloc(MYCONN, 1);
100 
101     if (p != 0) {
102 	p->db = db;
103 	p->path = strdup(path);
104 	p->modify = modify;
105 	if (p->path != 0) {
106 	    p->next = connections;
107 	    connections = p;
108 	} else {
109 	    free(p);
110 	}
111     }
112 }
113 
114 /*
115  * Open the database.
116  */
117 NCURSES_EXPORT(DB *)
118 _nc_db_open(const char *path, bool modify)
119 {
120     DB *result = 0;
121     int code;
122 
123     if (connections == 0)
124 	atexit(cleanup);
125 
126     if ((result = find_connection(path, modify)) == 0) {
127 
128 #if HASHED_DB_API >= 4
129 	db_create(&result, NULL, 0);
130 	if ((code = result->open(result,
131 				 NULL,
132 				 path,
133 				 NULL,
134 				 DB_HASH,
135 				 modify ? DB_CREATE : DB_RDONLY,
136 				 0644)) != 0) {
137 	    result = 0;
138 	}
139 #elif HASHED_DB_API >= 3
140 	db_create(&result, NULL, 0);
141 	if ((code = result->open(result,
142 				 path,
143 				 NULL,
144 				 DB_HASH,
145 				 modify ? DB_CREATE : DB_RDONLY,
146 				 0644)) != 0) {
147 	    result = 0;
148 	}
149 #elif HASHED_DB_API >= 2
150 	if ((code = db_open(path,
151 			    DB_HASH,
152 			    modify ? DB_CREATE : DB_RDONLY,
153 			    0644,
154 			    (DB_ENV *) 0,
155 			    (DB_INFO *) 0,
156 			    &result)) != 0) {
157 	    result = 0;
158 	}
159 #else
160 	if ((result = dbopen(path,
161 			     modify ? (O_CREAT | O_RDWR) : O_RDONLY,
162 			     0644,
163 			     DB_HASH,
164 			     NULL)) == 0) {
165 	    code = errno;
166 	}
167 #endif
168 	if (result != 0) {
169 	    make_connection(result, path, modify);
170 	    T(("opened %s", path));
171 	} else {
172 	    T(("cannot open %s: %s", path, strerror(code)));
173 	}
174     }
175     return result;
176 }
177 
178 /*
179  * Close the database.  Do not attempt to use the 'db' handle after this call.
180  */
181 NCURSES_EXPORT(int)
182 _nc_db_close(DB * db)
183 {
184     int result;
185 
186     drop_connection(db);
187 #if HASHED_DB_API >= 2
188     result = db->close(db, 0);
189 #else
190     result = db->close(db);
191 #endif
192     return result;
193 }
194 
195 /*
196  * Write a record to the database.
197  *
198  * Returns 0 on success.
199  *
200  * FIXME:  the FreeBSD cap_mkdb program assumes the database could have
201  * duplicates.  There appears to be no good reason for that (review/fix).
202  */
203 NCURSES_EXPORT(int)
204 _nc_db_put(DB * db, DBT * key, DBT * data)
205 {
206     int result;
207 #if HASHED_DB_API >= 2
208     /* remove any pre-existing value, since we do not want duplicates */
209     (void) db->del(db, NULL, key, 0);
210     result = db->put(db, NULL, key, data, DB_NOOVERWRITE);
211 #else
212     result = db->put(db, key, data, R_NOOVERWRITE);
213 #endif
214     return result;
215 }
216 
217 /*
218  * Read a record from the database.
219  *
220  * Returns 0 on success.
221  */
222 NCURSES_EXPORT(int)
223 _nc_db_get(DB * db, DBT * key, DBT * data)
224 {
225     int result;
226 
227     memset(data, 0, sizeof(*data));
228 #if HASHED_DB_API >= 2
229     result = db->get(db, NULL, key, data, 0);
230 #else
231     result = db->get(db, key, data, 0);
232 #endif
233     return result;
234 }
235 
236 /*
237  * Read the first record from the database, ignoring order.
238  *
239  * Returns 0 on success.
240  */
241 NCURSES_EXPORT(int)
242 _nc_db_first(DB * db, DBT * key, DBT * data)
243 {
244     int result;
245 
246     memset(key, 0, sizeof(*key));
247     memset(data, 0, sizeof(*data));
248 #if HASHED_DB_API >= 2
249     if ((result = db->cursor(db, NULL, &cursor, 0)) == 0) {
250 	result = cursor->c_get(cursor, key, data, DB_FIRST);
251     }
252 #else
253     result = db->seq(db, key, data, 0);
254 #endif
255     return result;
256 }
257 
258 /*
259  * Read the next record from the database, ignoring order.
260  *
261  * Returns 0 on success.
262  */
263 NCURSES_EXPORT(int)
264 _nc_db_next(DB * db, DBT * key, DBT * data)
265 {
266     int result;
267 
268 #if HASHED_DB_API >= 2
269     (void) db;
270     if (cursor != 0) {
271 	result = cursor->c_get(cursor, key, data, DB_NEXT);
272     } else {
273 	result = -1;
274     }
275 #else
276     result = db->seq(db, key, data, 0);
277 #endif
278     return result;
279 }
280 
281 /*
282  * Check if a record is a terminfo index record.  Index records are those that
283  * contain only an alias pointing to a list of aliases.
284  */
285 NCURSES_EXPORT(bool)
286 _nc_db_have_index(DBT * key, DBT * data, char **buffer, int *size)
287 {
288     bool result = FALSE;
289     int used = (int) data->size - 1;
290     char *have = (char *) data->data;
291 
292     (void) key;
293     if (*have++ == 2) {
294 	result = TRUE;
295     }
296     /*
297      * Update params in any case for consistency with _nc_db_have_data().
298      */
299     *buffer = have;
300     *size = used;
301     return result;
302 }
303 
304 /*
305  * Check if a record is the terminfo data record.  Ignore index records, e.g.,
306  * those that contain only an alias pointing to a list of aliases.
307  */
308 NCURSES_EXPORT(bool)
309 _nc_db_have_data(DBT * key, DBT * data, char **buffer, int *size)
310 {
311     bool result = FALSE;
312     int used = (int) data->size - 1;
313     char *have = (char *) data->data;
314 
315     if (*have++ == 0) {
316 	if (data->size > key->size
317 	    && IS_TIC_MAGIC(have)) {
318 	    result = TRUE;
319 	}
320     }
321     /*
322      * Update params in any case to make it simple to follow a index record
323      * to the data record.
324      */
325     *buffer = have;
326     *size = used;
327     return result;
328 }
329 
330 #else
331 
332 extern
333 NCURSES_EXPORT(void)
334 _nc_hashed_db(void);
335 
336 NCURSES_EXPORT(void)
337 _nc_hashed_db(void)
338 {
339 }
340 
341 #endif /* USE_HASHED_DB */
342