1 #include "registry_internals.h"
3 // ----------------------------------------------------------------------------
6 int registry_url_compare(void *a, void *b) {
7 if(((REGISTRY_URL *)a)->hash < ((REGISTRY_URL *)b)->hash) return -1;
8 else if(((REGISTRY_URL *)a)->hash > ((REGISTRY_URL *)b)->hash) return 1;
9 else return strcmp(((REGISTRY_URL *)a)->url, ((REGISTRY_URL *)b)->url);
12 #define registry_url_index_add(rc) (REGISTRY_URL *)avl_insert(&(registry.registry_urls_root_index), (avl *)(rc))
13 #define registry_url_index_del(rc) (REGISTRY_URL *)avl_remove(&(registry.registry_urls_root_index), (avl *)(rc))
15 REGISTRY_URL *registry_url_get(const char *url, size_t urllen) {
16 // protection from too big URLs
17 if(urllen > registry.max_url_length)
18 urllen = registry.max_url_length;
20 debug(D_REGISTRY, "Registry: registry_url_get('%s', %zu)", url, urllen);
22 char buf[sizeof(REGISTRY_URL) + urllen]; // no need for +1, 1 is already in REGISTRY_URL
23 REGISTRY_URL *n = (REGISTRY_URL *)&buf[0];
24 n->len = (uint16_t)urllen;
25 strncpyz(n->url, url, n->len);
26 n->hash = simple_hash(n->url);
28 REGISTRY_URL *u = (REGISTRY_URL *)avl_search(&(registry.registry_urls_root_index), (avl *)n);
30 debug(D_REGISTRY, "Registry: registry_url_get('%s', %zu): allocating %zu bytes", url, urllen, sizeof(REGISTRY_URL) + urllen);
31 u = callocz(1, sizeof(REGISTRY_URL) + urllen); // no need for +1, 1 is already in REGISTRY_URL
33 // a simple strcpy() should do the job
34 // but I prefer to be safe, since the caller specified urllen
35 u->len = (uint16_t)urllen;
36 strncpyz(u->url, url, u->len);
38 u->hash = simple_hash(u->url);
40 registry.urls_memory += sizeof(REGISTRY_URL) + urllen; // no need for +1, 1 is already in REGISTRY_URL
42 debug(D_REGISTRY, "Registry: registry_url_get('%s'): indexing it", url);
43 n = registry_url_index_add(u);
45 error("INTERNAL ERROR: registry_url_get(): url '%s' already exists in the registry as '%s'", u->url, n->url);
50 registry.urls_count++;
56 void registry_url_link(REGISTRY_URL *u) {
58 debug(D_REGISTRY, "Registry: registry_url_link('%s'): URL has now %u links", u->url, u->links);
61 void registry_url_unlink(REGISTRY_URL *u) {
64 debug(D_REGISTRY, "Registry: registry_url_unlink('%s'): No more links for this URL", u->url);
65 REGISTRY_URL *n = registry_url_index_del(u);
67 error("INTERNAL ERROR: registry_url_unlink(): request to delete url '%s', deleted url '%s'", u->url, n->url);
69 registry.urls_memory -= sizeof(REGISTRY_URL) + n->len; // no need for +1, 1 is already in REGISTRY_URL
73 debug(D_REGISTRY, "Registry: registry_url_unlink('%s'): URL has %u links left", u->url, u->links);