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