<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># GDBM_File.pm -- Perl 5 interface to GNU gdbm library.

=head1 NAME

GDBM_File - Perl5 access to the gdbm library.

=head1 SYNOPSIS

    use GDBM_File;
    [$db =] tie %hash, 'GDBM_File', $filename, GDBM_WRCREAT, 0640
                or die "$GDBM_File::gdbm_errno";
    # Use the %hash...

    $e = $db-&gt;errno;
    $e = $db-&gt;syserrno;
    $str = $db-&gt;strerror;
    $bool = $db-&gt;needs_recovery;

    $db-&gt;clear_error;

    $db-&gt;reorganize;
    $db-&gt;sync;

    $n = $db-&gt;count;

    $n = $db-&gt;flags;

    $str = $db-&gt;dbname;

    $db-&gt;cache_size;
    $db-&gt;cache_size($newsize);

    $n = $db-&gt;block_size;

    $bool = $db-&gt;sync_mode;
    $db-&gt;sync_mode($bool);

    $bool = $db-&gt;centfree;
    $db-&gt;centfree($bool);

    $bool = $db-&gt;coalesce;
    $db-&gt;coalesce($bool);

    $bool = $db-&gt;mmap;

    $size = $db-&gt;mmapsize;
    $db-&gt;mmapsize($newsize);

    $db-&gt;recover(%args);

    untie %hash ;

=head1 DESCRIPTION

B&lt;GDBM_File&gt; is a module which allows Perl programs to make use of the
facilities provided by the GNU gdbm library.  If you intend to use this
module you should really have a copy of the B&lt;GDBM manual&gt; at hand.
The manual is avaialble online at
L&lt;https://www.gnu.org.ua/software/gdbm/manual&gt;.

Most of the B&lt;gdbm&gt; functions are available through the B&lt;GDBM_File&gt;
interface.

Unlike Perl's built-in hashes, it is not safe to C&lt;delete&gt; the current
item from a GDBM_File tied hash while iterating over it with C&lt;each&gt;.
This is a limitation of the gdbm library.

=head2 Tie

Use the Perl built-in B&lt;tie&gt; to associate a B&lt;GDBM&gt; database with a Perl
hash:

   tie %hash, 'GDBM_File', $filename, $flags, $mode;

Here, I&lt;$filename&gt; is the name of the database file to open or create.
I&lt;$flags&gt; is a bitwise OR of I&lt;access mode&gt; and optional I&lt;modifiers&gt;.
Access mode is one of:

=over 4

=item B&lt;GDBM_READER&gt;

Open existing database file in read-only mode.

=item B&lt;GDBM_WRITER&gt;

Open existing database file in read-write mode.

=item B&lt;GDBM_WRCREAT&gt;

If the database file exists, open it in read-write mode.  If it doesn't,
create it first and open read-write.

=item B&lt;GDBM_NEWDB&gt;

Create new database and open it read-write.  If the database already exists,
truncate it first.

=back

A number of modifiers can be OR'd to the access mode.  Most of them are
rarely needed (see L&lt;https://www.gnu.org.ua/software/gdbm/manual/Open.html&gt;
for a complete list), but one is worth mentioning.  The B&lt;GDBM_NUMSYNC&gt;
modifier, when used with B&lt;GDBM_NEWDB&gt;, instructs B&lt;GDBM&gt; to create the
database in I&lt;extended&gt; (so called I&lt;numsync&gt;) format.  This format is
best suited for crash-tolerant implementations.  See B&lt;CRASH TOLERANCE&gt;
below for more information.

The I&lt;$mode&gt; parameter is the file mode for creating new database
file.  Use an octal constant or a combination of C&lt;S_I*&gt; constants
from the B&lt;Fcntl&gt; module.  This parameter is used if I&lt;$flags&gt; is
B&lt;GDBM_NEWDB&gt; or B&lt;GDBM_WRCREAT&gt;.

On success, B&lt;tie&gt; returns an object of class B&lt;GDBM_File&gt;.  On failure,
it returns B&lt;undef&gt;.  It is recommended to always check the return value,
to make sure your hash is successfully associated with the database file.
See B&lt;ERROR HANDLING&gt; below for examples.

