int main (int argc, char *argv[]) { arc_t *arc = NULL; arc_item_t *item = NULL; //int first = 0; unsigned long id; if (argc > 1) arc = arc_load (argv[1], NULL); else arc = arc_load (outfile, (arc_sig_t *)signature); if (arc == NULL) { printf ("*** Error: failed to open archive file \"%s\"!\n", outfile); exit (42); } printf ("%6s %10s %10s %s\n", "ID", "POS", "SIZE", "NAME"); printf ("%6s %10s %10s %s\n", "--", "---", "----", "----"); while ((item = arc_next (arc, item)) != NULL) { printf ("%6lu %10lu %10lu %s\n", item->id, item->pos, item->size, item->name); //if (!first) { // first = 1; // write_item (item); //} } printf ("\n"); id = 101; item = arc_byid (arc, id); if (item == NULL) printf ("*** Error: item with id %lu not found!\n", id); else { printf ("%6lu %10lu %10lu %s\n", item->id, item->pos, item->size, item->name); write_item (item); printf ("\n"); } return (0); }
int arc_load(arc_t *cache, const void *key, size_t klen, void *valuep, size_t vlen) { arc_object_t *obj = ht_get_deep_copy(cache->hash, (void *)key, klen, NULL, update_obj_cb, cache); if (obj) { //release_ref(cache->refcnt, obj->node); return 1; } obj = arc_object_create(cache, key, klen); if (!obj) return -1; // let our cache user initialize the underlying object cache->ops->init(key, klen, 0, (arc_resource_t)obj, obj->ptr, cache->ops->priv); cache->ops->store(obj->ptr, valuep, vlen, cache->ops->priv); retain_ref(cache->refcnt, obj->node); // NOTE: atomicity here is ensured by the hashtable implementation int rc = ht_set_if_not_exists(cache->hash, (void *)key, klen, obj, sizeof(arc_object_t)); switch(rc) { case -1: fprintf(stderr, "Can't set the new value in the internal hashtable\n"); release_ref(cache->refcnt, obj->node); break; case 1: // the object has been created in the meanwhile release_ref(cache->refcnt, obj->node); // XXX - yes, we have to release it twice release_ref(cache->refcnt, obj->node); return arc_load(cache, key, klen, valuep, vlen); case 0: break; default: fprintf(stderr, "Unknown return code from ht_set_if_not_exists() : %d\n", rc); release_ref(cache->refcnt, obj->node); rc = -1; } release_ref(cache->refcnt, obj->node); return rc; }