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