=head1 STATIC METHODS

=head2 GDBM_version

    $str = GDBM_File-&gt;GDBM_version;
    @ar = GDBM_File-&gt;GDBM_version;

Returns the version number of the underlying B&lt;libgdbm&gt; library. In scalar
context, returns the library version formatted as string:

    MINOR.MAJOR[.PATCH][ (GUESS)]

where I&lt;MINOR&gt;, I&lt;MAJOR&gt;, and I&lt;PATCH&gt; are version numbers, and I&lt;GUESS&gt; is
a guess level (see below).

In list context, returns a list:

    ( MINOR, MAJOR, PATCH [, GUESS] )

The I&lt;GUESS&gt; component is present only if B&lt;libgdbm&gt; version is 1.8.3 or
earlier. This is because earlier releases of B&lt;libgdbm&gt; did not include
information about their version and the B&lt;GDBM_File&gt; module has to implement
certain guesswork in order to determine it. I&lt;GUESS&gt; is a textual description
in string context, and a positive number indicating how rough the guess is
in list context. Possible values are:

=over 4

=item 1  - exact guess

The major and minor version numbers are guaranteed to be correct. The actual
patchlevel is most probably guessed right, but can be 1-2 less than indicated.

=item 2  - approximate

The major and minor number are guaranteed to be correct. The patchlevel is
set to the upper bound.

=item 3  - rough guess

The version is guaranteed to be not newer than B&lt;I&lt;MAJOR&gt;.I&lt;MINOR&gt;&gt;.

=back

=head1 ERROR HANDLING

=head2 $GDBM_File::gdbm_errno

When referenced in numeric context, retrieves the current value of the
B&lt;gdbm_errno&gt; variable, i.e. a numeric code describing the state of the
most recent operation on any B&lt;gdbm&gt; database.  Each numeric code has a
symbolic name associated with it.   For a comprehensive list  of these, see
L&lt;https://www.gnu.org.ua/software/gdbm/manual/Error-codes.html&gt;.  Notice,
that this list includes all error codes defined for the most recent
version of B&lt;gdbm&gt;.  Depending on the actual version of the library
B&lt;GDBM_File&gt; is built with, some of these may be missing.

In string context, B&lt;$gdbm_errno&gt; returns a human-readable description of
the error.  If necessary, this description includes the value of B&lt;$!&gt;.
This makes it possible to use it in diagnostic messages.  For example,
the usual tying sequence is

    tie %hash, 'GDBM_File', $filename, GDBM_WRCREAT, 0640
         or die "$GDBM_File::gdbm_errno";

The following, more complex, example illustrates how you can fall back
to read-only mode if the database file permissions forbid read-write
access:

    use Errno qw(EACCES);
    unless (tie(%hash, 'GDBM_File', $filename, GDBM_WRCREAT, 0640)) {
        if ($GDBM_File::gdbm_errno == GDBM_FILE_OPEN_ERROR
            &amp;&amp; $!{EACCES}) {
            if (tie(%hash, 'GDBM_File', $filename, GDBM_READER, 0640)) {
                die "$GDBM_File::gdbm_errno";
            }
        } else {
            die "$GDBM_File::gdbm_errno";
        }
    }

=head2 gdbm_check_syserr

    if (gdbm_check_syserr(gdbm_errno)) ...

Returns true if the system error number (B&lt;$!&gt;) gives more information on
the cause of the error.

=head1 DATABASE METHODS

=head2 close

    $db-&gt;close;

Closes the database.  Normally you would just do B&lt;untie&gt;.  However, you
will need to use this function if you have explicitly assigned the result
of B&lt;tie&gt; to a variable, and wish to release the database to another
users.  Consider the following code:

    $db = tie %hash, 'GDBM_File', $filename, GDBM_WRCREAT, 0640;
    # Do something with %hash or $db...
    untie %hash;
    $db-&gt;close;

In this example, doing B&lt;untie&gt; alone is not enough, since the database
would remain referenced by B&lt;$db&gt;, and, as a consequence, the database file
would remain locked.  Calling B&lt;$db-E&lt;gt&gt;close&gt; ensures the database file is
closed and unlocked.

