From edcc29dcff1b269b7748ab83adf21b2f3f97ebff Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 25 May 2021 15:20:06 +0900 Subject: [PATCH] Removed gdbm from ruby repo --- doc/maintainers.rdoc | 4 - doc/standard_library.rdoc | 1 - ext/gdbm/README | 1 - ext/gdbm/depend | 162 ----- ext/gdbm/extconf.rb | 19 - ext/gdbm/gdbm.c | 1306 ------------------------------------- ext/gdbm/gdbm.gemspec | 22 - test/gdbm/test_gdbm.rb | 734 --------------------- tool/sync_default_gems.rb | 7 - 9 files changed, 2256 deletions(-) delete mode 100644 ext/gdbm/README delete mode 100644 ext/gdbm/depend delete mode 100644 ext/gdbm/extconf.rb delete mode 100644 ext/gdbm/gdbm.c delete mode 100644 ext/gdbm/gdbm.gemspec delete mode 100644 test/gdbm/test_gdbm.rb diff --git a/doc/maintainers.rdoc b/doc/maintainers.rdoc index cb70587f19..975f4d4091 100644 --- a/doc/maintainers.rdoc +++ b/doc/maintainers.rdoc @@ -324,10 +324,6 @@ Yukihiro Matsumoto (matz) Aaron Patterson (tenderlove) https://github.com/ruby/fiddle https://rubygems.org/gems/fiddle -[ext/gdbm] - Yukihiro Matsumoto (matz) - https://github.com/ruby/gdbm - https://rubygems.org/gems/gdbm [ext/io/console] Nobuyuki Nakada (nobu) https://github.com/ruby/io-console diff --git a/doc/standard_library.rdoc b/doc/standard_library.rdoc index 2e201afa55..127ba76d31 100644 --- a/doc/standard_library.rdoc +++ b/doc/standard_library.rdoc @@ -87,7 +87,6 @@ Digest:: Provides a framework for message digest libraries Etc:: Provides access to information typically stored in UNIX /etc directory Fcntl:: Loads constants defined in the OS fcntl.h C header file Fiddle:: A libffi wrapper for Ruby -GDBM:: Ruby extension for the GNU dbm (gdbm) library IO:: Extensions for Ruby IO class, including #wait, #nonblock and ::console JSON:: Implements Javascript Object Notation for Ruby NKF:: Ruby extension for Network Kanji Filter diff --git a/ext/gdbm/README b/ext/gdbm/README deleted file mode 100644 index df7a261c68..0000000000 --- a/ext/gdbm/README +++ /dev/null @@ -1 +0,0 @@ -gdbm ext-library for Ruby 1.3 or later diff --git a/ext/gdbm/depend b/ext/gdbm/depend deleted file mode 100644 index 87f6a22129..0000000000 --- a/ext/gdbm/depend +++ /dev/null @@ -1,162 +0,0 @@ -# AUTOGENERATED DEPENDENCIES START -gdbm.o: $(RUBY_EXTCONF_H) -gdbm.o: $(arch_hdrdir)/ruby/config.h -gdbm.o: $(hdrdir)/ruby.h -gdbm.o: $(hdrdir)/ruby/internal/anyargs.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/char.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/double.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/fixnum.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/gid_t.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/int.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/intptr_t.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/long.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/long_long.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/mode_t.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/off_t.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/pid_t.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/short.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/size_t.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/st_data_t.h -gdbm.o: $(hdrdir)/ruby/internal/arithmetic/uid_t.h -gdbm.o: $(hdrdir)/ruby/internal/assume.h -gdbm.o: $(hdrdir)/ruby/internal/attr/alloc_size.h -gdbm.o: $(hdrdir)/ruby/internal/attr/artificial.h -gdbm.o: $(hdrdir)/ruby/internal/attr/cold.h -gdbm.o: $(hdrdir)/ruby/internal/attr/const.h -gdbm.o: $(hdrdir)/ruby/internal/attr/constexpr.h -gdbm.o: $(hdrdir)/ruby/internal/attr/deprecated.h -gdbm.o: $(hdrdir)/ruby/internal/attr/diagnose_if.h -gdbm.o: $(hdrdir)/ruby/internal/attr/enum_extensibility.h -gdbm.o: $(hdrdir)/ruby/internal/attr/error.h -gdbm.o: $(hdrdir)/ruby/internal/attr/flag_enum.h -gdbm.o: $(hdrdir)/ruby/internal/attr/forceinline.h -gdbm.o: $(hdrdir)/ruby/internal/attr/format.h -gdbm.o: $(hdrdir)/ruby/internal/attr/maybe_unused.h -gdbm.o: $(hdrdir)/ruby/internal/attr/noalias.h -gdbm.o: $(hdrdir)/ruby/internal/attr/nodiscard.h -gdbm.o: $(hdrdir)/ruby/internal/attr/noexcept.h -gdbm.o: $(hdrdir)/ruby/internal/attr/noinline.h -gdbm.o: $(hdrdir)/ruby/internal/attr/nonnull.h -gdbm.o: $(hdrdir)/ruby/internal/attr/noreturn.h -gdbm.o: $(hdrdir)/ruby/internal/attr/pure.h -gdbm.o: $(hdrdir)/ruby/internal/attr/restrict.h -gdbm.o: $(hdrdir)/ruby/internal/attr/returns_nonnull.h -gdbm.o: $(hdrdir)/ruby/internal/attr/warning.h -gdbm.o: $(hdrdir)/ruby/internal/attr/weakref.h -gdbm.o: $(hdrdir)/ruby/internal/cast.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_is.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_is/apple.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_is/clang.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_is/gcc.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_is/intel.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_is/msvc.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_is/sunpro.h -gdbm.o: $(hdrdir)/ruby/internal/compiler_since.h -gdbm.o: $(hdrdir)/ruby/internal/config.h -gdbm.o: $(hdrdir)/ruby/internal/constant_p.h -gdbm.o: $(hdrdir)/ruby/internal/core.h -gdbm.o: $(hdrdir)/ruby/internal/core/rarray.h -gdbm.o: $(hdrdir)/ruby/internal/core/rbasic.h -gdbm.o: $(hdrdir)/ruby/internal/core/rbignum.h -gdbm.o: $(hdrdir)/ruby/internal/core/rclass.h -gdbm.o: $(hdrdir)/ruby/internal/core/rdata.h -gdbm.o: $(hdrdir)/ruby/internal/core/rfile.h -gdbm.o: $(hdrdir)/ruby/internal/core/rhash.h -gdbm.o: $(hdrdir)/ruby/internal/core/robject.h -gdbm.o: $(hdrdir)/ruby/internal/core/rregexp.h -gdbm.o: $(hdrdir)/ruby/internal/core/rstring.h -gdbm.o: $(hdrdir)/ruby/internal/core/rstruct.h -gdbm.o: $(hdrdir)/ruby/internal/core/rtypeddata.h -gdbm.o: $(hdrdir)/ruby/internal/ctype.h -gdbm.o: $(hdrdir)/ruby/internal/dllexport.h -gdbm.o: $(hdrdir)/ruby/internal/dosish.h -gdbm.o: $(hdrdir)/ruby/internal/error.h -gdbm.o: $(hdrdir)/ruby/internal/eval.h -gdbm.o: $(hdrdir)/ruby/internal/event.h -gdbm.o: $(hdrdir)/ruby/internal/fl_type.h -gdbm.o: $(hdrdir)/ruby/internal/gc.h -gdbm.o: $(hdrdir)/ruby/internal/glob.h -gdbm.o: $(hdrdir)/ruby/internal/globals.h -gdbm.o: $(hdrdir)/ruby/internal/has/attribute.h -gdbm.o: $(hdrdir)/ruby/internal/has/builtin.h -gdbm.o: $(hdrdir)/ruby/internal/has/c_attribute.h -gdbm.o: $(hdrdir)/ruby/internal/has/cpp_attribute.h -gdbm.o: $(hdrdir)/ruby/internal/has/declspec_attribute.h -gdbm.o: $(hdrdir)/ruby/internal/has/extension.h -gdbm.o: $(hdrdir)/ruby/internal/has/feature.h -gdbm.o: $(hdrdir)/ruby/internal/has/warning.h -gdbm.o: $(hdrdir)/ruby/internal/intern/array.h -gdbm.o: $(hdrdir)/ruby/internal/intern/bignum.h -gdbm.o: $(hdrdir)/ruby/internal/intern/class.h -gdbm.o: $(hdrdir)/ruby/internal/intern/compar.h -gdbm.o: $(hdrdir)/ruby/internal/intern/complex.h -gdbm.o: $(hdrdir)/ruby/internal/intern/cont.h -gdbm.o: $(hdrdir)/ruby/internal/intern/dir.h -gdbm.o: $(hdrdir)/ruby/internal/intern/enum.h -gdbm.o: $(hdrdir)/ruby/internal/intern/enumerator.h -gdbm.o: $(hdrdir)/ruby/internal/intern/error.h -gdbm.o: $(hdrdir)/ruby/internal/intern/eval.h -gdbm.o: $(hdrdir)/ruby/internal/intern/file.h -gdbm.o: $(hdrdir)/ruby/internal/intern/gc.h -gdbm.o: $(hdrdir)/ruby/internal/intern/hash.h -gdbm.o: $(hdrdir)/ruby/internal/intern/io.h -gdbm.o: $(hdrdir)/ruby/internal/intern/load.h -gdbm.o: $(hdrdir)/ruby/internal/intern/marshal.h -gdbm.o: $(hdrdir)/ruby/internal/intern/numeric.h -gdbm.o: $(hdrdir)/ruby/internal/intern/object.h -gdbm.o: $(hdrdir)/ruby/internal/intern/parse.h -gdbm.o: $(hdrdir)/ruby/internal/intern/proc.h -gdbm.o: $(hdrdir)/ruby/internal/intern/process.h -gdbm.o: $(hdrdir)/ruby/internal/intern/random.h -gdbm.o: $(hdrdir)/ruby/internal/intern/range.h -gdbm.o: $(hdrdir)/ruby/internal/intern/rational.h -gdbm.o: $(hdrdir)/ruby/internal/intern/re.h -gdbm.o: $(hdrdir)/ruby/internal/intern/ruby.h -gdbm.o: $(hdrdir)/ruby/internal/intern/select.h -gdbm.o: $(hdrdir)/ruby/internal/intern/select/largesize.h -gdbm.o: $(hdrdir)/ruby/internal/intern/signal.h -gdbm.o: $(hdrdir)/ruby/internal/intern/sprintf.h -gdbm.o: $(hdrdir)/ruby/internal/intern/string.h -gdbm.o: $(hdrdir)/ruby/internal/intern/struct.h -gdbm.o: $(hdrdir)/ruby/internal/intern/thread.h -gdbm.o: $(hdrdir)/ruby/internal/intern/time.h -gdbm.o: $(hdrdir)/ruby/internal/intern/variable.h -gdbm.o: $(hdrdir)/ruby/internal/intern/vm.h -gdbm.o: $(hdrdir)/ruby/internal/interpreter.h -gdbm.o: $(hdrdir)/ruby/internal/iterator.h -gdbm.o: $(hdrdir)/ruby/internal/memory.h -gdbm.o: $(hdrdir)/ruby/internal/method.h -gdbm.o: $(hdrdir)/ruby/internal/module.h -gdbm.o: $(hdrdir)/ruby/internal/newobj.h -gdbm.o: $(hdrdir)/ruby/internal/rgengc.h -gdbm.o: $(hdrdir)/ruby/internal/scan_args.h -gdbm.o: $(hdrdir)/ruby/internal/special_consts.h -gdbm.o: $(hdrdir)/ruby/internal/static_assert.h -gdbm.o: $(hdrdir)/ruby/internal/stdalign.h -gdbm.o: $(hdrdir)/ruby/internal/stdbool.h -gdbm.o: $(hdrdir)/ruby/internal/symbol.h -gdbm.o: $(hdrdir)/ruby/internal/value.h -gdbm.o: $(hdrdir)/ruby/internal/value_type.h -gdbm.o: $(hdrdir)/ruby/internal/variable.h -gdbm.o: $(hdrdir)/ruby/internal/warning_push.h -gdbm.o: $(hdrdir)/ruby/internal/xmalloc.h -gdbm.o: $(hdrdir)/ruby/assert.h -gdbm.o: $(hdrdir)/ruby/backward.h -gdbm.o: $(hdrdir)/ruby/backward/2/assume.h -gdbm.o: $(hdrdir)/ruby/backward/2/attributes.h -gdbm.o: $(hdrdir)/ruby/backward/2/bool.h -gdbm.o: $(hdrdir)/ruby/backward/2/gcc_version_since.h -gdbm.o: $(hdrdir)/ruby/backward/2/inttypes.h -gdbm.o: $(hdrdir)/ruby/backward/2/limits.h -gdbm.o: $(hdrdir)/ruby/backward/2/long_long.h -gdbm.o: $(hdrdir)/ruby/backward/2/stdalign.h -gdbm.o: $(hdrdir)/ruby/backward/2/stdarg.h -gdbm.o: $(hdrdir)/ruby/defines.h -gdbm.o: $(hdrdir)/ruby/intern.h -gdbm.o: $(hdrdir)/ruby/missing.h -gdbm.o: $(hdrdir)/ruby/ruby.h -gdbm.o: $(hdrdir)/ruby/st.h -gdbm.o: $(hdrdir)/ruby/subst.h -gdbm.o: gdbm.c -# AUTOGENERATED DEPENDENCIES END diff --git a/ext/gdbm/extconf.rb b/ext/gdbm/extconf.rb deleted file mode 100644 index d1908ffa5c..0000000000 --- a/ext/gdbm/extconf.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: false -require 'mkmf' - -dir_config("gdbm") -if have_library("gdbm", "gdbm_open") and - have_header("gdbm.h") - checking_for("sizeof(DBM) is available") { - if try_compile(< - -const int sizeof_DBM = (int)sizeof(DBM); -SRC - $defs << '-DDBM_SIZEOF_DBM=sizeof(DBM)' - else - $defs << '-DDBM_SIZEOF_DBM=0' - end - } - create_makefile("gdbm") -end diff --git a/ext/gdbm/gdbm.c b/ext/gdbm/gdbm.c deleted file mode 100644 index 4a6377b685..0000000000 --- a/ext/gdbm/gdbm.c +++ /dev/null @@ -1,1306 +0,0 @@ -/************************************************ - - gdbm.c - - - $Author$ - modified at: Mon Jan 24 15:59:52 JST 1994 - - Documentation by Peter Adolphs < futzilogik at users dot sourceforge dot net > - -************************************************/ - -#include "ruby.h" - -#include -#include -#include - -/* - * Document-class: GDBM - * - * == Summary - * - * Ruby extension for GNU dbm (gdbm) -- a simple database engine for storing - * key-value pairs on disk. - * - * == Description - * - * GNU dbm is a library for simple databases. A database is a file that stores - * key-value pairs. Gdbm allows the user to store, retrieve, and delete data by - * key. It furthermore allows a non-sorted traversal of all key-value pairs. - * A gdbm database thus provides the same functionality as a hash. As - * with objects of the Hash class, elements can be accessed with []. - * Furthermore, GDBM mixes in the Enumerable module, thus providing convenient - * methods such as #find, #collect, #map, etc. - * - * A process is allowed to open several different databases at the same time. - * A process can open a database as a "reader" or a "writer". Whereas a reader - * has only read-access to the database, a writer has read- and write-access. - * A database can be accessed either by any number of readers or by exactly one - * writer at the same time. - * - * == Examples - * - * 1. Opening/creating a database, and filling it with some entries: - * - * require 'gdbm' - * - * gdbm = GDBM.new("fruitstore.db") - * gdbm["ananas"] = "3" - * gdbm["banana"] = "8" - * gdbm["cranberry"] = "4909" - * gdbm.close - * - * 2. Reading out a database: - * - * require 'gdbm' - * - * gdbm = GDBM.new("fruitstore.db") - * gdbm.each_pair do |key, value| - * print "#{key}: #{value}\n" - * end - * gdbm.close - * - * produces - * - * banana: 8 - * ananas: 3 - * cranberry: 4909 - * - * == Links - * - * * http://www.gnu.org/software/gdbm/ - */ -static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError; - -#if SIZEOF_LONG > SIZEOF_INT -#define TOO_LONG(n) ((long)(+(int)(n)) != (long)(n)) -#else -#define TOO_LONG(n) 0 -#endif - -#define RUBY_GDBM_RW_BIT 0x20000000 - -#define MY_BLOCK_SIZE (2048) -#define MY_FATAL_FUNC rb_gdbm_fatal - -NORETURN(static void rb_gdbm_fatal(const char *msg)); -NORETURN(static void closed_dbm(void)); - -static void -rb_gdbm_fatal(const char *msg) -{ - rb_raise(rb_eGDBMFatalError, "%s", msg); -} - -struct dbmdata { - int di_size; - GDBM_FILE di_dbm; -}; - -static void -closed_dbm(void) -{ - rb_raise(rb_eRuntimeError, "closed GDBM file"); -} - -#define GetDBM(obj, dbmp) do {\ - TypedData_Get_Struct((obj), struct dbmdata, &dbm_type, (dbmp));\ - if ((dbmp)->di_dbm == 0) closed_dbm();\ -} while (0) - -#define GetDBM2(obj, dbmp, dbm) do {\ - GetDBM((obj), (dbmp));\ - (dbm) = (dbmp)->di_dbm;\ -} while (0) - -static void -free_dbm(void *ptr) -{ - struct dbmdata *dbmp = ptr; - if (dbmp->di_dbm) - gdbm_close(dbmp->di_dbm); - xfree(dbmp); -} - -static size_t -memsize_dbm(const void *ptr) -{ - const struct dbmdata *dbmp = ptr; - size_t size = sizeof(*dbmp); - if (dbmp->di_dbm) - size += DBM_SIZEOF_DBM; - return size; -} - -static const rb_data_type_t dbm_type = { - "gdbm", - {0, free_dbm, memsize_dbm,}, - 0, 0, - RUBY_TYPED_FREE_IMMEDIATELY, -}; - -/* - * call-seq: - * gdbm.close -> nil - * - * Closes the associated database file. - */ -static VALUE -fgdbm_close(VALUE obj) -{ - struct dbmdata *dbmp; - - GetDBM(obj, dbmp); - gdbm_close(dbmp->di_dbm); - dbmp->di_dbm = 0; - - return Qnil; -} - -/* - * call-seq: - * gdbm.closed? -> true or false - * - * Returns true if the associated database file has been closed. - */ -static VALUE -fgdbm_closed(VALUE obj) -{ - struct dbmdata *dbmp; - - TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp); - if (dbmp->di_dbm == 0) - return Qtrue; - - return Qfalse; -} - -static VALUE -fgdbm_s_alloc(VALUE klass) -{ - struct dbmdata *dbmp; - - return TypedData_Make_Struct(klass, struct dbmdata, &dbm_type, dbmp); -} - -/* - * call-seq: - * GDBM.new(filename, mode = 0666, flags = nil) - * - * Creates a new GDBM instance by opening a gdbm file named _filename_. - * If the file does not exist, a new file with file mode _mode_ will be - * created. _flags_ may be one of the following: - * * *READER* - open as a reader - * * *WRITER* - open as a writer - * * *WRCREAT* - open as a writer; if the database does not exist, create a new one - * * *NEWDB* - open as a writer; overwrite any existing databases - * - * The values *WRITER*, *WRCREAT* and *NEWDB* may be combined with the following - * values by bitwise or: - * * *SYNC* - cause all database operations to be synchronized to the disk - * * *NOLOCK* - do not lock the database file - * - * If no _flags_ are specified, the GDBM object will try to open the database - * file as a writer and will create it if it does not already exist - * (cf. flag WRCREAT). If this fails (for instance, if another process - * has already opened the database as a reader), it will try to open the - * database file as a reader (cf. flag READER). - */ -static VALUE -fgdbm_initialize(int argc, VALUE *argv, VALUE obj) -{ - VALUE file, vmode, vflags; - GDBM_FILE dbm; - struct dbmdata *dbmp; - int mode, flags = 0; - - TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp); - if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) { - mode = 0666; /* default value */ - } - else if (NIL_P(vmode)) { - mode = -1; /* return nil if DB does not exist */ - } - else { - mode = NUM2INT(vmode); - } - - if (!NIL_P(vflags)) - flags = NUM2INT(vflags); - - FilePathValue(file); - -#ifdef GDBM_CLOEXEC - /* GDBM_CLOEXEC is available since gdbm 1.10. */ - flags |= GDBM_CLOEXEC; -#endif - - if (flags & RUBY_GDBM_RW_BIT) { - flags &= ~RUBY_GDBM_RW_BIT; - dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE, - flags, mode, MY_FATAL_FUNC); - } - else { - dbm = 0; - if (mode >= 0) - dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE, - GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC); - if (!dbm) - dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE, - GDBM_WRITER|flags, 0, MY_FATAL_FUNC); - if (!dbm) - dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE, - GDBM_READER|flags, 0, MY_FATAL_FUNC); - } - - if (dbm) { - rb_fd_fix_cloexec(gdbm_fdesc(dbm)); - } - - if (!dbm) { - if (mode == -1) return Qnil; - - if (gdbm_errno == GDBM_FILE_OPEN_ERROR || - gdbm_errno == GDBM_CANT_BE_READER || - gdbm_errno == GDBM_CANT_BE_WRITER) - rb_sys_fail_str(file); - else - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - - if (dbmp->di_dbm) - gdbm_close(dbmp->di_dbm); - dbmp->di_dbm = dbm; - dbmp->di_size = -1; - - return obj; -} - -/* - * call-seq: - * GDBM.open(filename, mode = 0666, flags = nil) - * GDBM.open(filename, mode = 0666, flags = nil) { |gdbm| ... } - * - * If called without a block, this is synonymous to GDBM::new. - * If a block is given, the new GDBM instance will be passed to the block - * as a parameter, and the corresponding database file will be closed - * after the execution of the block code has been finished. - * - * Example for an open call with a block: - * - * require 'gdbm' - * GDBM.open("fruitstore.db") do |gdbm| - * gdbm.each_pair do |key, value| - * print "#{key}: #{value}\n" - * end - * end - */ -static VALUE -fgdbm_s_open(int argc, VALUE *argv, VALUE klass) -{ - VALUE obj = fgdbm_s_alloc(klass); - - if (NIL_P(fgdbm_initialize(argc, argv, obj))) { - return Qnil; - } - - if (rb_block_given_p()) { - return rb_ensure(rb_yield, obj, fgdbm_close, obj); - } - - return obj; -} - -static VALUE -rb_gdbm_fetch(GDBM_FILE dbm, datum key) -{ - datum val; - VALUE str; - - val = gdbm_fetch(dbm, key); - if (val.dptr == 0) - return Qnil; - - str = rb_str_new(val.dptr, val.dsize); - free(val.dptr); - return str; -} - -static VALUE -rb_gdbm_fetch2(GDBM_FILE dbm, VALUE keystr) -{ - datum key; - long len; - - ExportStringValue(keystr); - len = RSTRING_LEN(keystr); - if (TOO_LONG(len)) return Qnil; - key.dptr = RSTRING_PTR(keystr); - key.dsize = (int)len; - - return rb_gdbm_fetch(dbm, key); -} - -static VALUE -rb_gdbm_fetch3(VALUE obj, VALUE keystr) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - - GetDBM2(obj, dbmp, dbm); - return rb_gdbm_fetch2(dbm, keystr); -} - -static VALUE -rb_gdbm_firstkey(GDBM_FILE dbm) -{ - datum key; - VALUE str; - - key = gdbm_firstkey(dbm); - if (key.dptr == 0) - return Qnil; - - str = rb_str_new(key.dptr, key.dsize); - free(key.dptr); - return str; -} - -static VALUE -rb_gdbm_nextkey(GDBM_FILE dbm, VALUE keystr) -{ - datum key, key2; - VALUE str; - long len; - - len = RSTRING_LEN(keystr); - if (TOO_LONG(len)) return Qnil; - key.dptr = RSTRING_PTR(keystr); - key.dsize = (int)len; - key2 = gdbm_nextkey(dbm, key); - if (key2.dptr == 0) - return Qnil; - - str = rb_str_new(key2.dptr, key2.dsize); - free(key2.dptr); - return str; -} - -static VALUE -fgdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone) -{ - VALUE valstr; - - valstr = rb_gdbm_fetch3(obj, keystr); - if (NIL_P(valstr)) { - if (ifnone == Qnil && rb_block_given_p()) - return rb_yield(keystr); - return ifnone; - } - return valstr; -} - -/* - * call-seq: - * gdbm[key] -> value - * - * Retrieves the _value_ corresponding to _key_. - */ -static VALUE -fgdbm_aref(VALUE obj, VALUE keystr) -{ - return rb_gdbm_fetch3(obj, keystr); -} - -/* - * call-seq: - * gdbm.fetch(key [, default]) -> value - * - * Retrieves the _value_ corresponding to _key_. If there is no value - * associated with _key_, _default_ will be returned instead. - */ -static VALUE -fgdbm_fetch_m(int argc, VALUE *argv, VALUE obj) -{ - VALUE keystr, valstr, ifnone; - - rb_scan_args(argc, argv, "11", &keystr, &ifnone); - valstr = fgdbm_fetch(obj, keystr, ifnone); - if (argc == 1 && !rb_block_given_p() && NIL_P(valstr)) - rb_raise(rb_eIndexError, "key not found"); - - return valstr; -} - -/* - * call-seq: - * gdbm.key(value) -> key - * - * Returns the _key_ for a given _value_. If several keys may map to the - * same value, the key that is found first will be returned. - */ -static VALUE -fgdbm_key(VALUE obj, VALUE valstr) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr2; - - ExportStringValue(valstr); - GetDBM2(obj, dbmp, dbm); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - valstr2 = rb_gdbm_fetch2(dbm, keystr); - if (!NIL_P(valstr2) && - (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) && - memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2), - (int)RSTRING_LEN(valstr)) == 0) { - return keystr; - } - } - return Qnil; -} - -/* :nodoc: */ -static VALUE -fgdbm_index(VALUE obj, VALUE value) -{ - rb_warn("GDBM#index is deprecated; use GDBM#key"); - return fgdbm_key(obj, value); -} - -/* - * call-seq: - * gdbm.select { |key, value| block } -> array - * - * Returns a new array of all key-value pairs of the database for which _block_ - * evaluates to true. - */ -static VALUE -fgdbm_select(VALUE obj) -{ - VALUE new = rb_ary_new(); - GDBM_FILE dbm; - struct dbmdata *dbmp; - VALUE keystr; - - GetDBM2(obj, dbmp, dbm); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)); - VALUE v = rb_yield(assoc); - - if (RTEST(v)) { - rb_ary_push(new, assoc); - } - GetDBM2(obj, dbmp, dbm); - } - - return new; -} - -/* - * call-seq: - * gdbm.values_at(key, ...) -> array - * - * Returns an array of the values associated with each specified _key_. - */ -static VALUE -fgdbm_values_at(int argc, VALUE *argv, VALUE obj) -{ - VALUE new = rb_ary_new2(argc); - int i; - - for (i=0; idi_size = -1; - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - else if (dbmp->di_size >= 0) { - dbmp->di_size--; - } - return obj; -} - -/* - * call-seq: - * gdbm.delete(key) -> value or nil - * - * Removes the key-value-pair with the specified _key_ from this database and - * returns the corresponding _value_. Returns nil if the database is empty. - */ -static VALUE -fgdbm_delete(VALUE obj, VALUE keystr) -{ - VALUE valstr; - - valstr = fgdbm_fetch(obj, keystr, Qnil); - rb_gdbm_delete(obj, keystr); - return valstr; -} - -/* - * call-seq: - * gdbm.shift -> (key, value) or nil - * - * Removes a key-value-pair from this database and returns it as a - * two-item array [ _key_, _value_ ]. Returns nil if the database is empty. - */ -static VALUE -fgdbm_shift(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr; - - rb_gdbm_modify(obj); - GetDBM2(obj, dbmp, dbm); - keystr = rb_gdbm_firstkey(dbm); - if (NIL_P(keystr)) return Qnil; - valstr = rb_gdbm_fetch2(dbm, keystr); - rb_gdbm_delete(obj, keystr); - - return rb_assoc_new(keystr, valstr); -} - -/* - * call-seq: - * gdbm.delete_if { |key, value| block } -> gdbm - * gdbm.reject! { |key, value| block } -> gdbm - * - * Deletes every key-value pair from _gdbm_ for which _block_ evaluates to true. - */ -static VALUE -fgdbm_delete_if(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr; - VALUE ret, ary = rb_ary_tmp_new(0); - long i; - int status = 0, n; - - rb_gdbm_modify(obj); - GetDBM2(obj, dbmp, dbm); - n = dbmp->di_size; - dbmp->di_size = -1; - - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - OBJ_FREEZE(keystr); - valstr = rb_gdbm_fetch2(dbm, keystr); - ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status); - if (status != 0) break; - if (RTEST(ret)) rb_ary_push(ary, keystr); - GetDBM2(obj, dbmp, dbm); - } - - for (i = 0; i < RARRAY_LEN(ary); i++) - rb_gdbm_delete(obj, RARRAY_AREF(ary, i)); - if (status) rb_jump_tag(status); - if (n > 0) dbmp->di_size = n - (int)RARRAY_LEN(ary); - rb_ary_clear(ary); - - return obj; -} - -/* - * call-seq: - * gdbm.clear -> gdbm - * - * Removes all the key-value pairs within _gdbm_. - */ -static VALUE -fgdbm_clear(VALUE obj) -{ - datum key, nextkey; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_gdbm_modify(obj); - GetDBM2(obj, dbmp, dbm); - dbmp->di_size = -1; - -#if 0 - while (key = gdbm_firstkey(dbm), key.dptr) { - if (gdbm_delete(dbm, key)) { - free(key.dptr); - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - free(key.dptr); - } -#else - while (key = gdbm_firstkey(dbm), key.dptr) { - for (; key.dptr; key = nextkey) { - nextkey = gdbm_nextkey(dbm, key); - if (gdbm_delete(dbm, key)) { - free(key.dptr); - if (nextkey.dptr) free(nextkey.dptr); - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - free(key.dptr); - } - } -#endif - dbmp->di_size = 0; - - return obj; -} - -/* - * call-seq: - * gdbm.invert -> hash - * - * Returns a hash created by using _gdbm_'s values as keys, and the keys - * as values. - */ -static VALUE -fgdbm_invert(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr; - VALUE hash = rb_hash_new(); - - GetDBM2(obj, dbmp, dbm); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - valstr = rb_gdbm_fetch2(dbm, keystr); - - rb_hash_aset(hash, valstr, keystr); - } - return hash; -} - -/* - * call-seq: - * gdbm[key]= value -> value - * gdbm.store(key, value) -> value - * - * Associates the value _value_ with the specified _key_. - */ -static VALUE -fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr) -{ - datum key, val; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_gdbm_modify(obj); - ExportStringValue(keystr); - ExportStringValue(valstr); - - key.dptr = RSTRING_PTR(keystr); - key.dsize = RSTRING_LENINT(keystr); - - val.dptr = RSTRING_PTR(valstr); - val.dsize = RSTRING_LENINT(valstr); - - GetDBM2(obj, dbmp, dbm); - dbmp->di_size = -1; - if (gdbm_store(dbm, key, val, GDBM_REPLACE)) { - if (errno == EPERM) rb_sys_fail(0); - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - - return valstr; -} - -static VALUE -update_i(RB_BLOCK_CALL_FUNC_ARGLIST(pair, dbm)) -{ - const VALUE *ptr; - Check_Type(pair, T_ARRAY); - if (RARRAY_LEN(pair) < 2) { - rb_raise(rb_eArgError, "pair must be [key, value]"); - } - ptr = RARRAY_CONST_PTR(pair); - fgdbm_store(dbm, ptr[0], ptr[1]); - return Qnil; -} - -/* - * call-seq: - * gdbm.update(other) -> gdbm - * - * Adds the key-value pairs of _other_ to _gdbm_, overwriting entries with - * duplicate keys with those from _other_. _other_ must have an each_pair - * method. - */ -static VALUE -fgdbm_update(VALUE obj, VALUE other) -{ - rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj); - return obj; -} - -/* - * call-seq: - * gdbm.replace(other) -> gdbm - * - * Replaces the content of _gdbm_ with the key-value pairs of _other_. - * _other_ must have an each_pair method. - */ -static VALUE -fgdbm_replace(VALUE obj, VALUE other) -{ - fgdbm_clear(obj); - rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj); - return obj; -} - -/* - * call-seq: - * gdbm.length -> fixnum - * gdbm.size -> fixnum - * - * Returns the number of key-value pairs in this database. - */ -static VALUE -fgdbm_length(VALUE obj) -{ - datum key, nextkey; - struct dbmdata *dbmp; - GDBM_FILE dbm; - int i = 0; - - GetDBM2(obj, dbmp, dbm); - if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size); - - for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) { - nextkey = gdbm_nextkey(dbm, key); - free(key.dptr); - i++; - } - dbmp->di_size = i; - - return INT2FIX(i); -} - -/* - * call-seq: - * gdbm.empty? -> true or false - * - * Returns true if the database is empty. - */ -static VALUE -fgdbm_empty_p(VALUE obj) -{ - datum key; - struct dbmdata *dbmp; - GDBM_FILE dbm; - - GetDBM(obj, dbmp); - if (dbmp->di_size < 0) { - dbm = dbmp->di_dbm; - - key = gdbm_firstkey(dbm); - if (key.dptr) { - free(key.dptr); - return Qfalse; - } - return Qtrue; - } - - if (dbmp->di_size == 0) return Qtrue; - return Qfalse; -} - -/* - * call-seq: - * gdbm.each_value { |value| block } -> gdbm - * - * Executes _block_ for each key in the database, passing the corresponding - * _value_ as a parameter. - */ -static VALUE -fgdbm_each_value(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr; - - RETURN_ENUMERATOR(obj, 0, 0); - - GetDBM2(obj, dbmp, dbm); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_yield(rb_gdbm_fetch2(dbm, keystr)); - GetDBM2(obj, dbmp, dbm); - } - return obj; -} - -/* - * call-seq: - * gdbm.each_key { |key| block } -> gdbm - * - * Executes _block_ for each key in the database, passing the - * _key_ as a parameter. - */ -static VALUE -fgdbm_each_key(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr; - - RETURN_ENUMERATOR(obj, 0, 0); - - GetDBM2(obj, dbmp, dbm); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_yield(keystr); - GetDBM2(obj, dbmp, dbm); - } - return obj; -} - -/* - * call-seq: - * gdbm.each_pair { |key, value| block } -> gdbm - * - * Executes _block_ for each key in the database, passing the _key_ and the - * corresponding _value_ as a parameter. - */ -static VALUE -fgdbm_each_pair(VALUE obj) -{ - GDBM_FILE dbm; - struct dbmdata *dbmp; - VALUE keystr; - - RETURN_ENUMERATOR(obj, 0, 0); - - GetDBM2(obj, dbmp, dbm); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr))); - GetDBM2(obj, dbmp, dbm); - } - - return obj; -} - -/* - * call-seq: - * gdbm.keys -> array - * - * Returns an array of all keys of this database. - */ -static VALUE -fgdbm_keys(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, ary; - - GetDBM2(obj, dbmp, dbm); - ary = rb_ary_new(); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_ary_push(ary, keystr); - } - - return ary; -} - -/* - * call-seq: - * gdbm.values -> array - * - * Returns an array of all values of this database. - */ -static VALUE -fgdbm_values(VALUE obj) -{ - datum key, nextkey; - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE valstr, ary; - - GetDBM2(obj, dbmp, dbm); - ary = rb_ary_new(); - for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) { - nextkey = gdbm_nextkey(dbm, key); - valstr = rb_gdbm_fetch(dbm, key); - free(key.dptr); - rb_ary_push(ary, valstr); - } - - return ary; -} - -/* - * call-seq: - * gdbm.include?(k) -> true or false - * gdbm.has_key?(k) -> true or false - * gdbm.member?(k) -> true or false - * gdbm.key?(k) -> true or false - * - * Returns true if the given key _k_ exists within the database. - * Returns false otherwise. - */ -static VALUE -fgdbm_has_key(VALUE obj, VALUE keystr) -{ - datum key; - struct dbmdata *dbmp; - GDBM_FILE dbm; - long len; - - ExportStringValue(keystr); - len = RSTRING_LENINT(keystr); - if (TOO_LONG(len)) return Qfalse; - key.dptr = RSTRING_PTR(keystr); - key.dsize = (int)len; - - GetDBM2(obj, dbmp, dbm); - if (gdbm_exists(dbm, key)) - return Qtrue; - return Qfalse; -} - -/* - * call-seq: - * gdbm.has_value?(v) -> true or false - * gdbm.value?(v) -> true or false - * - * Returns true if the given value _v_ exists within the database. - * Returns false otherwise. - */ -static VALUE -fgdbm_has_value(VALUE obj, VALUE valstr) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, valstr2; - - ExportStringValue(valstr); - GetDBM2(obj, dbmp, dbm); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - valstr2 = rb_gdbm_fetch2(dbm, keystr); - - if (!NIL_P(valstr2) && - (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) && - memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2), - (int)RSTRING_LEN(valstr)) == 0) { - return Qtrue; - } - } - return Qfalse; -} - -/* - * call-seq: - * gdbm.to_a -> array - * - * Returns an array of all key-value pairs contained in the database. - */ -static VALUE -fgdbm_to_a(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, ary; - - GetDBM2(obj, dbmp, dbm); - ary = rb_ary_new(); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr))); - } - - return ary; -} - -/* - * call-seq: - * gdbm.reorganize -> gdbm - * - * Reorganizes the database file. This operation removes reserved space of - * elements that have already been deleted. It is only useful after a lot of - * deletions in the database. - */ -static VALUE -fgdbm_reorganize(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_gdbm_modify(obj); - GetDBM2(obj, dbmp, dbm); - gdbm_reorganize(dbm); - rb_fd_fix_cloexec(gdbm_fdesc(dbm)); - return obj; -} - -/* - * call-seq: - * gdbm.sync -> gdbm - * - * Unless the _gdbm_ object has been opened with the *SYNC* flag, it is not - * guaranteed that database modification operations are immediately applied to - * the database file. This method ensures that all recent modifications - * to the database are written to the file. Blocks until all writing operations - * to the disk have been finished. - */ -static VALUE -fgdbm_sync(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - - rb_gdbm_modify(obj); - GetDBM2(obj, dbmp, dbm); - gdbm_sync(dbm); - return obj; -} - -/* - * call-seq: - * gdbm.cachesize = size -> size - * - * Sets the size of the internal bucket cache to _size_. - */ -static VALUE -fgdbm_set_cachesize(VALUE obj, VALUE val) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - int optval; - - GetDBM2(obj, dbmp, dbm); - optval = FIX2INT(val); - if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) { - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - return val; -} - -/* - * call-seq: - * gdbm.fastmode = boolean -> boolean - * - * Turns the database's fast mode on or off. If fast mode is turned on, gdbm - * does not wait for writes to be flushed to the disk before continuing. - * - * This option is obsolete for gdbm >= 1.8 since fast mode is turned on by - * default. See also: #syncmode= - */ -static VALUE -fgdbm_set_fastmode(VALUE obj, VALUE val) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - int optval; - - GetDBM2(obj, dbmp, dbm); - optval = 0; - if (RTEST(val)) - optval = 1; - - if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) { - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - return val; -} - -/* - * call-seq: - * gdbm.syncmode = boolean -> boolean - * - * Turns the database's synchronization mode on or off. If the synchronization - * mode is turned on, the database's in-memory state will be synchronized to - * disk after every database modification operation. If the synchronization - * mode is turned off, GDBM does not wait for writes to be flushed to the disk - * before continuing. - * - * This option is only available for gdbm >= 1.8 where syncmode is turned off - * by default. See also: #fastmode= - */ -static VALUE -fgdbm_set_syncmode(VALUE obj, VALUE val) -{ -#if !defined(GDBM_SYNCMODE) - fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue); - return val; -#else - struct dbmdata *dbmp; - GDBM_FILE dbm; - int optval; - - GetDBM2(obj, dbmp, dbm); - optval = 0; - if (RTEST(val)) - optval = 1; - - if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) { - rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno)); - } - return val; -#endif -} - -/* - * call-seq: - * gdbm.to_hash -> hash - * - * Returns a hash of all key-value pairs contained in the database. - */ -static VALUE -fgdbm_to_hash(VALUE obj) -{ - struct dbmdata *dbmp; - GDBM_FILE dbm; - VALUE keystr, hash; - - GetDBM2(obj, dbmp, dbm); - hash = rb_hash_new(); - for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); - keystr = rb_gdbm_nextkey(dbm, keystr)) { - - rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr)); - } - - return hash; -} - -/* - * call-seq: - * gdbm.reject { |key, value| block } -> hash - * - * Returns a hash copy of _gdbm_ where all key-value pairs from _gdbm_ for - * which _block_ evaluates to true are removed. See also: #delete_if - */ -static VALUE -fgdbm_reject(VALUE obj) -{ - return rb_hash_delete_if(fgdbm_to_hash(obj)); -} - -void -Init_gdbm(void) -{ - rb_cGDBM = rb_define_class("GDBM", rb_cObject); - rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError); - rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException); - rb_include_module(rb_cGDBM, rb_mEnumerable); - - rb_define_alloc_func(rb_cGDBM, fgdbm_s_alloc); - rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1); - - rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1); - rb_define_method(rb_cGDBM, "close", fgdbm_close, 0); - rb_define_method(rb_cGDBM, "closed?", fgdbm_closed, 0); - rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1); - rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1); - rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2); - rb_define_method(rb_cGDBM, "store", fgdbm_store, 2); - rb_define_method(rb_cGDBM, "index", fgdbm_index, 1); - rb_define_method(rb_cGDBM, "key", fgdbm_key, 1); - rb_define_method(rb_cGDBM, "select", fgdbm_select, 0); - rb_define_method(rb_cGDBM, "values_at", fgdbm_values_at, -1); - rb_define_method(rb_cGDBM, "length", fgdbm_length, 0); - rb_define_method(rb_cGDBM, "size", fgdbm_length, 0); - rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0); - rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0); - rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0); - rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0); - rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0); - rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0); - rb_define_method(rb_cGDBM, "values", fgdbm_values, 0); - rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0); - rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1); - rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0); - rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0); - rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0); - rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0); - rb_define_method(rb_cGDBM, "invert", fgdbm_invert, 0); - rb_define_method(rb_cGDBM, "update", fgdbm_update, 1); - rb_define_method(rb_cGDBM, "replace", fgdbm_replace, 1); - rb_define_method(rb_cGDBM, "reorganize", fgdbm_reorganize, 0); - rb_define_method(rb_cGDBM, "sync", fgdbm_sync, 0); - /* rb_define_method(rb_cGDBM, "setopt", fgdbm_setopt, 2); */ - rb_define_method(rb_cGDBM, "cachesize=", fgdbm_set_cachesize, 1); - rb_define_method(rb_cGDBM, "fastmode=", fgdbm_set_fastmode, 1); - rb_define_method(rb_cGDBM, "syncmode=", fgdbm_set_syncmode, 1); - - rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1); - rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1); - rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1); - - rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0); - rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0); - - /* flag for #new and #open: open database as a reader */ - rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER|RUBY_GDBM_RW_BIT)); - /* flag for #new and #open: open database as a writer */ - rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER|RUBY_GDBM_RW_BIT)); - /* flag for #new and #open: open database as a writer; if the database does not exist, create a new one */ - rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT|RUBY_GDBM_RW_BIT)); - /* flag for #new and #open: open database as a writer; overwrite any existing databases */ - rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB|RUBY_GDBM_RW_BIT)); - - /* flag for #new and #open. this flag is obsolete for gdbm >= 1.8 */ - rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST)); - /* this flag is obsolete in gdbm 1.8. - On gdbm 1.8, fast mode is default behavior. */ - - /* gdbm version 1.8 specific */ -#if defined(GDBM_SYNC) - /* flag for #new and #open. only for gdbm >= 1.8 */ - rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC)); -#endif -#if defined(GDBM_NOLOCK) - /* flag for #new and #open */ - rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK)); -#endif - /* version of the gdbm library*/ - rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version)); -} diff --git a/ext/gdbm/gdbm.gemspec b/ext/gdbm/gdbm.gemspec deleted file mode 100644 index d15d20847d..0000000000 --- a/ext/gdbm/gdbm.gemspec +++ /dev/null @@ -1,22 +0,0 @@ -# coding: utf-8 -# frozen_string_literal: true - -Gem::Specification.new do |spec| - spec.name = "gdbm" - spec.version = "2.1.0" - spec.authors = ["Yukihiro Matsumoto"] - spec.email = ["matz@ruby-lang.org"] - - spec.summary = "Ruby extension for GNU dbm." - spec.description = "Ruby extension for GNU dbm." - spec.homepage = "https://github.com/ruby/gdbm" - spec.licenses = ["Ruby", "BSD-2-Clause"] - - spec.files = ["ext/gdbm/extconf.rb", "ext/gdbm/gdbm.c"] - spec.bindir = "exe" - spec.executables = [] - spec.require_paths = ["lib"] - spec.extensions = ["ext/gdbm/extconf.rb"] - spec.required_ruby_version = ">= 2.3.0" - spec.metadata["msys2_mingw_dependencies"] = "gdbm" -end diff --git a/test/gdbm/test_gdbm.rb b/test/gdbm/test_gdbm.rb deleted file mode 100644 index e02282b451..0000000000 --- a/test/gdbm/test_gdbm.rb +++ /dev/null @@ -1,734 +0,0 @@ -# frozen_string_literal: false -begin - require 'gdbm' -rescue LoadError -end - -if defined? GDBM - require 'test/unit' - require 'envutil' unless defined?(EnvUtil) - require 'tmpdir' - require 'fileutils' - - class TestGDBM_RDONLY < Test::Unit::TestCase - def TestGDBM_RDONLY.uname_s - require 'rbconfig' - case RbConfig::CONFIG['target_os'] - when 'cygwin' - require 'etc' - Etc.uname[:sysname] - else - RbConfig::CONFIG['target_os'] - end - end - SYSTEM = uname_s - - def setup - @tmpdir = Dir.mktmpdir("tmptest_gdbm") - @prefix = "tmptest_gdbm_#{$$}" - @path = "#{@tmpdir}/#{@prefix}_" - - # prepare to make readonly GDBM file - GDBM.open("#{@tmpdir}/#{@prefix}_rdonly", 0400) {|gdbm| - gdbm['foo'] = 'FOO' - } - assert_instance_of(GDBM, @gdbm_rdonly = GDBM.new("#{@tmpdir}/#{@prefix}_rdonly", nil)) - end - def teardown - assert_nil(@gdbm_rdonly.close) - ObjectSpace.each_object(GDBM) do |obj| - obj.close unless obj.closed? - end - FileUtils.remove_entry_secure @tmpdir - end - - def test_delete_rdonly - skip("skipped because root can open anything") if Process.uid == 0 - - if /^CYGWIN_9/ !~ SYSTEM - assert_raise(GDBMError) { - @gdbm_rdonly.delete("foo") - } - - assert_nil(@gdbm_rdonly.delete("bar")) - end - end - end - - class TestGDBM < Test::Unit::TestCase - SYSTEM = TestGDBM_RDONLY::SYSTEM - - def setup - @tmpdir = Dir.mktmpdir("tmptest_gdbm") - @prefix = "tmptest_gdbm_#{$$}" - @path = "#{@tmpdir}/#{@prefix}_" - assert_instance_of(GDBM, @gdbm = GDBM.new(@path)) - end - def teardown - assert_nil(@gdbm.close) - ObjectSpace.each_object(GDBM) do |obj| - obj.close unless obj.closed? - end - begin - FileUtils.remove_entry_secure @tmpdir - rescue - system("fuser", *Dir.entries(@tmpdir).grep(/\A(?!\.\.?\z)/), chdir: @tmpdir) - else - return - end - FileUtils.remove_entry_secure @tmpdir - end - - def check_size(expect, gdbm=@gdbm) - assert_equal(expect, gdbm.size) - n = 0 - gdbm.each { n+=1 } - assert_equal(expect, n) - if expect == 0 - assert_equal(true, gdbm.empty?) - else - assert_equal(false, gdbm.empty?) - end - end - - def test_s_new_has_no_block - # GDBM.new ignore the block - foo = true - assert_instance_of(GDBM, gdbm = GDBM.new("#{@tmpdir}/#{@prefix}") { foo = false }) - assert_equal(foo, true) - assert_nil(gdbm.close) - end - def test_s_open_create_new - return if /^CYGWIN_9/ =~ SYSTEM - - save_mask = File.umask(0) - begin - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}")) - gdbm.close - assert_equal(File.stat("#{@tmpdir}/#{@prefix}").mode & 0777, 0666) unless /mswin|mingw/ =~ RUBY_PLATFORM - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}2", 0644)) - gdbm.close - assert_equal(File.stat("#{@tmpdir}/#{@prefix}2").mode & 0777, 0644) - ensure - File.umask save_mask - end - end - def test_s_open_no_create - skip "gdbm_open(GDBM_WRITER) is broken on libgdbm 1.8.0" if /1\.8\.0/ =~ GDBM::VERSION - assert_nil(gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", nil)) - ensure - gdbm.close if gdbm - end - def test_s_open_3rd_arg - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644, - GDBM::FAST)) - gdbm.close - - # gdbm 1.8.0 specific - if defined? GDBM::SYNC - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644, - GDBM::SYNC)) - gdbm.close - end - # gdbm 1.8.0 specific - if defined? GDBM::NOLOCK - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644, - GDBM::NOLOCK)) - gdbm.close - end - end - def test_s_open_with_block - assert_equal(GDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo) - end - - def open_db_child(dbname, *opts) - opts = [0644, *opts].map(&:inspect).join(', ') - args = [EnvUtil.rubybin, "-Ilib", "-rgdbm", "-e", <<-SRC, dbname] - STDOUT.sync = true - gdbm = GDBM.open(ARGV.shift, #{opts}) - puts gdbm.class - gets - SRC - IO.popen(args, "r+") do |f| - dbclass = f.gets - assert_equal("GDBM", dbclass.chomp) - yield - end - end - - def test_s_open_lock - skip "GDBM.open would block when opening already locked gdbm file on platforms without flock and with lockf" if /solaris|aix/ =~ RUBY_PLATFORM - - dbname = "#{@tmpdir}/#{@prefix}" - - open_db_child(dbname) do - assert_raise(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { - GDBM.open(dbname, 0644) {|gdbm| - assert(false) - } - } - end - end - -=begin - # Is it guaranteed on many OS? - def test_s_open_lock_one_process - # locking on one process - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644)) - assert_raise(Errno::EWOULDBLOCK) { - begin - GDBM.open("#{@tmpdir}/#{@prefix}", 0644) - rescue Errno::EAGAIN - raise Errno::EWOULDBLOCK - end - } - end -=end - - def test_s_open_nolock - dbname = "#{@tmpdir}/#{@prefix}" - - open_db_child(dbname, GDBM::NOLOCK) do - assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { - GDBM.open(dbname, 0644) {|gdbm2| - assert_instance_of(GDBM, gdbm2) - } - } - end - - STDERR.puts Dir.glob("#{dbname}*") if $DEBUG - - # The following test fails on Windows because flock() implementation - # is different from Unix. - return if /mswin|mingw/ =~ RUBY_PLATFORM - - open_db_child(dbname) do - assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { - # this test is failed on Cygwin98 (???) - GDBM.open(dbname, 0644, GDBM::NOLOCK) {|gdbm2| - assert_instance_of(GDBM, gdbm2) - } - } - end - end if defined? GDBM::NOLOCK # gdbm 1.8.0 specific - - def test_s_open_error - skip "because root can open anything" if Process.uid == 0 - - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0)) - assert_raise(Errno::EACCES, Errno::EWOULDBLOCK) { - GDBM.open("#{@tmpdir}/#{@prefix}", 0) - } - gdbm.close - end - - def test_close - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}")) - assert_nil(gdbm.close) - - # closed GDBM file - assert_raise(RuntimeError) { gdbm.close } - end - - def test_aref - assert_equal('bar', @gdbm['foo'] = 'bar') - assert_equal('bar', @gdbm['foo']) - - assert_nil(@gdbm['bar']) - end - - def test_fetch - assert_equal('bar', @gdbm['foo']='bar') - assert_equal('bar', @gdbm.fetch('foo')) - - # key not found - assert_raise(IndexError) { - @gdbm.fetch('bar') - } - - # test for `ifnone' arg - assert_equal('baz', @gdbm.fetch('bar', 'baz')) - - # test for `ifnone' block - assert_equal('foobar', @gdbm.fetch('bar') {|key| 'foo' + key }) - end - - def test_aset - num = 0 - 2.times {|i| - assert_equal('foo', @gdbm['foo'] = 'foo') - assert_equal('foo', @gdbm['foo']) - assert_equal('bar', @gdbm['foo'] = 'bar') - assert_equal('bar', @gdbm['foo']) - - num += 1 if i == 0 - assert_equal(num, @gdbm.size) - - # assign nil - assert_equal('', @gdbm['bar'] = '') - assert_equal('', @gdbm['bar']) - - num += 1 if i == 0 - assert_equal(num, @gdbm.size) - - # empty string - assert_equal('', @gdbm[''] = '') - assert_equal('', @gdbm['']) - - num += 1 if i == 0 - assert_equal(num, @gdbm.size) - - # Integer - assert_equal('200', @gdbm['100'] = '200') - assert_equal('200', @gdbm['100']) - - num += 1 if i == 0 - assert_equal(num, @gdbm.size) - - # Big key and value - assert_equal('y' * 100, @gdbm['x' * 100] = 'y' * 100) - assert_equal('y' * 100, @gdbm['x' * 100]) - - num += 1 if i == 0 - assert_equal(num, @gdbm.size) - } - end - - def test_key - assert_equal('bar', @gdbm['foo'] = 'bar') - assert_equal('foo', @gdbm.key('bar')) - assert_nil(@gdbm['bar']) - end - - def test_values_at - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - assert_equal(values.reverse, @gdbm.values_at(*keys.reverse)) - end - - def test_select_with_block - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - ret = @gdbm.select {|k,v| - assert_equal(k.upcase, v) - k != "bar" - } - assert_equal([['baz', 'BAZ'], ['foo', 'FOO']], - ret.sort) - end - - def test_length - num = 10 - assert_equal(0, @gdbm.size) - num.times {|i| - i = i.to_s - @gdbm[i] = i - } - assert_equal(num, @gdbm.size) - - @gdbm.shift - - assert_equal(num - 1, @gdbm.size) - end - - def test_empty? - assert_equal(true, @gdbm.empty?) - @gdbm['foo'] = 'FOO' - assert_equal(false, @gdbm.empty?) - end - - def test_each_pair - n = 0 - @gdbm.each_pair { n += 1 } - assert_equal(0, n) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - n = 0 - ret = @gdbm.each_pair {|key, val| - assert_not_nil(i = keys.index(key)) - assert_equal(val, values[i]) - - n += 1 - } - assert_equal(keys.size, n) - assert_equal(@gdbm, ret) - end - - def test_each_value - n = 0 - @gdbm.each_value { n += 1 } - assert_equal(0, n) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - n = 0 - ret = @gdbm.each_value {|val| - assert_not_nil(key = @gdbm.key(val)) - assert_not_nil(i = keys.index(key)) - assert_equal(val, values[i]) - - n += 1 - } - assert_equal(keys.size, n) - assert_equal(@gdbm, ret) - end - - def test_each_key - n = 0 - @gdbm.each_key { n += 1 } - assert_equal(0, n) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - n = 0 - ret = @gdbm.each_key {|key| - assert_not_nil(i = keys.index(key)) - assert_equal(@gdbm[key], values[i]) - - n += 1 - } - assert_equal(keys.size, n) - assert_equal(@gdbm, ret) - end - - def test_each_key_without_block - assert_kind_of Enumerator, @gdbm.each_key - end - - def test_keys - assert_equal([], @gdbm.keys) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - assert_equal(keys.sort, @gdbm.keys.sort) - assert_equal(values.sort, @gdbm.values.sort) - end - - def test_values - test_keys - end - - def test_shift - assert_nil(@gdbm.shift) - assert_equal(0, @gdbm.size) - - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - ret_keys = [] - ret_values = [] - while ret = @gdbm.shift - ret_keys.push ret[0] - ret_values.push ret[1] - - assert_equal(keys.size - ret_keys.size, @gdbm.size) - end - - assert_equal(keys.sort, ret_keys.sort) - assert_equal(values.sort, ret_values.sort) - end - - def test_delete - keys = %w(foo bar baz) - values = %w(FOO BAR BAZ) - key = keys[1] - - assert_nil(@gdbm.delete(key)) - assert_equal(0, @gdbm.size) - - @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values - - assert_equal('BAR', @gdbm.delete(key)) - assert_nil(@gdbm[key]) - assert_equal(2, @gdbm.size) - - assert_nil(@gdbm.delete(key)) - end - - def test_delete_with_block - key = 'no called block' - @gdbm[key] = 'foo' - assert_equal('foo', @gdbm.delete(key) {|k| k.replace 'called block'}) - assert_equal('no called block', key) - assert_equal(0, @gdbm.size) - - key = 'no called block' - assert_equal(:blockval, - @gdbm.delete(key) {|k| k.replace 'called block'; :blockval}) - assert_equal('called block', key) - assert_equal(0, @gdbm.size) - end - - def test_delete_if - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - - ret = @gdbm.delete_if {|key, val| key.to_i < 50} - assert_equal(@gdbm, ret) - check_size(50, @gdbm) - - ret = @gdbm.delete_if {|key, val| key.to_i >= 50} - assert_equal(@gdbm, ret) - check_size(0, @gdbm) - - # break - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - check_size(100, @gdbm) - n = 0; - @gdbm.delete_if {|key, val| - break if n > 50 - n+=1 - true - } - assert_equal(51, n) - check_size(49, @gdbm) - - @gdbm.clear - - # raise - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - check_size(100, @gdbm) - n = 0; - begin - @gdbm.delete_if {|key, val| - raise "runtime error" if n > 50 - n+=1 - true - } - rescue RuntimeError - end - assert_equal(51, n) - check_size(49, @gdbm) - end - - def test_reject - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - - hash = @gdbm.reject {|key, val| key.to_i < 50} - assert_instance_of(Hash, hash) - assert_equal(100, @gdbm.size) - - assert_equal(50, hash.size) - hash.each_pair {|key,val| - assert_equal(false, key.to_i < 50) - assert_equal(key, val) - } - - hash = @gdbm.reject {|key, val| key.to_i < 100} - assert_instance_of(Hash, hash) - assert_equal(true, hash.empty?) - end - - def test_clear - v = "1" - 100.times {v = v.next; @gdbm[v] = v} - - assert_equal(@gdbm, @gdbm.clear) - - # validate GDBM#size - i = 0 - @gdbm.each { i += 1 } - assert_equal(@gdbm.size, i) - assert_equal(0, i) - end - - def test_invert - v = "0" - 100.times {@gdbm[v] = v; v = v.next} - - hash = @gdbm.invert - assert_instance_of(Hash, hash) - assert_equal(100, hash.size) - hash.each_pair {|key, val| - assert_equal(key.to_i, val.to_i) - } - end - - def test_update - hash = {} - v = "0" - 100.times {v = v.next; hash[v] = v} - - @gdbm["101"] = "101" - @gdbm.update hash - assert_equal(101, @gdbm.size) - @gdbm.each_pair {|key, val| - assert_equal(key.to_i, val.to_i) - } - end - - def test_replace - hash = {} - v = "0" - 100.times {v = v.next; hash[v] = v} - - @gdbm["101"] = "101" - @gdbm.replace hash - assert_equal(100, @gdbm.size) - @gdbm.each_pair {|key, val| - assert_equal(key.to_i, val.to_i) - } - end - - def test_reorganize - size1 = File.size(@path) - i = "1" - 1000.times {i = i.next; @gdbm[i] = i} - @gdbm.clear - @gdbm.sync - - size2 = File.size(@path) - @gdbm.reorganize - @gdbm.sync - size3 = File.size(@path) - - # p [size1, size2, size3] - assert_equal(true, size1 < size2) - # this test is failed on Cygwin98. `GDBM version 1.8.0, as of May 19, 1999' - assert_equal(true, size3 < size2) - assert_equal(size1, size3) - end - - def test_sync - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0666, GDBM::FAST)) - assert_equal(gdbm.sync, gdbm) - gdbm.close - assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0666)) - assert_equal(gdbm.sync, gdbm) - gdbm.close - end - - def test_cachesize= - assert_equal(@gdbm.cachesize = 1024, 1024) - end - - def test_fastmode= - assert_equal(@gdbm.fastmode = true, true) - end - - def test_syncmode= - assert_equal(@gdbm.syncmode = true, true) - end - - def test_haskey? - assert_equal('bar', @gdbm['foo']='bar') - assert_equal(true, @gdbm.has_key?('foo')) - assert_equal(false, @gdbm.has_key?('bar')) - end - - def test_has_value? - assert_equal('bar', @gdbm['foo']='bar') - assert_equal(true, @gdbm.has_value?('bar')) - assert_equal(false, @gdbm.has_value?('foo')) - end - - def test_to_a - v = "0" - 100.times {v = v.next; @gdbm[v] = v} - - ary = @gdbm.to_a - assert_instance_of(Array, ary) - assert_equal(100, ary.size) - ary.each {|key,val| - assert_equal(key.to_i, val.to_i) - } - end - - def test_to_hash - v = "0" - 100.times {v = v.next; @gdbm[v] = v} - - hash = @gdbm.to_hash - assert_instance_of(Hash, hash) - assert_equal(100, hash.size) - hash.each {|key,val| - assert_equal(key.to_i, val.to_i) - } - end - end - - class TestGDBM2 < Test::Unit::TestCase - def setup - @tmproot = Dir.mktmpdir('ruby-gdbm') - end - - def teardown - FileUtils.remove_entry_secure @tmproot if File.directory?(@tmproot) - end - - def test_reader_open_notexist - assert_raise(Errno::ENOENT) { - GDBM.open("#{@tmproot}/a", 0666, GDBM::READER) - } - end - - def test_writer_open_notexist - skip "gdbm_open(GDBM_WRITER) is broken on libgdbm 1.8.0" if /1\.8\.0/ =~ GDBM::VERSION - - assert_raise(Errno::ENOENT) { - GDBM.open("#{@tmproot}/a", 0666, GDBM::WRITER) - } - end - - def test_wrcreat_open_notexist - v = GDBM.open("#{@tmproot}/a", 0666, GDBM::WRCREAT) - assert_instance_of(GDBM, v) - v.close - end - - def test_newdb_open_notexist - v = GDBM.open("#{@tmproot}/a", 0666, GDBM::NEWDB) - assert_instance_of(GDBM, v) - v.close - end - - def test_reader_open - GDBM.open("#{@tmproot}/a.dbm") {} # create a db. - v = GDBM.open("#{@tmproot}/a.dbm", nil, GDBM::READER) {|d| - assert_raise(GDBMError) { d["k"] = "v" } - true - } - assert(v) - end - - def test_newdb_open - GDBM.open("#{@tmproot}/a.dbm") {|dbm| - dbm["k"] = "v" - } - v = GDBM.open("#{@tmproot}/a.dbm", nil, GDBM::NEWDB) {|d| - assert_equal(0, d.length) - assert_nil(d["k"]) - true - } - assert(v) - end - - def test_freeze - GDBM.open("#{@tmproot}/a.dbm") {|d| - d.freeze - expected_error = defined?(FrozenError) ? FrozenError : RuntimeError - assert_raise(expected_error) { d["k"] = "v" } - } - end - end -end diff --git a/tool/sync_default_gems.rb b/tool/sync_default_gems.rb index f60bb6aff3..ad5b6c402a 100644 --- a/tool/sync_default_gems.rb +++ b/tool/sync_default_gems.rb @@ -17,7 +17,6 @@ REPOSITORIES = { "io-wait": 'ruby/io-wait', csv: 'ruby/csv', dbm: 'ruby/dbm', - gdbm: 'ruby/gdbm', etc: 'ruby/etc', date: 'ruby/date', zlib: 'ruby/zlib', @@ -191,12 +190,6 @@ def sync_default_gems(gem) cp_r("#{upstream}/test/io/wait", "test/io") cp_r("#{upstream}/io-wait.gemspec", "ext/io/wait") `git checkout ext/io/wait/depend` - when "gdbm" - rm_rf(%w[ext/gdbm test/gdbm]) - cp_r("#{upstream}/ext/gdbm", "ext") - cp_r("#{upstream}/test/gdbm", "test") - cp_r("#{upstream}/gdbm.gemspec", "ext/gdbm") - `git checkout ext/gdbm/depend ext/gdbm/README` when "etc" rm_rf(%w[ext/etc test/etc]) cp_r("#{upstream}/ext/etc", "ext")