crash-manager: Rewrite make_dump_path() for readability 50/212450/5
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 21 Aug 2019 13:29:31 +0000 (15:29 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 23 Aug 2019 08:38:23 +0000 (10:38 +0200)
Change-Id: I523ca95903f16118e87ca724cc035b82c879d459

src/crash-manager/crash-manager.c

index b01253c..d746ecb 100644 (file)
@@ -223,35 +223,36 @@ static int prepare_paths(struct crash_info* cinfo)
        return 1;
 }
 
-static int make_dump_dir(void)
+static bool make_dump_dir(void)
 {
-       struct stat st;
+       const char *dirs[] = {crash_crash_path, crash_temp_path};
 
-       if (!stat(crash_crash_path, &st)) {
-               if (!(st.st_mode & S_IFDIR)) {
-                       _E("%s (not DIR) is already exist", crash_crash_path);
-                       return -1;
-               }
-       } else {
-               if (mkdir(crash_crash_path, 0775) < 0) {
-                       _E("Failed to mkdir for %s", crash_crash_path);
-                       return -1;
-               }
-       }
+       for (size_t i = 0; i < ARRAY_SIZE(dirs); i++) {
+               const char *dirname = dirs[i];
 
-       if (!stat(crash_temp_path, &st)) {
-               if (!(st.st_mode & S_IFDIR)) {
-                       _E("%s (not DIR) is already exist", crash_temp_path);
-                       return -1;
-               }
-       } else {
-               if (mkdir(crash_temp_path, 0775) < 0) {
-                       _E("Failed to mkdir for %s", crash_temp_path);
-                       return -1;
+               int r = mkdir(dirname, 0775);
+               if (r >= 0)
+                       continue;
+
+               if (errno != EEXIST) {
+                       _E("Unable to create directory %s: %m", dirname);
+                       return false;
                }
+
+               struct stat st = {0};
+               r = stat(dirname, &st);
+               bool isdir = !!(st.st_mode & S_IFDIR);
+
+               if (!r && isdir)
+                       continue;
+               else if (!r && !isdir)
+                       errno = ENOTDIR;
+
+               _E("Failure while trying to ensure %s exists and it is directory: %m", dirname);
+               return false;
        }
 
-       return 0;
+       return true;
 }
 
 static bool get_cmd_info(struct crash_info *cinfo)
@@ -1275,7 +1276,7 @@ int main(int argc, char *argv[])
        }
 
        /* Create crash directories */
-       if (make_dump_dir() < 0) {
+       if (!make_dump_dir()) {
                res = EXIT_FAILURE;
                goto exit;
        }