aboutsummaryrefslogtreecommitdiff
path: root/src/gdbmtool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gdbmtool.c')
-rw-r--r--src/gdbmtool.c139
1 files changed, 49 insertions, 90 deletions
diff --git a/src/gdbmtool.c b/src/gdbmtool.c
index f9a4924..7fbecc0 100644
--- a/src/gdbmtool.c
+++ b/src/gdbmtool.c
@@ -1,6 +1,5 @@
/* This file is part of GDBM, the GNU data base manager.
- Copyright (C) 1990-1991, 1993, 2007, 2011, 2013, 2016-2020 Free
- Software Foundation, Inc.
+ Copyright (C) 1990-2021 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
@@ -216,49 +215,29 @@ print_bucket (FILE *fp, hash_bucket *bucket, const char *mesg, ...)
bucket->bucket_avail[index].av_size);
}
-size_t
-_gdbm_avail_list_size (GDBM_FILE dbf, size_t min_size)
+struct avail_list_counter
{
- int temp;
- int size;
- avail_block *av_stk;
- size_t lines;
-
- lines = 4 + dbf->header->avail.count;
- if (lines > min_size)
- return lines;
- /* Initialize the variables for a pass throught the avail stack. */
- temp = dbf->header->avail.next_block;
- size = (((dbf->header->avail.size * sizeof (avail_elem)) >> 1)
- + sizeof (avail_block));
- av_stk = emalloc (size);
-
- /* Traverse the stack. */
- while (temp)
- {
- if (gdbm_file_seek (dbf, temp, SEEK_SET) != temp)
- {
- terror ("lseek: %s", strerror (errno));
- break;
- }
-
- if (_gdbm_full_read (dbf, av_stk, size))
- {
- terror ("read: %s", gdbm_db_strerror (dbf));
- break;
- }
+ size_t min_size;
+ size_t lines;
+};
- if (gdbm_avail_block_valid_p (av_stk))
- {
- lines += av_stk->count;
- if (lines > min_size)
- break;
- }
- temp = av_stk->next_block;
- }
- free (av_stk);
+static int
+avail_list_count (avail_block *avblk, off_t off, void *data)
+{
+ struct avail_list_counter *ctr = data;
- return lines;
+ ctr->lines += avblk->count;
+ return ctr->lines > ctr->min_size;
+}
+
+size_t
+_gdbm_avail_list_size (GDBM_FILE dbf, size_t min_size)
+{
+ struct avail_list_counter ctr;
+ ctr.min_size = 0;
+ ctr.lines = 0;
+ gdbm_avail_traverse (dbf, avail_list_count, &ctr);
+ return ctr.lines;
}
static void
@@ -273,49 +252,25 @@ av_table_display (avail_elem *av_table, int count, FILE *fp)
}
}
+static int
+avail_list_print (avail_block *avblk, off_t n, void *data)
+{
+ FILE *fp = data;
+ fprintf (fp, _("\nblock = %lu\nsize = %d\ncount = %d\n"),
+ (unsigned long) n, avblk->size, avblk->count);
+ av_table_display (avblk->av_table, avblk->count, fp);
+ return 0;
+}
+
void
_gdbm_print_avail_list (FILE *fp, GDBM_FILE dbf)
{
- int temp;
- int size;
- avail_block *av_stk;
-
/* Print the the header avail block. */
fprintf (fp, _("\nheader block\nsize = %d\ncount = %d\n"),
dbf->header->avail.size, dbf->header->avail.count);
av_table_display (dbf->header->avail.av_table, dbf->header->avail.count, fp);
-
- /* Initialize the variables for a pass throught the avail stack. */
- temp = dbf->header->avail.next_block;
- size = (dbf->header->avail.size * sizeof (avail_elem))
- + sizeof (avail_block);
- av_stk = emalloc (size);
-
- /* Print the stack. */
- while (temp)
- {
- if (gdbm_file_seek (dbf, temp, SEEK_SET) != temp)
- {
- terror ("lseek: %s", strerror (errno));
- break;
- }
-
- if (_gdbm_full_read (dbf, av_stk, size))
- {
- terror ("read: %s", gdbm_db_strerror (dbf));
- break;
- }
-
- /* Print the block! */
- fprintf (fp, _("\nblock = %d\nsize = %d\ncount = %d\n"), temp,
- av_stk->size, av_stk->count);
- if (gdbm_avail_block_validate (dbf, av_stk) == 0)
- av_table_display (av_stk->av_table, av_stk->count, fp);
- else
- terror (_("invalid avail_block"));
- temp = av_stk->next_block;
- }
- free (av_stk);
+ if (gdbm_avail_traverse (dbf, avail_list_print, fp))
+ terror ("%s", gdbm_strerror (gdbm_errno));
}
void
@@ -526,6 +481,7 @@ nextkey_handler (struct handler_param *param)
return_data = gdbm_nextkey (gdbm_file, key_data);
if (return_data.dptr != NULL)
{
+ free (key_data.dptr);
key_data = return_data;
datum_format (param->fp, &key_data, dsdef[DS_KEY]);
fputc ('\n', param->fp);
@@ -788,17 +744,11 @@ print_dir_begin (struct handler_param *param GDBM_ARG_UNUSED, size_t *exp_count)
static size_t
bucket_count (void)
{
- int i;
- off_t last = 0;
size_t count = 0;
-
- for (i = 0; i < GDBM_DIR_COUNT (gdbm_file); i++)
+
+ if (gdbm_bucket_count (gdbm_file, &count))
{
- if (gdbm_file->dir[i] != last)
- {
- ++count;
- last = gdbm_file->dir[i];
- }
+ terror ("gdbm_bucket_count: %s", gdbm_strerror (gdbm_errno));
}
return count;
}
@@ -1235,7 +1185,7 @@ struct command command_tab[] = {
{ NULL } },
FALSE,
REPEAT_NOARG,
- N_("nextkey") },
+ N_("continue iteration: get next key and datum") },
{ S(store), T_CMD,
checkdb_begin, store_handler, NULL,
{ { N_("KEY"), GDBM_ARG_DATUM, DS_KEY },
@@ -1249,7 +1199,7 @@ struct command command_tab[] = {
{ { NULL } },
FALSE,
REPEAT_NEVER,
- N_("firstkey") },
+ N_("begin iteration: get first key and datum") },
{ S(reorganize), T_CMD,
checkdb_begin, reorganize_handler, NULL,
{ { NULL } },
@@ -1672,7 +1622,7 @@ kvpair_list (struct locus *loc, struct slist *s)
return p;
}
-static void
+void
kvlist_free (struct kvpair *kvp)
{
while (kvp)
@@ -1694,6 +1644,15 @@ kvlist_free (struct kvpair *kvp)
}
}
+struct kvpair *
+kvlist_find (struct kvpair *kv, char const *tag)
+{
+ for (; kv; kv = kv->next)
+ if (kv->key && strcmp (kv->key, tag) == 0)
+ break;
+ return kv;
+}
+
int
gdbmarg_free (struct gdbmarg *arg)
{

Return to:

Send suggestions and report system problems to the System administrator.