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