From 267fa5370abc48e7f7576bffcc23669c91e84d86 Mon Sep 17 00:00:00 2001 From: "sanghyeok.oh" Date: Fri, 24 Apr 2020 20:53:08 +0900 Subject: [PATCH] cleanup: convert local array into static variable Local arrays are not a good option for recursive funtion Change-Id: I94cd36fd873bd266a3effd978760f51a7c34498f Signed-off-by: sanghyeok.oh --- src/storage/cleanup.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/storage/cleanup.c b/src/storage/cleanup.c index 6e3b046..2d55593 100644 --- a/src/storage/cleanup.c +++ b/src/storage/cleanup.c @@ -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; } -- 2.7.4