Mon, 28 Oct 2024 15:22:28 +0100
add pwdstore_clone function and fix some pwdstore bugs
dav/main.c | file | annotate | diff | comparison | revisions | |
libidav/pwdstore.c | file | annotate | diff | comparison | revisions | |
libidav/pwdstore.h | file | annotate | diff | comparison | revisions |
--- a/dav/main.c Mon Oct 28 10:36:59 2024 +0100 +++ b/dav/main.c Mon Oct 28 15:22:28 2024 +0100 @@ -2675,13 +2675,17 @@ if(ret) { fprintf(stderr, "Error: saving srcrets store failed.\n"); } + if(cxListSize(locations) == 0) { + cxListDestroy(locations); + } + } else { + cxListDestroy(locations); } if(id) free(id); if(user) free(user); if(password) free(password); - cxListDestroy(locations); return ret; }
--- a/libidav/pwdstore.c Mon Oct 28 10:36:59 2024 +0100 +++ b/libidav/pwdstore.c Mon Oct 28 15:22:28 2024 +0100 @@ -109,6 +109,61 @@ return p; } +PwdStore* pwdstore_clone(PwdStore *p) { + CxBuffer *newbuffer = calloc(1, sizeof(CxBuffer)); + *newbuffer = *p->content; + newbuffer->space = malloc(p->content->capacity); + memcpy(newbuffer->space, p->content->space, p->content->capacity); + + DavKey *key = NULL; + if(p->key) { + key = malloc(sizeof(DavKey)); + key->data = malloc(p->key->length); + memcpy(key->data, p->key->data, p->key->length); + key->length = p->key->length; + key->type = p->key->type; + key->name = NULL; + } + + PwdStore *newp = calloc(1, sizeof(PwdStore)); + newp->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); + newp->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); + newp->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS); + newp->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); + newp->content = newbuffer; + newp->key = key; + newp->unlock_cmd = p->unlock_cmd ? strdup(p->unlock_cmd) : NULL; + newp->lock_cmd = p->lock_cmd ? strdup(p->lock_cmd) : NULL; + newp->encoffset = p->encoffset; + newp->isdecrypted = p->isdecrypted; + + CxIterator i = cxMapIterator(p->ids); + cx_foreach(CxMapEntry *, e, i) { + PwdEntry *entry = e->value; + PwdEntry *new_entry = malloc(sizeof(PwdEntry)); + new_entry->id = strdup(entry->id); + new_entry->user = entry->user ? strdup(entry->user) : NULL; + new_entry->password = entry->password ? strdup(entry->password) : NULL; + cxMapPut(newp->ids, *e->key, new_entry); + } + + i = cxMapIterator(p->index); + cx_foreach(CxMapEntry *, e, i) { + PwdIndexEntry *entry = e->value; + CxList *locations = NULL; + if(entry->locations) { + locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxIterator li = cxListIterator(entry->locations); + cx_foreach(char *, location, li) { + cxListAdd(locations, strdup(location)); + } + } + pwdstore_put_index(newp, entry->id, locations); + } + + return newp; +} + static int readval(CxBuffer *in, char **val, int allowzero) { // value = length string // length = uint32 @@ -172,6 +227,9 @@ if(ret) { pwdstore_put_index(p, id, locations); + if(cxListSize(locations) == 0) { + cxListDestroy(locations); + } } else { if(id) free(id); cxListDestroy(locations); @@ -381,13 +439,12 @@ } PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); newentry->id = id; - if(cxListSize(locations) > 0) { + if(locations && cxListSize(locations) > 0) { newentry->locations = locations; cxListAdd(p->locations, newentry); } else { newentry->locations = NULL; cxListAdd(p->noloc, newentry); - cxListDestroy(locations); } cxMapPut(p->index, cx_hash_key_str(id), newentry); } @@ -401,13 +458,15 @@ cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out); cxBufferWrite(e->id, 1, idlen, out); - CxIterator i = cxListIterator(e->locations); - cx_foreach(char *, location, i) { - uint32_t locationlen = strlen(location); - uint32_t netlocationlen = htonl(locationlen); - - cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); - cxBufferWrite(location, 1, locationlen, out); + if(e->locations) { + CxIterator i = cxListIterator(e->locations); + cx_foreach(char *, location, i) { + uint32_t locationlen = strlen(location); + uint32_t netlocationlen = htonl(locationlen); + + cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); + cxBufferWrite(location, 1, locationlen, out); + } } uint32_t terminate = 0;