=head2 errno

    $db-&gt;errno

Returns the last error status associated with this database.  In string
context, returns a human-readable description of the error.  See also
B&lt;$GDBM_File::gdbm_errno&gt; variable above.

=head2 syserrno

    $db-&gt;syserrno

Returns the last system error status (C C&lt;errno&gt; variable), associated with
this database,

=head2 strerror

    $db-&gt;strerror

Returns textual description of the last error that occurred in this database.

=head2 clear_error

    $db-&gt;clear_error

Clear error status.

=head2 needs_recovery

    $db-&gt;needs_recovery

Returns true if the database needs recovery.

=head2 reorganize

    $db-&gt;reorganize;

Reorganizes the database.

=head2 sync

    $db-&gt;sync;

Synchronizes recent changes to the database with its disk copy.

=head2 count

    $n = $db-&gt;count;

Returns number of keys in the database.

=head2 flags

    $db-&gt;flags;

Returns flags passed as 4th argument to B&lt;tie&gt;.

=head2 dbname

    $db-&gt;dbname;

Returns the database name (i.e. 3rd argument to B&lt;tie&gt;.

=head2 cache_size

    $db-&gt;cache_size;
    $db-&gt;cache_size($newsize);

Returns the size of the internal B&lt;GDBM&gt; cache for that database.

Called with argument, sets the size to I&lt;$newsize&gt;.

=head2 block_size

    $db-&gt;block_size;

Returns the block size of the database.

=head2 sync_mode

    $db-&gt;sync_mode;
    $db-&gt;sync_mode($bool);

Returns the status of the automatic synchronization mode. Called with argument,
enables or disables the sync mode, depending on whether $bool is B&lt;true&gt; or
B&lt;false&gt;.

When synchronization mode is on (B&lt;true&gt;), any changes to the database are
immediately written to the disk. This ensures database consistency in case
of any unforeseen errors (e.g. power failures), at the expense of considerable
slowdown of operation.

Synchronization mode is off by default.

=head2 centfree

    $db-&gt;centfree;
    $db-&gt;centfree($bool);

Returns status of the central free block pool (B&lt;0&gt; - disabled,
B&lt;1&gt; - enabled).

With argument, changes its status.

By default, central free block pool is disabled.

=head2 coalesce

    $db-&gt;coalesce;
    $db-&gt;coalesce($bool);

=head2 mmap

    $db-&gt;mmap;

Returns true if memory mapping is enabled.

This method will B&lt;croak&gt; if the B&lt;libgdbm&gt; library is complied without
memory mapping support.

=head2 mmapsize

    $db-&gt;mmapsize;
    $db-&gt;mmapsize($newsize);

If memory mapping is enabled, returns the size of memory mapping. With
argument, sets the size to B&lt;$newsize&gt;.

This method will B&lt;croak&gt; if the B&lt;libgdbm&gt; library is complied without
memory mapping support.

=head2 recover

    $db-&gt;recover(%args);

Recovers data from a failed database. B&lt;%args&gt; is optional and can contain
following keys:

=over 4

=item err =&gt; sub { ... }

Reference to code for detailed error reporting. Upon encountering an error,
B&lt;recover&gt; will call this sub with a single argument - a description of the
error.

=item backup =&gt; \$str

Creates a backup copy of the database before recovery and returns its
filename in B&lt;$str&gt;.

=item max_failed_keys =&gt; $n

Maximum allowed number of failed keys. If the actual number becomes equal
to I&lt;$n&gt;, B&lt;recover&gt; aborts and returns error.

=item max_failed_buckets =&gt; $n

Maximum allowed number of failed buckets. If the actual number becomes equal
to I&lt;$n&gt;, B&lt;recover&gt; aborts and returns error.

=item max_failures =&gt; $n

Maximum allowed number of failures during recovery.

=item stat =&gt; \%hash

Return recovery statistics in I&lt;%hash&gt;. Upon return, the following keys will
be present:

=over 8

=item recovered_keys

Number of successfully recovered keys.

=item recovered_buckets

Number of successfully recovered buckets.

=item failed_keys

Number of keys that failed to be retrieved.

=item failed_buckets

Number of buckets that failed to be retrieved.

=back

=back

=head2 convert

    $db-&gt;convert($format);

Changes the format of the database file referred to by B&lt;$db&gt;.

Starting from version 1.20, B&lt;gdbm&gt; supports two database file formats:
I&lt;standard&gt; and I&lt;extended&gt;.  The former is the traditional database
format, used by previous B&lt;gdbm&gt; versions.  The I&lt;extended&gt; format contains
additional data and is recommended for use in crash tolerant applications.

L&lt;https://www.gnu.org.ua/software/gdbm/manual/Numsync.html&gt;, for the
discussion of both formats.

The B&lt;$format&gt; argument sets the new desired database format.  It is
B&lt;GDBM_NUMSYNC&gt; to convert the database from standard to extended format, and
B&lt;0&gt; to convert it from extended to standard format.

If the database is already in the requested format, the function returns
success without doing anything.

=head2 dump

    $db-&gt;dump($filename, %options)

Creates a dump of the database file in I&lt;$filename&gt;.  Such file can be used
as a backup copy or sent over a wire to recreate the database on another
machine.  To create a database from the dump file, use the B&lt;load&gt; method.

B&lt;GDBM&gt; supports two dump formats: old I&lt;binary&gt; and new I&lt;ascii&gt;.  The
binary format is not portable across architectures and is deprecated.  It
is supported for backward compatibility.  The ascii format is portable and
stores additional meta-data about the file.  It was introduced with the
B&lt;gdbm&gt; version 1.11 and is the preferred dump format.  The B&lt;dump&gt; method
creates ascii dumps by default.

If the named file already exists, the function will refuse to overwrite and
will croak an error.  If it doesn't exist, it will be created with the
mode B&lt;0666&gt; modified by the current B&lt;umask&gt;.

These defaults can be altered using the following I&lt;%options&gt;:

=over 4

=item B&lt;binary&gt; =&gt; 1

Create dump in I&lt;binary&gt; format.

=item B&lt;mode&gt; =&gt; I&lt;MODE&gt;

Set file mode to I&lt;MODE&gt;.

=item B&lt;overwrite&gt; =&gt; 1

Silently overwrite existing files.

=back

=head2 load

    $db-&gt;load($filename, %options)

Load the data from the dump file I&lt;$filename&gt; into the database I&lt;$db&gt;.
The file must have been previously created using the B&lt;dump&gt; method.  File
format is recognized automatically.  By default, the function will croak
if the dump contains a key that already exists in the database.  It will
silently ignore the failure to restore database mode and/or ownership.
These defaults can be altered using the following I&lt;%options&gt;:

=over 4

=item B&lt;replace&gt; =&gt; 1

Replace existing keys.

=item B&lt;restore_mode&gt; =&gt; 0 | 1

If I&lt;0&gt;, don't try to restore the mode of the database file to that stored
in the dump.

=item B&lt;restore_owner&gt; =&gt; 0 | 1

If I&lt;0&gt;, don't try to restore the owner of the database file to that stored
in the dump.

=item B&lt;strict_errors&gt; =&gt; 1

Croak if failed to restore ownership and/or mode.

=back

The usual sequence to recreate a database from the dump file is:

    my %hash;
    my $db = tie %hash, 'GDBM_File', 'a.db', GDBM_NEWDB, 0640;
    $db-&gt;load('a.dump');

=head1 CRASH TOLERANCE

Crash tolerance is a new feature that, given appropriate support from the OS
and the filesystem, guarantees that a logically consistent recent state of the
database can be recovered following a crash, such as power outage, OS kernel
panic, or the like.

Crash tolerance support appeared in B&lt;gdbm&gt; version 1.21.  The theory behind
it is explained in "Crashproofing the Original NoSQL Key-Value Store",
by Terence Kelly (L&lt;https://queue.acm.org/detail.cfm?id=3487353&gt;).  A
detailed discussion of the B&lt;gdbm&gt; implementation is available in the
B&lt;GDBM Manual&gt; (L&lt;https://www.gnu.org.ua/software/gdbm/manual/Crash-Tolerance.html&gt;).  The information below describes the Perl interface.

For maximum robustness, we recommend to use I&lt;extended database format&gt;
for crash tolerant databases.  To create a database in extended format,
use the B&lt;GDBM_NEWDB|GDBM_NUMSYNC&gt; when opening the database, e.g.:

    $db = tie %hash, 'GDBM_File', $filename,
              GDBM_NEWDB|GDBM_NUMSYNC, 0640;

To convert existing database to the extended format, use the B&lt;convert&gt;
method, described above, e.g.:

    $db-&gt;convert(GDBM_NUMSYNC);

=head2 crash_tolerance_status

    GDBM_File-&gt;crash_tolerance_status;

This static method returns the status of crash tolerance support.  A
non-zero value means crash tolerance is compiled in and supported by
the operating system.

=head2 failure_atomic

    $db-&gt;failure_atomic($even, $odd)

Enables crash tolerance for the database B&lt;$db&gt;,  Arguments are
the pathnames of two files that will be created and filled with
I&lt;snapshots&gt; of the database file.  The two files must not exist
when this method is called and must reside on the same filesystem
as the database file.  This filesystem must be support the I&lt;reflink&gt;
operation (https://www.gnu.org.ua/software/gdbm/manual/Filesystems-supporting-crash-tolerance.html&gt;.

After a successful call to B&lt;failure_atomic&gt;, every call to B&lt;$db-&gt;sync&gt;
method will make an efficient reflink snapshot of the database file in
one of these files; consecutive calls to B&lt;sync&gt; alternate between the
two, hence the names.

The most recent of these files can be used to recover the database after
a crash.  To select the right snapshot, use the B&lt;latest_snapshot&gt;
static method.

=head2 latest_snapshot

    $file = GDBM_File-&gt;latest_snapshot($even, $odd);

    ($file, $error) = GDBM_File-&gt;latest_snapshot($even, $odd);

Given the two snapshot names (the ones used previously in a call to
B&lt;failure_atomic&gt;), this method selects the one suitable for database
recovery, i.e. the file which contains the most recent database snapshot.

In scalar context, it returns the selected file name or B&lt;undef&gt; in case
of failure.

In array context, the returns a list of two elements: the file name
and status code.  On success, the file name is defined and the code
is B&lt;GDBM_SNAPSHOT_OK&gt;.  On error, the file name is B&lt;undef&gt;, and
the status is one of the following:

=over 4

=item GDBM_SNAPSHOT_BAD

Neither snapshot file is applicable. This means that the crash has occurred
before a call to B&lt;failure_atomic&gt; completed.  In this case, it is best to
fall back on a safe backup copy of the data file.

=item GDBM_SNAPSHOT_ERR

A system error occurred.  Examine B&lt;$!&gt; for details.  See
&lt;https://www.gnu.org.ua/software/gdbm/manual/Crash-recovery.html&gt; for
a comprehensive list of error codes and their meaning.

=item GDBM_SNAPSHOT_SAME

The file modes and modification dates of both snapshot files are exactly the
same.  This can happen only for databases in standard format.

=item GDBM_SNAPSHOT_SUSPICIOUS

The I&lt;numsync&gt; counters of the two snapshots differ by more than one.  The
most probable reason is programmer's error: the two parameters refer to
snapshots belonging to different database files.

=back

=head1 AVAILABILITY

gdbm is available from any GNU archive.  The master site is
C&lt;ftp.gnu.org&gt;, but you are strongly urged to use one of the many
mirrors.  You can obtain a list of mirror sites from
L&lt;http://www.gnu.org/order/ftp.html&gt;.

=head1 SECURITY AND PORTABILITY

GDBM files are not portable across platforms.  If you wish to transfer
a GDBM file over the wire, dump it to a portable format first.

B&lt;Do not accept GDBM files from untrusted sources.&gt;

Robustness of GDBM against corrupted databases depends highly on its
version.  Versions prior to 1.15 did not implement any validity
checking, so that a corrupted or maliciously crafted database file
could cause perl to crash or even expose a security vulnerability.
Versions between 1.15 and 1.20 were progressively strengthened against
invalid inputs.  Finally, version 1.21 had undergone extensive fuzzy
checking which proved its ability to withstand any kinds of inputs
without crashing.

=head1 SEE ALSO

L&lt;perl(1)&gt;, L&lt;DB_File(3)&gt;, L&lt;perldbmfilter&gt;,
L&lt;gdbm(3)&gt;,
L&lt;https://www.gnu.org.ua/software/gdbm/manual.html&gt;.

=cut

package GDBM_File;

use strict;
use warnings;
our($VERSION, @ISA, @EXPORT);

require Carp;
require Tie::Hash;
use Exporter 'import';
require XSLoader;
@ISA = qw(Tie::Hash);
@EXPORT = qw(
        GDBM_CACHESIZE
        GDBM_CENTFREE
        GDBM_COALESCEBLKS
        GDBM_FAST
        GDBM_FASTMODE
        GDBM_INSERT
        GDBM_NEWDB
        GDBM_NOLOCK
        GDBM_OPENMASK
        GDBM_READER
        GDBM_REPLACE
        GDBM_SYNC
        GDBM_SYNCMODE
        GDBM_WRCREAT
        GDBM_WRITER
        GDBM_NOMMAP
        GDBM_CLOEXEC
        GDBM_BSEXACT
        GDBM_XVERIFY
        GDBM_PREREAD
        GDBM_NUMSYNC
        GDBM_SNAPSHOT_OK
        GDBM_SNAPSHOT_BAD
        GDBM_SNAPSHOT_ERR
        GDBM_SNAPSHOT_SAME
        GDBM_SNAPSHOT_SUSPICIOUS
        GDBM_NO_ERROR
        GDBM_MALLOC_ERROR
        GDBM_BLOCK_SIZE_ERROR
        GDBM_FILE_OPEN_ERROR
        GDBM_FILE_WRITE_ERROR
        GDBM_FILE_SEEK_ERROR
        GDBM_FILE_READ_ERROR
        GDBM_BAD_MAGIC_NUMBER
        GDBM_EMPTY_DATABASE
        GDBM_CANT_BE_READER
        GDBM_CANT_BE_WRITER
        GDBM_READER_CANT_DELETE
        GDBM_READER_CANT_STORE
        GDBM_READER_CANT_REORGANIZE
        GDBM_UNKNOWN_UPDATE
        GDBM_ITEM_NOT_FOUND
        GDBM_REORGANIZE_FAILED
        GDBM_CANNOT_REPLACE
        GDBM_ILLEGAL_DATA
        GDBM_OPT_ALREADY_SET
        GDBM_OPT_ILLEGAL
        GDBM_BYTE_SWAPPED
        GDBM_BAD_FILE_OFFSET
        GDBM_BAD_OPEN_FLAGS
        GDBM_FILE_STAT_ERROR
        GDBM_FILE_EOF
        GDBM_NO_DBNAME
        GDBM_ERR_FILE_OWNER
        GDBM_ERR_FILE_MODE
        GDBM_UNKNOWN_ERROR
        GDBM_NEED_RECOVERY
        GDBM_BACKUP_FAILED
        GDBM_DIR_OVERFLOW
        GDBM_BAD_BUCKET
        GDBM_BAD_HEADER
        GDBM_BAD_AVAIL
        GDBM_BAD_HASH_TABLE
        GDBM_BAD_DIR_ENTRY
        GDBM_FILE_CLOSE_ERROR
        GDBM_FILE_SYNC_ERROR
        GDBM_FILE_TRUNCATE_ERROR
        GDBM_BUCKET_CACHE_CORRUPTED
        GDBM_BAD_HASH_ENTRY
        GDBM_MALFORMED_DATA
        GDBM_OPT_BADVAL
        GDBM_ERR_SNAPSHOT_CLONE
        GDBM_ERR_REALPATH
        GDBM_ERR_USAGE
        gdbm_check_syserr
);

# This module isn't dual life, so no need for dev version numbers.
$VERSION = '1.24';

our $gdbm_errno;

XSLoader::load();

1;
</pre></body></html>