dav/finfo.c

changeset 747
efbd59642577
parent 736
40be8db6fe45
child 789
378b5ab86f77
--- a/dav/finfo.c	Sun Apr 16 14:12:24 2023 +0200
+++ b/dav/finfo.c	Fri Apr 21 21:25:32 2023 +0200
@@ -34,45 +34,42 @@
 #include <unistd.h>
 #include <errno.h>
 #include <sys/stat.h>
+#include <limits.h>
 
-#include <ucx/string.h>
-#include <ucx/list.h>
+#include <cx/string.h>
+#include <cx/list.h>
+#include <cx/array_list.h>
 #include <libidav/crypto.h>
 #include <libidav/utils.h>
 
 #include "libxattr.h"
 
 uint32_t parse_finfo_settings(const char *str, char **error) {
-    scstr_t s = scstr(str);
+    cxstring s = cx_str(str);
+    
+    if(!cx_strcmp(s, CX_STR("*")) || !cx_strcmp(s, CX_STR("a")) || !cx_strcmp(s, CX_STR("all"))) {
+        return FINFO_MTIME|FINFO_OWNER|FINFO_MODE|FINFO_XATTR;
+    } 
     
-    if(!sstrcmp(s, SC("*")) || !sstrcmp(s, SC("a")) || !sstrcmp(s, SC("all"))) {
-        return FINFO_MTIME|FINFO_OWNER|FINFO_MODE|FINFO_XATTR;
+    CxStrtokCtx fs = cx_strtok(s, CX_STR(","), INT_MAX);
+    cxstring f;
+    uint32_t finfo = 0;
+    char *err = NULL;
+    while(cx_strtok_next(&fs, &f)) {
+        if(!cx_strcasecmp(f, CX_STR("mtime"))) {
+            finfo |= FINFO_MTIME;
+        } else if(!cx_strcasecmp(f, CX_STR("owner"))) {
+            finfo |= FINFO_OWNER;
+        } else if(!cx_strcasecmp(f, CX_STR("mode"))) {
+            finfo |= FINFO_MODE;
+        } else if(!cx_strcasecmp(f, CX_STR("xattr"))) {
+            finfo |= FINFO_XATTR;
+        } else if(error && !err) {
+            err = cx_strdup(f).ptr;
+            continue;
+        }
     }
     
-    ssize_t count = 0;
-    sstr_t *fs = sstrsplit(s, SC(","), &count);
-    
-    char *err = NULL;
-    
-    uint32_t finfo = 0;
-    for(int i=0;i<count;i++) {
-        sstr_t f = fs[i];
-        if(!sstrcasecmp(f, SC("mtime"))) {
-            finfo |= FINFO_MTIME;
-        } else if(!sstrcasecmp(f, SC("owner"))) {
-            finfo |= FINFO_OWNER;
-        } else if(!sstrcasecmp(f, SC("mode"))) {
-            finfo |= FINFO_MODE;
-        } else if(!sstrcasecmp(f, SC("xattr"))) {
-            finfo |= FINFO_XATTR;
-        } else if(error && !err) {
-            err = fs[i].ptr;
-            continue;
-        }
-        free(f.ptr);
-    }
-    
-    free(fs);
     return err ? 0 : finfo;
 }
 
@@ -129,10 +126,26 @@
     return 0;
 }
 
+
+static void* array_realloc(void *array,
+            size_t capacity,
+            size_t elem_size,
+            struct cx_array_reallocator_s *alloc)
+{
+    return realloc(array, capacity * elem_size);
+}
+
 XAttributes* xml_get_attributes(DavXmlNode *xml) {
-    UcxList *names = NULL;
-    UcxList *values = NULL;
+    XAttributes *attributes = calloc(1, sizeof(XAttributes));
+    size_t x_names_size = 0;
+    size_t x_names_alloc = 8;
+    size_t x_values_size = 0;
+    size_t x_values_alloc = 8;
+    attributes->names = calloc(x_names_alloc, sizeof(char*));
+    attributes->values = calloc(x_values_alloc, sizeof(cxmutstr));
     
+    struct cx_array_reallocator_s re = { .realloc = array_realloc };
+     
     size_t count = 0;
     
     char *hash = NULL;
@@ -145,7 +158,16 @@
             } else if(!strcmp(node->name, "xattr")) {
                 char *xattr_name = dav_xml_get_attr(node, "name");
                 if(xattr_name) {                 
-                    names = ucx_list_append(names, strdup(xattr_name));
+                    char *xname = strdup(xattr_name);
+                    cx_array_copy(
+                            (void**)&attributes->names,
+                            &x_names_size,
+                            &x_names_alloc,
+                            count,
+                            &xname,
+                            sizeof(void*),
+                            1,
+                            &re);
                     
                     char *text = dav_xml_getstring(node->children);
                     if(!text) {
@@ -155,11 +177,19 @@
                     int len = 0;
                     char *val = util_base64decode_len(text, &len);
                     
-                    sstr_t *value = malloc(sizeof(sstr_t));
-                    value->ptr = val;
-                    value->length = len;
+                    cxmutstr value;
+                    value.ptr = val;
+                    value.length = len;
                     
-                    values = ucx_list_append(values, value);
+                    cx_array_copy(
+                            (void**)&attributes->values,
+                            &x_values_size,
+                            &x_values_alloc,
+                            count,
+                            &value,
+                            sizeof(cxmutstr),
+                            1,
+                            &re);
                     
                     count++;
                 }
@@ -167,24 +197,16 @@
         }
     }
     
-    XAttributes *attributes = NULL;
-    if(count > 0) {
-        attributes = calloc(1, sizeof(XAttributes));
-        attributes->hash = hash ? strdup(hash) : NULL;
-        attributes->nattr = count;
-        attributes->names = calloc(count, sizeof(char*));
-        attributes->values = calloc(count, sizeof(sstr_t));
-        int i=0;
-        UCX_FOREACH(elm, names) {
-            attributes->names[i] = elm->data;
-            i++;
-        }
-        i=0;
-        UCX_FOREACH(elm, values) {
-            attributes->values[i] = *(sstr_t*)elm->data;
-            i++;
-        }
-    }
+    if(count == 0) {
+        free(attributes->names);
+        free(attributes->values);
+        free(attributes);
+        return NULL;
+    } 
+    
+    attributes->hash = hash ? strdup(hash) : NULL;
+    attributes->nattr = count;
+
     return attributes;
 }
 
@@ -202,7 +224,7 @@
     XAttributes *xattr = malloc(sizeof(XAttributes));
     xattr->nattr = 0;
     xattr->names = calloc(nelm, sizeof(char*));
-    xattr->values = calloc(nelm, sizeof(sstr_t));
+    xattr->values = calloc(nelm, sizeof(cxmutstr));
     
     DAV_SHA_CTX *sha256 = dav_hash_init();
     
@@ -223,7 +245,7 @@
             dav_hash_update(sha256, value, valuelen);
             // add name and value
             xattr->names[nattr] = attributes[i];
-            sstr_t v;
+            cxmutstr v;
             v.ptr = value;
             v.length = valuelen;
             xattr->values[nattr] = v;
@@ -264,7 +286,7 @@
         last->next = attr;
         last = attr;
         
-        sstr_t value = xattr->values[i];
+        cxmutstr value = xattr->values[i];
         if(value.length > 0) {
             char *encval = util_base64encode(value.ptr, value.length);
             attr->children = dav_xml_createtextnode(encval);

mercurial