Ruby  2.5.0dev(2017-10-22revision60238)
dbm.c
Go to the documentation of this file.
1 /************************************************
2 
3  dbm.c -
4 
5  $Author$
6  created at: Mon Jan 24 15:59:52 JST 1994
7 
8  Copyright (C) 1995-2001 Yukihiro Matsumoto
9 
10 ************************************************/
11 
12 #include "ruby.h"
13 
14 #ifdef HAVE_CDEFS_H
15 # include <cdefs.h>
16 #endif
17 #ifdef HAVE_SYS_CDEFS_H
18 # include <sys/cdefs.h>
19 #endif
20 #include DBM_HDR
21 #include <fcntl.h>
22 #include <errno.h>
23 
24 #define DSIZE_TYPE TYPEOF_DATUM_DSIZE
25 #if SIZEOF_DATUM_DSIZE > SIZEOF_INT
26 # define RSTRING_DSIZE(s) RSTRING_LEN(s)
27 # define TOO_LONG(n) ((void)(n),0)
28 #else
29 # define RSTRING_DSIZE(s) RSTRING_LENINT(s)
30 # define TOO_LONG(n) ((long)(+(DSIZE_TYPE)(n)) != (n))
31 #endif
32 
33 static VALUE rb_cDBM, rb_eDBMError;
34 
35 #define RUBY_DBM_RW_BIT 0x20000000
36 
37 struct dbmdata {
38  long di_size;
40 };
41 
42 static void
43 closed_dbm(void)
44 {
45  rb_raise(rb_eDBMError, "closed DBM file");
46 }
47 
48 #define GetDBM(obj, dbmp) do {\
49  TypedData_Get_Struct((obj), struct dbmdata, &dbm_type, (dbmp));\
50  if ((dbmp) == 0) closed_dbm();\
51  if ((dbmp)->di_dbm == 0) closed_dbm();\
52 } while (0)
53 
54 #define GetDBM2(obj, dbmp, dbm) do {\
55  GetDBM((obj), (dbmp));\
56  (dbm) = (dbmp)->di_dbm;\
57 } while (0)
58 
59 static void
60 free_dbm(void *ptr)
61 {
62  struct dbmdata *dbmp = ptr;
63  if (dbmp) {
64  if (dbmp->di_dbm) dbm_close(dbmp->di_dbm);
65  xfree(dbmp);
66  }
67 }
68 
69 static size_t
70 memsize_dbm(const void *ptr)
71 {
72  size_t size = 0;
73  const struct dbmdata *dbmp = ptr;
74  if (dbmp) {
75  size += sizeof(*dbmp);
76  if (dbmp->di_dbm) size += DBM_SIZEOF_DBM;
77  }
78  return size;
79 }
80 
81 static const rb_data_type_t dbm_type = {
82  "dbm",
83  {0, free_dbm, memsize_dbm,},
84  0, 0,
86 };
87 
88 /*
89  * call-seq:
90  * dbm.close
91  *
92  * Closes the database.
93  */
94 static VALUE
95 fdbm_close(VALUE obj)
96 {
97  struct dbmdata *dbmp;
98 
99  GetDBM(obj, dbmp);
100  dbm_close(dbmp->di_dbm);
101  dbmp->di_dbm = 0;
102 
103  return Qnil;
104 }
105 
106 /*
107  * call-seq:
108  * dbm.closed? -> true or false
109  *
110  * Returns true if the database is closed, false otherwise.
111  */
112 static VALUE
113 fdbm_closed(VALUE obj)
114 {
115  struct dbmdata *dbmp;
116 
117  TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp);
118  if (dbmp == 0)
119  return Qtrue;
120  if (dbmp->di_dbm == 0)
121  return Qtrue;
122 
123  return Qfalse;
124 }
125 
126 static VALUE
127 fdbm_alloc(VALUE klass)
128 {
129  return TypedData_Wrap_Struct(klass, &dbm_type, 0);
130 }
131 
132 /*
133  * call-seq:
134  * DBM.new(filename[, mode[, flags]]) -> dbm
135  *
136  * Open a dbm database with the specified name, which can include a directory
137  * path. Any file extensions needed will be supplied automatically by the dbm
138  * library. For example, Berkeley DB appends '.db', and GNU gdbm uses two
139  * physical files with extensions '.dir' and '.pag'.
140  *
141  * The mode should be an integer, as for Unix chmod.
142  *
143  * Flags should be one of READER, WRITER, WRCREAT or NEWDB.
144  */
145 static VALUE
146 fdbm_initialize(int argc, VALUE *argv, VALUE obj)
147 {
148  VALUE file, vmode, vflags;
149  DBM *dbm;
150  struct dbmdata *dbmp;
151  int mode, flags = 0;
152 
153  if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
154  mode = 0666; /* default value */
155  }
156  else if (NIL_P(vmode)) {
157  mode = -1; /* return nil if DB not exist */
158  }
159  else {
160  mode = NUM2INT(vmode);
161  }
162 
163  if (!NIL_P(vflags))
164  flags = NUM2INT(vflags);
165 
166  FilePathValue(file);
167 
168  /*
169  * Note:
170  * gdbm 1.10 works with O_CLOEXEC. gdbm 1.9.1 silently ignore it.
171  */
172 #ifndef O_CLOEXEC
173 # define O_CLOEXEC 0
174 #endif
175 
176  if (flags & RUBY_DBM_RW_BIT) {
177  flags &= ~RUBY_DBM_RW_BIT;
178  dbm = dbm_open(RSTRING_PTR(file), flags|O_CLOEXEC, mode);
179  }
180  else {
181  dbm = 0;
182  if (mode >= 0) {
183  dbm = dbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT|O_CLOEXEC, mode);
184  }
185  if (!dbm) {
186  dbm = dbm_open(RSTRING_PTR(file), O_RDWR|O_CLOEXEC, 0);
187  }
188  if (!dbm) {
189  dbm = dbm_open(RSTRING_PTR(file), O_RDONLY|O_CLOEXEC, 0);
190  }
191  }
192 
193  if (dbm) {
194  /*
195  * History of dbm_pagfno() and dbm_dirfno() in ndbm and its compatibles.
196  * (dbm_pagfno() and dbm_dirfno() is not standardized.)
197  *
198  * 1986: 4.3BSD provides ndbm.
199  * It provides dbm_pagfno() and dbm_dirfno() as macros.
200  * 1991: gdbm-1.5 provides them as functions.
201  * They returns a same descriptor.
202  * (Earlier releases may have the functions too.)
203  * 1991: Net/2 provides Berkeley DB.
204  * It doesn't provide dbm_pagfno() and dbm_dirfno().
205  * 1992: 4.4BSD Alpha provides Berkeley DB with dbm_dirfno() as a function.
206  * dbm_pagfno() is a macro as DBM_PAGFNO_NOT_AVAILABLE.
207  * 1997: Berkeley DB 2.0 is released by Sleepycat Software, Inc.
208  * It defines dbm_pagfno() and dbm_dirfno() as macros.
209  * 2011: gdbm-1.9 creates a separate dir file.
210  * dbm_pagfno() and dbm_dirfno() returns different descriptors.
211  */
212 #if defined(HAVE_DBM_PAGFNO)
213  rb_fd_fix_cloexec(dbm_pagfno(dbm));
214 #endif
215 #if defined(HAVE_DBM_DIRFNO)
216  rb_fd_fix_cloexec(dbm_dirfno(dbm));
217 #endif
218 
219 #if defined(RUBYDBM_DB_HEADER) && defined(HAVE_TYPE_DBC)
220  /* Disable Berkeley DB error messages such as:
221  * DB->put: attempt to modify a read-only database */
222  ((DBC*)dbm)->dbp->set_errfile(((DBC*)dbm)->dbp, NULL);
223 #endif
224  }
225 
226  if (!dbm) {
227  if (mode == -1) return Qnil;
228  rb_sys_fail_str(file);
229  }
230 
231  dbmp = ALLOC(struct dbmdata);
232  DATA_PTR(obj) = dbmp;
233  dbmp->di_dbm = dbm;
234  dbmp->di_size = -1;
235 
236  return obj;
237 }
238 
239 /*
240  * call-seq:
241  * DBM.open(filename[, mode[, flags]]) -> dbm
242  * DBM.open(filename[, mode[, flags]]) {|dbm| block}
243  *
244  * Open a dbm database and yields it if a block is given. See also
245  * <code>DBM.new</code>.
246  */
247 static VALUE
248 fdbm_s_open(int argc, VALUE *argv, VALUE klass)
249 {
250  VALUE obj = fdbm_alloc(klass);
251 
252  if (NIL_P(fdbm_initialize(argc, argv, obj))) {
253  return Qnil;
254  }
255 
256  if (rb_block_given_p()) {
257  return rb_ensure(rb_yield, obj, fdbm_close, obj);
258  }
259 
260  return obj;
261 }
262 
263 static VALUE
264 fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
265 {
266  datum key, value;
267  struct dbmdata *dbmp;
268  DBM *dbm;
269  long len;
270 
271  ExportStringValue(keystr);
272  len = RSTRING_LEN(keystr);
273  if (TOO_LONG(len)) goto not_found;
274  key.dptr = RSTRING_PTR(keystr);
275  key.dsize = (DSIZE_TYPE)len;
276 
277  GetDBM2(obj, dbmp, dbm);
278  value = dbm_fetch(dbm, key);
279  if (value.dptr == 0) {
280  not_found:
281  if (NIL_P(ifnone) && rb_block_given_p()) {
282  keystr = rb_str_dup(keystr);
283  OBJ_TAINT(keystr);
284  return rb_yield(keystr);
285  }
286  return ifnone;
287  }
288  return rb_tainted_str_new(value.dptr, value.dsize);
289 }
290 
291 /*
292  * call-seq:
293  * dbm[key] -> string value or nil
294  *
295  * Return a value from the database by locating the key string
296  * provided. If the key is not found, returns nil.
297  */
298 static VALUE
299 fdbm_aref(VALUE obj, VALUE keystr)
300 {
301  return fdbm_fetch(obj, keystr, Qnil);
302 }
303 
304 /*
305  * call-seq:
306  * dbm.fetch(key[, ifnone]) -> value
307  *
308  * Return a value from the database by locating the key string
309  * provided. If the key is not found, returns +ifnone+. If +ifnone+
310  * is not given, raises IndexError.
311  */
312 static VALUE
313 fdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
314 {
315  VALUE keystr, valstr, ifnone;
316 
317  rb_scan_args(argc, argv, "11", &keystr, &ifnone);
318  valstr = fdbm_fetch(obj, keystr, ifnone);
319  if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
320  rb_raise(rb_eIndexError, "key not found");
321 
322  return valstr;
323 }
324 
325 /*
326  * call-seq:
327  * dbm.key(value) -> string
328  *
329  * Returns the key for the specified value.
330  */
331 static VALUE
332 fdbm_key(VALUE obj, VALUE valstr)
333 {
334  datum key, val;
335  struct dbmdata *dbmp;
336  DBM *dbm;
337  long len;
338 
339  ExportStringValue(valstr);
340  len = RSTRING_LEN(valstr);
341  if (TOO_LONG(len)) return Qnil;
342 
343  GetDBM2(obj, dbmp, dbm);
344  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
345  val = dbm_fetch(dbm, key);
346  if ((long)val.dsize == RSTRING_LEN(valstr) &&
347  memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) {
348  return rb_tainted_str_new(key.dptr, key.dsize);
349  }
350  }
351  return Qnil;
352 }
353 
354 /* :nodoc: */
355 static VALUE
356 fdbm_index(VALUE hash, VALUE value)
357 {
358  rb_warn("DBM#index is deprecated; use DBM#key");
359  return fdbm_key(hash, value);
360 }
361 
362 /*
363  * call-seq:
364  * dbm.select {|key, value| block} -> array
365  *
366  * Returns a new array consisting of the [key, value] pairs for which the code
367  * block returns true.
368  */
369 static VALUE
370 fdbm_select(VALUE obj)
371 {
372  VALUE new = rb_ary_new();
373  datum key, val;
374  DBM *dbm;
375  struct dbmdata *dbmp;
376 
377  GetDBM2(obj, dbmp, dbm);
378  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
379  VALUE assoc, v;
380  val = dbm_fetch(dbm, key);
381  assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
382  rb_tainted_str_new(val.dptr, val.dsize));
383  v = rb_yield(assoc);
384  if (RTEST(v)) {
385  rb_ary_push(new, assoc);
386  }
387  GetDBM2(obj, dbmp, dbm);
388  }
389 
390  return new;
391 }
392 
393 /*
394  * call-seq:
395  * dbm.values_at(key, ...) -> Array
396  *
397  * Returns an array containing the values associated with the given keys.
398  */
399 static VALUE
400 fdbm_values_at(int argc, VALUE *argv, VALUE obj)
401 {
402  VALUE new = rb_ary_new2(argc);
403  int i;
404 
405  for (i=0; i<argc; i++) {
406  rb_ary_push(new, fdbm_fetch(obj, argv[i], Qnil));
407  }
408 
409  return new;
410 }
411 
412 static void
413 fdbm_modify(VALUE obj)
414 {
415  if (OBJ_FROZEN(obj)) rb_error_frozen("DBM");
416 }
417 
418 /*
419  * call-seq:
420  * dbm.delete(key)
421  *
422  * Deletes an entry from the database.
423  */
424 static VALUE
425 fdbm_delete(VALUE obj, VALUE keystr)
426 {
427  datum key, value;
428  struct dbmdata *dbmp;
429  DBM *dbm;
430  VALUE valstr;
431  long len;
432 
433  fdbm_modify(obj);
434  ExportStringValue(keystr);
435  len = RSTRING_LEN(keystr);
436  if (TOO_LONG(len)) goto not_found;
437  key.dptr = RSTRING_PTR(keystr);
438  key.dsize = (DSIZE_TYPE)len;
439 
440  GetDBM2(obj, dbmp, dbm);
441 
442  value = dbm_fetch(dbm, key);
443  if (value.dptr == 0) {
444  not_found:
445  if (rb_block_given_p()) return rb_yield(keystr);
446  return Qnil;
447  }
448 
449  /* need to save value before dbm_delete() */
450  valstr = rb_tainted_str_new(value.dptr, value.dsize);
451 
452  if (dbm_delete(dbm, key)) {
453  dbmp->di_size = -1;
454  rb_raise(rb_eDBMError, "dbm_delete failed");
455  }
456  else if (dbmp->di_size >= 0) {
457  dbmp->di_size--;
458  }
459  return valstr;
460 }
461 
462 /*
463  * call-seq:
464  * dbm.shift() -> [key, value]
465  *
466  * Removes a [key, value] pair from the database, and returns it.
467  * If the database is empty, returns nil.
468  * The order in which values are removed/returned is not guaranteed.
469  */
470 static VALUE
471 fdbm_shift(VALUE obj)
472 {
473  datum key, val;
474  struct dbmdata *dbmp;
475  DBM *dbm;
476  VALUE keystr, valstr;
477 
478  fdbm_modify(obj);
479  GetDBM2(obj, dbmp, dbm);
480  dbmp->di_size = -1;
481 
482  key = dbm_firstkey(dbm);
483  if (!key.dptr) return Qnil;
484  val = dbm_fetch(dbm, key);
485  keystr = rb_tainted_str_new(key.dptr, key.dsize);
486  valstr = rb_tainted_str_new(val.dptr, val.dsize);
487  dbm_delete(dbm, key);
488 
489  return rb_assoc_new(keystr, valstr);
490 }
491 
492 /*
493  * call-seq:
494  * dbm.reject! {|key, value| block} -> self
495  * dbm.delete_if {|key, value| block} -> self
496  *
497  * Deletes all entries for which the code block returns true.
498  * Returns self.
499  */
500 static VALUE
501 fdbm_delete_if(VALUE obj)
502 {
503  datum key, val;
504  struct dbmdata *dbmp;
505  DBM *dbm;
506  VALUE keystr, valstr;
507  VALUE ret, ary = rb_ary_tmp_new(0);
508  int status = 0;
509  long i, n;
510 
511  fdbm_modify(obj);
512  GetDBM2(obj, dbmp, dbm);
513  n = dbmp->di_size;
514  dbmp->di_size = -1;
515 
516  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
517  val = dbm_fetch(dbm, key);
518  keystr = rb_tainted_str_new(key.dptr, key.dsize);
519  OBJ_FREEZE(keystr);
520  valstr = rb_tainted_str_new(val.dptr, val.dsize);
521  ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
522  if (status != 0) break;
523  if (RTEST(ret)) rb_ary_push(ary, keystr);
524  GetDBM2(obj, dbmp, dbm);
525  }
526 
527  for (i = 0; i < RARRAY_LEN(ary); i++) {
528  keystr = RARRAY_AREF(ary, i);
529  key.dptr = RSTRING_PTR(keystr);
530  key.dsize = (DSIZE_TYPE)RSTRING_LEN(keystr);
531  if (dbm_delete(dbm, key)) {
532  rb_raise(rb_eDBMError, "dbm_delete failed");
533  }
534  }
535  if (status) rb_jump_tag(status);
536  if (n > 0) dbmp->di_size = n - RARRAY_LEN(ary);
537  rb_ary_clear(ary);
538 
539  return obj;
540 }
541 
542 /*
543  * call-seq:
544  * dbm.clear
545  *
546  * Deletes all data from the database.
547  */
548 static VALUE
549 fdbm_clear(VALUE obj)
550 {
551  datum key;
552  struct dbmdata *dbmp;
553  DBM *dbm;
554 
555  fdbm_modify(obj);
556  GetDBM2(obj, dbmp, dbm);
557  dbmp->di_size = -1;
558  while (key = dbm_firstkey(dbm), key.dptr) {
559  if (dbm_delete(dbm, key)) {
560  rb_raise(rb_eDBMError, "dbm_delete failed");
561  }
562  }
563  dbmp->di_size = 0;
564 
565  return obj;
566 }
567 
568 /*
569  * call-seq:
570  * dbm.invert -> hash
571  *
572  * Returns a Hash (not a DBM database) created by using each value in the
573  * database as a key, with the corresponding key as its value.
574  */
575 static VALUE
576 fdbm_invert(VALUE obj)
577 {
578  datum key, val;
579  struct dbmdata *dbmp;
580  DBM *dbm;
581  VALUE keystr, valstr;
582  VALUE hash = rb_hash_new();
583 
584  GetDBM2(obj, dbmp, dbm);
585  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
586  val = dbm_fetch(dbm, key);
587  keystr = rb_tainted_str_new(key.dptr, key.dsize);
588  valstr = rb_tainted_str_new(val.dptr, val.dsize);
589  rb_hash_aset(hash, valstr, keystr);
590  }
591  return hash;
592 }
593 
594 static VALUE fdbm_store(VALUE,VALUE,VALUE);
595 
596 static VALUE
597 update_i(RB_BLOCK_CALL_FUNC_ARGLIST(pair, dbm))
598 {
599  const VALUE *ptr;
600  Check_Type(pair, T_ARRAY);
601  if (RARRAY_LEN(pair) < 2) {
602  rb_raise(rb_eArgError, "pair must be [key, value]");
603  }
604  ptr = RARRAY_CONST_PTR(pair);
605  fdbm_store(dbm, ptr[0], ptr[1]);
606  return Qnil;
607 }
608 
609 /*
610  * call-seq:
611  * dbm.update(obj)
612  *
613  * Updates the database with multiple values from the specified object.
614  * Takes any object which implements the each_pair method, including
615  * Hash and DBM objects.
616  */
617 static VALUE
618 fdbm_update(VALUE obj, VALUE other)
619 {
620  rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
621  return obj;
622 }
623 
624 /*
625  * call-seq:
626  * dbm.replace(obj)
627  *
628  * Replaces the contents of the database with the contents of the specified
629  * object. Takes any object which implements the each_pair method, including
630  * Hash and DBM objects.
631  */
632 static VALUE
633 fdbm_replace(VALUE obj, VALUE other)
634 {
635  fdbm_clear(obj);
636  rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
637  return obj;
638 }
639 
640 /*
641  * call-seq:
642  * dbm.store(key, value) -> value
643  * dbm[key] = value
644  *
645  * Stores the specified string value in the database, indexed via the
646  * string key provided.
647  */
648 static VALUE
649 fdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
650 {
651  datum key, val;
652  struct dbmdata *dbmp;
653  DBM *dbm;
654 
655  fdbm_modify(obj);
656  keystr = rb_obj_as_string(keystr);
657  valstr = rb_obj_as_string(valstr);
658 
659  key.dptr = RSTRING_PTR(keystr);
660  key.dsize = RSTRING_DSIZE(keystr);
661 
662  val.dptr = RSTRING_PTR(valstr);
663  val.dsize = RSTRING_DSIZE(valstr);
664 
665  GetDBM2(obj, dbmp, dbm);
666  dbmp->di_size = -1;
667  if (dbm_store(dbm, key, val, DBM_REPLACE)) {
668  dbm_clearerr(dbm);
669  if (errno == EPERM) rb_sys_fail(0);
670  rb_raise(rb_eDBMError, "dbm_store failed");
671  }
672 
673  return valstr;
674 }
675 
676 /*
677  * call-seq:
678  * dbm.length -> integer
679  * dbm.size -> integer
680  *
681  * Returns the number of entries in the database.
682  */
683 static VALUE
684 fdbm_length(VALUE obj)
685 {
686  datum key;
687  struct dbmdata *dbmp;
688  DBM *dbm;
689  int i = 0;
690 
691  GetDBM2(obj, dbmp, dbm);
692  if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
693 
694  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
695  i++;
696  }
697  dbmp->di_size = i;
698 
699  return INT2FIX(i);
700 }
701 
702 /*
703  * call-seq:
704  * dbm.empty?
705  *
706  * Returns true if the database is empty, false otherwise.
707  */
708 static VALUE
709 fdbm_empty_p(VALUE obj)
710 {
711  datum key;
712  struct dbmdata *dbmp;
713  DBM *dbm;
714 
715  GetDBM2(obj, dbmp, dbm);
716  if (dbmp->di_size < 0) {
717  dbm = dbmp->di_dbm;
718 
719  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
720  return Qfalse;
721  }
722  }
723  else {
724  if (dbmp->di_size)
725  return Qfalse;
726  }
727  return Qtrue;
728 }
729 
730 /*
731  * call-seq:
732  * dbm.each_value {|value| block} -> self
733  *
734  * Calls the block once for each value string in the database. Returns self.
735  */
736 static VALUE
737 fdbm_each_value(VALUE obj)
738 {
739  datum key, val;
740  struct dbmdata *dbmp;
741  DBM *dbm;
742 
743  RETURN_ENUMERATOR(obj, 0, 0);
744 
745  GetDBM2(obj, dbmp, dbm);
746  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
747  val = dbm_fetch(dbm, key);
749  GetDBM2(obj, dbmp, dbm);
750  }
751  return obj;
752 }
753 
754 /*
755  * call-seq:
756  * dbm.each_key {|key| block} -> self
757  *
758  * Calls the block once for each key string in the database. Returns self.
759  */
760 static VALUE
761 fdbm_each_key(VALUE obj)
762 {
763  datum key;
764  struct dbmdata *dbmp;
765  DBM *dbm;
766 
767  RETURN_ENUMERATOR(obj, 0, 0);
768 
769  GetDBM2(obj, dbmp, dbm);
770  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
772  GetDBM2(obj, dbmp, dbm);
773  }
774  return obj;
775 }
776 
777 /*
778  * call-seq:
779  * dbm.each_pair {|key,value| block} -> self
780  *
781  * Calls the block once for each [key, value] pair in the database.
782  * Returns self.
783  */
784 static VALUE
785 fdbm_each_pair(VALUE obj)
786 {
787  datum key, val;
788  DBM *dbm;
789  struct dbmdata *dbmp;
790  VALUE keystr, valstr;
791 
792  RETURN_ENUMERATOR(obj, 0, 0);
793 
794  GetDBM2(obj, dbmp, dbm);
795 
796  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
797  val = dbm_fetch(dbm, key);
798  keystr = rb_tainted_str_new(key.dptr, key.dsize);
799  valstr = rb_tainted_str_new(val.dptr, val.dsize);
800  rb_yield(rb_assoc_new(keystr, valstr));
801  GetDBM2(obj, dbmp, dbm);
802  }
803 
804  return obj;
805 }
806 
807 /*
808  * call-seq:
809  * dbm.keys -> array
810  *
811  * Returns an array of all the string keys in the database.
812  */
813 static VALUE
814 fdbm_keys(VALUE obj)
815 {
816  datum key;
817  struct dbmdata *dbmp;
818  DBM *dbm;
819  VALUE ary;
820 
821  GetDBM2(obj, dbmp, dbm);
822 
823  ary = rb_ary_new();
824  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
825  rb_ary_push(ary, rb_tainted_str_new(key.dptr, key.dsize));
826  }
827 
828  return ary;
829 }
830 
831 /*
832  * call-seq:
833  * dbm.values -> array
834  *
835  * Returns an array of all the string values in the database.
836  */
837 static VALUE
838 fdbm_values(VALUE obj)
839 {
840  datum key, val;
841  struct dbmdata *dbmp;
842  DBM *dbm;
843  VALUE ary;
844 
845  GetDBM2(obj, dbmp, dbm);
846  ary = rb_ary_new();
847  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
848  val = dbm_fetch(dbm, key);
849  rb_ary_push(ary, rb_tainted_str_new(val.dptr, val.dsize));
850  }
851 
852  return ary;
853 }
854 
855 /*
856  * call-seq:
857  * dbm.include?(key) -> boolean
858  * dbm.has_key?(key) -> boolean
859  * dbm.member?(key) -> boolean
860  * dbm.key?(key) -> boolean
861  *
862  * Returns true if the database contains the specified key, false otherwise.
863  */
864 static VALUE
865 fdbm_has_key(VALUE obj, VALUE keystr)
866 {
867  datum key, val;
868  struct dbmdata *dbmp;
869  DBM *dbm;
870  long len;
871 
872  ExportStringValue(keystr);
873  len = RSTRING_LEN(keystr);
874  if (TOO_LONG(len)) return Qfalse;
875  key.dptr = RSTRING_PTR(keystr);
876  key.dsize = (DSIZE_TYPE)len;
877 
878  GetDBM2(obj, dbmp, dbm);
879  val = dbm_fetch(dbm, key);
880  if (val.dptr) return Qtrue;
881  return Qfalse;
882 }
883 
884 /*
885  * call-seq:
886  * dbm.has_value?(value) -> boolean
887  * dbm.value?(value) -> boolean
888  *
889  * Returns true if the database contains the specified string value, false
890  * otherwise.
891  */
892 static VALUE
893 fdbm_has_value(VALUE obj, VALUE valstr)
894 {
895  datum key, val;
896  struct dbmdata *dbmp;
897  DBM *dbm;
898  long len;
899 
900  ExportStringValue(valstr);
901  len = RSTRING_LEN(valstr);
902  if (TOO_LONG(len)) return Qfalse;
903  val.dptr = RSTRING_PTR(valstr);
904  val.dsize = (DSIZE_TYPE)len;
905 
906  GetDBM2(obj, dbmp, dbm);
907  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
908  val = dbm_fetch(dbm, key);
909  if ((DSIZE_TYPE)val.dsize == (DSIZE_TYPE)RSTRING_LEN(valstr) &&
910  memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
911  return Qtrue;
912  }
913  return Qfalse;
914 }
915 
916 /*
917  * call-seq:
918  * dbm.to_a -> array
919  *
920  * Converts the contents of the database to an array of [key, value] arrays,
921  * and returns it.
922  */
923 static VALUE
924 fdbm_to_a(VALUE obj)
925 {
926  datum key, val;
927  struct dbmdata *dbmp;
928  DBM *dbm;
929  VALUE ary;
930 
931  GetDBM2(obj, dbmp, dbm);
932  ary = rb_ary_new();
933  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
934  val = dbm_fetch(dbm, key);
936  rb_tainted_str_new(val.dptr, val.dsize)));
937  }
938 
939  return ary;
940 }
941 
942 /*
943  * call-seq:
944  * dbm.to_hash -> hash
945  *
946  * Converts the contents of the database to an in-memory Hash object, and
947  * returns it.
948  */
949 static VALUE
950 fdbm_to_hash(VALUE obj)
951 {
952  datum key, val;
953  struct dbmdata *dbmp;
954  DBM *dbm;
955  VALUE hash;
956 
957  GetDBM2(obj, dbmp, dbm);
958  hash = rb_hash_new();
959  for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
960  val = dbm_fetch(dbm, key);
961  rb_hash_aset(hash, rb_tainted_str_new(key.dptr, key.dsize),
962  rb_tainted_str_new(val.dptr, val.dsize));
963  }
964 
965  return hash;
966 }
967 
968 /*
969  * call-seq:
970  * dbm.reject {|key,value| block} -> Hash
971  *
972  * Converts the contents of the database to an in-memory Hash, then calls
973  * Hash#reject with the specified code block, returning a new Hash.
974  */
975 static VALUE
976 fdbm_reject(VALUE obj)
977 {
978  return rb_hash_delete_if(fdbm_to_hash(obj));
979 }
980 
981 /*
982  * == Introduction
983  *
984  * The DBM class provides a wrapper to a Unix-style
985  * {dbm}[http://en.wikipedia.org/wiki/Dbm] or Database Manager library.
986  *
987  * Dbm databases do not have tables or columns; they are simple key-value
988  * data stores, like a Ruby Hash except not resident in RAM. Keys and values
989  * must be strings.
990  *
991  * The exact library used depends on how Ruby was compiled. It could be any
992  * of the following:
993  *
994  * - The original ndbm library is released in 4.3BSD.
995  * It is based on dbm library in Unix Version 7 but has different API to
996  * support multiple databases in a process.
997  * - {Berkeley DB}[http://en.wikipedia.org/wiki/Berkeley_DB] versions
998  * 1 thru 5, also known as BDB and Sleepycat DB, now owned by Oracle
999  * Corporation.
1000  * - Berkeley DB 1.x, still found in 4.4BSD derivatives (FreeBSD, OpenBSD, etc).
1001  * - {gdbm}[http://www.gnu.org/software/gdbm/], the GNU implementation of dbm.
1002  * - {qdbm}[http://fallabs.com/qdbm/index.html], another open source
1003  * reimplementation of dbm.
1004  *
1005  * All of these dbm implementations have their own Ruby interfaces
1006  * available, which provide richer (but varying) APIs.
1007  *
1008  * == Cautions
1009  *
1010  * Before you decide to use DBM, there are some issues you should consider:
1011  *
1012  * - Each implementation of dbm has its own file format. Generally, dbm
1013  * libraries will not read each other's files. This makes dbm files
1014  * a bad choice for data exchange.
1015  *
1016  * - Even running the same OS and the same dbm implementation, the database
1017  * file format may depend on the CPU architecture. For example, files may
1018  * not be portable between PowerPC and 386, or between 32 and 64 bit Linux.
1019  *
1020  * - Different versions of Berkeley DB use different file formats. A change to
1021  * the OS may therefore break DBM access to existing files.
1022  *
1023  * - Data size limits vary between implementations. Original Berkeley DB was
1024  * limited to 2GB of data. Dbm libraries also sometimes limit the total
1025  * size of a key/value pair, and the total size of all the keys that hash
1026  * to the same value. These limits can be as little as 512 bytes. That said,
1027  * gdbm and recent versions of Berkeley DB do away with these limits.
1028  *
1029  * Given the above cautions, DBM is not a good choice for long term storage of
1030  * important data. It is probably best used as a fast and easy alternative
1031  * to a Hash for processing large amounts of data.
1032  *
1033  * == Example
1034  *
1035  * require 'dbm'
1036  * db = DBM.open('rfcs', 0666, DBM::WRCREAT)
1037  * db['822'] = 'Standard for the Format of ARPA Internet Text Messages'
1038  * db['1123'] = 'Requirements for Internet Hosts - Application and Support'
1039  * db['3068'] = 'An Anycast Prefix for 6to4 Relay Routers'
1040  * puts db['822']
1041  */
1042 void
1044 {
1045  rb_cDBM = rb_define_class("DBM", rb_cObject);
1046  /* Document-class: DBMError
1047  * Exception class used to return errors from the dbm library.
1048  */
1049  rb_eDBMError = rb_define_class("DBMError", rb_eStandardError);
1051 
1052  rb_define_alloc_func(rb_cDBM, fdbm_alloc);
1053  rb_define_singleton_method(rb_cDBM, "open", fdbm_s_open, -1);
1054 
1055  rb_define_method(rb_cDBM, "initialize", fdbm_initialize, -1);
1056  rb_define_method(rb_cDBM, "close", fdbm_close, 0);
1057  rb_define_method(rb_cDBM, "closed?", fdbm_closed, 0);
1058  rb_define_method(rb_cDBM, "[]", fdbm_aref, 1);
1059  rb_define_method(rb_cDBM, "fetch", fdbm_fetch_m, -1);
1060  rb_define_method(rb_cDBM, "[]=", fdbm_store, 2);
1061  rb_define_method(rb_cDBM, "store", fdbm_store, 2);
1062  rb_define_method(rb_cDBM, "index", fdbm_index, 1);
1063  rb_define_method(rb_cDBM, "key", fdbm_key, 1);
1064  rb_define_method(rb_cDBM, "select", fdbm_select, 0);
1065  rb_define_method(rb_cDBM, "values_at", fdbm_values_at, -1);
1066  rb_define_method(rb_cDBM, "length", fdbm_length, 0);
1067  rb_define_method(rb_cDBM, "size", fdbm_length, 0);
1068  rb_define_method(rb_cDBM, "empty?", fdbm_empty_p, 0);
1069  rb_define_method(rb_cDBM, "each", fdbm_each_pair, 0);
1070  rb_define_method(rb_cDBM, "each_value", fdbm_each_value, 0);
1071  rb_define_method(rb_cDBM, "each_key", fdbm_each_key, 0);
1072  rb_define_method(rb_cDBM, "each_pair", fdbm_each_pair, 0);
1073  rb_define_method(rb_cDBM, "keys", fdbm_keys, 0);
1074  rb_define_method(rb_cDBM, "values", fdbm_values, 0);
1075  rb_define_method(rb_cDBM, "shift", fdbm_shift, 0);
1076  rb_define_method(rb_cDBM, "delete", fdbm_delete, 1);
1077  rb_define_method(rb_cDBM, "delete_if", fdbm_delete_if, 0);
1078  rb_define_method(rb_cDBM, "reject!", fdbm_delete_if, 0);
1079  rb_define_method(rb_cDBM, "reject", fdbm_reject, 0);
1080  rb_define_method(rb_cDBM, "clear", fdbm_clear, 0);
1081  rb_define_method(rb_cDBM, "invert", fdbm_invert, 0);
1082  rb_define_method(rb_cDBM, "update", fdbm_update, 1);
1083  rb_define_method(rb_cDBM, "replace", fdbm_replace, 1);
1084 
1085  rb_define_method(rb_cDBM, "include?", fdbm_has_key, 1);
1086  rb_define_method(rb_cDBM, "has_key?", fdbm_has_key, 1);
1087  rb_define_method(rb_cDBM, "member?", fdbm_has_key, 1);
1088  rb_define_method(rb_cDBM, "has_value?", fdbm_has_value, 1);
1089  rb_define_method(rb_cDBM, "key?", fdbm_has_key, 1);
1090  rb_define_method(rb_cDBM, "value?", fdbm_has_value, 1);
1091 
1092  rb_define_method(rb_cDBM, "to_a", fdbm_to_a, 0);
1093  rb_define_method(rb_cDBM, "to_hash", fdbm_to_hash, 0);
1094 
1095  /* Indicates that dbm_open() should open the database in read-only mode */
1096  rb_define_const(rb_cDBM, "READER", INT2FIX(O_RDONLY|RUBY_DBM_RW_BIT));
1097 
1098  /* Indicates that dbm_open() should open the database in read/write mode */
1099  rb_define_const(rb_cDBM, "WRITER", INT2FIX(O_RDWR|RUBY_DBM_RW_BIT));
1100 
1101  /* Indicates that dbm_open() should open the database in read/write mode,
1102  * and create it if it does not already exist
1103  */
1104  rb_define_const(rb_cDBM, "WRCREAT", INT2FIX(O_RDWR|O_CREAT|RUBY_DBM_RW_BIT));
1105 
1106  /* Indicates that dbm_open() should open the database in read/write mode,
1107  * create it if it does not already exist, and delete all contents if it
1108  * does already exist.
1109  */
1110  rb_define_const(rb_cDBM, "NEWDB", INT2FIX(O_RDWR|O_CREAT|O_TRUNC|RUBY_DBM_RW_BIT));
1111 
1112  {
1113  VALUE version;
1114 #if defined(_DBM_IOERR)
1115  version = rb_str_new2("ndbm (4.3BSD)");
1116 #elif defined(RUBYDBM_GDBM_HEADER)
1117 # if defined(HAVE_DECLARED_LIBVAR_GDBM_VERSION)
1118  /* since gdbm 1.9 */
1119  version = rb_str_new2(gdbm_version);
1120 # elif defined(HAVE_UNDECLARED_LIBVAR_GDBM_VERSION)
1121  /* ndbm.h doesn't declare gdbm_version until gdbm 1.8.3.
1122  * See extconf.rb for more information. */
1123  RUBY_EXTERN char *gdbm_version;
1124  version = rb_str_new2(gdbm_version);
1125 # else
1126  version = rb_str_new2("GDBM (unknown)");
1127 # endif
1128 #elif defined(RUBYDBM_DB_HEADER)
1129 # if defined(HAVE_DB_VERSION)
1130  /* The version of the dbm library, if using Berkeley DB */
1131  version = rb_str_new2(db_version(NULL, NULL, NULL));
1132 # else
1133  version = rb_str_new2("Berkeley DB (unknown)");
1134 # endif
1135 #elif defined(_RELIC_H)
1136 # if defined(HAVE_DPVERSION)
1137  version = rb_sprintf("QDBM %s", dpversion);
1138 # else
1139  version = rb_str_new2("QDBM (unknown)");
1140 # endif
1141 #else
1142  version = rb_str_new2("ndbm (unknown)");
1143 #endif
1144  /*
1145  * Identifies ndbm library version.
1146  *
1147  * Examples:
1148  *
1149  * - "ndbm (4.3BSD)"
1150  * - "Berkeley DB 4.8.30: (April 9, 2010)"
1151  * - "Berkeley DB (unknown)" (4.4BSD, maybe)
1152  * - "GDBM version 1.8.3. 10/15/2002 (built Jul 1 2011 12:32:45)"
1153  * - "QDBM 1.8.78"
1154  *
1155  */
1156  rb_define_const(rb_cDBM, "VERSION", version);
1157  }
1158 }
DBM * di_dbm
Definition: dbm.c:39
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:992
void rb_warn(const char *fmt,...)
Definition: error.c:246
#define RARRAY_LEN(a)
Definition: ruby.h:1019
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1138
#define GetDBM2(obj, dbmp, dbm)
Definition: dbm.c:54
#define NUM2INT(x)
Definition: ruby.h:684
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:835
#define FilePathValue(v)
Definition: ruby.h:594
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
Definition: eval.c:821
#define Qtrue
Definition: ruby.h:437
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1162
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1183
#define OBJ_FREEZE(x)
Definition: ruby.h:1306
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:924
long di_size
Definition: dbm.c:38
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:544
#define Check_Type(v, t)
Definition: ruby.h:562
VALUE rb_ary_clear(VALUE ary)
Definition: array.c:3501
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
Definition: dbm.c:37
#define DATA_PTR(dta)
Definition: ruby.h:1106
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
char * dptr
Definition: sdbm.h:51
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
#define T_ARRAY
Definition: ruby.h:498
#define DSIZE_TYPE
Definition: dbm.c:24
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1035
#define TOO_LONG(n)
Definition: dbm.c:30
#define rb_ary_new2
Definition: intern.h:90
VALUE rb_eArgError
Definition: error.c:802
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Definition: ruby.h:1851
#define RSTRING_DSIZE(s)
Definition: dbm.c:29
#define O_CLOEXEC
Definition: sdbm.h:50
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1616
#define DBM_REPLACE
Definition: sdbm.h:67
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1893
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1410
VALUE rb_ary_new(void)
Definition: array.c:499
VALUE rb_eIndexError
Definition: error.c:803
#define NIL_P(v)
Definition: ruby.h:451
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2691
#define RUBY_DBM_RW_BIT
Definition: dbm.c:35
int argc
Definition: ruby.c:187
#define Qfalse
Definition: ruby.h:436
#define rb_str_new2
Definition: intern.h:835
void Init_dbm(void)
Definition: dbm.c:1043
void rb_error_frozen(const char *what)
Definition: error.c:2584
#define ALLOC(type)
Definition: ruby.h:1588
void rb_sys_fail(const char *mesg)
Definition: error.c:2403
#define RSTRING_LEN(str)
Definition: ruby.h:971
VALUE rb_yield(VALUE)
Definition: vm_eval.c:973
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:1021
int errno
#define RUBY_EXTERN
Definition: missing.h:77
VALUE rb_mEnumerable
Definition: enum.c:19
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1452
Definition: sdbm.h:20
VALUE rb_hash_new(void)
Definition: hash.c:424
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:639
#define Qnil
Definition: ruby.h:438
VALUE rb_eStandardError
Definition: error.c:799
int dsize
Definition: sdbm.h:52
unsigned long VALUE
Definition: ruby.h:85
#define EPERM
Definition: _sdbm.c:93
int memcmp(const void *s1, const void *s2, size_t len)
Definition: memcmp.c:7
VALUE rb_str_dup(VALUE)
Definition: string.c:1488
register unsigned int len
Definition: zonetab.h:51
#define RSTRING_PTR(str)
Definition: ruby.h:975
int size
Definition: encoding.c:57
#define INT2FIX(i)
Definition: ruby.h:232
#define ExportStringValue(v)
Definition: ruby.h:587
#define RARRAY_AREF(a, i)
Definition: ruby.h:1033
void rb_fd_fix_cloexec(int fd)
Definition: io.c:233
#define RTEST(v)
Definition: ruby.h:450
#define OBJ_FROZEN(x)
Definition: ruby.h:1304
#define RETURN_ENUMERATOR(obj, argc, argv)
Definition: intern.h:238
#define GetDBM(obj, dbmp)
Definition: dbm.c:48
VALUE rb_hash_delete_if(VALUE hash)
Definition: hash.c:1253
void void xfree(void *)
VALUE rb_tainted_str_new(const char *, long)
Definition: string.c:854
#define rb_intern(str)
#define NULL
Definition: _sdbm.c:102
void rb_sys_fail_str(VALUE mesg)
Definition: error.c:2409
#define OBJ_TAINT(x)
Definition: ruby.h:1298
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
char ** argv
Definition: ruby.c:188