From 05912c4a395168eedf1c46975e277220f1b59978 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Wed, 21 Aug 2019 15:29:31 +0200 Subject: [PATCH] crash-manager: Rewrite make_dump_path() for readability Change-Id: I523ca95903f16118e87ca724cc035b82c879d459 --- src/crash-manager/crash-manager.c | 49 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c index b01253c..d746ecb 100644 --- a/src/crash-manager/crash-manager.c +++ b/src/crash-manager/crash-manager.c @@ -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; } -- 2.7.4