{"action":"created","comment":{"url":"https://api.github.com/repos/sysrepo/sysrepo/pulls/comments/48718192","id":48718192,"diff_hunk":"@@ -0,0 +1,476 @@\n+/**\n+ * @file rp_data_tree.c\n+ * @author Rastislav Szabo <raszabo@cisco.com>, Lukas Macko <lmacko@cisco.com>\n+ * @brief \n+ *\n+ * @copyright\n+ * Copyright 2015 Cisco Systems, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+#include \"rp_data_tree.h\"\n+#include \"xpath_processor.h\"\n+#include \"sr_common.h\"\n+\n+\n+/**\n+ * @brief Creates part of xpath for leaf and container nodes. Arguments specify if the namespace and trailing slash\n+ * should be included.\n+ */\n+static int\n+rp_dt_create_xpath_for_cont_leaf_node(const struct lyd_node *data_tree, char **xpath, bool namespace, bool trailing_slash)\n+{\n+    CHECK_NULL_ARG2(data_tree, xpath);\n+    char *s = NULL;\n+    size_t len = 1; /* terminating null byte*/\n+    size_t ns_len = 0;\n+    size_t node_len = 0;\n+\n+    /* calculate length */\n+    if (namespace){\n+        ns_len = strlen(data_tree->schema->module->name) + 1; /*namespace + colon*/\n+        len += ns_len;\n+    }\n+    node_len = strlen(data_tree->schema->name);\n+    len += node_len;\n+    if (trailing_slash){\n+        len++;\n+    }\n+    s = calloc(sizeof(char),len);\n+    if (NULL == s){\n+        SR_LOG_ERR_MSG(\"Memory allocation failed\");\n+        return SR_ERR_NOMEM;\n+    }\n+\n+    /* copy string */\n+    if(namespace){\n+        strcpy(s, data_tree->schema->module->name);\n+        s[ns_len-1] = ':';\n+    }\n+    strcpy(s+ns_len, data_tree->schema->name);\n+\n+    if(trailing_slash){\n+        s[ns_len+node_len] = '/';\n+    }\n+\n+    *xpath = s;\n+    return SR_ERR_OK;\n+}\n+\n+/**\n+ * @brief Creates part of xpath for list nodes. Arguments specify if the namespace and trailing slash\n+ * should be included.\n+ */\n+static int\n+rp_dt_create_xpath_for_list_node(const struct lyd_node *data_tree, char **xpath, bool namespace, bool trailing_slash)\n+{\n+    CHECK_NULL_ARG2(data_tree, xpath);\n+    char *s = NULL;\n+    size_t len = 1; /* terminating null byte*/\n+    size_t ns_len = 0;\n+    size_t offset = 0;\n+\n+    /* calculate length*/\n+    if (namespace) {\n+        ns_len = strlen(data_tree->schema->module->name);\n+        len += ns_len + 1; /*namespace + colon*/\n+    }\n+    len += strlen(data_tree->schema->name);\n+    if (trailing_slash) {\n+        len++;\n+    }\n+    /* lookup keys */\n+    struct lys_node_list *sch_list = (struct lys_node_list *) data_tree->schema;\n+\n+    struct lyd_node_leaf_list **key_nodes = calloc(sch_list->keys_size, sizeof(struct lyd_node_leaf_list *));\n+    if (key_nodes == NULL){\n+        SR_LOG_ERR_MSG(\"Memory allocation failed\");\n+        return SR_ERR_NOMEM;\n+    }\n+    struct lyd_node *c = data_tree->child;\n+    size_t matched = 0;\n+\n+    while (c != NULL){\n+        for (int k=0; k < sch_list->keys_size; k++){\n+            if(0 == strcmp(sch_list->keys[k]->name, c->schema->name)){\n+                key_nodes[matched] = (struct lyd_node_leaf_list *) c;\n+                len += strlen(key_nodes[matched]->value_str); /*key value*/\n+                len += strlen(c->schema->name); /*key name*/\n+                len += 5; /*delimiting characters [='']*/\n+                matched++;\n+                break;\n+            }\n+        }\n+        c = c->next;\n+    }\n+    if (matched != sch_list->keys_size){\n+        SR_LOG_ERR_MSG(\"Keys not found\");\n+        free(key_nodes);\n+        return SR_ERR_INTERNAL;\n+    }\n+\n+    s = calloc(sizeof(char), len);\n+    if (NULL == s) {\n+        SR_LOG_ERR_MSG(\"Memory allocation failed\");\n+        return SR_ERR_NOMEM;\n+    }\n+\n+    /* copy string */\n+    if (namespace) {\n+        strcpy(s, data_tree->schema->module->name);\n+        s[ns_len] = ':';\n+        offset += ns_len+1;\n+    }\n+    strcpy(s+offset, data_tree->schema->name);\n+    offset += strlen(data_tree->schema->name);\n+    for (int k=0; k < sch_list->keys_size; k++){\n+        s[offset++]='[';\n+        strcpy(s+offset, key_nodes[k]->schema->name);\n+        offset += strlen(key_nodes[k]->schema->name);\n+        s[offset++]='=';\n+        s[offset++]='\\'';\n+        strcpy(s+offset, key_nodes[k]->value_str);\n+        offset += strlen(key_nodes[k]->value_str);\n+        s[offset++]='\\'';\n+        s[offset++]=']';\n+    }\n+\n+    if (trailing_slash) {\n+        s[offset] = '/';\n+    }\n+\n+    free(key_nodes);\n+    *xpath = s;\n+    return SR_ERR_OK;\n+}\n+\n+/**\n+ * @brief Creates xpath for the selected node.\n+ */\n+static int\n+rp_dt_create_xpath_for_node(const struct lyd_node *data_tree, char **xpath)\n+{\n+    CHECK_NULL_ARG2(data_tree, xpath);\n+    int rc = 0;\n+    char **parts = NULL;\n+    char *result = NULL;\n+    size_t offset = 0;\n+    size_t length = 0;\n+    size_t level = 0;\n+\n+    /*find node depth*/\n+    const struct lyd_node *n = data_tree;\n+    while (NULL != n){\n+        n = n->parent;\n+        level++;\n+    }\n+    parts = calloc(level, sizeof(char *));\n+    if (NULL == parts){\n+        SR_LOG_ERR_MSG(\"Memory allocation failed.\");\n+        return SR_ERR_NOMEM;\n+    }\n+\n+    size_t i = level-1;\n+    n = data_tree;\n+    /*create parts of xpath */\n+    while (NULL != n){\n+        /*append slash to all nodes except the last one*/\n+        bool slash = i!=(level-1);\n+        /*print namespace only for the root node*/\n+        bool namespace = i==0;\n+        if (n->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)){\n+            rc = rp_dt_create_xpath_for_cont_leaf_node(n, &parts[i], namespace, slash);\n+            if (SR_ERR_OK != rc){\n+               SR_LOG_ERR_MSG(\"Creating xpath failed.\");\n+               return rc;","path":"src/rp_data_tree.c","position":196,"original_position":196,"commit_id":"ab48f3fb5dcac70a88b297cd169271f7073a50a9","original_commit_id":"ab48f3fb5dcac70a88b297cd169271f7073a50a9","user":{"login":"rastislavszabo","id":15926980,"avatar_url":"https://avatars.githubusercontent.com/u/15926980?v=3","gravatar_id":"","url":"https://api.github.com/users/rastislavszabo","html_url":"https://github.com/rastislavszabo","followers_url":"https://api.github.com/users/rastislavszabo/followers","following_url":"https://api.github.com/users/rastislavszabo/following{/other_user}","gists_url":"https://api.github.com/users/rastislavszabo/gists{/gist_id}","starred_url":"https://api.github.com/users/rastislavszabo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rastislavszabo/subscriptions","organizations_url":"https://api.github.com/users/rastislavszabo/orgs","repos_url":"https://api.github.com/users/rastislavszabo/repos","events_url":"https://api.github.com/users/rastislavszabo/events{/privacy}","received_events_url":"https://api.github.com/users/rastislavszabo/received_events","type":"User","site_admin":false},"body":"memleak - parts is not freed","created_at":"2016-01-04T10:23:18Z","updated_at":"2016-01-04T10:23:18Z","html_url":"https://github.com/sysrepo/sysrepo/pull/10#discussion_r48718192","pull_request_url":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10","_links":{"self":{"href":"https://api.github.com/repos/sysrepo/sysrepo/pulls/comments/48718192"},"html":{"href":"https://github.com/sysrepo/sysrepo/pull/10#discussion_r48718192"},"pull_request":{"href":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10"}}},"pull_request":{"url":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10","id":54957473,"html_url":"https://github.com/sysrepo/sysrepo/pull/10","diff_url":"https://github.com/sysrepo/sysrepo/pull/10.diff","patch_url":"https://github.com/sysrepo/sysrepo/pull/10.patch","issue_url":"https://api.github.com/repos/sysrepo/sysrepo/issues/10","number":10,"state":"open","locked":false,"title":"rp_data_tree","user":{"login":"lukasmacko","id":16013493,"avatar_url":"https://avatars.githubusercontent.com/u/16013493?v=3","gravatar_id":"","url":"https://api.github.com/users/lukasmacko","html_url":"https://github.com/lukasmacko","followers_url":"https://api.github.com/users/lukasmacko/followers","following_url":"https://api.github.com/users/lukasmacko/following{/other_user}","gists_url":"https://api.github.com/users/lukasmacko/gists{/gist_id}","starred_url":"https://api.github.com/users/lukasmacko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukasmacko/subscriptions","organizations_url":"https://api.github.com/users/lukasmacko/orgs","repos_url":"https://api.github.com/users/lukasmacko/repos","events_url":"https://api.github.com/users/lukasmacko/events{/privacy}","received_events_url":"https://api.github.com/users/lukasmacko/received_events","type":"User","site_admin":false},"body":"Helper functions for request processor that handle lookup in datatree using xpath or location id, retrieving value of a node and creating xpath for a node + test. sysrepo.h update","created_at":"2016-01-04T09:44:47Z","updated_at":"2016-01-04T10:23:18Z","closed_at":null,"merged_at":null,"merge_commit_sha":"922447fbdad823ebcb8711a24835a89ad56bf3d2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10/commits","review_comments_url":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10/comments","review_comment_url":"https://api.github.com/repos/sysrepo/sysrepo/pulls/comments{/number}","comments_url":"https://api.github.com/repos/sysrepo/sysrepo/issues/10/comments","statuses_url":"https://api.github.com/repos/sysrepo/sysrepo/statuses/ab48f3fb5dcac70a88b297cd169271f7073a50a9","head":{"label":"lukasmacko:rp_data_tree","ref":"rp_data_tree","sha":"ab48f3fb5dcac70a88b297cd169271f7073a50a9","user":{"login":"lukasmacko","id":16013493,"avatar_url":"https://avatars.githubusercontent.com/u/16013493?v=3","gravatar_id":"","url":"https://api.github.com/users/lukasmacko","html_url":"https://github.com/lukasmacko","followers_url":"https://api.github.com/users/lukasmacko/followers","following_url":"https://api.github.com/users/lukasmacko/following{/other_user}","gists_url":"https://api.github.com/users/lukasmacko/gists{/gist_id}","starred_url":"https://api.github.com/users/lukasmacko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukasmacko/subscriptions","organizations_url":"https://api.github.com/users/lukasmacko/orgs","repos_url":"https://api.github.com/users/lukasmacko/repos","events_url":"https://api.github.com/users/lukasmacko/events{/privacy}","received_events_url":"https://api.github.com/users/lukasmacko/received_events","type":"User","site_admin":false},"repo":{"id":47984351,"name":"sysrepo","full_name":"lukasmacko/sysrepo","owner":{"login":"lukasmacko","id":16013493,"avatar_url":"https://avatars.githubusercontent.com/u/16013493?v=3","gravatar_id":"","url":"https://api.github.com/users/lukasmacko","html_url":"https://github.com/lukasmacko","followers_url":"https://api.github.com/users/lukasmacko/followers","following_url":"https://api.github.com/users/lukasmacko/following{/other_user}","gists_url":"https://api.github.com/users/lukasmacko/gists{/gist_id}","starred_url":"https://api.github.com/users/lukasmacko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukasmacko/subscriptions","organizations_url":"https://api.github.com/users/lukasmacko/orgs","repos_url":"https://api.github.com/users/lukasmacko/repos","events_url":"https://api.github.com/users/lukasmacko/events{/privacy}","received_events_url":"https://api.github.com/users/lukasmacko/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lukasmacko/sysrepo","description":"","fork":true,"url":"https://api.github.com/repos/lukasmacko/sysrepo","forks_url":"https://api.github.com/repos/lukasmacko/sysrepo/forks","keys_url":"https://api.github.com/repos/lukasmacko/sysrepo/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lukasmacko/sysrepo/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lukasmacko/sysrepo/teams","hooks_url":"https://api.github.com/repos/lukasmacko/sysrepo/hooks","issue_events_url":"https://api.github.com/repos/lukasmacko/sysrepo/issues/events{/number}","events_url":"https://api.github.com/repos/lukasmacko/sysrepo/events","assignees_url":"https://api.github.com/repos/lukasmacko/sysrepo/assignees{/user}","branches_url":"https://api.github.com/repos/lukasmacko/sysrepo/branches{/branch}","tags_url":"https://api.github.com/repos/lukasmacko/sysrepo/tags","blobs_url":"https://api.github.com/repos/lukasmacko/sysrepo/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lukasmacko/sysrepo/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lukasmacko/sysrepo/git/refs{/sha}","trees_url":"https://api.github.com/repos/lukasmacko/sysrepo/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lukasmacko/sysrepo/statuses/{sha}","languages_url":"https://api.github.com/repos/lukasmacko/sysrepo/languages","stargazers_url":"https://api.github.com/repos/lukasmacko/sysrepo/stargazers","contributors_url":"https://api.github.com/repos/lukasmacko/sysrepo/contributors","subscribers_url":"https://api.github.com/repos/lukasmacko/sysrepo/subscribers","subscription_url":"https://api.github.com/repos/lukasmacko/sysrepo/subscription","commits_url":"https://api.github.com/repos/lukasmacko/sysrepo/commits{/sha}","git_commits_url":"https://api.github.com/repos/lukasmacko/sysrepo/git/commits{/sha}","comments_url":"https://api.github.com/repos/lukasmacko/sysrepo/comments{/number}","issue_comment_url":"https://api.github.com/repos/lukasmacko/sysrepo/issues/comments{/number}","contents_url":"https://api.github.com/repos/lukasmacko/sysrepo/contents/{+path}","compare_url":"https://api.github.com/repos/lukasmacko/sysrepo/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lukasmacko/sysrepo/merges","archive_url":"https://api.github.com/repos/lukasmacko/sysrepo/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lukasmacko/sysrepo/downloads","issues_url":"https://api.github.com/repos/lukasmacko/sysrepo/issues{/number}","pulls_url":"https://api.github.com/repos/lukasmacko/sysrepo/pulls{/number}","milestones_url":"https://api.github.com/repos/lukasmacko/sysrepo/milestones{/number}","notifications_url":"https://api.github.com/repos/lukasmacko/sysrepo/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lukasmacko/sysrepo/labels{/name}","releases_url":"https://api.github.com/repos/lukasmacko/sysrepo/releases{/id}","created_at":"2015-12-14T15:41:47Z","updated_at":"2015-12-14T15:41:48Z","pushed_at":"2016-01-04T09:36:27Z","git_url":"git://github.com/lukasmacko/sysrepo.git","ssh_url":"git@github.com:lukasmacko/sysrepo.git","clone_url":"https://github.com/lukasmacko/sysrepo.git","svn_url":"https://github.com/lukasmacko/sysrepo","homepage":null,"size":158,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"sysrepo:master","ref":"master","sha":"7008f98b5033875e585e20ed431377988dcd9979","user":{"login":"sysrepo","id":10215741,"avatar_url":"https://avatars.githubusercontent.com/u/10215741?v=3","gravatar_id":"","url":"https://api.github.com/users/sysrepo","html_url":"https://github.com/sysrepo","followers_url":"https://api.github.com/users/sysrepo/followers","following_url":"https://api.github.com/users/sysrepo/following{/other_user}","gists_url":"https://api.github.com/users/sysrepo/gists{/gist_id}","starred_url":"https://api.github.com/users/sysrepo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sysrepo/subscriptions","organizations_url":"https://api.github.com/users/sysrepo/orgs","repos_url":"https://api.github.com/users/sysrepo/repos","events_url":"https://api.github.com/users/sysrepo/events{/privacy}","received_events_url":"https://api.github.com/users/sysrepo/received_events","type":"Organization","site_admin":false},"repo":{"id":47689902,"name":"sysrepo","full_name":"sysrepo/sysrepo","owner":{"login":"sysrepo","id":10215741,"avatar_url":"https://avatars.githubusercontent.com/u/10215741?v=3","gravatar_id":"","url":"https://api.github.com/users/sysrepo","html_url":"https://github.com/sysrepo","followers_url":"https://api.github.com/users/sysrepo/followers","following_url":"https://api.github.com/users/sysrepo/following{/other_user}","gists_url":"https://api.github.com/users/sysrepo/gists{/gist_id}","starred_url":"https://api.github.com/users/sysrepo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sysrepo/subscriptions","organizations_url":"https://api.github.com/users/sysrepo/orgs","repos_url":"https://api.github.com/users/sysrepo/repos","events_url":"https://api.github.com/users/sysrepo/events{/privacy}","received_events_url":"https://api.github.com/users/sysrepo/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/sysrepo/sysrepo","description":"YANG-based datastore for Unix/Linux applications","fork":false,"url":"https://api.github.com/repos/sysrepo/sysrepo","forks_url":"https://api.github.com/repos/sysrepo/sysrepo/forks","keys_url":"https://api.github.com/repos/sysrepo/sysrepo/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sysrepo/sysrepo/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sysrepo/sysrepo/teams","hooks_url":"https://api.github.com/repos/sysrepo/sysrepo/hooks","issue_events_url":"https://api.github.com/repos/sysrepo/sysrepo/issues/events{/number}","events_url":"https://api.github.com/repos/sysrepo/sysrepo/events","assignees_url":"https://api.github.com/repos/sysrepo/sysrepo/assignees{/user}","branches_url":"https://api.github.com/repos/sysrepo/sysrepo/branches{/branch}","tags_url":"https://api.github.com/repos/sysrepo/sysrepo/tags","blobs_url":"https://api.github.com/repos/sysrepo/sysrepo/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sysrepo/sysrepo/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sysrepo/sysrepo/git/refs{/sha}","trees_url":"https://api.github.com/repos/sysrepo/sysrepo/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sysrepo/sysrepo/statuses/{sha}","languages_url":"https://api.github.com/repos/sysrepo/sysrepo/languages","stargazers_url":"https://api.github.com/repos/sysrepo/sysrepo/stargazers","contributors_url":"https://api.github.com/repos/sysrepo/sysrepo/contributors","subscribers_url":"https://api.github.com/repos/sysrepo/sysrepo/subscribers","subscription_url":"https://api.github.com/repos/sysrepo/sysrepo/subscription","commits_url":"https://api.github.com/repos/sysrepo/sysrepo/commits{/sha}","git_commits_url":"https://api.github.com/repos/sysrepo/sysrepo/git/commits{/sha}","comments_url":"https://api.github.com/repos/sysrepo/sysrepo/comments{/number}","issue_comment_url":"https://api.github.com/repos/sysrepo/sysrepo/issues/comments{/number}","contents_url":"https://api.github.com/repos/sysrepo/sysrepo/contents/{+path}","compare_url":"https://api.github.com/repos/sysrepo/sysrepo/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sysrepo/sysrepo/merges","archive_url":"https://api.github.com/repos/sysrepo/sysrepo/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sysrepo/sysrepo/downloads","issues_url":"https://api.github.com/repos/sysrepo/sysrepo/issues{/number}","pulls_url":"https://api.github.com/repos/sysrepo/sysrepo/pulls{/number}","milestones_url":"https://api.github.com/repos/sysrepo/sysrepo/milestones{/number}","notifications_url":"https://api.github.com/repos/sysrepo/sysrepo/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sysrepo/sysrepo/labels{/name}","releases_url":"https://api.github.com/repos/sysrepo/sysrepo/releases{/id}","created_at":"2015-12-09T12:27:03Z","updated_at":"2016-01-04T09:47:46Z","pushed_at":"2016-01-04T09:44:52Z","git_url":"git://github.com/sysrepo/sysrepo.git","ssh_url":"git@github.com:sysrepo/sysrepo.git","clone_url":"https://github.com/sysrepo/sysrepo.git","svn_url":"https://github.com/sysrepo/sysrepo","homepage":"http://sysrepo.org","size":166,"stargazers_count":1,"watchers_count":1,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10"},"html":{"href":"https://github.com/sysrepo/sysrepo/pull/10"},"issue":{"href":"https://api.github.com/repos/sysrepo/sysrepo/issues/10"},"comments":{"href":"https://api.github.com/repos/sysrepo/sysrepo/issues/10/comments"},"review_comments":{"href":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10/comments"},"review_comment":{"href":"https://api.github.com/repos/sysrepo/sysrepo/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/sysrepo/sysrepo/pulls/10/commits"},"statuses":{"href":"https://api.github.com/repos/sysrepo/sysrepo/statuses/ab48f3fb5dcac70a88b297cd169271f7073a50a9"}}}}