Opens or creates a
GDBM database file.
The arguments are:
The name of the file (the complete name,
GDBM does not append any
characters to this name).
This parameter is used only when
gdbm_open has to create a new
database file and represents the size of a single transfer from disk to
memory. If its value is less than 512, the file system block
size is used instead. The size is adjusted so that the block can hold
exact number of directory entries, so that the effective block size
can be slightly greater than requested. However, if the
GDBM_BSEXACT flag is set and the size needs to be adjusted, the
function will return with error status, setting the
flags is set to
GDBM_READER, the user wants to just read the
database and any call to
gdbm_delete will fail.
Many readers can access the database at the same time. If
GDBM_WRITER, the user wants both read and write access
to the database and requires exclusive access. If
flags is set
GDBM_WRCREAT, the user wants both read and write access to
the database and wants it created if it does not already exist. If
flags is set to
GDBM_NEWDB, the user want a new database
created, regardless of whether one existed, and wants read and write
access to the new database. If an existing database file is opened with
GDBM_NEWDB flag, the existing data are destroyed, and an
empty database structure is created in its place.
The following constants may also be logically or’d into the database flags:
Set the close-on-exec flag on the database file descriptor. The
libc must support the
(see O_CLOEXEC in open(2) man page).
Don’t lock the database file. Use this flag if you intend to do locking separately. See Locking.
Disable memory mapping mechanism. Note, that this degrades performance.
GDBM file to memory, read its contents immediately,
instead of when needed (prefault reading). This can be
advantageous if you open a read-only database and are going to
do a lot of look-ups on it. In this case entire database will be
pre-read and look-ups will operate on an in-memory copy. In
GDBM_PREREAD should not be used if you open a
database (even in read-only mode) only to do a couple of look-ups.
Finally, never use
GDBM_PREREAD when opening a database for
updates, especially for inserts: this will degrade performance.
This flag has no effect if
GDBM_NOMMAP is given, or if the
operating system does not support prefault reading. It is known
to work on Linux and FreeBSD kernels.
Enable additional consistency checks. With this flag, eventual corruptions of the database are discovered when opening it, instead of when a corrupted structure is read during normal operation. However, on large databases, it can slow down the opening process.
See Additional functions.
The following additional flags are valid when the database is opened
for writing (i.e. together with
Synchronize all database operations to disk immediately. Notice, that this option entails severe performance degradation and does not necessarily ensure that the resulting database state is consistent. In general, we discourage its use (see Sync). See Crash Tolerance, for a discussion of how to ensure database consistency with minimal performance overhead.
A reverse of
GDBM_SYNC. Synchronize writes only when needed.
This is the default. The flag is provided for compatibility with
previous versions of
The following flags can be used together with
also take effect when used with
GDBM_WRCREAT, if the requested
database file doesn’t exist:
If this flag is set and the requested block_size cannot be used
gdbm_open will refuse to create the
databases. In this case it will set the
GDBM_BLOCK_SIZE_ERROR and return
Useful only together with
GDBM_NEWDB, this bit instructs
gdbm_open to create new database in extended database
format, a format best suitable for effective crash recovery.
See Numsync, for a detailed discussion of this format, and
Crash Tolerance, for a discussion of crash recovery.
File mode1, which is used if the file is created.
This parameter is deprecated and must always be
Early versions of
GDBM (prior to 1.13) lacked proper error
handling and would abort on any “fatal” error (such as out of memory
condition, disk write error, or the like). In these versions,
fatal_func was provided as a hook, allowing the caller to do
proper cleanup before such abnormal exit. As of version
1.22, this functionality is deprecated, although still
supported for backward compatibility.
The return value, is the pointer needed by all other functions to
GDBM file. If the return is the
gdbm_open was not successful. The errors can be found in
gdbm_errno variable (see gdbm_errno). Available
error codes are discussed in Error codes.
In all of the following calls, the parameter dbf refers to the pointer
gdbm_fd_open, described below).
Alternative function for opening a
GDBM database. The fd
argument is the file descriptor of the database file obtained by a
creat(2) or similar functions. The
descriptor is not dup’ed, and will be closed when the returned
GDBM_FILE is closed. Use
dup(2) if that is not
In case of error, the function behaves like
does not close fd. This can be altered by the following
value passed in the flags argument:
Close fd before exiting on error.
Copy file ownership and mode from src to dst.