GDBM(3)
GDBM(3) β GDBM User Reference π«Άβ¨
okay bestie, welcome to the GDBM library manual, part 1! π
this is the GNU database manager, and it also includes dbm and ndbm compatibility. let's get into it.
β¨ NAME β¨
GDBM β The GNU database manager. Includes dbm and ndbm compatibility. π
π¦ SYNOPSIS π¦
#include <gdbm.h>
extern gdbm_error gdbm_errno;
extern char *gdbm_version;
extern int gdbm_version[3];
GDBM_FILE gdbm_open (const char *name, int block_size,
int flags, int mode,
void (*fatal_func)(const char *));
GDBM_FILE gdbm_fd_open (int FD, const char *name, int block_size,
int flags, int mode,
void (*fatal_func)(const char *));
GDBM_FILE gdbm_open_ext (const char *name, int flags,
struct gdbm_open_spec *spec);
void gdbm_open_spec_init (struct gdbm_open_spec *spec);
int gdbm_close (GDBM_FILE dbf);
int gdbm_store (GDBM_FILE dbf, datum key, datum content, int flag);
datum gdbm_fetch (GDBM_FILE dbf, datum key);
int gdbm_delete (GDBM_FILE dbf, datum key);
datum gdbm_firstkey (GDBM_FILE dbf);
datum gdbm_nextkey (GDBM_FILE dbf, datum key);
int gdbm_recover (GDBM_FILE dbf, gdbm_recovery *rcvr, intflags);
int gdbm_reorganize (GDBM_FILE dbf);
int gdbm_sync (GDBM_FILE dbf);
int gdbm_exists (GDBM_FILE dbf, datum key);
const char *gdbm_strerror (gdbm_error errno);
int gdbm_setopt (GDBM_FILE dbf, int option, int value, int size);
int gdbm_fdesc (GDBM_FILE dbf);
int gdbm_count (GDBM_FILE dbf, gdbm_count_t *pcount);
int gdbm_bucket_count (GDBM_FILE dbf, size_t *pcount);
int gdbm_avail_verify (GDBM_FILE dbf);
π Crash Tolerance (see below):
int gdbm_failure_atomic (GDBM_FILE dbf, const char *even, const char
*odd);
int gdbm_latest_snapshot (const char *even, const char *odd, const char
**result);
π NOTICE π
This manpage is a short description of the GDBM library. For a detailed discussion, including examples and usage recommendations, refer to the GDBM Manual available in Texinfo format. To access it, run:
info gdbm
The documentation is also available online at
https://www.gnu.org/software/gdbm/manual
Should any discrepancies occur between this manpage and the GDBM Manual, the later shall be considered the authoritative source.
πΈ DESCRIPTION πΈ
GNU dbm is a library of routines that manages data files that contain key/data pairs. The access provided is that of storing, retrieval, and deletion by key and a non-sorted traversal of all keys. A process is allowed to use multiple data files at the same time. β¨
β¨ Opening a database β¨
A process that opens a gdbm file is designated as a "reader" or a "writer". Only one writer may open a gdbm file and many readers may open the file. Readers and writers can not open the gdbm file at the same time. The procedure for opening a gdbm file is:
GDBM_FILE gdbm_open (const char *name, int block_size,
int flags, int mode,
void (*fatal_func)(const char *));
nameβ is the name of the file (the complete name, gdbm does not append any characters to this name). πblock_sizeβ is the size of a single transfer from disk to memory. If the value is less than512, 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. This adjustment is disabled if theGDBM_BSEXACTflag is used.flagsβ a bitmask, composed of the access mode and one or more modifier flags. The access mode bit designates the process as a reader or writer and must be one of the following:GDBM_READERβ reader πGDBM_WRITERβ writer βοΈGDBM_WRCREATβ writer - if database does not exist create new oneGDBM_NEWDBβ writer - create new database regardless if one exists
Additional flags (modifiers) can be combined with these values by bitwise OR. Not all of them are meaningful with all access modes.
Flags that are valid for any value of access mode are:
GDBM_CLOEXECβ Set the close-on-exec flag on the database file descriptor.GDBM_NOLOCKβ Prevents the library from performing any locking on the database file.GDBM_NOMMAPβ Instructsgdbm_opento disable the use ofmmap(2).GDBM_PREREADβ When mapping 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 read at once and searches will operate on an in-memory copy. In contrast,GDBM_PREREADshould not be used if you open a database (even in read-only mode) only to retrieve a couple of keys. Finally, never useGDBM_PREREADwhen opening a database for updates, especially for inserts: this will degrade performance. This flag has no effect ifGDBM_NOMMAPis given, or if the operating system does not support prefault reading. It is known to work on Linux and FreeBSD kernels.GDBM_XVERIFYβ 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.
The following additional flags are valid when the database is opened for writing (GDBM_WRITER, GDBM_WRCREAT, or GDBM_NEWDB):
GDBM_SYNCβ Causes all database operations to be synchronized to the disk.
NOTE: this option entails severe performance degradation and does not necessarily ensure that the resulting database state is consistent, therefore we discourage its use. For a discussion of how to ensure database consistency with minimal performance overhead, see CRASH TOLERANCE below.
GDBM_FASTβ A reverse ofGDBM_SYNC: synchronize writes only when needed. This is the default. This flag is provided only for compatibility with previous versions of GDBM.
The following flags can be used together with GDBM_NEWDB. They also take effect when used with GDBM_WRCREAT, if the requested database file doesn't exist:
GDBM_BSEXACTβ If this flag is set and the requestedblock_sizevalue cannot be used,gdbm_openwill refuse to create the database. In this case it will set thegdbm_errnovariable toGDBM_BLOCK_SIZE_ERRORand returnNULL. Without this flag,gdbm_openwill silently adjust theblock_sizeto a usable value, as described above.GDBM_NUMSYNCβ Create new database in extended database format, a format best suited for effective crash recovery. For a detailed discussion, see the CRASH RECOVERY chapter below.modeβ is the file mode (seechmod(2)andopen(2)). It is used if the file is created.fatal_funcβ is a function to be called when gdbm encounters a fatal error. This parameter is deprecated and must always beNULL.
The return value is the pointer needed by all other routines to access that gdbm file. If the return is the NULL pointer, gdbm_open was not successful. In this case, the reason of the failure can be found in the gdbm_errno variable. If the following call returns true (non-zero value):
gdbm_check_syserr(gdbm_open)
the system errno variable must be examined in order to obtain more detail about the failure.
GDBM_FILE gdbm_fd_open (int FD, const char *name, int block_size,
int flags, int mode,
void (*fatal_func)(const char *));
This is an alternative entry point to gdbm_open. FD is a valid file descriptor obtained as a result of a call to open(2) or creat(2). The function opens (or creates) a GDBM database this descriptor refers to. The descriptor is not dup'ed, and will be closed when the returned GDBM_FILE is closed. Use dup (2) if that is not desirable.
In case of error, the function behaves like gdbm_open and does not close FD. This can be altered by the following value passed in flags:
GDBM_CLOERRORβ CloseFDbefore exiting on error.
The rest of arguments are the same as for gdbm_open.
GDBM_FILE gdbm_open_ext (const char *name, int flags,
struct gdbm_open_spec *spec);
This is an extended interface for opening and creating database files. The meaning of name and flags is the same as described above. Third argument, spec, is a pointer to a structure that supplies additional information about how to open the database. It can be NULL, in which case the call is equivalent to
gdbm_open (name, 0, flags, 0600, NULL)
The struct gdbm_open_ext type has at least the following fields:
int fdβ Unless-1, this is the file descriptor of an already opened database file. The function will then act as improvedgdbm_fd_open.int modeβ File mode to use if the file needs be created.int block_sizeβ Block size to be used when creating new database. Zero stands for default value. See the description of theblock_sizeargument ingdbm_open, for details.int lock_waitβ This field, together with the two fields described below, controls how to handle failed attempts to lock the file. Its possible values are:GDBM_LOCKWAIT_NONEβ ReturnNULLimmediately. This is the default.GDBM_LOCKWAIT_RETRYβ Sleep for the amount of time defined by thelock_intervalfield, and retry the attempt. In case of failure, repeat until total time spent exceedslock_timeout.
GDBM_LOCKWAIT_SIGNALβ okay so bestie, this one waits until locking succeeds or the timeout defined bylock_timeoutexpires, whichever happens first. Signal delivery is used to catch timeout expiration, so this method is not advised for use in multi-threaded programs. πstruct timespec lock_timeoutβ the overall timeout for file locking operation. β³struct timespec lock_intervalβ the time interval between two successive locking attempts. It's used iflock_waitis set toGDBM_LOCKWAIT_RETRY. π
A struct gdbm_open_spec variable can be initialized to default values by assigning it the GDBM_OPEN_SPEC_INITIALIZER value, or by using the gdbm_open_spec_init function. The latter is defined as:
void gdbm_open_spec_init (struct gdbm_open_spec *spec);
Calling convention π«Ά
All GDBM functions take as their first parameter the database handle (GDBM_FILE), which you get back from gdbm_open, gdbm_fd_open, or gdbm_open_ext. β¨ No cap.
Any value stored in the GDBM database is described by datum, an aggregate type defined as:
typedef struct
{
char *dptr;
int dsize;
} datum;
The dptr field points to the actual data. Its type is char * for historical reasons. Actually it should have been typed void *. Programmers are free to store data of arbitrary complexity, both scalar and aggregate, in this field. π¦
The dsize field contains the number of bytes stored in dptr. π
The datum type is used to describe both keys and content (values) in the database. Values of this type can be passed as arguments or returned from GDBM function calls. π
GDBM functions that return datum indicate failure by setting its dptr field to NULL. π«
Functions returning integer value, indicate success by returning 0 and failure by returning a non-zero value (the only exception to this rule is gdbm_exists, for which the return value is reversed). β οΈ
If the returned value indicates failure, the gdbm_errno variable contains an integer value indicating what went wrong. A similar value is associated with the dbf handle and can be accessed using the gdbm_last_errno function. Immediately after return from a function, both values are exactly equal. Subsequent GDBM calls with another dbf as argument may alter the value of the global gdbm_errno, but the value returned by gdbm_last_errno will always indicate the most recent code of an error that occurred for that particular database. Programmers are encouraged to use such per-database error codes. π
Sometimes the actual reason of the failure can be clarified by examining the system errno value. To make sure its value is meaningful for a given GDBM error code, use the gdbm_check_syserr function. The function takes error code as argument and returns 1 if the errno is meaningful for that error, or 0 if it is irrelevant. π
Similarly to gdbm_errno, the latest errno value associated with a particular database can be obtained using the gdbm_last_syserr function. π
The gdbm_clear_error clears the error indicator (both GDBM and system error codes) associated with a database handle. π§Ή
Some critical errors leave the database in a structurally inconsistent state. If that happens, all subsequent GDBM calls accessing that database will fail with the GDBM error code of GDBM_NEED_RECOVERY (a special function gdbm_needs_recovery is also provided, which returns true if the database handle given as its argument is structurally inconsistent). To return such databases to consistent state, use the gdbm_recover function (see below). π¨
The GDBM_NEED_RECOVERY error cannot be cleared using gdbm_clear_error. β
Error functions π οΈ
This section describes the error handling functions outlined above. π
gdbm_error gdbm_last_errno (GDBM_FILE dbf)β returns the error code of the most recent failure encountered when operating ondbf. πint gdbm_last_syserr (GDBM_FILE dbf)β returns the value of the systemerrnovariable associated with the most recent failure that occurred ondbf. Notice that not allgdbm_errorcodes have a relevant system error code. Use the following function to determine if a given code has. π§int gdbm_check_syserr (gdbm_error err)β returns 1, if systemerrnovalue should be checked to get more info on the error described by GDBM codeerr. βvoid gdbm_clear_error (GDBM_FILE dbf)β clears the error state for the databasedbf. This function is called implicitly upon entry to any GDBM function that operates onGDBM_FILE. TheGDBM_NEED_RECOVERYerror cannot be cleared. π«int gdbm_needs_recovery (GDBM_FILE dbf)β return 1 if the database filedbfis in inconsistent state and needs recovery. πconst char *gdbm_strerror (gdbm_error err)β returns a textual description of the error codeerr. πconst char *gdbm_db_strerror (GDBM_FILE dbf)β returns a textual description of the recent error in databasedbf. This description includes the systemerrnovalue, if relevant. π
Closing the database πͺ
It is important that every database file opened is also closed. This is needed to update the reader/writer count on the file. This is done by:
int gdbm_close (GDBM_FILE dbf);
No cap, close your files bestie. π
Database lookups π
int gdbm_exists (GDBM_FILE dbf, datum key);β if the key is found within the database, the return value will be true (1). If nothing appropriate is found, false (0) is returned andgdbm_errnoset toGDBM_NO_ERROR. On error, returns 0 and setsgdbm_errno. πdatum gdbm_fetch (GDBM_FILE dbf, datum key);βdbfis the pointer returned bygdbm_open.keyis the key data. If thedptrelement of the return value isNULL, thegdbm_errnovariable should be examined. The value ofGDBM_ITEM_NOT_FOUNDmeans no data was found for that key. Other value means an error occurred. Otherwise the return value is a pointer to the found data. The storage space for thedptrelement is allocated usingmalloc(3). GDBM does not automatically free this data. It is the programmer's responsibility to free this storage when it is no longer needed. ποΈ
Iterating over the database π
The following two routines allow for iterating over all items in the database. Such iteration is not key sequential, but it is guaranteed to visit every key in the database exactly once. (The order has to do with the hash values.) π²
datum gdbm_firstkey (GDBM_FILE dbf);β returns first key in the database. π₯datum gdbm_nextkey (GDBM_FILE dbf, datum key);β given a key, returns the database key that follows it. End of iteration is marked by returningdatumwithdptrfield set toNULLand setting thegdbm_errnovalue toGDBM_ITEM_NOT_FOUND. π
After successful return from both functions, dptr points to data allocated by malloc(3). It is the caller responsibility to free the data when no longer needed. π§Ή
A typical iteration loop looks like:
datum key, nextkey, content;
key = gdbm_firstkey (dbf);
while (key.dptr)
{
content = gdbm_fetch (dbf, key);
/* Do something with key and/or content */
nextkey = gdbm_nextkey (dbf, key);
free (key.dptr);
key = nextkey;
}
These functions are intended to visit the database in read-only algorithms. Avoid any database modifications within the iteration loop. File visiting is based on a hash table. The gdbm_delete and, in most cases, gdbm_store, functions rearrange the hash table to make sure that any collisions in the table do not leave some item `un-findable'. Thus, a call to either of these functions changes the order in which the keys are ordered. Therefore, these functions should not be used when iterating over all the keys in the database. For example, the following loop is wrong: it is possible that some keys will not be visited or will be visited twice if it is executed: π«
key = gdbm_firstkey (dbf);
while (key.dptr)
{
nextkey = gdbm_nextkey (dbf, key);
if (some condition)
gdbm_delete ( dbf, key );
free (key.dptr);
key = nextkey;
}
Updating the database π
int gdbm_store (GDBM_FILE dbf, datum key, datum content, int flag);βdbfis the pointer returned bygdbm_open.keyis the key data.contentis the data to be associated with the key.flagcan have one of the following values:GDBM_INSERTβ insert only, generate an error if key exists; π«GDBM_REPLACEβ replace contents if key exists. π
The function returns 0 on success and -1 on failure. If the key already exists in the database and the flag is GDBM_INSERT, the function does not modify the database. It sets gdbm_errno to GDBM_CANNOT_REPLACE and returns 1. π€
int gdbm_delete (GDBM_FILE dbf, datum key);β looks up and deletes the given key from the databasedbf. The return value is 0 if there was a successful delete or -1 on error. In the latter case, thegdbm_errnovalueGDBM_ITEM_NOT_FOUNDindicates that the key is not present in the database. Othergdbm_errnovalues indicate failure. ποΈ
Recovering structural consistency π
If a function leaves the database in structurally inconsistent state, it can be recovered using the gdbm_recover function. π
int gdbm_recover (GDBM_FILE dbf, gdbm_recovery * rcvr, int flags)β check the database fileDBFand fix eventual inconsistencies. Thercvrargument can be used both to control the recovery and to return additional statistics about the process, as indicated byflags. For a detailed discussion of these arguments and their usage, see the GDBM Manual, chapter Recovering structural consistency. You can passNULLasrcvrand 0 asflags, if no such control is needed. By default, this function first checks the database for inconsistencies and attempts recovery only if some were found. The specialflagsbitGDBM_RCVR_FORCEinstructsgdbm_recoveryto skip this check and to perform database recovery unconditionally. π§
Export and import πΎβ¨
okay so bestie, GDBM database files can be exported (that's "dumped") into what we call flat files, or imported (that's "loaded") back out of them. A flat file has exactly the same data as the original database, but lowkey it can't be used for searches or updates. Its whole purpose is to hold onto the data so you can restore it later when the need arrives. So basically: flat files are for backups and for yeeting databases over the wire. π‘
As of GDBM version 1.21, there are two flat file formats:
- The ASCII file format encodes all data in Base64 and stores not only the key/data pairs, but also the original database file metadata β like file name, mode and ownership. Files in this format can be sent without additional encapsulation over transmission channels that normally only allow ASCII data, such as, e.g. SMTP. And because of that extra metadata, they let you restore an exact copy of the database, including file ownership and privileges β which is highkey important if the database in question contained some security-related data. π This is the preferred format.
- The other one is the binary format. It stores only key/data pairs and keeps no information about the database file itself. It cannot be used to copy databases between different architectures. The binary format was introduced in GDBM version 1.9.1 and is retained mainly for backward compatibility. (It's giving legacy, ngl.)
The following functions are what you use to export or import GDBM database files. π«Ά
int gdbm_dump (GDBM_FILE dbf, const char *filename,
int format, int open_flag, int mode)
Dumps the database file dbf to the file filename in the requested format. The allowed values for format are:
GDBM_DUMP_FMT_ASCIIβ to create an ASCII dump file β¨GDBM_DUMP_FMT_BINARYβ to create a binary dump
The value of open_flag tells gdbm_dump what to do if filename already exists:
- If it's
GDBM_NEWDB, the function will create a new output file, replacing it if it already exists. - If its value is
GDBM_WRCREAT, the file will be created if it does not exist. But if it does exist,gdbm_dumpwill return error. π«
The file mode to use when creating the output file is defined by the mode parameter. Its meaning is the same as for open(2).
int gdbm_load (GDBM_FILE *pdbf, const char *filename,
int flag, int meta_mask, unsigned long *errline)
Loads data from the dump file filename into the database pointed to by pdbf. If pdbf is NULL, the function will try to create a new database. On success, the new GDBM_FILE object will be stored in the memory location pointed to by pdbf. But listen β if the dump file carries no information about the original database file name, the function will set gdbm_errno to GDBM_NO_DBNAME and return -1, indicating failure. π
Otherwise, if pdbf points to an already open GDBM_FILE, the function will load data from filename into that database.
The flag parameter controls the function behavior if a key from the dump file already exists in the database. See the gdbm_store function for its possible values. π
The meta_mask parameter can be used to disable restoring certain bits of the file's meta-data from the information in the input dump file. It is a binary OR of zero or more of the following:
GDBM_META_MASK_MODEβ Do not restore file mode.GDBM_META_MASK_OWNERβ Do not restore file owner.
Other functions π οΈπ
int gdbm_reorganize (GDBM_FILE dbf);
If you have had a lot of deletions and would like to shrink the space used by the GDBM file, this routine will reorganize the database. β¨ (It's giving "clean your room" energy but for buckets.)
int gdbm_sync (GDBM_FILE dbf);
Synchronizes the changes in dbf with its disk file. πΎ
It will not return until the disk file state is synchronized with the in-memory state of the database. Periodt.
int gdbm_setopt (GDBM_FILE dbf, int option, void *value, int size);
Query or change some parameter of an already opened database. The option argument defines what parameter to set or retrieve.
- If the set operation is requested,
valuepoints to the new value. Its actual data type depends onoption. - If the get operation is requested,
valuepoints to a memory region where to store the return value.
In both cases, size contains the actual size of the memory pointed to by value.
Possible values of option are: π
GDBM_SETCACHESIZE,GDBM_CACHESIZEβ Set the size of the internal bucket cache. Thevalueshould point to asize_tholding the desired cache size, or the constantGDBM_CACHE_AUTO, to select the best cache size automatically.
By default, a newly open database is configured to adapt the cache size to the number of index buckets in the database file. This provides for the best performance. π₯
Use this option if you wish to limit the memory usage at the expense of performance. If you chose to do so, please bear in mind that cache becomes effective when its size is greater than 2/3 of the number of index bucket counts in the database. The best performance results are achieved when cache size equals the number of buckets.
GDBM_GETCACHESIZEβ Return the size of the internal bucket cache. Thevalueshould point to asize_tvariable, where the size will be stored.GDBM_GETFLAGSβ Return the flags describing current state of the database. Thevalueshould point to anintvariable where to store the flags. On success, its value will be similar to the flags used when opening the database, except that it will reflect the current state (which may have been altered by another calls togdbm_setopt).GDBM_FASTMODEβ Enable or disable the fast writes mode, similar to theGDBM_FASToption togdbm_open.
This option is retained for compatibility with previous versions of GDBM. (Legacy icon behavior tbh.)
GDBM_SETSYNCMODE,GDBM_SYNCMODEβ Turn on or off immediate disk synchronization after updates. Thevalueshould point to an integer:1to turn synchronization on, and0to turn it off.
NOTE: setting this option entails severe performance degradation and does not necessarily ensure that the resulting database state is consistent, therefore we discourage its use. For a discussion of how to ensure database consistency with minimal performance overhead, see CRASH TOLERANCE below.
GDBM_GETSYNCMODEβ Return the current synchronization status. Thevalueshould point to anintwhere the status will be stored.GDBM_SETCENTFREE,GDBM_CENTFREEβ Enable or disable central free block pool. The default is off, which is how previous versions of GDBM handled free blocks. If set, this option causes all subsequent free blocks to be placed in the global pool, allowing (in theory) more file space to be reused more quickly. Thevalueshould point to an integer:TRUEto turn central block pool on, andFALSEto turn it off.
The GDBM_CENTFREE alias is provided for compatibility with earlier versions.
GDBM_SETCOALESCEBLKS,GDBM_COALESCEBLKSβ Set free block merging to either on or off. The default is off, which is how previous versions of GDBM handled free blocks. If set, this option causes adjacent free blocks to be merged. This can become a CPU expensive process with time, though, especially if used in conjunction withGDBM_CENTFREE. Thevalueshould point to an integer:TRUEto turn free block merging on, andFALSEto turn it off.GDBM_GETCOALESCEBLKSβ Return the current status of free block merging. Thevalueshould point to anintwhere the status will be stored.GDBM_SETMAXMAPSIZEβ Sets maximum size of a memory mapped region. Thevalueshould point to a value of typesize_t,unsigned longorunsigned. The actual value is rounded to the nearest page boundary (the page size is obtained fromsysconf(_SC_PAGESIZE)).GDBM_GETMAXMAPSIZEβ Return the maximum size of a memory mapped region. Thevalueshould point to a value of typesize_twhere to return the data.GDBM_SETMMAPβ Enable or disable memory mapping mode. Thevalueshould point to an integer:TRUEto enable memory mapping orFALSEto disable it.GDBM_GETMMAPβ Check whether memory mapping is enabled. Thevalueshould point to an integer where to return the status.GDBM_GETDBNAMEβ Return the name of the database disk file. Thevalueshould point to a variable of typechar**. A pointer to the newly allocated copy of the file name will be placed there. The caller is responsible for freeing this memory when no longer needed. π«Ά (Don't leak, bestie.)GDBM_GETBLOCKSIZEβ Return the block size in bytes. Thevalueshould point toint.
int gdbm_fdesc (GDBM_FILE dbf);
Returns the file descriptor of the database dbf. π
CRASH TOLERANCE π₯π¦
By default GNU dbm does not protect the integrity of its databases from corruption or destruction due to failures such as power outages, operating system kernel panics, or application process crashes. Such failures could damage or destroy the underlying database. π
Starting with release 1.21 GNU dbm includes a mechanism that, if used correctly, enables post-crash recovery to a consistent state of the underlying database. This mechanism requires OS and filesystem support and must be requested when gdbm is compiled. The crash-tolerance mechanism is a "pure opt-in" feature, in the sense that it has no effects whatsoever except on those applications that explicitly request it. For details, see the chapter Crash Tolerance in the GDBM manual. β¨
πΈ GLOBAL VARIABLES πΈ
okay bestie, these are the global-ish things the library keeps around for you π
gdbm_error gdbm_errnoβ this one holds the code of the most recent error that happened. but listen, it's not a C variable in the proper sense, okay? you can read its value, you can assign any value to it, but the moment you try to take its address you get a syntax error. so don't do that π. also it's a per-thread memory location, so each thread gets its own lil copy β¨const char *gdbm_versionβ a string with the library version number and the build date in it. cute, informative, iconic πint const gdbm_version_number[3]β this one's got the library version numbers in a lil trio: major, minor, and patchlevel π
π¦ VERSIONING π¦
so the version tea is kept in two places. the version of the library itself lives in the gdbm_version_number variable we just talked about. and then, on top of that, the header file gdbm.h defines these macros:
GDBM_VERSION_MAJORβ major version number.GDBM_VERSION_MINORβ minor version number.GDBM_VERSION_PATCHβ patchlevel number. and heads up,0means there's no patchlevel.
you can use these to check whether your header file actually corresponds to the library your program is linked with π (mismatch? not the vibe).
and there's a function for comparing two version numbers:
int gdbm_version_cmp (int const a[3], int const b[3])
it compares two version numbers formatted the same way as gdbm_version_number. it returns a negative number if a is older than b, a positive number if a is newer than b, and 0 if they're equal. no cap, that's the whole thing π«Ά
π ERROR CODES π
okay this is the big list, stay with me, it's giving encyclopedia but every single one matters π₯
GDBM_NO_ERRORβ no error occurred. the dream. the fantasy. β¨GDBM_MALLOC_ERRORβ memory allocation failed πGDBM_BLOCK_SIZE_ERRORβ this one gets set by thegdbm_openfunction when the value of itsblock_sizeargument is incorrect and theGDBM_BSEXACTflag is set.GDBM_FILE_OPEN_ERRORβ the library couldn't open a disk file. can be set bygdbm_open,gdbm_fd_open,gdbm_dumpandgdbm_load.
peek at the system errno variable for the more detailed diagnostics π
GDBM_FILE_WRITE_ERRORβ writing to a disk file failed. can be set bygdbm_open,gdbm_fd_open,gdbm_dumpandgdbm_load.
again, check the system errno variable for the detailed tea.
GDBM_FILE_SEEK_ERRORβ positioning inside a disk file failed. can be set by thegdbm_openfunction.
and yes, check the system errno variable for a more detailed diagnostics.
GDBM_FILE_READ_ERRORβ reading from a disk file failed. can be set bygdbm_open,gdbm_dumpandgdbm_load.
you already know β inspect the system errno variable for a more detailed diagnostics.
GDBM_BAD_MAGIC_NUMBERβ the file passed as an argument to thegdbm_openfunction is not a valid gdbm file: it's got a wrong magic number πͺβGDBM_EMPTY_DATABASEβ the file passed as an argument togdbm_openis not a valid gdbm file: it has zero length. this error gets returned unless theflagsargument has theGDBM_NEWDBbit set.GDBM_CANT_BE_READERβ set by thegdbm_openfunction if it can't lock the file when called inGDBM_READERmode.GDBM_CANT_BE_WRITERβ set by thegdbm_openfunction if it can't lock the file when called in writer mode.GDBM_READER_CANT_DELETEβ set bygdbm_delete, if it tried to operate on a database that's open in read-only mode.GDBM_READER_CANT_STOREβ set bygdbm_storeif it tried to operate on a database that's open in read-only mode.GDBM_READER_CANT_REORGANIZEβ set bygdbm_reorganizeif it tried to operate on a database that's open in read-only mode.GDBM_ITEM_NOT_FOUNDβ the requested item wasn't found. set bygdbm_deleteandgdbm_fetchwhen the requested key value isn't in the database πGDBM_REORGANIZE_FAILEDβ thegdbm_reorganizefunction can't create a temporary database.GDBM_CANNOT_REPLACEβ cannot replace an existing item. set bygdbm_storeif the requested key value is found in the database and the flag parameter is notGDBM_REPLACE.GDBM_MALFORMED_DATAβ the input data was malformed in some way. when it comes back fromgdbm_load, it means the input file wasn't a valid gdbm dump file. when it comes back fromgdbm_store, it means either thekeyorcontentparameter had itsdptrfield set toNULL.
also: GDBM_ILLEGAL_DATA is an alias for this error code, kept around for backward compatibility. periodt.
GDBM_OPT_ALREADY_SETβ the requested option can only be set once, and it was already set. as of version 1.21, this error code is no longer used. in prior versions it could have been returned by thegdbm_setoptfunction when setting theGDBM_CACHESIZEvalue.GDBM_OPT_BADVALβ the option argument isn't valid, or thevalueargument points to an invalid value, in a call to thegdbm_setoptfunction.
GDBM_OPT_ILLEGAL is an alias for this error code, kept for backward compatibility. modern applications should not use it π
ββοΈ
GDBM_BYTE_SWAPPEDβ thegdbm_openfunction is trying to open a database that was created on a machine with a different byte ordering.GDBM_BAD_FILE_OFFSETβ thegdbm_openfunction sets this error code if the file it's trying to open has a wrong magic number.GDBM_BAD_OPEN_FLAGSβ set by thegdbm_dumpfunction if it's supplied an invalidflagsargument.GDBM_FILE_STAT_ERRORβ getting information about a disk file failed. the systemerrnowill give more details about the error.
this error can be set by: gdbm_open, gdbm_reorganize.
GDBM_FILE_EOFβ end of file was hit where more data was expected to be present. this can happen when fetching data from the database, and it usually means the database is truncated or otherwise corrupted π
this error can be set by any GDBM function that does I/O. some of these functions are: gdbm_delete, gdbm_exists, gdbm_fetch, gdbm_export, gdbm_import, gdbm_reorganize, gdbm_firstkey, gdbm_nextkey, gdbm_store.
GDBM_NO_DBNAMEβ the output database name isn't specified. set bygdbm_loadif the first argument points toNULLand the input file doesn't specify the database name.GDBM_ERR_FILE_OWNERβ set bygdbm_loadif it can't restore the database file owner. it's a mild error condition, meaning the data have been restored successfully β only changing the target file owner failed. check the systemerrnovariable for a more detailed diagnostics.GDBM_ERR_FILE_MODEβ set bygdbm_loadif it can't restore the database file mode. it's a mild error condition, meaning the data have been restored successfully β only changing the target file owner failed. check the systemerrnovariable for a more detailed diagnostics.GDBM_NEED_RECOVERYβ the database is in an inconsistent state and needs recovery. callgdbm_recoverif you get this one πGDBM_BACKUP_FAILEDβ the GDBM engine can't create a backup copy of the file.GDBM_DIR_OVERFLOWβ the bucket directory would overflow the size limit during an attempt to split a hash bucket. this can pop up while storing a new key.GDBM_BAD_BUCKETβ an invalid index bucket was encountered in the database. database recovery is needed.GDBM_BAD_HEADERβ set bygdbm_openandgdbm_fd_openif the first block read from the database file doesn't contain a valid GDBM header.GDBM_BAD_AVAILβ the available space stack is invalid. can be set bygdbm_openandgdbm_fd_openif extended database verification was requested (GDBM_XVERIFY). it's also set by thegdbm_avail_verifyfunction.
the database needs recovery.
GDBM_BAD_HASH_TABLEβ the hash table in a bucket is invalid. can be set by:gdbm_delete,gdbm_exists,gdbm_fetch,gdbm_firstkey,gdbm_nextkey, andgdbm_store.
the database needs recovery.
GDBM_BAD_DIR_ENTRYβ a bad directory entry was found in the bucket. database recovery is needed.GDBM_FILE_CLOSE_ERRORβ thegdbm_closefunction couldn't close the database file descriptor. the systemerrnovariable contains the corresponding error code.GDBM_FILE_SYNC_ERRORβ cached content couldn't be synchronized to disk. examine theerrnovariable to get more info.
database recovery is needed.
GDBM_FILE_TRUNCATE_ERRORβ the file cannot be truncated. examine theerrnovariable to get more info.
this error is set by gdbm_open and gdbm_fd_open when called with the GDBM_NEWDB flag.
GDBM_BUCKET_CACHE_CORRUPTEDβ the bucket cache structure is corrupted. database recovery is needed.GDBM_BAD_HASH_ENTRYβ this one gets set during sequential access (@pxref{Sequential}), if the next hash table entry doesn't contain the expected key. that means the bucket is malformed or corrupted, and the database needs recovery.GDBM_ERR_SNAPSHOT_CLONEβ set by thegdbm_failure_atomicfunction if it couldn't clone the database file into a snapshot. check the systemerrnovariable for the underlying cause. and heads up: iferrnoisEINVALorENOSYS, crash tolerance settings will be removed from the database.GDBM_ERR_REALPATHβ set by thegdbm_failure_atomicfunction if the call to therealpathfunction failed.realpathis used to figure out the actual path names of the snapshot files. examine the systemerrnovariable for details.GDBM_ERR_USAGEβ function usage error. that includes invalid argument values, and the like π
π¦ DBM COMPATIBILITY ROUTINES π¦
Okay bestie, storytime: GDBM ships a whole extra compatibility library called libgdbm_compat, and it's for all the old-school programs out there that still expect the classic UNIX dbm or ndbm interfaces β think stuff like, e.g. Sendmail. The way that this library is optional though? Not every binary distribution has it, so it might just be absent from yours. π
And because a full deep-dive on the compatibility API would be its own whole saga (way beyond the scope of this doc, fr fr), we're only giving you a lil' short reference down here. Want the full tea? Go read the GDBM Manual, chapter Compatibility with standard dbm and ndbm. π
πΈ DBM compatibility routines πΈ
In dbm compatibility mode there's a big rule: only one file may be opened at a time. Also everyone using it is assumed to be a writer. And if the database file happens to be read only? It's gonna fail as a writer β but it'll still get opened as a reader, so, slay, it lives. π«Ά
Also important: all returned pointers in datum structures point to data that the compatibility library will free. Treat them like static pointers, exactly the way standard UNIX dbm does. Do not go freeing them yourself, I am begging. π
The following interfaces are provided:
#include <dbm.h>
int dbminit (const char *name);
int store (datum key, datum content);
datum fetch (datum key);
int delete (datum key);
datum firstkey (void);
datum nextkey (datum key);
int dbmclose (void);
π NDBM Compatibility routines π
This mode is the glow-up: multiple databases can be opened. Each database gets its own lil' handle of type DBM * to identify it. π
Same deal as the original NDBM though β all returned pointers in datum structures point to data that will be freed by the compatibility library, so treat them as static pointers. Periodt.
The following interfaces are provided:
#include <ndbm.h>
DBM *dbm_open (const char *name, int flags, int mode);
void dbm_close (DBM *file);
datum dbm_fetch (DBM *file, datum key);
int dbm_store (DBM *file, datum key, datum content, int flags);
int dbm_delete (DBM *file, datum key);
datum dbm_firstkey (DBM *file);
datum dbm_nextkey (DBM *file, datum key);
int dbm_error (DBM *file);
int dbm_clearerr (DBM *file);
int dbm_pagfno (DBM *file);
int dbm_dirfno (DBM *file);
int dbm_rdonly (DBM *file);
β¨ LINKING β¨
To get at this library you pass -lgdbm as the last parameter on the compile line, like:
gcc -o prog prog.c -lgdbm
And if you wanna use the dbm or ndbm compatibility routines? You gotta link in the gdbm_compat library too. For example:
gcc -o prog proc.c -lgdbm -lgdbm_compat
Note the spelling difference in the examples, bestie β the first one compilesprog.c, the second compilesproc.c. That's literally what the original says, so I'm keeping it exactly as-is. π
π BUG REPORTS π
Found a bug? Send your bug reports to <bug-gdbm@gnu.org>. Don't be shy, they wanna hear it. π
π SEE ALSO π
π AUTHORS π
by Philip A. Nelson, Jason Downs and Sergey Poznyakoff; crash tolerance by Terence Kelly. Iconic lineup, no cap. π
π COPYRIGHT π
Copyright Β© 1990 - 2025 Free Software Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version.
GDBM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with GDBM. If not, see <http://gnu.org/licenses/gpl.html>
π«Ά CONTACTS π«Ά
Wanna contact the original author? Here's how:
e-mail: phil@cs.wwu.edu
us-mail: Philip A. Nelson
Computer Science Department
Western Washington University
Bellingham, WA 98226
Wanna contact the current maintainers? Here's how:
e-mail: downsj@downsj.com
and
e-mail: gray@gnu.org
Got questions or feedback specifically about crash tolerance? You can reach Terence Kelly at:
e-mail: tpkelly @ { acm.org, cs.princeton.edu, eecs.umich.edu }
aaaand that's the whole page, bestie!! π this little gem is GDBM(3), dated March 6, 2025, straight from the GDBM source β go forth and slay those databases πβ¨πΈ
π¦ where this page comes from
- alpine 3.23: gdbm-doc 1.26-r0 Β· GPL-3.0-or-later
- alpine 3.24: gdbm-doc 1.26-r0 Β· GPL-3.0-or-later
- arch rolling: gdbm 1.26-2 Β· GPL-3.0-or-later
- centos-stream 10: gdbm-devel 1:1.26-15.el10 Β· GPL-3.0-or-later
- nixpkgs unstable: gdbm 1.26
- ubuntu questing: libgdbm-dev 1.26-1
- ubuntu resolute: libgdbm-dev 1.26-1build1 Β· GPL-3.0-or-later
- ubuntu stonking: libgdbm-dev 1.26-1build1 Β· GPL-3.0-or-later
- Copyright (C) 2011-2025 Free Software Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms