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)
}
/* Create crash directories */
- if (make_dump_dir() < 0) {
+ if (!make_dump_dir()) {
res = EXIT_FAILURE;
goto exit;
}