/** * iterate_keys - call a function once for each key in the db. * @iterfunc: The function to call. * @ctx: A context pointer * * Calls iterfunc once for each key in the database. ctx is passed * unaltered to iterfunc. This function is intended to aid database dumps * and statistic calculations. * * Returns the number of keys we iterated over. */ static int keyd_iterate_keys(struct onak_dbctx *dbctx, void (*iterfunc)(void *ctx, struct openpgp_publickey *key), void *ctx) { int keyd_fd = (intptr_t) dbctx->priv; struct buffer_ctx keybuf; struct openpgp_packet_list *packets = NULL; struct openpgp_publickey *key = NULL; ssize_t bytes = 0; ssize_t count = 0; int numkeys = 0; if (keyd_send_cmd(keyd_fd, KEYD_CMD_KEYITER)) { keybuf.offset = 0; read(keyd_fd, &keybuf.size, sizeof(keybuf.size)); while (keybuf.size > 0) { keybuf.buffer = malloc(keybuf.size); bytes = count = 0; logthing(LOGTHING_TRACE, "Getting %d bytes of key data.", keybuf.size); while (bytes >= 0 && count < keybuf.size) { bytes = read(keyd_fd, &keybuf.buffer[count], keybuf.size - count); logthing(LOGTHING_TRACE, "Read %d bytes.", bytes); count += bytes; } read_openpgp_stream(buffer_fetchchar, &keybuf, &packets, 0); parse_keys(packets, &key); if (iterfunc != NULL && key != NULL) { iterfunc(ctx, key); } free_publickey(key); key = NULL; free_packet_list(packets); packets = NULL; free(keybuf.buffer); keybuf.buffer = NULL; keybuf.size = keybuf.offset = 0; numkeys++; read(keyd_fd, &keybuf.size, sizeof(keybuf.size)); } } return numkeys; }
void genhash_iter(genhash_t* h, void (*iterfunc)(const void* key, const void* val, void *arg), void *arg) { size_t i=0; struct genhash_entry_t *p=NULL; cb_assert(h != NULL); for(i=0; i<h->size; i++) { for(p=h->buckets[i]; p!=NULL; p=p->next) { iterfunc(p->key, p->value, arg); } } }
void genhash_iter_key(genhash_t *h, const void *key, lcb_size_t klen, void (*iterfunc)(const void *key, lcb_size_t klen, const void *val, lcb_size_t vlen, void *arg), void *arg) { lcb_size_t n = 0; struct genhash_entry_t *p = NULL; lcb_assert(h != NULL); n = h->ops.hashfunc(key, klen) % h->size; lcb_assert(n < h->size); for (p = h->buckets[n]; p != NULL; p = p->next) { if (h->ops.hasheq(key, klen, p->key, p->nkey)) { iterfunc(p->key, p->nkey, p->value, p->nvalue, arg); } } }
void genhash_iter_key(genhash_t* h, const void* key, void (*iterfunc)(const void* key, const void* val, void *arg), void *arg) { int n=0; struct genhash_entry_t *p=NULL; cb_assert(h != NULL); n=h->ops.hashfunc(key) % h->size; cb_assert(n >= 0); cb_assert(n < (int) h->size); for(p=h->buckets[n]; p!=NULL; p=p->next) { if(h->ops.hasheq(key, p->key)) { iterfunc(p->key, p->value, arg); } } }
int johnny_hash_iter(johnny_hash_t* h, johnny_iter_func_t* iterfunc, void* ctx) { johnny_hash_bucket_t* iter; size_t i; assert(h && "hash is null"); assert(h->buckets && "hash has no buckets"); for(i = 0; i < h->num_buckets; i++) { iter = h->buckets[i]; while(iter) { if(!iterfunc(ctx, iter->item)) return 0; iter = iter->next; } } return 1; }