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