--- a/dav/db.c Sun Apr 16 14:12:24 2023 +0200 +++ b/dav/db.c Fri Apr 21 21:25:32 2023 +0200 @@ -33,7 +33,7 @@ #include "db.h" -#include <ucx/utils.h> +#include <cx/utils.h> #include <libidav/utils.h> @@ -55,8 +55,11 @@ free(dav_dir); SyncDatabase *db = malloc(sizeof(SyncDatabase)); - db->resources = ucx_map_new(2048); - db->conflict = ucx_map_new(16); + db->resources = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 2048); + db->conflict = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); + + db->resources->destructor_data = (cx_destructor_func)local_resource_free; + db->conflict->destructor_data = (cx_destructor_func)local_resource_free; xmlTextReaderPtr reader = xmlReaderForFile(db_file, NULL, 0); if(!reader) { @@ -82,7 +85,7 @@ if(xstreq(name, "resource")) { LocalResource *res = process_resource(reader); if(res) { - ucx_map_cstr_put(db->resources, res->path, res); + cxMapPut(db->resources, cx_hash_key_str(res->path), res); } else { error = 1; break; @@ -90,7 +93,7 @@ } else if(xstreq(name, "conflict")) { LocalResource *res = process_conflict(reader); if(res) { - ucx_map_cstr_put(db->conflict, res->path, res); + cxMapPut(db->conflict, cx_hash_key_str(res->path), res); } else { error = 1; break; @@ -109,7 +112,10 @@ } void process_parts(xmlTextReaderPtr reader, LocalResource *res) { - UcxList *parts = NULL; + // TODO: rewrite using low level array + + CxList *parts = cxLinkedListCreateSimple(CX_STORE_POINTERS); + parts->destructor_data = (cx_destructor_func)filepart_free; FilePart *current_part = NULL; @@ -121,12 +127,11 @@ const xmlChar *name = xmlTextReaderConstName(reader); int depth = xmlTextReaderDepth(reader); - int part = TRUE; if(type == XML_READER_TYPE_ELEMENT) { if(depth == 3 && xstreq(name, "part")) { current_part = calloc(1, sizeof(FilePart)); current_part->block = count; - parts = ucx_list_append(parts, current_part); + cxListAdd(parts, current_part); count++; } else if(depth == 4) { if(xstreq(name, "hash")) { @@ -159,25 +164,21 @@ } } - if(err) { - ucx_list_free_content(parts, (ucx_destructor)filepart_free); - ucx_list_free(parts); - return; - } + if(!err) { + FilePart *file_parts = calloc(count, sizeof(FilePart)); + size_t i = 0; + CxIterator iter = cxListIterator(parts); + cx_foreach(FilePart*, p, iter) { + file_parts[i] = *p; + free(p); + i++; + } + + res->parts = file_parts; + res->numparts = count; + } - FilePart *file_parts = calloc(count, sizeof(FilePart)); - size_t i = 0; - UCX_FOREACH(elm, parts) { - FilePart *p = elm->data; - file_parts[i] = *p; - free(p); - i++; - } - - ucx_list_free(parts); - - res->parts = file_parts; - res->numparts = count; + cxListDestroy(parts); } LocalResource* process_resource(xmlTextReaderPtr reader) { @@ -410,9 +411,9 @@ xmlTextWriterStartElement(writer, BAD_CAST "directory"); // write all resource entries - UcxMapIterator i = ucx_map_iterator(db->resources); + CxIterator i = cxMapIteratorValues(db->resources); LocalResource *res; - UCX_MAP_FOREACH(key, res, i) { + cx_foreach(LocalResource*, res, i) { // <resource> xmlTextWriterStartElement(writer, BAD_CAST "resource"); @@ -687,8 +688,8 @@ */ // write all conflict entries - i = ucx_map_iterator(db->conflict); - UCX_MAP_FOREACH(key, res, i) { + i = cxMapIteratorValues(db->conflict); + cx_foreach(LocalResource*, res, i) { // <conflict> xmlTextWriterStartElement(writer, BAD_CAST "conflict"); @@ -720,10 +721,8 @@ } void destroy_db(SyncDatabase *db) { - ucx_map_free_content(db->resources, (ucx_destructor)local_resource_free); - ucx_map_free_content(db->conflict, (ucx_destructor)local_resource_free); - ucx_map_free(db->resources); - ucx_map_free(db->conflict); + cxMapDestroy(db->resources); + cxMapDestroy(db->conflict); free(db); } @@ -741,7 +740,7 @@ free(res->etag); } if(res->cached_tags) { - ucx_buffer_free(res->cached_tags); + cxBufferFree(res->cached_tags); } if(res->tags_hash) { free(res->tags_hash); @@ -790,10 +789,10 @@ xattr->hash = nullstrdup(src->xattr->hash); xattr->nattr = src->xattr->nattr; xattr->names = calloc(xattr->nattr, sizeof(char*)); - xattr->values = calloc(xattr->nattr, sizeof(sstr_t)); + xattr->values = calloc(xattr->nattr, sizeof(cxmutstr)); for(int i=0;i<xattr->nattr;i++) { xattr->names[i] = strdup(src->xattr->names[i]); - xattr->values[i] = sstrdup(src->xattr->values[i]); + xattr->values[i] = cx_strdup(cx_strcast(src->xattr->values[i])); } newres->xattr = xattr; } @@ -825,15 +824,14 @@ free(part); } -UcxMap* create_hash_index(SyncDatabase *db) { - UcxMap *hmap = ucx_map_new(db->resources->count + 64); +CxMap* create_hash_index(SyncDatabase *db) { + CxMap *hmap = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, db->resources->size + 64); - UcxMapIterator i = ucx_map_iterator(db->resources); - UcxKey key; + CxIterator i = cxMapIteratorValues(db->resources); LocalResource *res; - UCX_MAP_FOREACH(key, res, i) { + cx_foreach(LocalResource*, res, i) { if(res->hash) { - ucx_map_cstr_put(hmap, res->hash, res); + cxMapPut(hmap, cx_hash_key_str(res->hash), res); } }