1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 2006-2011,2013 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey 2006-on *
32 ****************************************************************************/
34 #include <curses.priv.h>
36 #include <hashed_db.h>
40 MODULE_ID("$Id: hashed_db.c,v 1.19 2020/02/02 23:34:34 tom Exp $")
42 #if HASHED_DB_API >= 2
46 typedef struct _myconn {
53 static MYCONN *connections;
58 while (connections != 0) {
59 _nc_db_close(connections->db);
64 find_connection(const char *path, bool modify)
69 for (p = connections; p != 0; p = p->next) {
70 if (!strcmp(p->path, path) && p->modify == modify) {
80 drop_connection(DB * db)
84 for (p = connections, q = 0; p != 0; q = p, p = p->next) {
89 connections = p->next;
98 make_connection(DB * db, const char *path, bool modify)
100 MYCONN *p = typeCalloc(MYCONN, 1);
104 p->path = strdup(path);
107 p->next = connections;
119 _nc_db_open(const char *path, bool modify)
124 if (connections == 0)
127 if ((result = find_connection(path, modify)) == 0) {
129 #if HASHED_DB_API >= 4
130 db_create(&result, NULL, 0);
131 if ((code = result->open(result,
136 modify ? DB_CREATE : DB_RDONLY,
140 #elif HASHED_DB_API >= 3
141 db_create(&result, NULL, 0);
142 if ((code = result->open(result,
146 modify ? DB_CREATE : DB_RDONLY,
150 #elif HASHED_DB_API >= 2
151 if ((code = db_open(path,
153 modify ? DB_CREATE : DB_RDONLY,
161 if ((result = dbopen(path,
162 modify ? (O_CREAT | O_RDWR) : O_RDONLY,
170 make_connection(result, path, modify);
171 T(("opened %s", path));
173 T(("cannot open %s: %s", path, strerror(code)));
180 * Close the database. Do not attempt to use the 'db' handle after this call.
183 _nc_db_close(DB * db)
188 #if HASHED_DB_API >= 2
189 result = db->close(db, 0);
191 result = db->close(db);
197 * Write a record to the database.
199 * Returns 0 on success.
201 * FIXME: the FreeBSD cap_mkdb program assumes the database could have
202 * duplicates. There appears to be no good reason for that (review/fix).
205 _nc_db_put(DB * db, DBT * key, DBT * data)
208 #if HASHED_DB_API >= 2
209 /* remove any pre-existing value, since we do not want duplicates */
210 (void) db->del(db, NULL, key, 0);
211 result = db->put(db, NULL, key, data, DB_NOOVERWRITE);
213 result = db->put(db, key, data, R_NOOVERWRITE);
219 * Read a record from the database.
221 * Returns 0 on success.
224 _nc_db_get(DB * db, DBT * key, DBT * data)
228 memset(data, 0, sizeof(*data));
229 #if HASHED_DB_API >= 2
230 result = db->get(db, NULL, key, data, 0);
232 result = db->get(db, key, data, 0);
238 * Read the first record from the database, ignoring order.
240 * Returns 0 on success.
243 _nc_db_first(DB * db, DBT * key, DBT * data)
247 memset(key, 0, sizeof(*key));
248 memset(data, 0, sizeof(*data));
249 #if HASHED_DB_API >= 2
250 if ((result = db->cursor(db, NULL, &cursor, 0)) == 0) {
251 result = cursor->c_get(cursor, key, data, DB_FIRST);
254 result = db->seq(db, key, data, 0);
260 * Read the next record from the database, ignoring order.
262 * Returns 0 on success.
265 _nc_db_next(DB * db, DBT * key, DBT * data)
269 #if HASHED_DB_API >= 2
272 result = cursor->c_get(cursor, key, data, DB_NEXT);
277 result = db->seq(db, key, data, R_NEXT);
283 * Check if a record is a terminfo index record. Index records are those that
284 * contain only an alias pointing to a list of aliases.
287 _nc_db_have_index(DBT * key, DBT * data, char **buffer, int *size)
290 int used = (int) data->size - 1;
291 char *have = (char *) data->data;
298 * Update params in any case for consistency with _nc_db_have_data().
306 * Check if a record is the terminfo data record. Ignore index records, e.g.,
307 * those that contain only an alias pointing to a list of aliases.
310 _nc_db_have_data(DBT * key, DBT * data, char **buffer, int *size)
313 int used = (int) data->size - 1;
314 char *have = (char *) data->data;
317 if (data->size > key->size
318 && IS_TIC_MAGIC(have)) {
323 * Update params in any case to make it simple to follow a index record
324 * to the data record.
342 #endif /* USE_HASHED_DB */