cleanup: convert local array into static variable 54/231854/4
authorsanghyeok.oh <sanghyeok.oh@samsung.com>
Fri, 24 Apr 2020 11:53:08 +0000 (20:53 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Tue, 19 May 2020 05:25:28 +0000 (05:25 +0000)
Local arrays are not a good option for recursive funtion

Change-Id: I94cd36fd873bd266a3effd978760f51a7c34498f
Signed-off-by: sanghyeok.oh <sanghyeok.oh@samsung.com>
src/storage/cleanup.c

index 6e3b046..2d55593 100644 (file)
@@ -178,8 +178,10 @@ static int cleanup_recursive(const char *path, GList *except, int target)
        DIR *dir = NULL;
        struct dirent *dent;
        struct stat fstat;
-       char sub_path[PATH_MAX] = {0,};
+       static char sub_path[PATH_MAX] = {0,};
+       static int depth = 0;
        int ret;
+       char *ptr;
 
        if (except && g_list_find_custom(except, path, (GCompareFunc)find_except_item))
                return 0;
@@ -203,7 +205,10 @@ static int cleanup_recursive(const char *path, GList *except, int target)
                        return -EINVAL;
                }
 
-               while((dent = readdir(dir))) {
+               if (!depth)
+                       strncpy(sub_path, path, sizeof(sub_path) - 1);
+
+               while ((dent = readdir(dir))) {
                        if(strcmp(dent->d_name, ".") == 0 ||
                                strcmp(dent->d_name, "..") == 0)
                                continue;
@@ -218,8 +223,18 @@ static int cleanup_recursive(const char *path, GList *except, int target)
                                continue;
                        }
 
-                       snprintf(sub_path, PATH_MAX, "%s/%s", path, dent->d_name);
+                       /* attach sub path */
+                       snprintf(sub_path + strlen(sub_path), PATH_MAX - strlen(sub_path), "/%s", dent->d_name);
+
+                       ++depth;
                        ret = cleanup_recursive(sub_path, except, (target == CLEANUP_TARGET_OLDFILE)? target: CLEANUP_TARGET_ALL);
+                       --depth;
+
+                       /* detach sub path */
+                       ptr = strrchr(sub_path, '/');
+                       if (ptr)
+                               *ptr = 0;
+
                        if (ret != 0)
                                break;
                }