diff options
Diffstat (limited to 'src/gdbmtool.c')
-rw-r--r-- | src/gdbmtool.c | 127 |
1 files changed, 43 insertions, 84 deletions
diff --git a/src/gdbmtool.c b/src/gdbmtool.c index f9a4924..7fbecc0 100644 --- a/src/gdbmtool.c +++ b/src/gdbmtool.c @@ -1,4 +1,3 @@ /* 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. @@ -218,45 +217,25 @@ print_bucket (FILE *fp, hash_bucket *bucket, const char *mesg, ...) -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 min_size; 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) +static int +avail_list_count (avail_block *avblk, off_t off, void *data) { - terror ("lseek: %s", strerror (errno)); - break; - } + struct avail_list_counter *ctr = data; - if (_gdbm_full_read (dbf, av_stk, size)) - { - terror ("read: %s", gdbm_db_strerror (dbf)); - break; + ctr->lines += avblk->count; + return ctr->lines > ctr->min_size; } - if (gdbm_avail_block_valid_p (av_stk)) +size_t +_gdbm_avail_list_size (GDBM_FILE dbf, size_t min_size) { - lines += av_stk->count; - if (lines > min_size) - break; - } - temp = av_stk->next_block; - } - free (av_stk); - - return lines; + struct avail_list_counter ctr; + ctr.min_size = 0; + ctr.lines = 0; + gdbm_avail_traverse (dbf, avail_list_count, &ctr); + return ctr.lines; } @@ -275,2 +254,12 @@ 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 @@ -278,6 +267,2 @@ _gdbm_print_avail_list (FILE *fp, GDBM_FILE dbf) { - int temp; - int size; - avail_block *av_stk; - /* Print the the header avail block. */ @@ -286,34 +271,4 @@ _gdbm_print_avail_list (FILE *fp, GDBM_FILE dbf) 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)); } @@ -528,2 +483,3 @@ nextkey_handler (struct handler_param *param) { + free (key_data.dptr); key_data = return_data; @@ -790,13 +746,7 @@ 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)); } @@ -1237,3 +1187,3 @@ struct command command_tab[] = { REPEAT_NOARG, - N_("nextkey") }, + N_("continue iteration: get next key and datum") }, { S(store), T_CMD, @@ -1251,3 +1201,3 @@ struct command command_tab[] = { REPEAT_NEVER, - N_("firstkey") }, + N_("begin iteration: get first key and datum") }, { S(reorganize), T_CMD, @@ -1674,3 +1624,3 @@ kvpair_list (struct locus *loc, struct slist *s) -static void +void kvlist_free (struct kvpair *kvp) @@ -1696,2 +1646,11 @@ 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 |