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