Allow to use only filename in the except list 72/233772/1 accepted/tizen/5.5/unified/20200519.155055 submit/tizen_5.5/20200519.052556
authorYunmi Ha <yunmi.ha@samsung.com>
Mon, 18 May 2020 08:42:15 +0000 (17:42 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Tue, 19 May 2020 05:11:57 +0000 (05:11 +0000)
- When use filepath in the except list,
it will compare with full path of item.

- When use filename only in the except list,
it will compare without path of item.

Change-Id: If66104561e641ee8cfacb162147fe5786c513d15
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
(cherry picked from commit a728bf91087b8b213bc6114923d8f40e8d5c575a)

src/storage/cleanup.c

index 712ac86..6e3b046 100644 (file)
@@ -96,24 +96,54 @@ static void cleanup_cancel()
        pthread_mutex_unlock(&mutex_cancel);
 }
 
-static int find_sub_except_item(const char *item, const char *path)
+static int find_except_item(const char *item, const char *path)
 {
-       if (strstr(item, path)) {
-               _D("Find except item: %s, path: %s", item, path);
-               return 0;
-       }
+       char *sep;
+       int ret;
+
+       if (!item || !path)
+               return -1;
 
-       return -1;
+       _D("item: %s, path:%s", item, path);
+
+       sep = strchr(item, '/');
+       if (sep)
+               ret = strcmp(item, path);
+       else {
+               if (strstr(path, item))
+                       ret = 0;
+               else
+                       ret = -1;
+       }
+       _D("result: %d", ret);
+       return ret;
 }
 
-static int remove_dir(const char *path, GList *except)
+static int remove_dir(const char *path)
 {
+       DIR *dir;
+       struct dirent *dent;
+       bool bEmpty = true;
+
        if (!path)
                return -EINVAL;
 
-       if (!except)
-               REMOVE(path)
-       else if (except && !g_list_find_custom(except, path, (GCompareFunc)find_sub_except_item))
+       dir = opendir(path);
+       if (!dir) {
+               _E("Failed to open dir(%s).", path);
+               return -EINVAL;
+       }
+
+       while((dent = readdir(dir))) {
+               if(strcmp(dent->d_name, ".") == 0 ||
+                       strcmp(dent->d_name, "..") == 0)
+                       continue;
+
+               bEmpty = false;
+               _D("path (%s) is not empty.", path);
+       }
+
+       if (bEmpty)
                REMOVE(path);
 
        return 0;
@@ -151,7 +181,7 @@ static int cleanup_recursive(const char *path, GList *except, int target)
        char sub_path[PATH_MAX] = {0,};
        int ret;
 
-       if (except && g_list_find_custom(except, path, (GCompareFunc)strcmp))
+       if (except && g_list_find_custom(except, path, (GCompareFunc)find_except_item))
                return 0;
 
        if (is_cleanup_canceled())
@@ -195,7 +225,7 @@ static int cleanup_recursive(const char *path, GList *except, int target)
                }
                closedir(dir);
                if (!ret && (target == CLEANUP_TARGET_ALL))
-                       ret = remove_dir(path, except);
+                       ret = remove_dir(path);
        } else if (target == CLEANUP_TARGET_OLDFILE)
                remove_oldfile(path);
        else