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