void dpl_vec_common_prefixes_free(dpl_vec_t *vec) { int i; for (i = 0;i < vec->n_items;i++) dpl_common_prefix_free((dpl_common_prefix_t *) dpl_vec_get(vec, i)); dpl_vec_free(vec); }
static dpl_status_t parse_list_bucket_common_prefixes(xmlNode *node, dpl_vec_t *vec) { xmlNode *tmp; dpl_common_prefix_t *common_prefix = NULL; int ret; common_prefix = malloc(sizeof (*common_prefix)); if (NULL == common_prefix) goto bad; memset(common_prefix, 0, sizeof (*common_prefix)); for (tmp = node; NULL != tmp; tmp = tmp->next) { if (tmp->type == XML_ELEMENT_NODE) { DPRINTF("name: %s\n", tmp->name); if (!strcmp((char *) tmp->name, "Prefix")) { common_prefix->prefix = strdup((char *) tmp->children->content); if (NULL == common_prefix->prefix) goto bad; } } else if (tmp->type == XML_TEXT_NODE) { DPRINTF("content: %s\n", tmp->content); } } ret = dpl_vec_add(vec, common_prefix); if (DPL_SUCCESS != ret) goto bad; return DPL_SUCCESS; bad: if (NULL != common_prefix) dpl_common_prefix_free(common_prefix); return DPL_FAILURE; }
dpl_status_t dpl_cdmi_parse_list_bucket(dpl_ctx_t *ctx, const char *buf, int len, const char *prefix, dpl_vec_t *objects, dpl_vec_t *common_prefixes) { int ret, ret2; json_tokener *tok = NULL; json_object *obj = NULL; json_object *children = NULL; int n_children, i; dpl_common_prefix_t *common_prefix = NULL; dpl_object_t *object = NULL; // write(1, buf, len); tok = json_tokener_new(); if (NULL == tok) { ret = DPL_ENOMEM; goto end; } obj = json_tokener_parse_ex(tok, buf, len); if (NULL == obj) { ret = DPL_FAILURE; goto end; } children = json_object_object_get(obj, "children"); if (NULL == children) { ret = DPL_FAILURE; goto end; } if (json_type_array != json_object_get_type(children)) { ret = DPL_FAILURE; goto end; } n_children = json_object_array_length(children); for (i = -1;i < n_children;i++) { char name[1024]; int name_len; if (-1 == i) { // add the directory itself to the list snprintf(name, sizeof (name), "%s", NULL != prefix ? prefix : "/"); } else { json_object *child = json_object_array_get_idx(children, i); if (json_type_string != json_object_get_type(child)) { ret = DPL_FAILURE; goto end; } snprintf(name, sizeof (name), "%s%s", NULL != prefix ? prefix : "", json_object_get_string(child)); } name_len = strlen(name); if (name_len > 0 && name[name_len-1] == '/') { //this is a directory common_prefix = malloc(sizeof (*common_prefix)); if (NULL == common_prefix) { ret = DPL_ENOMEM; goto end; } memset(common_prefix, 0, sizeof (*common_prefix)); common_prefix->prefix = strdup(name); if (NULL == common_prefix->prefix) { ret = DPL_ENOMEM; goto end; } ret2 = dpl_vec_add(common_prefixes, common_prefix); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } common_prefix = NULL; } else { object = malloc(sizeof (*object)); if (NULL == object) { ret = DPL_ENOMEM; goto end; } memset(object, 0, sizeof (*object)); object->path = strdup(name); if (NULL == object->path) { ret = DPL_ENOMEM; goto end; } if (name_len > 0 && name[name_len-1] == '?') { //this is a symbolic link: remove final '?' object->path[name_len - 1] = '\0'; } ret2 = dpl_vec_add(objects, object); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } object = NULL; } } ret = DPL_SUCCESS; end: if (NULL != common_prefix) dpl_common_prefix_free(common_prefix); if (NULL != object) dpl_object_free(object); if (NULL != obj) json_object_put(obj); if (NULL != tok) json_tokener_free(tok); return ret; }