0
|
1 /* Database access routines
|
|
2 Copyright (C) 1996, William M. Perry
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not in FSF. */
|
|
22
|
|
23 /* Written by Bill Perry */
|
185
|
24 /* Substantially rewritten by Martin Buchholz */
|
207
|
25 /* db 2.x support added by Andreas Jaeger */
|
0
|
26
|
|
27 #include <config.h>
|
|
28 #include "lisp.h"
|
272
|
29 #include "sysfile.h"
|
0
|
30 #include <errno.h>
|
|
31
|
149
|
32 #ifndef HAVE_DATABASE
|
272
|
33 #error HAVE_DATABASE not defined!!
|
|
34 #endif
|
149
|
35
|
157
|
36 #include "database.h" /* Our include file */
|
149
|
37
|
0
|
38 #ifdef HAVE_BERKELEY_DB
|
149
|
39 /* Work around Berkeley DB's use of int types which are defined
|
|
40 slightly differently in the not quite yet standard <inttypes.h>.
|
|
41 See db.h for details of why we're resorting to this... */
|
207
|
42 /* glibc 2.1 doesn't have this problem with DB 2.x */
|
|
43 #if !(defined __GLIBC__ && __GLIBC_MINOR__ >= 1)
|
149
|
44 #ifdef HAVE_INTTYPES_H
|
|
45 #define __BIT_TYPES_DEFINED__
|
|
46 #include <inttypes.h>
|
|
47 typedef uint8_t u_int8_t;
|
|
48 typedef uint16_t u_int16_t;
|
|
49 typedef uint32_t u_int32_t;
|
|
50 #ifdef WE_DONT_NEED_QUADS
|
|
51 typedef uint64_t u_int64_t;
|
|
52 #endif /* WE_DONT_NEED_QUADS */
|
|
53 #endif /* HAVE_INTTYPES_H */
|
207
|
54 #endif /* !(defined __GLIBC__ && __GLIBC_MINOR__ >= 1) */
|
149
|
55 #include DB_H_PATH /* Berkeley db's header file */
|
207
|
56 #ifndef DB_VERSION_MAJOR
|
|
57 # define DB_VERSION_MAJOR 1
|
|
58 #endif /* DB_VERSION_MAJOR */
|
0
|
59 Lisp_Object Qberkeley_db;
|
272
|
60 Lisp_Object Qhash, Qbtree, Qrecno, Qunknown;
|
149
|
61 #endif /* HAVE_BERKELEY_DB */
|
|
62
|
|
63 #ifdef HAVE_DBM
|
|
64 #include <ndbm.h>
|
|
65 Lisp_Object Qdbm;
|
|
66 #endif /* HAVE_DBM */
|
|
67
|
|
68 Lisp_Object Qdatabasep;
|
0
|
69
|
207
|
70 typedef enum { DB_DBM, DB_BERKELEY, DB_IS_UNKNOWN } XEMACS_DB_TYPE;
|
0
|
71
|
272
|
72 struct Lisp_Database;
|
0
|
73
|
149
|
74 typedef struct
|
0
|
75 {
|
272
|
76 Lisp_Object (*get_subtype) (struct Lisp_Database *);
|
|
77 Lisp_Object (*get_type) (struct Lisp_Database *);
|
|
78 Lisp_Object (*get) (struct Lisp_Database *, Lisp_Object);
|
|
79 int (*put) (struct Lisp_Database *, Lisp_Object, Lisp_Object, Lisp_Object);
|
|
80 int (*rem) (struct Lisp_Database *, Lisp_Object);
|
|
81 void (*map) (struct Lisp_Database *, Lisp_Object);
|
|
82 void (*close) (struct Lisp_Database *);
|
|
83 Lisp_Object (*last_error) (struct Lisp_Database *);
|
0
|
84 } DB_FUNCS;
|
|
85
|
272
|
86 struct Lisp_Database
|
0
|
87 {
|
|
88 struct lcrecord_header header;
|
|
89 Lisp_Object fname;
|
|
90 XEMACS_DB_TYPE type;
|
|
91 int mode;
|
173
|
92 int access_;
|
94
|
93 int dberrno;
|
185
|
94 int live_p;
|
|
95 #ifdef HAVE_DBM
|
|
96 DBM *dbm_handle;
|
|
97 #endif
|
|
98 #ifdef HAVE_BERKELEY_DB
|
|
99 DB *db_handle;
|
|
100 #endif
|
0
|
101 DB_FUNCS *funcs;
|
149
|
102 #ifdef MULE
|
|
103 Lisp_Object coding_system;
|
|
104 #endif
|
0
|
105 };
|
|
106
|
272
|
107 #define XDATABASE(x) XRECORD (x, database, struct Lisp_Database)
|
0
|
108 #define XSETDATABASE(x, p) XSETRECORD (x, p, database)
|
|
109 #define DATABASEP(x) RECORDP (x, database)
|
|
110 #define GC_DATABASEP(x) GC_RECORDP (x, database)
|
|
111 #define CHECK_DATABASE(x) CHECK_RECORD (x, database)
|
209
|
112 #define CONCHECK_DATABASE(x) CONCHECK_RECORD (x, database)
|
185
|
113 #define DATABASE_LIVE_P(x) (x->live_p)
|
0
|
114
|
185
|
115 #define CHECK_LIVE_DATABASE(db) do { \
|
272
|
116 CHECK_DATABASE (db); \
|
185
|
117 if (!DATABASE_LIVE_P (XDATABASE(db))) \
|
|
118 signal_simple_error ("Attempting to access closed database", db); \
|
|
119 } while (0)
|
|
120
|
|
121
|
272
|
122 static struct Lisp_Database *
|
209
|
123 allocate_database (void)
|
0
|
124 {
|
272
|
125 struct Lisp_Database *db =
|
|
126 alloc_lcrecord_type (struct Lisp_Database, lrecord_database);
|
0
|
127
|
272
|
128 db->fname = Qnil;
|
|
129 db->live_p = 0;
|
185
|
130 #ifdef HAVE_BERKELEY_DB
|
272
|
131 db->db_handle = NULL;
|
185
|
132 #endif
|
|
133 #ifdef HAVE_DBM
|
272
|
134 db->dbm_handle = NULL;
|
185
|
135 #endif
|
272
|
136 db->access_ = 0;
|
|
137 db->mode = 0;
|
|
138 db->dberrno = 0;
|
|
139 db->type = DB_IS_UNKNOWN;
|
149
|
140 #ifdef MULE
|
272
|
141 db->coding_system = Fget_coding_system (Qbinary);
|
149
|
142 #endif
|
272
|
143 return db;
|
0
|
144 }
|
|
145
|
|
146 static Lisp_Object
|
|
147 mark_database (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
148 {
|
272
|
149 struct Lisp_Database *db = XDATABASE (obj);
|
0
|
150
|
272
|
151 ((markobj) (db->fname));
|
173
|
152 return Qnil;
|
0
|
153 }
|
|
154
|
|
155 static void
|
|
156 print_database (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
157 {
|
209
|
158 char buf[64];
|
272
|
159 struct Lisp_Database *db = XDATABASE (obj);
|
0
|
160
|
|
161 if (print_readably)
|
272
|
162 error ("printing unreadable object #<database 0x%x>", db->header.uid);
|
209
|
163
|
|
164 write_c_string ("#<database \"", printcharfun);
|
272
|
165 print_internal (db->fname, printcharfun, 0);
|
209
|
166 sprintf (buf, "\" (%s/%s/%s) 0x%x>",
|
272
|
167 (char *) string_data (XSYMBOL (db->funcs->get_type (db))->name),
|
|
168 (char *) string_data (XSYMBOL (db->funcs->get_subtype (db))->name),
|
|
169 (!DATABASE_LIVE_P (db) ? "closed" :
|
|
170 (db->access_ & O_WRONLY) ? "writeonly" :
|
|
171 (db->access_ & O_RDWR) ? "readwrite" : "readonly"),
|
|
172 db->header.uid);
|
209
|
173 write_c_string (buf, printcharfun);
|
0
|
174 }
|
|
175
|
|
176 static void
|
|
177 finalize_database (void *header, int for_disksave)
|
|
178 {
|
272
|
179 struct Lisp_Database *db = (struct Lisp_Database *) header;
|
0
|
180
|
|
181 if (for_disksave)
|
|
182 {
|
185
|
183 Lisp_Object obj;
|
|
184 XSETOBJ (obj, Lisp_Type_Record, (void *) db);
|
|
185
|
0
|
186 signal_simple_error
|
272
|
187 ("Can't dump an emacs containing database objects", obj);
|
0
|
188 }
|
|
189 db->funcs->close (db);
|
|
190 }
|
|
191
|
272
|
192 DEFINE_LRECORD_IMPLEMENTATION ("database", database,
|
|
193 mark_database, print_database,
|
|
194 finalize_database, 0, 0,
|
|
195 struct Lisp_Database);
|
|
196
|
203
|
197 DEFUN ("close-database", Fclose_database, 1, 1, 0, /*
|
272
|
198 Close database DATABASE.
|
20
|
199 */
|
272
|
200 (database))
|
0
|
201 {
|
272
|
202 struct Lisp_Database *db;
|
|
203 CHECK_LIVE_DATABASE (database);
|
|
204 db = XDATABASE (database);
|
|
205 db->funcs->close (db);
|
|
206 db->live_p = 0;
|
173
|
207 return Qnil;
|
0
|
208 }
|
|
209
|
20
|
210 DEFUN ("database-type", Fdatabase_type, 1, 1, 0, /*
|
272
|
211 Return the type of database DATABASE.
|
20
|
212 */
|
272
|
213 (database))
|
0
|
214 {
|
272
|
215 CHECK_DATABASE (database);
|
0
|
216
|
272
|
217 return XDATABASE (database)->funcs->get_type (XDATABASE (database));
|
0
|
218 }
|
|
219
|
20
|
220 DEFUN ("database-subtype", Fdatabase_subtype, 1, 1, 0, /*
|
272
|
221 Return the subtype of database DATABASE, if any.
|
20
|
222 */
|
272
|
223 (database))
|
0
|
224 {
|
272
|
225 CHECK_DATABASE (database);
|
185
|
226
|
272
|
227 return XDATABASE (database)->funcs->get_subtype (XDATABASE (database));
|
0
|
228 }
|
|
229
|
20
|
230 DEFUN ("database-live-p", Fdatabase_live_p, 1, 1, 0, /*
|
272
|
231 Return t if OBJ is an active database.
|
20
|
232 */
|
|
233 (obj))
|
0
|
234 {
|
272
|
235 return DATABASEP (obj) && DATABASE_LIVE_P (XDATABASE (obj)) ? Qt : Qnil;
|
0
|
236 }
|
|
237
|
20
|
238 DEFUN ("database-file-name", Fdatabase_file_name, 1, 1, 0, /*
|
272
|
239 Return the filename associated with the database DATABASE.
|
20
|
240 */
|
272
|
241 (database))
|
0
|
242 {
|
272
|
243 CHECK_DATABASE (database);
|
185
|
244
|
272
|
245 return XDATABASE (database)->fname;
|
0
|
246 }
|
|
247
|
20
|
248 DEFUN ("databasep", Fdatabasep, 1, 1, 0, /*
|
272
|
249 Return t if OBJ is a database.
|
20
|
250 */
|
|
251 (obj))
|
0
|
252 {
|
173
|
253 return DATABASEP (obj) ? Qt : Qnil;
|
0
|
254 }
|
|
255
|
|
256 #ifdef HAVE_DBM
|
|
257 static void
|
272
|
258 dbm_map (struct Lisp_Database *db, Lisp_Object func)
|
0
|
259 {
|
|
260 datum keydatum, valdatum;
|
|
261 Lisp_Object key, val;
|
|
262
|
185
|
263 for (keydatum = dbm_firstkey (db->dbm_handle);
|
0
|
264 keydatum.dptr != NULL;
|
185
|
265 keydatum = dbm_nextkey (db->dbm_handle))
|
0
|
266 {
|
185
|
267 valdatum = dbm_fetch (db->dbm_handle, keydatum);
|
0
|
268 key = make_string ((unsigned char *) keydatum.dptr, keydatum.dsize);
|
|
269 val = make_string ((unsigned char *) valdatum.dptr, valdatum.dsize);
|
|
270 call2 (func, key, val);
|
|
271 }
|
|
272 }
|
|
273
|
|
274 static Lisp_Object
|
272
|
275 dbm_get (struct Lisp_Database *db, Lisp_Object key)
|
0
|
276 {
|
|
277 datum keydatum, valdatum;
|
185
|
278
|
14
|
279 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
280 keydatum.dsize = XSTRING_LENGTH (key);
|
185
|
281 valdatum = dbm_fetch (db->dbm_handle, keydatum);
|
0
|
282
|
|
283 return (valdatum.dptr
|
|
284 ? make_string ((unsigned char *) valdatum.dptr, valdatum.dsize)
|
|
285 : Qnil);
|
|
286 }
|
|
287
|
|
288 static int
|
272
|
289 dbm_put (struct Lisp_Database *db,
|
185
|
290 Lisp_Object key, Lisp_Object val, Lisp_Object replace)
|
0
|
291 {
|
|
292 datum keydatum, valdatum;
|
185
|
293
|
14
|
294 valdatum.dptr = (char *) XSTRING_DATA (val);
|
|
295 valdatum.dsize = XSTRING_LENGTH (val);
|
|
296 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
297 keydatum.dsize = XSTRING_LENGTH (key);
|
0
|
298
|
185
|
299 return !dbm_store (db->dbm_handle, keydatum, valdatum,
|
|
300 NILP (replace) ? DBM_INSERT : DBM_REPLACE);
|
0
|
301 }
|
|
302
|
|
303 static int
|
272
|
304 dbm_remove (struct Lisp_Database *db, Lisp_Object key)
|
0
|
305 {
|
|
306 datum keydatum;
|
185
|
307
|
14
|
308 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
309 keydatum.dsize = XSTRING_LENGTH (key);
|
185
|
310
|
|
311 return dbm_delete (db->dbm_handle, keydatum);
|
0
|
312 }
|
|
313
|
|
314 static Lisp_Object
|
272
|
315 dbm_type (struct Lisp_Database *db)
|
0
|
316 {
|
173
|
317 return Qdbm;
|
0
|
318 }
|
|
319
|
272
|
320 static Lisp_Object
|
|
321 dbm_subtype (struct Lisp_Database *db)
|
0
|
322 {
|
272
|
323 return Qnil;
|
0
|
324 }
|
|
325
|
|
326 static Lisp_Object
|
272
|
327 dbm_lasterr (struct Lisp_Database *db)
|
0
|
328 {
|
272
|
329 return lisp_strerror (db->dberrno);
|
0
|
330 }
|
|
331
|
|
332 static void
|
272
|
333 dbm_closeit (struct Lisp_Database *db)
|
0
|
334 {
|
185
|
335 if (db->dbm_handle)
|
|
336 {
|
|
337 dbm_close (db->dbm_handle);
|
|
338 db->dbm_handle = NULL;
|
|
339 }
|
0
|
340 }
|
|
341
|
|
342 static DB_FUNCS ndbm_func_block =
|
|
343 {
|
|
344 dbm_subtype,
|
|
345 dbm_type,
|
|
346 dbm_get,
|
|
347 dbm_put,
|
|
348 dbm_remove,
|
|
349 dbm_map,
|
|
350 dbm_closeit,
|
|
351 dbm_lasterr
|
|
352 };
|
185
|
353 #endif /* HAVE_DBM */
|
0
|
354
|
|
355 #ifdef HAVE_BERKELEY_DB
|
|
356 static Lisp_Object
|
272
|
357 berkdb_type (struct Lisp_Database *db)
|
0
|
358 {
|
173
|
359 return Qberkeley_db;
|
0
|
360 }
|
|
361
|
272
|
362 static Lisp_Object
|
|
363 berkdb_subtype (struct Lisp_Database *db)
|
0
|
364 {
|
185
|
365 if (!db->db_handle)
|
272
|
366 return Qnil;
|
0
|
367
|
185
|
368 switch (db->db_handle->type)
|
|
369 {
|
272
|
370 case DB_BTREE: return Qbtree;
|
|
371 case DB_HASH: return Qhash;
|
|
372 case DB_RECNO: return Qrecno;
|
|
373 default: return Qunknown;
|
185
|
374 }
|
0
|
375 }
|
|
376
|
|
377 static Lisp_Object
|
272
|
378 berkdb_lasterr (struct Lisp_Database *db)
|
0
|
379 {
|
272
|
380 return lisp_strerror (db->dberrno);
|
0
|
381 }
|
|
382
|
|
383 static Lisp_Object
|
272
|
384 berkdb_get (struct Lisp_Database *db, Lisp_Object key)
|
0
|
385 {
|
185
|
386 /* #### Needs mule-izing */
|
0
|
387 DBT keydatum, valdatum;
|
|
388 int status = 0;
|
|
389
|
207
|
390 #if DB_VERSION_MAJOR == 2
|
|
391 /* Always initialize keydatum, valdatum. */
|
272
|
392 xzero (keydatum);
|
|
393 xzero (valdatum);
|
207
|
394 #endif /* DV_VERSION_MAJOR = 2 */
|
|
395
|
14
|
396 keydatum.data = XSTRING_DATA (key);
|
|
397 keydatum.size = XSTRING_LENGTH (key);
|
185
|
398
|
207
|
399 #if DB_VERSION_MAJOR == 1
|
185
|
400 status = db->db_handle->get (db->db_handle, &keydatum, &valdatum, 0);
|
207
|
401 #else
|
|
402 status = db->db_handle->get (db->db_handle, NULL, &keydatum, &valdatum, 0);
|
|
403 #endif /* DB_VERSION_MAJOR */
|
0
|
404
|
|
405 if (!status)
|
185
|
406 return make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
0
|
407
|
207
|
408 #if DB_VERSION_MAJOR == 1
|
94
|
409 db->dberrno = (status == 1) ? -1 : errno;
|
207
|
410 #else
|
|
411 db->dberrno = (status < 0) ? -1 : errno;
|
|
412 #endif /* DB_VERSION_MAJOR */
|
|
413
|
173
|
414 return Qnil;
|
0
|
415 }
|
|
416
|
|
417 static int
|
272
|
418 berkdb_put (struct Lisp_Database *db,
|
0
|
419 Lisp_Object key,
|
|
420 Lisp_Object val,
|
|
421 Lisp_Object replace)
|
|
422 {
|
|
423 DBT keydatum, valdatum;
|
|
424 int status = 0;
|
|
425
|
207
|
426 #if DB_VERSION_MAJOR == 2
|
|
427 /* Always initalize keydatum, valdatum. */
|
272
|
428 xzero (keydatum);
|
|
429 xzero (valdatum);
|
207
|
430 #endif /* DV_VERSION_MAJOR = 2 */
|
|
431
|
14
|
432 keydatum.data = XSTRING_DATA (key);
|
|
433 keydatum.size = XSTRING_LENGTH (key);
|
|
434 valdatum.data = XSTRING_DATA (val);
|
|
435 valdatum.size = XSTRING_LENGTH (val);
|
207
|
436 #if DB_VERSION_MAJOR == 1
|
185
|
437 status = db->db_handle->put (db->db_handle, &keydatum, &valdatum,
|
207
|
438 NILP (replace) ? R_NOOVERWRITE : 0);
|
94
|
439 db->dberrno = (status == 1) ? -1 : errno;
|
207
|
440 #else
|
|
441 status = db->db_handle->put (db->db_handle, NULL, &keydatum, &valdatum,
|
|
442 NILP (replace) ? DB_NOOVERWRITE : 0);
|
|
443 db->dberrno = (status < 0) ? -1 : errno;
|
|
444 #endif/* DV_VERSION_MAJOR = 2 */
|
|
445
|
0
|
446 return status;
|
|
447 }
|
|
448
|
|
449 static int
|
272
|
450 berkdb_remove (struct Lisp_Database *db, Lisp_Object key)
|
0
|
451 {
|
|
452 DBT keydatum;
|
|
453 int status;
|
|
454
|
207
|
455 #if DB_VERSION_MAJOR == 2
|
|
456 /* Always initialize keydatum. */
|
272
|
457 xzero (keydatum);
|
207
|
458 #endif /* DV_VERSION_MAJOR = 2 */
|
|
459
|
14
|
460 keydatum.data = XSTRING_DATA (key);
|
|
461 keydatum.size = XSTRING_LENGTH (key);
|
185
|
462
|
207
|
463 #if DB_VERSION_MAJOR == 1
|
185
|
464 status = db->db_handle->del (db->db_handle, &keydatum, 0);
|
207
|
465 #else
|
|
466 status = db->db_handle->del (db->db_handle, NULL, &keydatum, 0);
|
|
467 #endif /* DB_VERSION_MAJOR */
|
|
468
|
0
|
469 if (!status)
|
|
470 return 0;
|
185
|
471
|
207
|
472 #if DB_VERSION_MAJOR == 1
|
94
|
473 db->dberrno = (status == 1) ? -1 : errno;
|
207
|
474 #else
|
|
475 db->dberrno = (status < 0) ? -1 : errno;
|
|
476 #endif /* DB_VERSION_MAJOR */
|
|
477
|
0
|
478 return 1;
|
|
479 }
|
|
480
|
|
481 static void
|
272
|
482 berkdb_map (struct Lisp_Database *db, Lisp_Object func)
|
0
|
483 {
|
|
484 DBT keydatum, valdatum;
|
|
485 Lisp_Object key, val;
|
185
|
486 DB *dbp = db->db_handle;
|
0
|
487 int status;
|
|
488
|
207
|
489 #if DB_VERSION_MAJOR == 1
|
0
|
490 for (status = dbp->seq (dbp, &keydatum, &valdatum, R_FIRST);
|
|
491 status == 0;
|
|
492 status = dbp->seq (dbp, &keydatum, &valdatum, R_NEXT))
|
|
493 {
|
185
|
494 /* ### Needs mule-izing */
|
|
495 key = make_string ((Bufbyte *) keydatum.data, keydatum.size);
|
|
496 val = make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
0
|
497 call2 (func, key, val);
|
|
498 }
|
207
|
499 #else
|
|
500 DBC *dbcp;
|
|
501 /* Initialize the key/data pair so the flags aren't set. */
|
272
|
502 xzero (keydatum);
|
|
503 xzero (valdatum);
|
207
|
504
|
335
|
505 #if DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >=6
|
|
506 status = dbp->cursor (dbp, NULL, &dbcp, 0);
|
|
507 #else
|
207
|
508 status = dbp->cursor (dbp, NULL, &dbcp);
|
335
|
509 #endif
|
207
|
510 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
|
|
511 status == 0;
|
|
512 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
|
|
513 {
|
|
514 /* ### Needs mule-izing */
|
|
515 key = make_string ((Bufbyte *) keydatum.data, keydatum.size);
|
|
516 val = make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
|
517 call2 (func, key, val);
|
|
518 }
|
|
519 dbcp->c_close (dbcp);
|
|
520 #endif /* DB_VERSION_MAJOR */
|
0
|
521 }
|
|
522
|
|
523 static void
|
272
|
524 berkdb_close (struct Lisp_Database *db)
|
0
|
525 {
|
185
|
526 if (db->db_handle)
|
0
|
527 {
|
207
|
528 #if DB_VERSION_MAJOR == 1
|
185
|
529 db->db_handle->sync (db->db_handle, 0);
|
|
530 db->db_handle->close (db->db_handle);
|
207
|
531 #else
|
|
532 db->db_handle->sync (db->db_handle, 0);
|
|
533 db->db_handle->close (db->db_handle, 0);
|
|
534 #endif /* DB_VERSION_MAJOR */
|
185
|
535 db->db_handle = NULL;
|
0
|
536 }
|
|
537 }
|
|
538
|
|
539 static DB_FUNCS berk_func_block =
|
|
540 {
|
|
541 berkdb_subtype,
|
|
542 berkdb_type,
|
|
543 berkdb_get,
|
|
544 berkdb_put,
|
|
545 berkdb_remove,
|
|
546 berkdb_map,
|
|
547 berkdb_close,
|
|
548 berkdb_lasterr
|
|
549 };
|
185
|
550 #endif /* HAVE_BERKELEY_DB */
|
0
|
551
|
203
|
552 DEFUN ("database-last-error", Fdatabase_last_error, 0, 1, 0, /*
|
272
|
553 Return the last error associated with DATABASE.
|
20
|
554 */
|
272
|
555 (database))
|
0
|
556 {
|
272
|
557 if (NILP (database))
|
151
|
558 return lisp_strerror (errno);
|
185
|
559
|
272
|
560 CHECK_DATABASE (database);
|
185
|
561
|
272
|
562 return XDATABASE (database)->funcs->last_error (XDATABASE (database));
|
0
|
563 }
|
|
564
|
203
|
565 DEFUN ("open-database", Fopen_database, 1, 5, 0, /*
|
272
|
566 Return a new database object opened on FILE.
|
|
567 Optional arguments TYPE and SUBTYPE specify the database type.
|
|
568 Optional argument ACCESS specifies the access rights, which may be any
|
0
|
569 combination of 'r' 'w' and '+', for read, write, and creation flags.
|
272
|
570 Optional argument MODE gives the permissions to use when opening FILE,
|
|
571 and defaults to 0755.
|
20
|
572 */
|
173
|
573 (file, type, subtype, access_, mode))
|
0
|
574 {
|
187
|
575 /* This function can GC */
|
0
|
576 int modemask;
|
|
577 int accessmask = 0;
|
272
|
578 struct Lisp_Database *db = NULL;
|
185
|
579 char *filename;
|
187
|
580 struct gcpro gcpro1, gcpro2;
|
0
|
581
|
70
|
582 CHECK_STRING (file);
|
187
|
583 GCPRO2 (file, access_);
|
|
584 file = Fexpand_file_name (file, Qnil);
|
|
585 UNGCPRO;
|
185
|
586 filename = (char *) XSTRING_DATA (file);
|
0
|
587
|
173
|
588 if (NILP (access_))
|
0
|
589 {
|
|
590 accessmask = O_RDWR | O_CREAT;
|
|
591 }
|
|
592 else
|
|
593 {
|
|
594 char *acc;
|
173
|
595 CHECK_STRING (access_);
|
|
596 acc = (char *) XSTRING_DATA (access_);
|
185
|
597
|
0
|
598 if (strchr (acc, '+'))
|
|
599 accessmask |= O_CREAT;
|
185
|
600
|
272
|
601 {
|
|
602 char *rp = strchr (acc, 'r');
|
|
603 char *wp = strchr (acc, 'w');
|
|
604 if (rp && wp) accessmask |= O_RDWR;
|
|
605 else if (wp) accessmask |= O_WRONLY;
|
|
606 else accessmask |= O_RDONLY;
|
|
607 }
|
0
|
608 }
|
|
609
|
|
610 if (NILP (mode))
|
|
611 {
|
185
|
612 modemask = 0755; /* rwxr-xr-x */
|
0
|
613 }
|
|
614 else
|
|
615 {
|
|
616 CHECK_INT (mode);
|
|
617 modemask = XINT (mode);
|
|
618 }
|
|
619
|
|
620 #ifdef HAVE_DBM
|
|
621 if (NILP (type) || EQ (type, Qdbm))
|
|
622 {
|
272
|
623 DBM *dbase = dbm_open (filename, accessmask, modemask);
|
|
624 if (!dbase)
|
185
|
625 return Qnil;
|
|
626
|
272
|
627 db = allocate_database ();
|
|
628 db->dbm_handle = dbase;
|
|
629 db->type = DB_DBM;
|
|
630 db->funcs = &ndbm_func_block;
|
0
|
631 goto db_done;
|
|
632 }
|
185
|
633 #endif /* HAVE_DBM */
|
0
|
634
|
|
635 #ifdef HAVE_BERKELEY_DB
|
|
636 if (NILP (type) || EQ (type, Qberkeley_db))
|
|
637 {
|
185
|
638 DBTYPE real_subtype;
|
272
|
639 DB *dbase;
|
211
|
640 #if DB_VERSION_MAJOR != 1
|
207
|
641 int status;
|
211
|
642 #endif
|
|
643
|
185
|
644 if (EQ (subtype, Qhash) || NILP (subtype))
|
|
645 real_subtype = DB_HASH;
|
|
646 else if (EQ (subtype, Qbtree))
|
|
647 real_subtype = DB_BTREE;
|
|
648 else if (EQ (subtype, Qrecno))
|
|
649 real_subtype = DB_RECNO;
|
|
650 else
|
|
651 signal_simple_error ("Unsupported subtype", subtype);
|
|
652
|
207
|
653 #if DB_VERSION_MAJOR == 1
|
272
|
654 dbase = dbopen (filename, accessmask, modemask, real_subtype, NULL);
|
|
655 if (!dbase)
|
185
|
656 return Qnil;
|
207
|
657 #else
|
|
658 /* Berkeley DB Version 2 has only the accessmask DB_CREATE and DB_RDONLY,
|
|
659 other flags shouldn't be set */
|
|
660 if (NILP (access_))
|
272
|
661 accessmask = DB_CREATE;
|
207
|
662 else
|
|
663 {
|
|
664 char *acc;
|
|
665 CHECK_STRING (access_);
|
|
666 acc = (char *) XSTRING_DATA (access_);
|
|
667 accessmask = 0;
|
272
|
668
|
207
|
669 if (strchr (acc, '+'))
|
|
670 accessmask |= DB_CREATE;
|
185
|
671
|
207
|
672 if (strchr (acc, 'r') && !strchr (acc, 'w'))
|
|
673 accessmask |= DB_RDONLY;
|
|
674 }
|
272
|
675 status = db_open (filename, real_subtype, accessmask,
|
|
676 modemask, NULL , NULL, &dbase);
|
207
|
677 if (status)
|
|
678 return Qnil;
|
|
679 #endif /* DB_VERSION_MAJOR */
|
272
|
680
|
|
681 db = allocate_database ();
|
|
682 db->db_handle = dbase;
|
|
683 db->type = DB_BERKELEY;
|
|
684 db->funcs = &berk_func_block;
|
0
|
685 goto db_done;
|
|
686 }
|
185
|
687 #endif /* HAVE_BERKELEY_DB */
|
|
688
|
0
|
689 signal_simple_error ("Unsupported database type", type);
|
173
|
690 return Qnil;
|
0
|
691
|
|
692 db_done:
|
272
|
693 db->live_p = 1;
|
|
694 db->fname = file;
|
|
695 db->mode = modemask;
|
|
696 db->access_ = accessmask;
|
0
|
697
|
272
|
698 {
|
|
699 Lisp_Object retval;
|
|
700 XSETDATABASE (retval, db);
|
|
701 return retval;
|
|
702 }
|
0
|
703 }
|
|
704
|
203
|
705 DEFUN ("put-database", Fput_database, 3, 4, 0, /*
|
272
|
706 Store KEY and VALUE in DATABASE.
|
|
707 If optional fourth arg REPLACE is non-nil,
|
|
708 replace any existing entry in the database.
|
20
|
709 */
|
272
|
710 (key, value, database, replace))
|
0
|
711 {
|
272
|
712 CHECK_LIVE_DATABASE (database);
|
0
|
713 CHECK_STRING (key);
|
272
|
714 CHECK_STRING (value);
|
185
|
715 {
|
272
|
716 struct Lisp_Database *db = XDATABASE (database);
|
|
717 int status = db->funcs->put (db, key, value, replace);
|
185
|
718 return status ? Qt : Qnil;
|
|
719 }
|
0
|
720 }
|
|
721
|
203
|
722 DEFUN ("remove-database", Fremove_database, 2, 2, 0, /*
|
0
|
723 Remove KEY from DATABASE.
|
20
|
724 */
|
272
|
725 (key, database))
|
0
|
726 {
|
272
|
727 CHECK_LIVE_DATABASE (database);
|
0
|
728 CHECK_STRING (key);
|
272
|
729 {
|
|
730 struct Lisp_Database *db = XDATABASE (database);
|
|
731 int status = db->funcs->rem (db, key);
|
|
732 return status ? Qt : Qnil;
|
|
733 }
|
0
|
734 }
|
185
|
735
|
203
|
736 DEFUN ("get-database", Fget_database, 2, 3, 0, /*
|
272
|
737 Return value for KEY in DATABASE.
|
0
|
738 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
739 */
|
272
|
740 (key, database, default_))
|
0
|
741 {
|
272
|
742 CHECK_LIVE_DATABASE (database);
|
0
|
743 CHECK_STRING (key);
|
185
|
744 {
|
272
|
745 struct Lisp_Database *db = XDATABASE (database);
|
|
746 Lisp_Object retval = db->funcs->get (db, key);
|
185
|
747 return NILP (retval) ? default_ : retval;
|
|
748 }
|
0
|
749 }
|
|
750
|
20
|
751 DEFUN ("map-database", Fmapdatabase, 2, 2, 0, /*
|
0
|
752 Map FUNCTION over entries in DATABASE, calling it with two args,
|
|
753 each key and value in the database.
|
20
|
754 */
|
272
|
755 (function, database))
|
0
|
756 {
|
272
|
757 CHECK_LIVE_DATABASE (database);
|
0
|
758
|
272
|
759 XDATABASE (database)->funcs->map (XDATABASE (database), function);
|
185
|
760
|
0
|
761 return Qnil;
|
|
762 }
|
|
763
|
|
764 void
|
272
|
765 syms_of_database (void)
|
0
|
766 {
|
|
767 defsymbol (&Qdatabasep, "databasep");
|
|
768 #ifdef HAVE_DBM
|
|
769 defsymbol (&Qdbm, "dbm");
|
|
770 #endif
|
|
771 #ifdef HAVE_BERKELEY_DB
|
|
772 defsymbol (&Qberkeley_db, "berkeley-db");
|
|
773 defsymbol (&Qhash, "hash");
|
|
774 defsymbol (&Qbtree, "btree");
|
|
775 defsymbol (&Qrecno, "recno");
|
272
|
776 defsymbol (&Qunknown, "unknown");
|
0
|
777 #endif
|
|
778
|
203
|
779 DEFSUBR (Fopen_database);
|
20
|
780 DEFSUBR (Fdatabasep);
|
|
781 DEFSUBR (Fmapdatabase);
|
203
|
782 DEFSUBR (Fput_database);
|
|
783 DEFSUBR (Fget_database);
|
|
784 DEFSUBR (Fremove_database);
|
20
|
785 DEFSUBR (Fdatabase_type);
|
|
786 DEFSUBR (Fdatabase_subtype);
|
203
|
787 DEFSUBR (Fdatabase_last_error);
|
20
|
788 DEFSUBR (Fdatabase_live_p);
|
|
789 DEFSUBR (Fdatabase_file_name);
|
203
|
790 DEFSUBR (Fclose_database);
|
0
|
791 }
|
|
792
|
|
793 void
|
272
|
794 vars_of_database (void)
|
0
|
795 {
|
|
796 #ifdef HAVE_DBM
|
|
797 Fprovide (Qdbm);
|
|
798 #endif
|
|
799 #ifdef HAVE_BERKELEY_DB
|
|
800 Fprovide (Qberkeley_db);
|
|
801 #endif
|
|
802 }
|