From b257c32d2c264f78897e24c6952ea5297709e44c Mon Sep 17 00:00:00 2001 From: Ralph Castain Date: Wed, 18 Jan 2017 12:05:47 -0800 Subject: [PATCH] Cleanup the os_dirpath logic so it doesn't error out if the directory actually gets created (regardless of what mkdir returns), and pretty-prints the error if it does error out. Signed-off-by: Ralph Castain --- opal/util/help-opal-util.txt | 21 +++++++++++++++++ opal/util/os_dirpath.c | 45 ++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/opal/util/help-opal-util.txt b/opal/util/help-opal-util.txt index 7206b72dae..07b49fd128 100644 --- a/opal/util/help-opal-util.txt +++ b/opal/util/help-opal-util.txt @@ -1,6 +1,7 @@ # -*- text -*- # # Copyright (c) 2009 Cisco Systems, Inc. All rights reserved. +# Copyright (c) 2017 Intel, Inc. All rights reserved. # $COPYRIGHT$ # # Additional copyrights may follow @@ -93,3 +94,23 @@ resource: Limit: %s Please correct the request and try again. +# +[dir-mode] +While working through a directory tree, we were unable to set +a directory to the desired mode: + + Directory: %s + Mode: %0x + Error: %s + +Please check to ensure you have adequate permissions to perform +the desired operation. +# +[mkdir-failed] +A call to mkdir was unable to create the desired directory: + + Directory: %s + Error: %s + +Please check to ensure you have adequate permissions to perform +the desired operation. diff --git a/opal/util/os_dirpath.c b/opal/util/os_dirpath.c index 6116e9feb5..ff25eb7d2c 100644 --- a/opal/util/os_dirpath.c +++ b/opal/util/os_dirpath.c @@ -11,7 +11,7 @@ * All rights reserved. * Copyright (c) 2015-2016 Research Organization for Information Science * and Technology (RIST). All rights reserved. - * Copyright (c) 2016 Intel, Inc. All rights reserved. + * Copyright (c) 2016-2017 Intel, Inc. All rights reserved. * $COPYRIGHT$ * * Additional copyrights may follow @@ -40,6 +40,7 @@ #include "opal/util/output.h" #include "opal/util/os_dirpath.h" +#include "opal/util/show_help.h" #include "opal/util/argv.h" #include "opal/util/os_path.h" #include "opal/constants.h" @@ -64,10 +65,8 @@ int opal_os_dirpath_create(const char *path, const mode_t mode) if (0 == (ret = chmod(path, (buf.st_mode | mode)))) { /* successfully change mode */ return(OPAL_SUCCESS); } - opal_output(0, - "opal_os_dirpath_create: " - "Error: Unable to create directory (%s), unable to set the correct mode [%d] (%s)\n", - path, errno, strerror(errno)); + opal_show_help("help-opal-util.txt", "dir-mode", true, + path, mode, strerror(errno)); return(OPAL_ERR_PERM); /* can't set correct mode */ } @@ -113,14 +112,11 @@ int opal_os_dirpath_create(const char *path, const mode_t mode) strcat(tmp, parts[i]); } - /* Now that we finally have the name to check, check it. - Create it if it doesn't exist. */ + /* Now that we have the name, try to create it */ ret = mkdir(tmp, mode); - if ((0 > ret && EEXIST != errno) || 0 != stat(tmp, &buf)) { - opal_output(0, - "opal_os_dirpath_create: " - "Error: Unable to create the sub-directory (%s) of (%s), mkdir failed [%d]\n", - tmp, path, ret); + if (0 != stat(tmp, &buf)) { + opal_show_help("help-opal-util.txt", "mkdir-failed", true, + tmp, strerror(errno)); opal_argv_free(parts); free(tmp); return OPAL_ERROR; @@ -265,19 +261,19 @@ bool opal_os_dirpath_is_empty(const char *path ) { struct dirent *ep; if (NULL != path) { /* protect against error */ - dp = opendir(path); - if (NULL != dp) { - while ((ep = readdir(dp))) { - if ((0 != strcmp(ep->d_name, ".")) && - (0 != strcmp(ep->d_name, ".."))) { + dp = opendir(path); + if (NULL != dp) { + while ((ep = readdir(dp))) { + if ((0 != strcmp(ep->d_name, ".")) && + (0 != strcmp(ep->d_name, ".."))) { closedir(dp); - return false; - } - } - closedir(dp); - return true; - } - return false; + return false; + } + } + closedir(dp); + return true; + } + return false; } return true; @@ -306,4 +302,3 @@ int opal_os_dirpath_access(const char *path, const mode_t in_mode ) { return( OPAL_ERR_NOT_FOUND ); } } -