diff -r 6740adb5fccd -r 017a4f09e6fa dav/pwd.c --- a/dav/pwd.c Thu Sep 20 17:14:55 2018 +0200 +++ b/dav/pwd.c Sun Sep 23 08:13:50 2018 +0200 @@ -56,6 +56,8 @@ PwdStore *p = malloc(sizeof(PwdStore)); p->ids = ucx_map_new(16); p->locations = NULL; + p->noloc = NULL; + p->index = ucx_map_new(16); p->content = buf; p->key = NULL; p->encoffset = PWDS_HEADER_SIZE; @@ -72,6 +74,9 @@ PwdStore* pwdstore_new(void) { PwdStore *p = calloc(1, sizeof(PwdStore)); p->ids = ucx_map_new(16); + p->locations = NULL; + p->noloc = NULL; + p->index = ucx_map_new(16); p->content = ucx_buffer_new(NULL, PWDS_HEADER_SIZE, UCX_BUFFER_AUTOEXTEND); PWDS_MAGIC(p) = PWDS_MAGIC_CHAR; PWDS_VERSION(p) = 1; @@ -118,7 +123,21 @@ char *password = NULL; int res = 0; + int ret = 0; if((res += readval(in, &id, FALSE)) == 1) { + if(index) { + if((res += readval(in, &location, TRUE)) == 2) { + pwdstore_put_index(p, id, location); + ret = 1; + } + } else { + if((res += readval(in, &user, FALSE)) == 2) { + if((res += readval(in, &password, FALSE)) == 3) { + pwdstore_put(p, id, user, password); + ret = 1; + } + } + } if((res += readval(in, &location, TRUE)) == 2) { if(!index) { if((res += readval(in, &user, FALSE)) == 3) { @@ -128,12 +147,6 @@ } } - int ret = 0; - if((!index && res == 4) || (index && res == 2)) { - pwdstore_put(p, id, location, user, password); - ret = 1; - } - if(id) free(id); if(location) free(location); if(user) free(user); @@ -215,7 +228,6 @@ void pwdstore_free_entry(PwdEntry *e) { if(e->id) free(e->id); - if(e->location) free(e->location); if(e->user) free(e->user); if(e->password) free(e->password); free(e); @@ -235,11 +247,7 @@ } int pwdstore_has_id(PwdStore *s, const char *id) { - return ucx_map_cstr_get(s->ids, id) ? 1 : 0; -} - -int pwdstore_has_location(PwdStore *s, const char *location) { - return 0; + return ucx_map_cstr_get(s->index, id) ? 1 : 0; } PwdEntry* pwdstore_get(PwdStore *p, const char *id) { @@ -251,16 +259,44 @@ } } -void pwdstore_put(PwdStore *p, const char *id, const char *location, const char *username, const char *password) { +void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) { PwdEntry *entry = malloc(sizeof(PwdEntry)); entry->id = strdup(id); - entry->location = location ? strdup(location) : NULL; - entry->user = username ? strdup(username) : NULL; - entry->password = password ? strdup(password) : NULL; + entry->user = strdup(username); + entry->password = strdup(password); ucx_map_cstr_put(p->ids, id, entry); - +} + +void pwdstore_put_index(PwdStore *p, const char *id, const char *location) { + PwdIndexEntry *e = ucx_map_cstr_get(p->index, id); + if(e) { + return; + } + PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); + newentry->id = strdup(id); if(location) { - p->locations = ucx_list_append(p->locations, entry); + newentry->location = strdup(location); + p->locations = ucx_list_append(p->locations, newentry); + } else { + newentry->location = NULL; + p->noloc = ucx_list_append(p->noloc, newentry); + } + ucx_map_cstr_put(p->index, id, newentry); +} + +void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) { + uint32_t idlen = strlen(e->id); + uint32_t locationlen = e->location ? strlen(e->location) : 0; + uint32_t netidlen = htonl(idlen); + uint32_t netlocationlen = htonl(locationlen); + + ucx_buffer_putc(out, 0); // type + + ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out); + ucx_buffer_write(e->id, 1, idlen, out); + ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out); + if(e->location) { + ucx_buffer_write(e->location, 1, locationlen, out); } } @@ -272,6 +308,16 @@ UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); + // create index + UCX_FOREACH(elm, p->noloc) { + PwdIndexEntry *e = elm->data; + write_index_entry(index, e); + } + UCX_FOREACH(elm, p->locations) { + PwdIndexEntry *e = elm->data; + write_index_entry(index, e); + } + UcxMapIterator i = ucx_map_iterator(p->ids); PwdEntry *value; UCX_MAP_FOREACH(key, value, i) { @@ -280,33 +326,17 @@ } uint32_t idlen = strlen(value->id); - uint32_t locationlen = value->location ? strlen(value->location) : 0; uint32_t ulen = strlen(value->user); uint32_t plen = strlen(value->password); uint32_t netidlen = htonl(idlen); - uint32_t netlocationlen = htonl(locationlen); uint32_t netulen = htonl(ulen); uint32_t netplen = htonl(plen); - // index buffer - ucx_buffer_putc(index, 0); // type - - ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), index); - ucx_buffer_write(value->id, 1, idlen, index); - ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), index); - if(value->location) { - ucx_buffer_write(value->location, 1, locationlen, index); - } - // content buffer ucx_buffer_putc(content, 0); // type ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content); ucx_buffer_write(value->id, 1, idlen, content); - ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), content); - if(value->location) { - ucx_buffer_write(value->location, 1, locationlen, content); - } ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content); ucx_buffer_write(value->user, 1, ulen, content); ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content);