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