aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS2
-rw-r--r--compat/dbminit.c2
-rw-r--r--compat/dbmopen.c24
-rw-r--r--src/bucket.c89
-rw-r--r--src/falloc.c81
-rw-r--r--src/findkey.c30
-rw-r--r--src/gdbm.h.in1
-rw-r--r--src/gdbm_load.c4
-rw-r--r--src/gdbmcount.c2
-rw-r--r--src/gdbmdefs.h2
-rw-r--r--src/gdbmdelete.c7
-rw-r--r--src/gdbmdump.c14
-rw-r--r--src/gdbmerrno.c8
-rw-r--r--src/gdbmexists.c2
-rw-r--r--src/gdbmexp.c12
-rw-r--r--src/gdbmfetch.c4
-rw-r--r--src/gdbmimp.c10
-rw-r--r--src/gdbmload.c12
-rw-r--r--src/gdbmopen.c48
-rw-r--r--src/gdbmreorg.c26
-rw-r--r--src/gdbmseq.c21
-rw-r--r--src/gdbmsetopt.c40
-rw-r--r--src/gdbmstore.c43
-rw-r--r--src/gdbmsync.c2
-rw-r--r--src/mmap.c6
-rw-r--r--src/proto.h8
-rw-r--r--src/update.c69
27 files changed, 363 insertions, 206 deletions
diff --git a/NEWS b/NEWS
index 4efabdb..a564b2e 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,8 @@ Instead it sets gdbm_errno to GDBM_MALLOC_ERROR and returns NULL datum.
** gdbm_clear_error
+** gdbm_needs_recovery
+
Version 1.12, 2016-05-16
diff --git a/compat/dbminit.c b/compat/dbminit.c
index f5203d1..526410d 100644
--- a/compat/dbminit.c
+++ b/compat/dbminit.c
@@ -48,7 +48,7 @@ dbminit (char *file)
/* Did we successfully open the file? */
if (_gdbm_file == NULL)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, TRUE);
return -1;
}
}
diff --git a/compat/dbmopen.c b/compat/dbmopen.c
index af423df..f576b37 100644
--- a/compat/dbmopen.c
+++ b/compat/dbmopen.c
@@ -71,7 +71,7 @@ ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
if (fstat (pagfd, &pagst))
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 1); /* FIXME: special code? */
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, TRUE); /* FIXME: special code? */
return -1;
}
@@ -89,14 +89,14 @@ ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
return pagfd;
else
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, TRUE);
return -1;
}
}
}
else
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
}
@@ -104,7 +104,7 @@ ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
/* ok */;
else if (st.st_size != DEF_DIR_SIZE)
{
- gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, 1);
+ gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, FALSE);
return -1;
}
else
@@ -112,13 +112,13 @@ ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
fd = open (file_name, flags);
if (fd == -1)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return fd;
}
if (read (fd, dirbuf, sizeof (dirbuf)) != sizeof (dirbuf))
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
close (fd);
return -1;
}
@@ -135,7 +135,7 @@ ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
return fd;
}
close (fd);
- gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, 1);
+ gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, FALSE);
return -1;
}
}
@@ -151,7 +151,7 @@ ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
if (write (fd, dirbuf, sizeof (dirbuf)) != sizeof (dirbuf))
{
- gdbm_set_errno (NULL, GDBM_FILE_WRITE_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_FILE_WRITE_ERROR, FALSE);
close (fd);
fd = -1;
}
@@ -168,7 +168,7 @@ ndbm_open_dir_file (const char *base, int pagfd, int mode)
if (!file_name)
{
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
fd = ndbm_open_dir_file0 (strcat (strcpy (file_name, base), DIRSUF),
@@ -212,7 +212,7 @@ dbm_open (char *file, int flags, int mode)
pag_file = (char *) malloc (strlen (file) + 5);
if (!pag_file)
{
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 1); /* For the hell of it. */
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE); /* For the hell of it. */
return NULL;
}
@@ -250,7 +250,7 @@ dbm_open (char *file, int flags, int mode)
if (!dbm)
{
free (pag_file);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 1); /* For the hell of it. */
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE); /* For the hell of it. */
return NULL;
}
@@ -259,7 +259,7 @@ dbm_open (char *file, int flags, int mode)
/* Did we successfully open the file? */
if (dbm->file == NULL)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 1);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
free (dbm);
dbm = NULL;
}
diff --git a/src/bucket.c b/src/bucket.c
index ee0e157..96ed8b4 100644
--- a/src/bucket.c
+++ b/src/bucket.c
@@ -49,7 +49,7 @@ _gdbm_new_bucket (GDBM_FILE dbf, hash_bucket *bucket, int bits)
bucket. In any case, the requested bucket becomes the "current" bucket
and dbf->bucket points to the correct bucket. */
-void
+int
_gdbm_get_bucket (GDBM_FILE dbf, int dir_index)
{
int rc;
@@ -63,8 +63,11 @@ _gdbm_get_bucket (GDBM_FILE dbf, int dir_index)
if (dbf->bucket_cache == NULL)
{
- if(_gdbm_init_cache(dbf, DEFAULT_CACHESIZE) == -1)
- _gdbm_fatal(dbf, _("couldn't init cache"));
+ if (_gdbm_init_cache (dbf, DEFAULT_CACHESIZE) == -1)
+ {
+ _gdbm_fatal (dbf, _("couldn't init cache"));
+ return -1;
+ }
}
/* Is that one is not already current, we must find it. */
@@ -77,14 +80,17 @@ _gdbm_get_bucket (GDBM_FILE dbf, int dir_index)
{
dbf->bucket = dbf->bucket_cache[index].ca_bucket;
dbf->cache_entry = &dbf->bucket_cache[index];
- return;
+ return 0;
}
}
/* It is not in the cache, read it from the disk. */
dbf->last_read = (dbf->last_read + 1) % dbf->cache_size;
if (dbf->bucket_cache[dbf->last_read].ca_changed)
- _gdbm_write_bucket (dbf, &dbf->bucket_cache[dbf->last_read]);
+ {
+ if (_gdbm_write_bucket (dbf, &dbf->bucket_cache[dbf->last_read]))
+ return -1;
+ }
dbf->bucket_cache[dbf->last_read].ca_adr = bucket_adr;
dbf->bucket = dbf->bucket_cache[dbf->last_read].ca_bucket;
dbf->cache_entry = &dbf->bucket_cache[dbf->last_read];
@@ -94,14 +100,22 @@ _gdbm_get_bucket (GDBM_FILE dbf, int dir_index)
/* Read the bucket. */
file_pos = __lseek (dbf, bucket_adr, SEEK_SET);
if (file_pos != bucket_adr)
- _gdbm_fatal (dbf, _("lseek error"));
+ {
+ _gdbm_fatal (dbf, _("lseek error"));
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ return -1;
+ }
rc = _gdbm_full_read (dbf, dbf->bucket, dbf->header->bucket_size);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ gdbm_set_errno (dbf, rc, TRUE);
+ return -1;
+ }
}
- return;
+ return 0;
}
int
@@ -131,13 +145,13 @@ _gdbm_read_bucket_at (GDBM_FILE dbf, off_t off, hash_bucket *bucket,
file_pos = __lseek (dbf, off, SEEK_SET);
if (file_pos != off)
{
- gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, 1);
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
return -1;
}
rc = _gdbm_full_read (dbf, bucket, size);
if (rc)
{
- gdbm_set_errno (dbf, rc, 1);
+ gdbm_set_errno (dbf, rc, TRUE);
return -1;
}
return 0;
@@ -147,7 +161,7 @@ _gdbm_read_bucket_at (GDBM_FILE dbf, off_t off, hash_bucket *bucket,
a new bucket. This doesn't require any disk reads because all hash values
are stored in the buckets. Splitting the current bucket may require
doubling the size of the hash directory. */
-void
+int
_gdbm_split_bucket (GDBM_FILE dbf, int next_insert)
{
hash_bucket *bucket[2]; /* Pointers to the new buckets. */
@@ -176,14 +190,16 @@ _gdbm_split_bucket (GDBM_FILE dbf, int next_insert)
bucket_element *old_el; /* Pointer into the old bucket. */
int select; /* Used to index bucket during movement. */
-
/* No directories are yet old. */
old_count = 0;
if (dbf->bucket_cache == NULL)
{
- if(_gdbm_init_cache(dbf, DEFAULT_CACHESIZE) == -1)
- _gdbm_fatal(dbf, _("couldn't init cache"));
+ if (_gdbm_init_cache(dbf, DEFAULT_CACHESIZE) == -1)
+ {
+ _gdbm_fatal (dbf, _("couldn't init cache"));
+ return -1;
+ }
}
while (dbf->bucket->count == dbf->header->bucket_elems)
@@ -197,7 +213,10 @@ _gdbm_split_bucket (GDBM_FILE dbf, int next_insert)
while (dbf->bucket_cache[cache_0].ca_bucket == dbf->bucket);
bucket[0] = dbf->bucket_cache[cache_0].ca_bucket;
if (dbf->bucket_cache[cache_0].ca_changed)
- _gdbm_write_bucket (dbf, &dbf->bucket_cache[cache_0]);
+ {
+ if (_gdbm_write_bucket (dbf, &dbf->bucket_cache[cache_0]))
+ return -1;
+ }
do
{
dbf->last_read = (dbf->last_read + 1) % dbf->cache_size;
@@ -206,13 +225,20 @@ _gdbm_split_bucket (GDBM_FILE dbf, int next_insert)
while (dbf->bucket_cache[cache_1].ca_bucket == dbf->bucket);
bucket[1] = dbf->bucket_cache[cache_1].ca_bucket;
if (dbf->bucket_cache[cache_1].ca_changed)
- _gdbm_write_bucket (dbf, &dbf->bucket_cache[cache_1]);
+ {
+ if (_gdbm_write_bucket (dbf, &dbf->bucket_cache[cache_1]))
+ return -1;
+ }
new_bits = dbf->bucket->bucket_bits+1;
_gdbm_new_bucket (dbf, bucket[0], new_bits);
_gdbm_new_bucket (dbf, bucket[1], new_bits);
- adr_0 = _gdbm_alloc (dbf, dbf->header->bucket_size);
+ adr_0 = _gdbm_alloc (dbf, dbf->header->bucket_size);
+ if (adr_0 == 0)
+ return -1;
dbf->bucket_cache[cache_0].ca_adr = adr_0;
adr_1 = _gdbm_alloc (dbf, dbf->header->bucket_size);
+ if (adr_1 == 0)
+ return -1;
dbf->bucket_cache[cache_1].ca_adr = adr_1;
/* Double the directory size if necessary. */
@@ -220,8 +246,16 @@ _gdbm_split_bucket (GDBM_FILE dbf, int next_insert)
{
dir_size = dbf->header->dir_size * 2;
dir_adr = _gdbm_alloc (dbf, dir_size);
+ if (dir_adr == 0)
+ return -1;
new_dir = (off_t *) malloc (dir_size);
- if (new_dir == NULL) _gdbm_fatal (dbf, _("malloc error"));
+ if (new_dir == NULL)
+ {
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("malloc error"));
+ return -1;
+ }
+
for (index = 0; index < GDBM_DIR_COUNT (dbf); index++)
{
new_dir[2*index] = dbf->dir[index];
@@ -258,6 +292,8 @@ _gdbm_split_bucket (GDBM_FILE dbf, int next_insert)
/* Allocate avail space for the bucket[1]. */
bucket[1]->bucket_avail[0].av_adr
= _gdbm_alloc (dbf, dbf->header->block_size);
+ if (bucket[1]->bucket_avail[0].av_adr == 0)
+ return -1;
bucket[1]->bucket_avail[0].av_size = dbf->header->block_size;
bucket[1]->av_count = 1;
@@ -329,13 +365,15 @@ _gdbm_split_bucket (GDBM_FILE dbf, int next_insert)
/* Get rid of old directories. */
for (index = 0; index < old_count; index++)
_gdbm_free (dbf, old_adr[index], old_size[index]);
+
+ return 0;
}
/* The only place where a bucket is written. CA_ENTRY is the
cache entry containing the bucket to be written. */
-void
+int
_gdbm_write_bucket (GDBM_FILE dbf, cache_elem *ca_entry)
{
int rc;
@@ -343,12 +381,21 @@ _gdbm_write_bucket (GDBM_FILE dbf, cache_elem *ca_entry)
file_pos = __lseek (dbf, ca_entry->ca_adr, SEEK_SET);
if (file_pos != ca_entry->ca_adr)
- _gdbm_fatal (dbf, _("lseek error"));
+ {
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("lseek error"));
+ return -1;
+ }
rc = _gdbm_full_write (dbf, ca_entry->ca_bucket, dbf->header->bucket_size);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ return -1;
+ }
ca_entry->ca_changed = FALSE;
ca_entry->ca_data.hash_val = -1;
ca_entry->ca_data.elem_loc = -1;
+ return 0;
}
diff --git a/src/falloc.c b/src/falloc.c
index 32aad51..58a9431 100644
--- a/src/falloc.c
+++ b/src/falloc.c
@@ -28,8 +28,8 @@
static avail_elem get_elem (int, avail_elem [], int *);
static avail_elem get_block (int, GDBM_FILE);
-static void push_avail_block (GDBM_FILE);
-static void pop_avail_block (GDBM_FILE);
+static int push_avail_block (GDBM_FILE);
+static int pop_avail_block (GDBM_FILE);
static void adjust_bucket_avail (GDBM_FILE);
/* Allocate space in the file DBF for a block NUM_BYTES in length. Return
@@ -65,7 +65,8 @@ _gdbm_alloc (GDBM_FILE dbf, int num_bytes)
something on the stack. */
if ((dbf->header->avail.count <= (dbf->header->avail.size >> 1))
&& (dbf->header->avail.next_block != 0))
- pop_avail_block (dbf);
+ if (pop_avail_block (dbf))
+ return 0;
/* check the header avail table next */
av_el = get_elem (num_bytes, dbf->header->avail.av_table,
@@ -114,7 +115,7 @@ _gdbm_free (GDBM_FILE dbf, off_t file_adr, int num_bytes)
{
if (dbf->header->avail.count == dbf->header->avail.size)
{
- push_avail_block (dbf);
+ push_avail_block (dbf);//FIXME: return
}
_gdbm_put_av_elem (temp, dbf->header->avail.av_table,
&dbf->header->avail.count, dbf->coalesce_blocks);
@@ -130,7 +131,7 @@ _gdbm_free (GDBM_FILE dbf, off_t file_adr, int num_bytes)
{
if (dbf->header->avail.count == dbf->header->avail.size)
{
- push_avail_block (dbf);
+ push_avail_block (dbf); //FIXME: return
}
_gdbm_put_av_elem (temp, dbf->header->avail.av_table,
&dbf->header->avail.count, dbf->coalesce_blocks);
@@ -155,7 +156,7 @@ _gdbm_free (GDBM_FILE dbf, off_t file_adr, int num_bytes)
now called even when the avail block is not empty, so we must be
smart about things. */
-static void
+static int
pop_avail_block (GDBM_FILE dbf)
{
int rc;
@@ -168,7 +169,8 @@ pop_avail_block (GDBM_FILE dbf)
{
/* We're kind of stuck here, so we re-split the header in order to
avoid crashing. Sigh. */
- push_avail_block(dbf);
+ if (push_avail_block (dbf))
+ return -1;
}
/* Set up variables. */
@@ -178,14 +180,29 @@ pop_avail_block (GDBM_FILE dbf)
/* Allocate space for the block. */
new_blk = (avail_block *) malloc (new_el.av_size);
- if (new_blk == NULL) _gdbm_fatal(dbf, _("malloc failed"));
+ if (new_blk == NULL)
+ {
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, TRUE);
+ _gdbm_fatal(dbf, _("malloc failed"));
+ return -1;
+ }
/* Read the block. */
file_pos = __lseek (dbf, new_el.av_adr, SEEK_SET);
- if (file_pos != new_el.av_adr) _gdbm_fatal (dbf, _("lseek error"));
+ if (file_pos != new_el.av_adr)
+ {
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("lseek error"));
+ return -1;
+ }
+
rc = _gdbm_full_read (dbf, new_blk, new_el.av_size);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ return -1;
+ }
/* Add the elements from the new block to the header. */
index = 0;
@@ -195,16 +212,17 @@ pop_avail_block (GDBM_FILE dbf)
&& dbf->header->avail.count < dbf->header->avail.size)
{
/* With luck, this will merge a lot of blocks! */
- _gdbm_put_av_elem(new_blk->av_table[index],
- dbf->header->avail.av_table,
- &dbf->header->avail.count, TRUE);
+ _gdbm_put_av_elem (new_blk->av_table[index],
+ dbf->header->avail.av_table,
+ &dbf->header->avail.count, TRUE);
index++;
}
if (dbf->header->avail.count == dbf->header->avail.size)
{
/* We're kind of stuck here, so we re-split the header in order to
avoid crashing. Sigh. */
- push_avail_block(dbf);
+ if (push_avail_block (dbf))
+ return -1;
}
}
@@ -220,18 +238,21 @@ pop_avail_block (GDBM_FILE dbf)
{
/* We're kind of stuck here, so we re-split the header in order to
avoid crashing. Sigh. */
- push_avail_block(dbf);
+ if (push_avail_block (dbf))
+ return -1;
}
_gdbm_put_av_elem (new_el, dbf->header->avail.av_table,
&dbf->header->avail.count, TRUE);
free (new_blk);
+
+ return 0;
}
/* Splits the header avail block and pushes half onto the avail stack. */
-static void
+static int
push_avail_block (GDBM_FILE dbf)
{
int av_size;
@@ -253,10 +274,15 @@ push_avail_block (GDBM_FILE dbf)
new_loc = get_block (av_size, dbf);
av_adr = new_loc.av_adr;
-
/* Split the header block. */
temp = (avail_block *) malloc (av_size);
- if (temp == NULL) _gdbm_fatal (dbf, _("malloc error"));
+ if (temp == NULL)
+ {
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("malloc error"));
+ return -1;
+ }
+
/* Set the size to be correct AFTER the pop_avail_block. */
temp->size = dbf->header->avail.size;
temp->count = 0;
@@ -279,14 +305,25 @@ push_avail_block (GDBM_FILE dbf)
/* Update the disk. */
file_pos = __lseek (dbf, av_adr, SEEK_SET);
- if (file_pos != av_adr) _gdbm_fatal (dbf, _("lseek error"));
+ if (file_pos != av_adr)
+ {
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("lseek error"));
+ return -1;
+ }
+
rc = _gdbm_full_write (dbf, temp, av_size);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
- free (temp);
-}
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ return -1;
+ }
+ free (temp);
+ return 0;
+}
/* Get_elem returns an element in the AV_TABLE block which is
larger than SIZE. AV_COUNT is the number of elements in the
diff --git a/src/findkey.c b/src/findkey.c
index 6b13fbf..34091e1 100644
--- a/src/findkey.c
+++ b/src/findkey.c
@@ -54,17 +54,30 @@ _gdbm_read_entry (GDBM_FILE dbf, int elem_loc)
data_ca->dptr = (char *) malloc (1);
else
data_ca->dptr = (char *) malloc (key_size+data_size);
- if (data_ca->dptr == NULL) _gdbm_fatal (dbf, _("malloc error"));
-
+ if (data_ca->dptr == NULL)
+ {
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, FALSE);
+ _gdbm_fatal (dbf, _("malloc error"));
+ return NULL;
+ }
/* Read into the cache. */
file_pos = __lseek (dbf, dbf->bucket->h_table[elem_loc].data_pointer,
SEEK_SET);
if (file_pos != dbf->bucket->h_table[elem_loc].data_pointer)
- _gdbm_fatal (dbf, _("lseek error"));
+ {
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("lseek error"));
+ return NULL;
+ }
+
rc = _gdbm_full_read (dbf, data_ca->dptr, key_size+data_size);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ return NULL;
+ }
return data_ca->dptr;
}
@@ -92,8 +105,9 @@ _gdbm_findkey (GDBM_FILE dbf, datum key, char **ret_dptr, int *ret_hash_val)
new_hash_val = _gdbm_hash (key);
if (ret_hash_val)
*ret_hash_val = new_hash_val;
- _gdbm_get_bucket (dbf, new_hash_val>> (31-dbf->header->dir_bits));
-
+ if (_gdbm_get_bucket (dbf, new_hash_val >> (31 - dbf->header->dir_bits)))
+ return -1;
+
/* Is the element the last one found for this bucket? */
if (dbf->cache_entry->ca_data.elem_loc != -1
&& new_hash_val == dbf->cache_entry->ca_data.hash_val
@@ -130,6 +144,8 @@ _gdbm_findkey (GDBM_FILE dbf, datum key, char **ret_dptr, int *ret_hash_val)
/* This may be the one we want.
The only way to tell is to read it. */
file_key = _gdbm_read_entry (dbf, elem_loc);
+ if (!file_key)
+ return -1;
if (memcmp (file_key, key.dptr, key_size) == 0)
{
/* This is the item. */
@@ -149,7 +165,7 @@ _gdbm_findkey (GDBM_FILE dbf, datum key, char **ret_dptr, int *ret_hash_val)
}
/* If we get here, we never found the key. */
- gdbm_set_errno (dbf, GDBM_ITEM_NOT_FOUND, 0);
+ gdbm_set_errno (dbf, GDBM_ITEM_NOT_FOUND, FALSE);
return -1;
}
diff --git a/src/gdbm.h.in b/src/gdbm.h.in
index 83f99c2..a3d0461 100644
--- a/src/gdbm.h.in
+++ b/src/gdbm.h.in
@@ -183,6 +183,7 @@ extern const char * const gdbm_errlist[];
extern int gdbm_last_errno (GDBM_FILE dbf);
extern void gdbm_set_errno (GDBM_FILE dbf, gdbm_error ec, int fatal);
extern void gdbm_clear_error (GDBM_FILE dbf);
+extern int gdbm_needs_recovery (GDBM_FILE dbf);
/* extra prototypes */
diff --git a/src/gdbm_load.c b/src/gdbm_load.c
index fd404b9..c4b9ee6 100644
--- a/src/gdbm_load.c
+++ b/src/gdbm_load.c
@@ -53,13 +53,13 @@ set_meta_info (GDBM_FILE dbf)
{
if (fchown (fd, owner_uid, owner_gid))
{
- gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, 0);
+ gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, FALSE);
return 1;
}
}
if ((meta_mask & GDBM_META_MASK_MODE) && fchmod (fd, mode))
{
- gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, 0);
+ gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, FALSE);
return 1;
}
}
diff --git a/src/gdbmcount.c b/src/gdbmcount.c
index 472f0d3..a301d0c 100644
--- a/src/gdbmcount.c
+++ b/src/gdbmcount.c
@@ -46,7 +46,7 @@ gdbm_count (GDBM_FILE dbf, gdbm_count_t *pcount)
sdir = malloc (dbf->header->dir_size);
if (!sdir)
{
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
diff --git a/src/gdbmdefs.h b/src/gdbmdefs.h
index 34c8b55..ebe412b 100644
--- a/src/gdbmdefs.h
+++ b/src/gdbmdefs.h
@@ -241,7 +241,7 @@ struct gdbm_file_info
{ \
if (dbf->need_recovery) \
{ \
- gdbm_set_errno (dbf, GDBM_NEED_RECOVERY, 1); \
+ gdbm_set_errno (dbf, GDBM_NEED_RECOVERY, TRUE); \
return onerr; \
} \
} \
diff --git a/src/gdbmdelete.c b/src/gdbmdelete.c
index faa4957..a735d0d 100644
--- a/src/gdbmdelete.c
+++ b/src/gdbmdelete.c
@@ -41,12 +41,12 @@ gdbm_delete (GDBM_FILE dbf, datum key)
/* First check to make sure this guy is a writer. */
if (dbf->read_write == GDBM_READER)
{
- gdbm_set_errno (dbf, GDBM_READER_CANT_DELETE, 0);
+ gdbm_set_errno (dbf, GDBM_READER_CANT_DELETE, FALSE);
return -1;
}
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
/* Find the item. */
elem_loc = _gdbm_findkey (dbf, key, NULL, NULL);
@@ -98,6 +98,5 @@ gdbm_delete (GDBM_FILE dbf, datum key)
dbf->cache_entry->ca_data.elem_loc = -1;
/* Do the writes. */
- _gdbm_end_update (dbf);
- return 0;
+ return _gdbm_end_update (dbf);
}
diff --git a/src/gdbmdump.c b/src/gdbmdump.c
index 49f39bf..d4ec5b6 100644
--- a/src/gdbmdump.c
+++ b/src/gdbmdump.c
@@ -99,7 +99,7 @@ _gdbm_dump_ascii (GDBM_FILE dbf, FILE *fp)
{
free (key.dptr);
free (data.dptr);
- gdbm_set_errno (dbf, rc, 0);
+ gdbm_set_errno (dbf, rc, FALSE);
break;
}
}
@@ -142,13 +142,13 @@ gdbm_dump_to_file (GDBM_FILE dbf, FILE *fp, int format)
break;
default:
- gdbm_set_errno (NULL, GDBM_BAD_OPEN_FLAGS, 0);
+ gdbm_set_errno (NULL, GDBM_BAD_OPEN_FLAGS, FALSE);
return EINVAL;
}
if (rc == 0 && ferror (fp))
{
- gdbm_set_errno (NULL, GDBM_FILE_WRITE_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_WRITE_ERROR, FALSE);
rc = -1;
}
@@ -172,7 +172,7 @@ gdbm_dump (GDBM_FILE dbf, const char *filename, int fmt, int open_flags,
nfd = open (filename, O_WRONLY | O_CREAT | O_EXCL, mode);
if (nfd == -1)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
break;
@@ -180,12 +180,12 @@ gdbm_dump (GDBM_FILE dbf, const char *filename, int fmt, int open_flags,
nfd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (nfd == -1)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
break;
default:
- gdbm_set_errno (NULL, GDBM_BAD_OPEN_FLAGS, 0);
+ gdbm_set_errno (NULL, GDBM_BAD_OPEN_FLAGS, FALSE);
return -1;
}
@@ -193,7 +193,7 @@ gdbm_dump (GDBM_FILE dbf, const char *filename, int fmt, int open_flags,
if (!fp)
{
close (nfd);
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
rc = gdbm_dump_to_file (dbf, fp, fmt);
diff --git a/src/gdbmerrno.c b/src/gdbmerrno.c
index 4eeff8c..8882489 100644
--- a/src/gdbmerrno.c
+++ b/src/gdbmerrno.c
@@ -50,6 +50,14 @@ gdbm_last_errno (GDBM_FILE dbf)
return dbf->last_error;
}
+int
+gdbm_needs_recovery (GDBM_FILE dbf)
+{
+ if (!dbf)
+ return 0;
+ return dbf->need_recovery;
+}
+
/* Clear error state for the database DBF. */
void
gdbm_clear_error (GDBM_FILE dbf)
diff --git a/src/gdbmexists.c b/src/gdbmexists.c
index e44f04e..70ee989 100644
--- a/src/gdbmexists.c
+++ b/src/gdbmexists.c
@@ -33,7 +33,7 @@ gdbm_exists (GDBM_FILE dbf, datum key)
if (_gdbm_findkey (dbf, key, NULL, NULL) < 0)
{
if (gdbm_errno == GDBM_ITEM_NOT_FOUND)
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
return 0;
}
return 1;
diff --git a/src/gdbmexp.c b/src/gdbmexp.c
index f188040..4092c62 100644
--- a/src/gdbmexp.c
+++ b/src/gdbmexp.c
@@ -84,7 +84,7 @@ gdbm_export_to_file (GDBM_FILE dbf, FILE *fp)
write_fail:
- gdbm_set_errno (NULL, GDBM_FILE_WRITE_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_WRITE_ERROR, FALSE);
return -1;
}
@@ -101,7 +101,7 @@ gdbm_export (GDBM_FILE dbf, const char *exportfile, int flags, int mode)
nfd = open (exportfile, O_WRONLY | O_CREAT | O_EXCL, mode);
if (nfd == -1)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
break;
@@ -109,15 +109,15 @@ gdbm_export (GDBM_FILE dbf, const char *exportfile, int flags, int mode)
nfd = open (exportfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (nfd == -1)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
break;
default:
#ifdef GDBM_BAD_OPEN_FLAGS
- gdbm_set_errno (NULL, GDBM_BAD_OPEN_FLAGS, 0);
+ gdbm_set_errno (NULL, GDBM_BAD_OPEN_FLAGS, FALSE);
#else
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
#endif
return -1;
}
@@ -126,7 +126,7 @@ gdbm_export (GDBM_FILE dbf, const char *exportfile, int flags, int mode)
if (!fp)
{
close (nfd);
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
diff --git a/src/gdbmfetch.c b/src/gdbmfetch.c
index 25a6c1d..8deee9c 100644
--- a/src/gdbmfetch.c
+++ b/src/gdbmfetch.c
@@ -41,7 +41,7 @@ gdbm_fetch (GDBM_FILE dbf, datum key)
GDBM_ASSERT_CONSISTENCY (dbf, return_val);
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
/* Find the key and return a pointer to the data. */
elem_loc = _gdbm_findkey (dbf, key, &find_data, NULL);
@@ -57,7 +57,7 @@ gdbm_fetch (GDBM_FILE dbf, datum key)
return_val.dptr = (char *) malloc (return_val.dsize);
if (return_val.dptr == NULL)
{
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, FALSE);
return return_val;
}
memcpy (return_val.dptr, find_data, return_val.dsize);
diff --git a/src/gdbmimp.c b/src/gdbmimp.c
index 6227a39..57d1933 100644
--- a/src/gdbmimp.c
+++ b/src/gdbmimp.c
@@ -47,7 +47,7 @@ gdbm_import_from_file (GDBM_FILE dbf, FILE *fp, int flag)
{
if ((rret = fgetc (fp)) == -1)
{
- gdbm_set_errno (NULL, GDBM_FILE_READ_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_READ_ERROR, FALSE);
return -1;
}
@@ -69,7 +69,7 @@ gdbm_import_from_file (GDBM_FILE dbf, FILE *fp, int flag)
kbuffer = malloc (kbufsize);
if (kbuffer == NULL)
{
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
dbufsize = 512;
@@ -77,7 +77,7 @@ gdbm_import_from_file (GDBM_FILE dbf, FILE *fp, int flag)
if (dbuffer == NULL)
{
free (kbuffer);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
@@ -163,7 +163,7 @@ gdbm_import_from_file (GDBM_FILE dbf, FILE *fp, int flag)
if (ec == GDBM_NO_ERROR)
return count;
- gdbm_set_errno (NULL, ec, 0);
+ gdbm_set_errno (NULL, ec, FALSE);
return -1;
}
@@ -176,7 +176,7 @@ gdbm_import (GDBM_FILE dbf, const char *importfile, int flag)
fp = fopen (importfile, "r");
if (!fp)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
rc = gdbm_import_from_file (dbf, fp, flag);
diff --git a/src/gdbmload.c b/src/gdbmload.c
index 5723b95..04e1f0a 100644
--- a/src/gdbmload.c
+++ b/src/gdbmload.c
@@ -373,13 +373,13 @@ _set_gdbm_meta_info (GDBM_FILE dbf, char *param, int meta_mask)
}
if (fchown (fd, owner_uid, owner_gid))
{
- gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, 0);
+ gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, FALSE);
rc = 1;
}
}
if ((meta_flags & META_MODE) && fchmod (fd, mode))
{
- gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, 0);
+ gdbm_set_errno (dbf, GDBM_ERR_FILE_OWNER, FALSE);
rc = 1;
}
}
@@ -581,7 +581,7 @@ gdbm_load_from_file (GDBM_FILE *pdbf, FILE *fp, int replace,
*line = 0;
if (!*pdbf)
{
- gdbm_set_errno (NULL, GDBM_NO_DBNAME, 0);
+ gdbm_set_errno (NULL, GDBM_NO_DBNAME, FALSE);
return -1;
}
if (gdbm_import_from_file (*pdbf, fp, replace) == -1)
@@ -596,7 +596,7 @@ gdbm_load_from_file (GDBM_FILE *pdbf, FILE *fp, int replace,
{
if (!*pdbf)
{
- gdbm_set_errno (NULL, GDBM_NO_DBNAME, 0);
+ gdbm_set_errno (NULL, GDBM_NO_DBNAME, FALSE);
return -1;
}
rc = gdbm_load_bdb_dump (&df, *pdbf, replace);
@@ -608,7 +608,7 @@ gdbm_load_from_file (GDBM_FILE *pdbf, FILE *fp, int replace,
{
if (line)
*line = df.line;
- gdbm_set_errno (NULL, rc, 0);
+ gdbm_set_errno (NULL, rc, FALSE);
return -1;
}
return 0;
@@ -625,7 +625,7 @@ gdbm_load (GDBM_FILE *pdbf, const char *filename, int replace,
fp = fopen (filename, "r");
if (!fp)
{
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
rc = gdbm_load_from_file (pdbf, fp, replace, meta_mask, line);
diff --git a/src/gdbmopen.c b/src/gdbmopen.c
index a45cb04..e0bb102 100644
--- a/src/gdbmopen.c
+++ b/src/gdbmopen.c
@@ -67,13 +67,13 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
int fbits = 0; /* additional bits for open(2) flags */
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (NULL, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_NO_ERROR, FALSE);
/* Allocate new info structure. */
dbf = (GDBM_FILE) malloc (sizeof (*dbf));
if (dbf == NULL)
{
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return NULL;
}
@@ -98,7 +98,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (dbf->name == NULL)
{
free (dbf);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return NULL;
}
strcpy (dbf->name, file);
@@ -158,7 +158,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
{
SAVE_ERRNO (free (dbf->name);
free (dbf));
- gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return NULL;
}
@@ -168,7 +168,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
SAVE_ERRNO (close (dbf->desc);
free (dbf->name);
free (dbf));
- gdbm_set_errno (NULL, GDBM_FILE_STAT_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_STAT_ERROR, FALSE);
return NULL;
}
@@ -178,7 +178,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
close (dbf->desc);
free (dbf->name);
free (dbf);
- gdbm_set_errno (NULL, GDBM_EMPTY_DATABASE, 0);
+ gdbm_set_errno (NULL, GDBM_EMPTY_DATABASE, FALSE);
return NULL;
}
@@ -226,7 +226,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (dbf->header == NULL)
{
gdbm_close (dbf);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return NULL;
}
@@ -247,7 +247,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (dbf->header->dir_size != dbf->header->block_size)
{
gdbm_close (dbf);
- gdbm_set_errno (NULL, GDBM_BLOCK_SIZE_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_BLOCK_SIZE_ERROR, FALSE);
return NULL;
}
@@ -256,7 +256,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (dbf->dir == NULL)
{
gdbm_close (dbf);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return NULL;
}
dbf->header->dir = dbf->header->block_size;
@@ -270,7 +270,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (dbf->bucket == NULL)
{
gdbm_close (dbf);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return NULL;
}
_gdbm_new_bucket (dbf, dbf->bucket, 0);
@@ -296,7 +296,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (rc)
{
SAVE_ERRNO (gdbm_close (dbf));
- gdbm_set_errno (NULL, rc, 0);
+ gdbm_set_errno (NULL, rc, FALSE);
return NULL;
}
@@ -305,7 +305,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (rc)
{
SAVE_ERRNO (gdbm_close (dbf));
- gdbm_set_errno (NULL, rc, 0);
+ gdbm_set_errno (NULL, rc, FALSE);
return NULL;
}
@@ -314,7 +314,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (rc)
{
SAVE_ERRNO (gdbm_close (dbf));
- gdbm_set_errno (NULL, rc, 0);
+ gdbm_set_errno (NULL, rc, FALSE);
return NULL;
}
@@ -335,7 +335,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (rc)
{
SAVE_ERRNO (gdbm_close (dbf));
- gdbm_set_errno (NULL, rc, 0);
+ gdbm_set_errno (NULL, rc, FALSE);
return NULL;
}
@@ -349,14 +349,14 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
case GDBM_OMAGIC_SWAP:
case GDBM_MAGIC32_SWAP:
case GDBM_MAGIC64_SWAP:
- gdbm_set_errno (NULL, GDBM_BYTE_SWAPPED, 0);
+ gdbm_set_errno (NULL, GDBM_BYTE_SWAPPED, FALSE);
break;
case GDBM_MAGIC32:
case GDBM_MAGIC64:
- gdbm_set_errno (NULL, GDBM_BAD_FILE_OFFSET, 0);
+ gdbm_set_errno (NULL, GDBM_BAD_FILE_OFFSET, FALSE);
break;
default:
- gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, 0);
+ gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, FALSE);
}
return NULL;
}
@@ -366,7 +366,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (dbf->header == NULL)
{
gdbm_close (dbf);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return NULL;
}
memcpy (dbf->header, &partial_header, sizeof (gdbm_file_header));
@@ -375,7 +375,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (rc)
{
SAVE_ERRNO (gdbm_close (dbf));
- gdbm_set_errno (NULL, rc, 0);
+ gdbm_set_errno (NULL, rc, FALSE);
return NULL;
}
@@ -384,7 +384,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (dbf->dir == NULL)
{
gdbm_close (dbf);
- gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return NULL;
}
@@ -393,7 +393,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (file_pos != dbf->header->dir)
{
SAVE_ERRNO (gdbm_close (dbf));
- gdbm_set_errno (NULL, GDBM_FILE_SEEK_ERROR, 0);
+ gdbm_set_errno (NULL, GDBM_FILE_SEEK_ERROR, FALSE);
return NULL;
}
@@ -401,7 +401,7 @@ gdbm_open (const char *file, int block_size, int flags, int mode,
if (rc)
{
SAVE_ERRNO (gdbm_close (dbf));
- gdbm_set_errno (NULL, rc, 0);
+ gdbm_set_errno (NULL, rc, FALSE);
return NULL;
}
@@ -450,7 +450,7 @@ _gdbm_init_cache(GDBM_FILE dbf, size_t size)
dbf->bucket_cache = (cache_elem *) malloc(sizeof(cache_elem) * size);
if(dbf->bucket_cache == NULL)
{
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 1);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, TRUE);
return -1;
}
dbf->cache_size = size;
@@ -461,7 +461,7 @@ _gdbm_init_cache(GDBM_FILE dbf, size_t size)
= (hash_bucket *) malloc (dbf->header->bucket_size);
if ((dbf->bucket_cache[index]).ca_bucket == NULL)
{
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 1);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, TRUE);
return -1;
}
(dbf->bucket_cache[index]).ca_adr = 0;
diff --git a/src/gdbmreorg.c b/src/gdbmreorg.c
index 8a0cc64..13fa7fc 100644
--- a/src/gdbmreorg.c
+++ b/src/gdbmreorg.c
@@ -71,26 +71,26 @@ gdbm_reorganize (GDBM_FILE dbf)
/* Readers can not reorganize! */
if (dbf->read_write == GDBM_READER)
{
- gdbm_set_errno (dbf, GDBM_READER_CANT_REORGANIZE, 0);
+ gdbm_set_errno (dbf, GDBM_READER_CANT_REORGANIZE, FALSE);
return -1;
}
/* Get the mode for the old file */
if (fstat (dbf->desc, &fileinfo))
{
- gdbm_set_errno (dbf, GDBM_FILE_STAT_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_FILE_STAT_ERROR, FALSE);
return -1;
}
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
/* Construct new name for temporary file. */
len = strlen (dbf->name);
new_name = (char *) malloc (len + 3);
if (new_name == NULL)
{
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
strcpy (&new_name[0], dbf->name);
@@ -111,7 +111,7 @@ gdbm_reorganize (GDBM_FILE dbf)
if (new_dbf == NULL)
{
free (new_name);
- gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, 0);
+ gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, FALSE);
return -1;
}
@@ -128,7 +128,7 @@ gdbm_reorganize (GDBM_FILE dbf)
if (gdbm_store (new_dbf, key, data, GDBM_INSERT) != 0)
{
gdbm_close (new_dbf);
- gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, 0);
+ gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, FALSE);
unlink (new_name);
free (new_name);
return -1;
@@ -138,7 +138,7 @@ gdbm_reorganize (GDBM_FILE dbf)
{
/* ERROR! Abort and don't finish reorganize. */
gdbm_close (new_dbf);
- gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, 0);
+ gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, FALSE);
unlink (new_name);
free (new_name);
return -1;
@@ -150,7 +150,11 @@ gdbm_reorganize (GDBM_FILE dbf)
}
/* Write everything. */
- _gdbm_end_update (new_dbf);
+ if (_gdbm_end_update (new_dbf))
+ {
+ gdbm_close (new_dbf);
+ return -1;
+ }
gdbm_sync (new_dbf);
#if HAVE_MMAP
@@ -161,7 +165,7 @@ gdbm_reorganize (GDBM_FILE dbf)
if (rename (new_name, dbf->name) != 0)
{
- gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, 0);
+ gdbm_set_errno (NULL, GDBM_REORGANIZE_FAILED, FALSE);
gdbm_close (new_dbf);
free (new_name);
return -1;
@@ -214,7 +218,5 @@ gdbm_reorganize (GDBM_FILE dbf)
/* Force the right stuff for a correct bucket cache. */
dbf->cache_entry = &dbf->bucket_cache[0];
- _gdbm_get_bucket (dbf, 0);
-
- return 0;
+ return _gdbm_get_bucket (dbf, 0);
}
diff --git a/src/gdbmseq.c b/src/gdbmseq.c
index 831339a..4320366 100644
--- a/src/gdbmseq.c
+++ b/src/gdbmseq.c
@@ -22,10 +22,6 @@
#include "gdbmdefs.h"
-/* Special extern for this file. */
-extern char *_gdbm_read_entry (GDBM_FILE , int);
-
-
/* Find and read the next entry in the hash structure for DBF starting
at ELEM_LOC of the current bucket and using RETURN_VAL as the place to
put the data that is found.
@@ -61,11 +57,14 @@ get_next_key (GDBM_FILE dbf, int elem_loc, datum *return_val)
/* Check to see if there was a next bucket. */
if (dbf->bucket_dir < GDBM_DIR_COUNT (dbf))
- _gdbm_get_bucket (dbf, dbf->bucket_dir);
+ {
+ if (_gdbm_get_bucket (dbf, dbf->bucket_dir))
+ return;
+ }
else
{
/* No next key, just return. */
- gdbm_set_errno (dbf, GDBM_ITEM_NOT_FOUND, 0);
+ gdbm_set_errno (dbf, GDBM_ITEM_NOT_FOUND, FALSE);
return;
}
}
@@ -74,6 +73,8 @@ get_next_key (GDBM_FILE dbf, int elem_loc, datum *return_val)
/* Found the next key, read it into return_val. */
find_data = _gdbm_read_entry (dbf, elem_loc);
+ if (!find_data)
+ return;
return_val->dsize = dbf->bucket->h_table[elem_loc].key_size;
if (return_val->dsize == 0)
return_val->dptr = (char *) malloc (1);
@@ -82,7 +83,7 @@ get_next_key (GDBM_FILE dbf, int elem_loc, datum *return_val)
if (return_val->dptr == NULL)
{
return_val->dsize = 0;
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, FALSE);
}
else
memcpy (return_val->dptr, find_data, return_val->dsize);
@@ -104,7 +105,7 @@ gdbm_firstkey (GDBM_FILE dbf)
GDBM_ASSERT_CONSISTENCY (dbf, return_val);
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
/* Get the first bucket. */
_gdbm_get_bucket (dbf, 0);
@@ -131,12 +132,12 @@ gdbm_nextkey (GDBM_FILE dbf, datum key)
GDBM_ASSERT_CONSISTENCY (dbf, return_val);
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
/* Do we have a valid key? */
if (key.dptr == NULL)
{
- gdbm_set_errno (dbf, GDBM_ITEM_NOT_FOUND, 0); /* FIXME: special error code perhaps */
+ gdbm_set_errno (dbf, GDBM_ITEM_NOT_FOUND, FALSE); /* FIXME: special error code perhaps */
return return_val;
}
diff --git a/src/gdbmsetopt.c b/src/gdbmsetopt.c
index 47e01ad..39b5e04 100644
--- a/src/gdbmsetopt.c
+++ b/src/gdbmsetopt.c
@@ -68,13 +68,13 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
/* Optval will point to the new size of the cache. */
if (dbf->bucket_cache != NULL)
{
- gdbm_set_errno (dbf, GDBM_OPT_ALREADY_SET, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ALREADY_SET, FALSE);
return -1;
}
if (get_size (optval, optlen, &sz))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
return _gdbm_init_cache (dbf, (sz > 9) ? sz : 10);
@@ -82,7 +82,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_GETCACHESIZE:
if (!optval || optlen != sizeof (size_t))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
*(size_t*) optval = dbf->cache_size;
@@ -92,7 +92,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_FASTMODE:
if ((n = getbool (optval, optlen)) == -1)
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
dbf->fast_write = n;
@@ -104,7 +104,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
/* Optval will point to either true or false. */
if ((n = getbool (optval, optlen)) == -1)
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
dbf->fast_write = !n;
@@ -113,7 +113,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_GETSYNCMODE:
if (!optval || optlen != sizeof (int))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
*(int*) optval = !dbf->fast_write;
@@ -124,7 +124,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
/* Optval will point to either true or false. */
if ((n = getbool (optval, optlen)) == -1)
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
dbf->central_free = n;
@@ -133,7 +133,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_GETCENTFREE:
if (!optval || optlen != sizeof (int))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
*(int*) optval = !dbf->central_free;
@@ -144,7 +144,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
/* Optval will point to either true or false. */
if ((n = getbool (optval, optlen)) == -1)
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
dbf->coalesce_blocks = n;
@@ -153,7 +153,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_GETCOALESCEBLKS:
if (!optval || optlen != sizeof (int))
{
- gdbm_set_errno (NULL, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (NULL, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
*(int*) optval = dbf->coalesce_blocks;
@@ -164,7 +164,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
#if HAVE_MMAP
if ((n = getbool (optval, optlen)) == -1)
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
__fsync (dbf);
@@ -183,7 +183,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
dbf->memory_mapping = FALSE;
}
#else
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
#endif
break;
@@ -191,7 +191,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_GETMMAP:
if (!optval || optlen != sizeof (int))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
*(int*) optval = dbf->memory_mapping;
@@ -205,7 +205,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
if (get_size (optval, optlen, &sz))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
dbf->mapped_size_max = ((sz + page_size - 1) / page_size) *
@@ -214,14 +214,14 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
break;
}
#else
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
#endif
case GDBM_GETMAXMAPSIZE:
if (!optval || optlen != sizeof (size_t))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
*(size_t*) optval = dbf->mapped_size_max;
@@ -231,7 +231,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_GETFLAGS:
if (!optval || optlen != sizeof (int))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
else
@@ -250,7 +250,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
case GDBM_GETDBNAME:
if (!optval || optlen != sizeof (char*))
{
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
else
@@ -258,7 +258,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
char *p = strdup (dbf->name);
if (!p)
{
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
*(char**) optval = p;
@@ -266,7 +266,7 @@ gdbm_setopt (GDBM_FILE dbf, int optflag, void *optval, int optlen)
break;
default:
- gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, 0);
+ gdbm_set_errno (dbf, GDBM_OPT_ILLEGAL, FALSE);
return -1;
}
diff --git a/src/gdbmstore.c b/src/gdbmstore.c
index ec4b96b..7aec604 100644
--- a/src/gdbmstore.c
+++ b/src/gdbmstore.c
@@ -53,7 +53,7 @@ gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags)
/* First check to make sure this guy is a writer. */
if (dbf->read_write == GDBM_READER)
{
- gdbm_set_errno (dbf, GDBM_READER_CANT_STORE, 0);
+ gdbm_set_errno (dbf, GDBM_READER_CANT_STORE, FALSE);
return -1;
}
@@ -61,12 +61,12 @@ gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags)
NULL dptr returned by a lookup procedure indicates an error. */
if ((key.dptr == NULL) || (content.dptr == NULL))
{
- gdbm_set_errno (dbf, GDBM_ILLEGAL_DATA, 0);
+ gdbm_set_errno (dbf, GDBM_ILLEGAL_DATA, FALSE);
return -1;
}
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
/* Look for the key in the file.
A side effect loads the correct bucket and calculates the hash value. */
@@ -97,19 +97,23 @@ gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags)
}
else
{
- gdbm_set_errno (dbf, GDBM_CANNOT_REPLACE, 0);
+ gdbm_set_errno (dbf, GDBM_CANNOT_REPLACE, FALSE);
return 1;
}
}
else if (gdbm_errno == GDBM_ITEM_NOT_FOUND)
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0); /* clear error state */
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE); /* clear error state */
else
return -1;
/* Get the file address for the new space.
(Current bucket's free space is first place to look.) */
if (file_adr == 0)
- file_adr = _gdbm_alloc (dbf, new_size);
+ {
+ file_adr = _gdbm_alloc (dbf, new_size);
+ if (file_adr == 0)
+ return -1;
+ }
/* If this is a new entry in the bucket, we need to do special things. */
if (elem_loc == -1)
@@ -117,7 +121,8 @@ gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags)
if (dbf->bucket->count == dbf->header->bucket_elems)
{
/* Split the current bucket. */
- _gdbm_split_bucket (dbf, new_hash_val);
+ if (_gdbm_split_bucket (dbf, new_hash_val))
+ return -1;
}
/* Find space to insert into bucket and set elem_loc to that place. */
@@ -141,20 +146,32 @@ gdbm_store (GDBM_FILE dbf, datum key, datum content, int flags)
/* Write the data to the file. */
file_pos = __lseek (dbf, file_adr, SEEK_SET);
if (file_pos != file_adr)
- _gdbm_fatal (dbf, _("lseek error"));
+ {
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("lseek error"));
+ return -1;
+ }
+
rc = _gdbm_full_write (dbf, key.dptr, key.dsize);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ return -1;
+ }
+
rc = _gdbm_full_write (dbf, content.dptr, content.dsize);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ return -1;
+ }
/* Current bucket has changed. */
dbf->cache_entry->ca_changed = TRUE;
dbf->bucket_changed = TRUE;
/* Write everything that is needed to the disk. */
- _gdbm_end_update (dbf);
- return 0;
-
+ return _gdbm_end_update (dbf);
}
diff --git a/src/gdbmsync.c b/src/gdbmsync.c
index 1d2a1d4..fcc1b41 100644
--- a/src/gdbmsync.c
+++ b/src/gdbmsync.c
@@ -31,7 +31,7 @@ gdbm_sync (GDBM_FILE dbf)
GDBM_ASSERT_CONSISTENCY (dbf, );
/* Initialize the gdbm_errno variable. */
- gdbm_set_errno (dbf, GDBM_NO_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
/* Do the sync on the file. */
__fsync (dbf);
diff --git a/src/mmap.c b/src/mmap.c
index 5e89b28..d5fe5c0 100644
--- a/src/mmap.c
+++ b/src/mmap.c
@@ -99,7 +99,7 @@ _gdbm_internal_remap (GDBM_FILE dbf, size_t size)
if (p == MAP_FAILED)
{
dbf->mapped_region = NULL;
- gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
@@ -135,7 +135,7 @@ _gdbm_mapped_remap (GDBM_FILE dbf, off_t size, int flag)
if (_gdbm_file_size (dbf, &file_size))
{
SAVE_ERRNO (_gdbm_mapped_unmap (dbf));
- gdbm_set_errno (dbf, GDBM_FILE_STAT_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_FILE_STAT_ERROR, FALSE);
return -1;
}
@@ -330,7 +330,7 @@ _gdbm_mapped_lseek (GDBM_FILE dbf, off_t offset, int whence)
off_t file_size;
if (_gdbm_file_size (dbf, &file_size))
{
- gdbm_set_errno (dbf, GDBM_FILE_STAT_ERROR, 0);
+ gdbm_set_errno (dbf, GDBM_FILE_STAT_ERROR, FALSE);
return -1;
}
needle = file_size - offset;
diff --git a/src/proto.h b/src/proto.h
index 1b7a40a..07ff378 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -20,12 +20,12 @@
/* From bucket.c */
void _gdbm_new_bucket (GDBM_FILE, hash_bucket *, int);
-void _gdbm_get_bucket (GDBM_FILE, int);
+int _gdbm_get_bucket (GDBM_FILE, int);
int _gdbm_read_bucket_at (GDBM_FILE dbf, off_t off, hash_bucket *bucket,
size_t size);
-void _gdbm_split_bucket (GDBM_FILE, int);
-void _gdbm_write_bucket (GDBM_FILE, cache_elem *);
+int _gdbm_split_bucket (GDBM_FILE, int);
+int _gdbm_write_bucket (GDBM_FILE, cache_elem *);
/* From falloc.c */
off_t _gdbm_alloc (GDBM_FILE, int);
@@ -40,7 +40,7 @@ int _gdbm_findkey (GDBM_FILE, datum, char **, int *);
int _gdbm_hash (datum);
/* From update.c */
-void _gdbm_end_update (GDBM_FILE);
+int _gdbm_end_update (GDBM_FILE);
void _gdbm_fatal (GDBM_FILE, const char *);
/* From gdbmopen.c */
diff --git a/src/update.c b/src/update.c
index 88f5e39..2ddfa4b 100644
--- a/src/update.c
+++ b/src/update.c
@@ -1,8 +1,8 @@
/* update.c - The routines for updating the file to a consistent state. */
/* This file is part of GDBM, the GNU data base manager.
- Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 Free Software Foundation,
- Inc.
+ Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013,
+ 2016 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
@@ -22,31 +22,41 @@
#include "gdbmdefs.h"
-static void write_header (GDBM_FILE);
-
/* This procedure writes the header back to the file described by DBF. */
-static void
+static int
write_header (GDBM_FILE dbf)
{
off_t file_pos; /* Return value for lseek. */
int rc;
file_pos = __lseek (dbf, 0L, SEEK_SET);
- if (file_pos != 0) _gdbm_fatal (dbf, _("lseek error"));
+ if (file_pos != 0)
+ {
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("lseek error"));
+ return -1;
+ }
+
rc = _gdbm_full_write (dbf, dbf->header, dbf->header->block_size);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ gdbm_set_errno (dbf, rc, TRUE);
+ return -1;
+ }
/* Sync the file if fast_write is FALSE. */
if (dbf->fast_write == FALSE)
__fsync (dbf);
+
+ return 0;
}
/* After all changes have been made in memory, we now write them
all to disk. */
-void
+int
_gdbm_end_update (GDBM_FILE dbf)
{
off_t file_pos; /* Return value for lseek. */
@@ -55,7 +65,8 @@ _gdbm_end_update (GDBM_FILE dbf)
/* Write the current bucket. */
if (dbf->bucket_changed && (dbf->cache_entry != NULL))
{
- _gdbm_write_bucket (dbf, dbf->cache_entry);
+ if (_gdbm_write_bucket (dbf, dbf->cache_entry))
+ return -1;
dbf->bucket_changed = FALSE;
}
@@ -69,7 +80,10 @@ _gdbm_end_update (GDBM_FILE dbf)
for (index = 0; index < dbf->cache_size; index++)
{
if (dbf->bucket_cache[index].ca_changed)
- _gdbm_write_bucket (dbf, &dbf->bucket_cache[index]);
+ {
+ if (_gdbm_write_bucket (dbf, &dbf->bucket_cache[index]))
+ return -1;
+ }
}
}
dbf->second_changed = FALSE;
@@ -79,10 +93,21 @@ _gdbm_end_update (GDBM_FILE dbf)
if (dbf->directory_changed)
{
file_pos = __lseek (dbf, dbf->header->dir, SEEK_SET);
- if (file_pos != dbf->header->dir) _gdbm_fatal (dbf, _("lseek error"));
+ if (file_pos != dbf->header->dir)
+ {
+ gdbm_set_errno (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
+ _gdbm_fatal (dbf, _("lseek error"));
+ return -1;
+ }
+
rc = _gdbm_full_write (dbf, dbf->dir, dbf->header->dir_size);
if (rc)
- _gdbm_fatal (dbf, gdbm_strerror (rc));
+ {
+ gdbm_set_errno (dbf, rc, TRUE);
+ _gdbm_fatal (dbf, gdbm_strerror (rc));
+ return -1;
+ }
+
dbf->directory_changed = FALSE;
if (!dbf->header_changed && dbf->fast_write == FALSE)
__fsync (dbf);
@@ -91,22 +116,24 @@ _gdbm_end_update (GDBM_FILE dbf)
/* Final write of the header. */
if (dbf->header_changed)
{
- write_header (dbf);
+ if (write_header (dbf))
+ return -1;
dbf->header_changed = FALSE;
}
+
+ return 0;
}
-/* If a fatal error is detected, come here and exit. VAL tells which fatal
- error occured. */
+/* For backward compatibility, if the caller defined fatal_err function,
+ call it upon fatal error and exit. */
void
_gdbm_fatal (GDBM_FILE dbf, const char *val)
{
- if ((dbf != NULL) && (dbf->fatal_err != NULL))
- (*dbf->fatal_err) (val);
- else
- fprintf (stderr, _("gdbm fatal: %s\n"), val ? val : "");
- exit (1);
- /* NOTREACHED */
+ if (dbf && dbf->fatal_err)
+ {
+ (*dbf->fatal_err) (val);
+ exit (1);
+ }
}

Return to:

Send suggestions and report system problems to the System administrator.