[logcleanup] Fix svace/coverity issue 06/218606/4 accepted/tizen/unified/20191128.162015 submit/tizen/20191126.094154
authorYunmi Ha <yunmi.ha@samsung.com>
Tue, 26 Nov 2019 02:39:13 +0000 (11:39 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Tue, 26 Nov 2019 09:06:22 +0000 (18:06 +0900)
- check return value

Change-Id: Ie861d6972c3bfa2c8346f8a25e72e0039e344e6e
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
src/logcleanup/cleanup.c

index 1cae8d3..48bc594 100644 (file)
 
 static int cleanup_recursive(const char *path)
 {
-       int ret;
+       int ret = 0;
        struct stat f;
        DIR *dir = NULL;
        struct dirent *dent;
        char sub_path[PATH_MAX] = {0,};
 
-       ret = lstat(path, &f);
-       if (ret)
+       if (access(path, W_OK) < 0)
                return -EINVAL;
 
-       if ((f.st_mode & S_IFMT) == S_IFDIR) {
-               if (access(path, W_OK) != 0)
-                       return -EINVAL;
+       if (lstat(path, &f) < 0)
+               return -EINVAL;
 
+       if ((f.st_mode & S_IFMT) == S_IFDIR) {
                dir = opendir(path);
                if (!dir)
                        return -EINVAL;
@@ -60,7 +59,6 @@ static int cleanup_recursive(const char *path)
                                break;
                }
                closedir(dir);
-               return ret;
        } else {
                regex_t regex;
 
@@ -68,12 +66,14 @@ static int cleanup_recursive(const char *path)
                if (ret)
                        return -EINVAL;
 
-               if (!regexec(&regex, path, 0, NULL, 0))
-                       remove(path);
+               if (!regexec(&regex, path, 0, NULL, 0)) {
+                       if (remove(path) < 0)
+                               ret = -errno;
+               }
 
                regfree(&regex);
        }
-       return 0;
+       return ret;
 }
 
 int main(int argc, char *argv[])