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