1 /****************************************************************************
2 * Copyright (c) 2006-2011,2013 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey 2006-on *
31 ****************************************************************************/
33 #include <curses.priv.h>
35 #include <hashed_db.h>
39 MODULE_ID("$Id: hashed_db.c,v 1.17 2013/12/15 00:33:01 tom Exp $")
41 #if HASHED_DB_API >= 2
45 typedef struct _myconn {
52 static MYCONN *connections;
57 while (connections != 0) {
58 _nc_db_close(connections->db);
63 find_connection(const char *path, bool modify)
68 for (p = connections; p != 0; p = p->next) {
69 if (!strcmp(p->path, path) && p->modify == modify) {
79 drop_connection(DB * db)
83 for (p = connections, q = 0; p != 0; q = p, p = p->next) {
88 connections = p->next;
97 make_connection(DB * db, const char *path, bool modify)
99 MYCONN *p = typeCalloc(MYCONN, 1);
103 p->path = strdup(path);
106 p->next = connections;
118 _nc_db_open(const char *path, bool modify)
123 if (connections == 0)
126 if ((result = find_connection(path, modify)) == 0) {
128 #if HASHED_DB_API >= 4
129 db_create(&result, NULL, 0);
130 if ((code = result->open(result,
135 modify ? DB_CREATE : DB_RDONLY,
139 #elif HASHED_DB_API >= 3
140 db_create(&result, NULL, 0);
141 if ((code = result->open(result,
145 modify ? DB_CREATE : DB_RDONLY,
149 #elif HASHED_DB_API >= 2
150 if ((code = db_open(path,
152 modify ? DB_CREATE : DB_RDONLY,
160 if ((result = dbopen(path,
161 modify ? (O_CREAT | O_RDWR) : O_RDONLY,
169 make_connection(result, path, modify);
170 T(("opened %s", path));
172 T(("cannot open %s: %s", path, strerror(code)));
179 * Close the database. Do not attempt to use the 'db' handle after this call.
182 _nc_db_close(DB * db)
187 #if HASHED_DB_API >= 2
188 result = db->close(db, 0);
190 result = db->close(db);
196 * Write a record to the database.
198 * Returns 0 on success.
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).
204 _nc_db_put(DB * db, DBT * key, DBT * data)
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);
212 result = db->put(db, key, data, R_NOOVERWRITE);
218 * Read a record from the database.
220 * Returns 0 on success.
223 _nc_db_get(DB * db, DBT * key, DBT * data)
227 memset(data, 0, sizeof(*data));
228 #if HASHED_DB_API >= 2
229 result = db->get(db, NULL, key, data, 0);
231 result = db->get(db, key, data, 0);
237 * Read the first record from the database, ignoring order.
239 * Returns 0 on success.
242 _nc_db_first(DB * db, DBT * key, DBT * data)
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);
253 result = db->seq(db, key, data, 0);
259 * Read the next record from the database, ignoring order.
261 * Returns 0 on success.
264 _nc_db_next(DB * db, DBT * key, DBT * data)
268 #if HASHED_DB_API >= 2
271 result = cursor->c_get(cursor, key, data, DB_NEXT);
276 result = db->seq(db, key, data, 0);
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.
286 _nc_db_have_index(DBT * key, DBT * data, char **buffer, int *size)
289 int used = (int) data->size - 1;
290 char *have = (char *) data->data;
297 * Update params in any case for consistency with _nc_db_have_data().
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.
309 _nc_db_have_data(DBT * key, DBT * data, char **buffer, int *size)
312 int used = (int) data->size - 1;
313 char *have = (char *) data->data;
316 if (data->size > key->size
317 && IS_TIC_MAGIC(have)) {
322 * Update params in any case to make it simple to follow a index record
323 * to the data record.
341 #endif /* USE_HASHED_DB */