mesh: Modify check of the node directory name upon removal
authorInga Stotland <inga.stotland@intel.com>
Mon, 24 Jun 2019 17:41:42 +0000 (10:41 -0700)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 15:12:18 +0000 (20:42 +0530)
This removes check for "mesh" as the parent directory name and, instead,
verifies that the node configuration directory name is the hexadecimal
string representating the node's UUID.

Change-Id: Id7ec5d7cb4c609fea7ea79232da8c78cc18f09af
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/storage.c

index 3c5fb30..754e0a8 100644 (file)
@@ -52,20 +52,6 @@ static const char *bak_ext = ".bak";
 static const char *tmp_ext = ".tmp";
 static const char *storage_dir;
 
-/* This is a thread-safe always malloced version of dirname which will work
- * regardless of which underlying dirname() implementation is used.
- */
-static char *alloc_dirname(const char *path)
-{
-       char *tmp = l_strdup(path);
-       char *dir;
-
-       dir = dirname(tmp);
-       strncpy(tmp, dir, strlen(path) + 1);
-
-       return tmp;
-}
-
 static bool read_node_cb(struct mesh_db_node *db_node, void *user_data)
 {
        struct mesh_node *node = user_data;
@@ -485,20 +471,20 @@ void storage_save_config(struct mesh_node *node, bool no_wait,
                l_idle_oneshot(idle_save_config, info, NULL);
 }
 
-static int create_dir(const char *dirname)
+static int create_dir(const char *dir_name)
 {
        struct stat st;
        char dir[PATH_MAX + 1], *prev, *next;
        int err;
 
-       err = stat(dirname, &st);
+       err = stat(dir_name, &st);
        if (!err && S_ISREG(st.st_mode))
                return 0;
 
        memset(dir, 0, PATH_MAX + 1);
        strcat(dir, "/");
 
-       prev = strchr(dirname, '/');
+       prev = strchr(dir_name, '/');
 
        while (prev) {
                next = strchr(prev + 1, '/');
@@ -516,7 +502,7 @@ static int create_dir(const char *dirname)
                prev = next;
        }
 
-       mkdir(dirname, 0755);
+       mkdir(dir_name, 0755);
 
        return 0;
 }
@@ -639,7 +625,8 @@ static int del_fobject(const char *fpath, const struct stat *sb, int typeflag,
 /* Permanently remove node configuration */
 void storage_remove_node_config(struct mesh_node *node)
 {
-       char *node_path, *mesh_path, *mesh_name;
+       char *node_path, *node_name;
+       char uuid[33];
        struct json_object *jnode;
 
        if (!node)
@@ -649,19 +636,20 @@ void storage_remove_node_config(struct mesh_node *node)
        jnode = node_jconfig_get(node);
        if (jnode)
                json_object_put(jnode);
+
        node_jconfig_set(node, NULL);
 
        node_path = node_path_get(node);
        l_debug("Delete node config %s", node_path);
 
        /* Make sure path name of node follows expected guidelines */
-       mesh_path = alloc_dirname(node_path);
-       mesh_name = basename(mesh_path);
-       if (strcmp(mesh_name, "mesh"))
-               goto done;
+       if (!hex2str(node_uuid_get(node), 16, uuid, sizeof(uuid)))
+               return;
 
-       nftw(node_path, del_fobject, 5, FTW_DEPTH | FTW_PHYS);
+       node_name = basename(node_path);
 
-done:
-       l_free(mesh_path);
+       if (strcmp(node_name, uuid))
+               return;
+
+       nftw(node_path, del_fobject, 5, FTW_DEPTH | FTW_PHYS);
 }