diff --git a/opal/util/Makefile.am b/opal/util/Makefile.am index 0100ef6230..2d761b660f 100644 --- a/opal/util/Makefile.am +++ b/opal/util/Makefile.am @@ -40,7 +40,7 @@ headers = \ malloc.h \ numtostr.h \ opal_environ.h \ - os_create_dirpath.h \ + os_dirpath.h \ os_path.h \ output.h \ path.h \ @@ -69,7 +69,7 @@ libopalutil_la_SOURCES = \ malloc.c \ numtostr.c \ opal_environ.c \ - os_create_dirpath.c \ + os_dirpath.c \ os_path.c \ output.c \ path.c \ diff --git a/opal/util/os_create_dirpath.c b/opal/util/os_create_dirpath.c deleted file mode 100644 index 3c07a493db..0000000000 --- a/opal/util/os_create_dirpath.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana - * University Research and Technology - * Corporation. All rights reserved. - * Copyright (c) 2004-2005 The University of Tennessee and The University - * of Tennessee Research Foundation. All rights - * reserved. - * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, - * University of Stuttgart. All rights reserved. - * Copyright (c) 2004-2005 The Regents of the University of California. - * All rights reserved. - * $COPYRIGHT$ - * - * Additional copyrights may follow - * - * $HEADER$ - */ - - -#include "opal_config.h" - -#include -#ifdef HAVE_UNISTD_H -#include -#endif -#ifdef HAVE_LIBGEN_H -#include -#endif -#include - -#include "opal/util/os_create_dirpath.h" -#include "opal/util/argv.h" -#include "opal/constants.h" - -#ifdef __WINDOWS__ -#define PATH_SEP "\\" -#else -#define PATH_SEP "/" -#endif - -static const char *path_sep = PATH_SEP; - - -int opal_os_create_dirpath(const char *path, const mode_t mode) -{ - struct stat buf; - char **parts, *tmp; - int i, len; - - if (NULL == path) { /* protect ourselves from errors */ - return(OPAL_ERROR); - } - - if (0 == stat(path, &buf)) { /* already exists */ - if (mode == (mode & buf.st_mode)) { /* has correct mode */ - return(OPAL_SUCCESS); - } - if (0 == chmod(path, (buf.st_mode | mode))) { /* successfully change mode */ - return(OPAL_SUCCESS); - } - return(OPAL_ERROR); /* can't set correct mode */ - } - - /* quick -- try to make directory */ - if (0 == mkdir(path, mode)) { - return(OPAL_SUCCESS); - } - - /* didnt work, so now have to build our way down the tree */ - /* Split the requested path up into its individual parts */ - - parts = opal_argv_split(path, path_sep[0]); - - /* Ensure to allocate enough space for tmp: the strlen of the - incoming path + 1 (for \0) */ - - tmp = malloc(strlen(path) + 1); - tmp[0] = '\0'; - - /* Iterate through all the subdirectory names in the path, - building up a directory name. Check to see if that dirname - exists. If it doesn't, create it. */ - - /* Notes about stat(): Windows has funny definitions of what will - return 0 from stat(). "C:" will return failure, while "C:\" - will return success. Similarly, "C:\foo" will return success, - while "C:\foo\" will return failure (assuming that a folder - named "foo" exists under C:\). - - POSIX implementations of stat() are generally a bit more - forgiving; most will return true for "/foo" and "/foo/" - (assuming /foo exists). But we might as well abide by the same - rules as Windows and generally disallow checking for names - ending with path_sep (the only possible allowable one is - checking for "/", which is the root directory, and is - guaranteed to exist on valid POSIX filesystems, and is - therefore not worth checking for). */ - - len = opal_argv_count(parts); - for (i = 0; i < len; ++i) { - if (i == 0) { - -#ifdef __WINDOWS__ - /* In the Windows case, check for ":" case (i.e., - an absolute pathname). If this is the case, ensure - that it ends in a path_sep. */ - - if (2 == strlen(parts[0]) && isalpha(parts[0][0]) && - ':' == parts[0][1]) { - strcat(tmp, parts[i]); - strcat(tmp, path_sep); - } - - /* Otherwise, it's a relative path. Per the comment - above, we don't want a '\' at the end, so just append - this part. */ - - else { - strcat(tmp, parts[i]); - } -#else - /* If in POSIX-land, ensure that we never end a directory - name with path_sep */ - - if ('/' == path[0]) { - strcat(tmp, path_sep); - } - strcat(tmp, parts[i]); -#endif - } - - /* If it's not the first part, ensure that there's a - preceeding path_sep and then append this part */ - - else { - if (path_sep[0] != tmp[strlen(tmp) - 1]) { - strcat(tmp, path_sep); - } - strcat(tmp, parts[i]); - } - - /* Now that we finally have the name to check, check it. - Create it if it doesn't exist. */ - - if (0 != stat(tmp, &buf)) { - if (0 != mkdir(tmp, mode) && 0 != stat(tmp, &buf)) { - opal_argv_free(parts); - free(tmp); - return OPAL_ERROR; - } - } - } - - /* All done */ - - opal_argv_free(parts); - free(tmp); - return OPAL_SUCCESS; -} diff --git a/opal/util/os_create_dirpath.h b/opal/util/os_create_dirpath.h deleted file mode 100644 index f4a9de2dcf..0000000000 --- a/opal/util/os_create_dirpath.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana - * University Research and Technology - * Corporation. All rights reserved. - * Copyright (c) 2004-2005 The University of Tennessee and The University - * of Tennessee Research Foundation. All rights - * reserved. - * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, - * University of Stuttgart. All rights reserved. - * Copyright (c) 2004-2005 The Regents of the University of California. - * All rights reserved. - * $COPYRIGHT$ - * - * Additional copyrights may follow - * - * $HEADER$ - */ - -/** @file: - * Creates a directory tree set to the specified permissions. - * - * The opal_os_create_dirpath() function creates a directory - * tree, with each directory that is created in the tree having the specified - * access permissions. Existing directories within the tree are left - * untouched - however, if they do not permit the user to create a directory - * within them, the function will return an error condition. - * - * If the specified full path name already exists, the - * opal_os_create_dirpath() function will check to ensure that - * the final directory in the tree has at least the specified access permission. In other - * words, if the directory has read-write-execute for all, and the user - * has requested read-write access for just the user, then the function - * will consider the directory acceptable. If the minimal permissions are - * not currently provided, the function will attempt to change the - * access permissions of the directory to add the specified - * permissions. The function will return OPAL_ERROR if this cannot - * be done. - **/ - -#ifndef OPAL_OS_CREATE_DIRPATH_H -#define OPAL_OS_CREATE_DIRPATH_H - -#include "opal_config.h" -#include -#ifdef HAVE_SYS_TYPES_H -#include -#endif -#ifdef HAVE_SYS_STAT_H -#include -#endif - -/** - * @param path A pointer to a string that contains the path name to be built. - * @param mode A mode_t bit mask that specifies the access permissions for the - * directories being constructed. - * @retval OPAL_SUCCESS If the directory tree has been successfully created with - * the specified access permissions. - * @retval OPAL_ERROR If the directory tree could not be created with the - * specified access permissions. - */ - -OMPI_DECLSPEC int opal_os_create_dirpath(const char *path, const mode_t mode); - -#endif diff --git a/opal/util/os_dirpath.c b/opal/util/os_dirpath.c new file mode 100644 index 0000000000..3a918f78e3 --- /dev/null +++ b/opal/util/os_dirpath.c @@ -0,0 +1,448 @@ +/* + * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana + * University Research and Technology + * Corporation. All rights reserved. + * Copyright (c) 2004-2005 The University of Tennessee and The University + * of Tennessee Research Foundation. All rights + * reserved. + * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, + * University of Stuttgart. All rights reserved. + * Copyright (c) 2004-2005 The Regents of the University of California. + * All rights reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + */ + + +#include "opal_config.h" + +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_LIBGEN_H +#include +#endif +#include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_DIRENT_H +#include +#endif + +#include "opal/util/os_dirpath.h" +#include "opal/util/argv.h" +#include "opal/util/os_path.h" +#include "opal/constants.h" + +#ifdef __WINDOWS__ +#define PATH_SEP "\\" +#else +#define PATH_SEP "/" +#endif + +static const char *path_sep = PATH_SEP; + + +int opal_os_dirpath_create(const char *path, const mode_t mode) +{ + struct stat buf; + char **parts, *tmp; + int i, len; + + if (NULL == path) { /* protect ourselves from errors */ + return(OPAL_ERROR); + } + + if (0 == stat(path, &buf)) { /* already exists */ + if (mode == (mode & buf.st_mode)) { /* has correct mode */ + return(OPAL_SUCCESS); + } + if (0 == chmod(path, (buf.st_mode | mode))) { /* successfully change mode */ + return(OPAL_SUCCESS); + } + return(OPAL_ERROR); /* can't set correct mode */ + } + + /* quick -- try to make directory */ + if (0 == mkdir(path, mode)) { + return(OPAL_SUCCESS); + } + + /* didnt work, so now have to build our way down the tree */ + /* Split the requested path up into its individual parts */ + + parts = opal_argv_split(path, path_sep[0]); + + /* Ensure to allocate enough space for tmp: the strlen of the + incoming path + 1 (for \0) */ + + tmp = malloc(strlen(path) + 1); + tmp[0] = '\0'; + + /* Iterate through all the subdirectory names in the path, + building up a directory name. Check to see if that dirname + exists. If it doesn't, create it. */ + + /* Notes about stat(): Windows has funny definitions of what will + return 0 from stat(). "C:" will return failure, while "C:\" + will return success. Similarly, "C:\foo" will return success, + while "C:\foo\" will return failure (assuming that a folder + named "foo" exists under C:\). + + POSIX implementations of stat() are generally a bit more + forgiving; most will return true for "/foo" and "/foo/" + (assuming /foo exists). But we might as well abide by the same + rules as Windows and generally disallow checking for names + ending with path_sep (the only possible allowable one is + checking for "/", which is the root directory, and is + guaranteed to exist on valid POSIX filesystems, and is + therefore not worth checking for). */ + + len = opal_argv_count(parts); + for (i = 0; i < len; ++i) { + if (i == 0) { + +#ifdef __WINDOWS__ + /* In the Windows case, check for ":" case (i.e., + an absolute pathname). If this is the case, ensure + that it ends in a path_sep. */ + + if (2 == strlen(parts[0]) && isalpha(parts[0][0]) && + ':' == parts[0][1]) { + strcat(tmp, parts[i]); + strcat(tmp, path_sep); + } + + /* Otherwise, it's a relative path. Per the comment + above, we don't want a '\' at the end, so just append + this part. */ + + else { + strcat(tmp, parts[i]); + } +#else + /* If in POSIX-land, ensure that we never end a directory + name with path_sep */ + + if ('/' == path[0]) { + strcat(tmp, path_sep); + } + strcat(tmp, parts[i]); +#endif + } + + /* If it's not the first part, ensure that there's a + preceeding path_sep and then append this part */ + + else { + if (path_sep[0] != tmp[strlen(tmp) - 1]) { + strcat(tmp, path_sep); + } + strcat(tmp, parts[i]); + } + + /* Now that we finally have the name to check, check it. + Create it if it doesn't exist. */ + + if (0 != stat(tmp, &buf)) { + if (0 != mkdir(tmp, mode) && 0 != stat(tmp, &buf)) { + opal_argv_free(parts); + free(tmp); + return OPAL_ERROR; + } + } + } + + /* All done */ + + opal_argv_free(parts); + free(tmp); + return OPAL_SUCCESS; +} + +int opal_os_dirpath_destroy(const char *path, + bool recursive, + opal_os_dirpath_destroy_callback_fn_t cbfunc) +{ + int rc, exit_status = OPAL_SUCCESS; + bool is_dir = false; + +#ifndef WIN32 + DIR *dp; + struct dirent *ep; + char *filenm; +#ifndef HAVE_STRUCT_DIRENT_D_TYPE + int ret; + struct stat buf; +#endif + + if (NULL == path) { /* protect against error */ + return OPAL_ERROR; + } + + /* + * Make sure we have access to the the base directory + */ + if( OPAL_SUCCESS != (rc = opal_os_dirpath_access(path, 0) ) ) { + exit_status = rc; + goto cleanup; + } + + /* + * Open up the directory + */ + dp = opendir(path); + if (NULL == dp) { + return OPAL_ERROR; + } + + while (NULL != (ep = readdir(dp)) ) { + /* skip: + * - . and .. + */ + if ((0 == strcmp(ep->d_name, ".")) || + (0 == strcmp(ep->d_name, "..")) ) { + continue; + } + + /* Check to see if it is a directory */ +#ifdef HAVE_STRUCT_DIRENT_D_TYPE + if (DT_DIR == ep->d_type) { + is_dir = true; + } +#else /* have dirent.d_type */ + rc = stat(filenm, &buf); + if (rc < 0 || S_ISDIR(buf.st_mode)) { + is_dir = true; + } +#endif /* have dirent.d_type */ + + /* + * If not recursively decending, then if we find a directory then fail + * since we were not told to remove it. + */ + if( is_dir && !recursive) { + /* Set the error indicating that we found a directory, + * but continue removing files + */ + exit_status = OPAL_ERROR; + continue; + } + + /* Will the caller allow us to remove this file/directory? */ + if(NULL != cbfunc) { + /* + * Caller does not wish to remove this file/directory, + * continue with the rest of the entries + */ + if( ! (cbfunc(path, ep->d_name)) ) { + continue; + } + } + /* Directories are recursively destroyed */ + filenm = opal_os_path(false, path, ep->d_name, NULL); + if(is_dir) { + if( OPAL_SUCCESS != (rc = opal_os_dirpath_destroy(filenm, + recursive, + cbfunc) ) ) { + exit_status = rc; + goto cleanup; + } + } + /* Files are removed right here */ + else { + if( 0 != (rc = unlink(filenm) ) ) { + exit_status = OPAL_ERROR; + } + free(filenm); + } + } + + /* + * Done with this directory + */ + closedir(dp); +#else + bool empty = false; + char search_path[MAX_PATH]; + HANDLE file; + WIN32_FIND_DATA file_data; + TCHAR *file_name; + + if (NULL == path) { + return OPAL_ERROR; + } + + strncpy(search_path, path, strlen(path)+1); + strncat (search_path, "\\*", 3); + file = FindFirstFile(search_path, &file_data); + + if (INVALID_HANDLE_VALUE == file) { + FindClose(&file_data); + return; + } + + do { + /* + * Skip + * - . and .. + */ + if ((0 == strcmp(file_data.cFileName, ".")) || + (0 == strcmp(file_data.cFileName, "..")) ) { + continue; + } + + if(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + is_dir = true; + } + /* + * If not recursively decending, then if we find a directory then fail + * since we were not told to remove it. + */ + if( is_dir && !recursive) { + /* Set the error indicating that we found a directory, + * but continue removing files + */ + exit_status = OPAL_ERROR; + continue; + } + + /* Will the caller allow us to remove this file/directory */ + if( NULL != cbfunc) { + if( !cbfunc(path, file_data.cFileName) ) { + continue; + } + } + + file_name = opal_os_path(false, path, file_data.cFileName, NULL); + if( is_dir ) { + if( OPAL_SUCCESS != (rc = opal_os_dirpath_destroy(file_name, + recursive, + cbfunc) ) ) { + exit_status = rc; + goto cleanup; + } + } + else { + DeleteFile(file_name); + } + + /* + * Are we done yet? + */ + if (0 == FindNextFile(file, &file_data)) { + empty = true; + } + + } while(!empty); + + FindClose(&file_data); +#endif + + cleanup: + + /* + * If the directory is empty, them remove it + */ + if(opal_os_dirpath_is_empty(path)) { + rmdir(path); + } + + return exit_status; +} + +bool opal_os_dirpath_is_empty(const char *path ) { +#ifndef __WINDOWS__ + DIR *dp; + 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, ".."))) { + closedir(dp); + return false; + } + } + closedir(dp); + return true; + } + return false; + } + + return true; +#else + char search_path[MAX_PATH]; + HANDLE file; + WIN32_FIND_DATA file_data; + + if (NULL != path) { + strncpy(search_path, path, strlen(path)+1); + strncat (search_path, "\\*", 3); + + file = FindFirstFile(search_path, &file_data); + if (INVALID_HANDLE_VALUE == file) { + FindClose(&file_data); + return true; + } + + if (0 != strcmp(file_data.cFileName, ".") || 0 != strcmp(file_data.cFileName, "..")) { + FindClose(&file_data); + return false; + } + + while (0 != FindNextFile(file, &file_data)) { + if (0 != strcmp(file_data.cFileName, ".") || 0 != strcmp(file_data.cFileName, "..")) { + FindClose(&file_data); + return false; + } + } + } + + FindClose(&file_data); + return true; +#endif /* ifndef __WINDOWS__ */ +} + +int opal_os_dirpath_access(const char *path, const mode_t in_mode ) { +#ifndef __WINDOWS__ + struct stat buf; + mode_t loc_mode = S_IRWXU; /* at the least, I need to be able to do anything */ +#else + struct __stat64 buf; + mode_t loc_mode = _S_IREAD | _S_IWRITE | _S_IEXEC; +#endif + + /* + * If there was no mode specified, use the default mode + */ + if( 0 != in_mode ) { + loc_mode = in_mode; + } + +#ifndef __WINDOWS__ + if (0 == stat(path, &buf)) { /* exists - check access */ +#else + if (0 == _stat64(path, &buf)) { /* exist -- check */ +#endif + if ((buf.st_mode & loc_mode) == loc_mode) { /* okay, I can work here */ + return(OPAL_SUCCESS); + } + else { + /* Don't have access rights to the existing path */ + return(OPAL_ERROR); + } + } + else { + /* We could not find the path */ + return( OPAL_ERR_NOT_FOUND ); + } +} + diff --git a/opal/util/os_dirpath.h b/opal/util/os_dirpath.h new file mode 100644 index 0000000000..c27a09c880 --- /dev/null +++ b/opal/util/os_dirpath.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana + * University Research and Technology + * Corporation. All rights reserved. + * Copyright (c) 2004-2005 The University of Tennessee and The University + * of Tennessee Research Foundation. All rights + * reserved. + * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, + * University of Stuttgart. All rights reserved. + * Copyright (c) 2004-2005 The Regents of the University of California. + * All rights reserved. + * $COPYRIGHT$ + * + * Additional copyrights may follow + * + * $HEADER$ + */ + +/** @file: + * Creates a directory tree set to the specified permissions. + * + * The opal_os_dirpath_create() function creates a directory + * tree, with each directory that is created in the tree having the specified + * access permissions. Existing directories within the tree are left + * untouched - however, if they do not permit the user to create a directory + * within them, the function will return an error condition. + * + * If the specified full path name already exists, the + * opal_os_dirpath_create() function will check to ensure that + * the final directory in the tree has at least the specified access permission. In other + * words, if the directory has read-write-execute for all, and the user + * has requested read-write access for just the user, then the function + * will consider the directory acceptable. If the minimal permissions are + * not currently provided, the function will attempt to change the + * access permissions of the directory to add the specified + * permissions. The function will return OPAL_ERROR if this cannot + * be done. + **/ + +#ifndef OPAL_OS_DIRPATH_CREATE_H +#define OPAL_OS_DIRPATH_CREATE_H + +#include "opal_config.h" +#include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif + +/** + * @param path A pointer to a string that contains the path name to be built. + * @param mode A mode_t bit mask that specifies the access permissions for the + * directories being constructed. + * @retval OPAL_SUCCESS If the directory tree has been successfully created with + * the specified access permissions. + * @retval OPAL_ERROR If the directory tree could not be created with the + * specified access permissions. + */ + +OMPI_DECLSPEC int opal_os_dirpath_create(const char *path, const mode_t mode); + +/** + * Check to see if a directory is empty + * + * @param path A pointer to a string that contains the path name to be checked. + * + * @retval true If the directory is empty + * @retval false If the directory is not empty + */ +OMPI_DECLSPEC bool opal_os_dirpath_is_empty(const char *path); + +/** + * Check access to the directory + * + * @param path A pointer to a string that contains the path name to be checked. + * @param mode A mode_t bit mask that specifies the access permissions for the + * directory to be accessed. + * + * @retval OPAL_SUCCESS If directory exists, and permissions match + * @retval OPAL_ERR_NOT_FOUND If directory does not exist + * @retval OPAL_ERROR If directory exists, and permissions do not match + */ +OMPI_DECLSPEC int opal_os_dirpath_access(const char *path, const mode_t mode ); + +/** + * Callback for opal_os_dirpath_destroy(). Call for every file/directory before + * taking action to remove/unlink it. + * + * @param root A pointer to a string that contains the base path name (e.g., /tmp/foo from /tmp/foo/bar) + * @param path A pointer to a string that contains the file or directory (e.g., bar from /tmp/foo/bar) + * + * @retval true Allow the program to remove the file/directory + * @retval false Do not allow the program to remove the file/directory + */ +typedef bool (*opal_os_dirpath_destroy_callback_fn_t)(const char *root, const char *path); + +/** + * Destroy a directory + * + * @param path A pointer to a string that contains the path name to be destroyed + * @param recursive Recursively desend the directory removing all files and directories. + * if set to 'false' then the directory must be empty to succeed. + * @param cbfunc A function that will be called before removing a file or directory. + * If NULL, then assume all remove. + * + * @retval OPAL_SUCCESS If the directory was successfully removed or removed to the + * specification of the user (i.e., obeyed the callback function). + * @retval OPAL_ERR_NOT_FOUND If directory does not exist. + * @retval OPAL_ERROR If the directory cannnot be removed, accessed properly, or contains + * directories that could not be removed.. + */ +OMPI_DECLSPEC int opal_os_dirpath_destroy(const char *path, + bool recursive, + opal_os_dirpath_destroy_callback_fn_t cbfunc); + +#endif diff --git a/orte/mca/pls/bproc_orted/pls_bproc_orted.c b/orte/mca/pls/bproc_orted/pls_bproc_orted.c index 6598cf36c7..2be5eaf099 100644 --- a/orte/mca/pls/bproc_orted/pls_bproc_orted.c +++ b/orte/mca/pls/bproc_orted/pls_bproc_orted.c @@ -31,7 +31,7 @@ #include "opal/mca/base/mca_base_param.h" #include "opal/runtime/opal_progress.h" #include "opal/threads/condition.h" -#include "opal/util/os_create_dirpath.h" +#include "opal/util/os_dirpath.h" #include "opal/util/os_path.h" #include "opal/util/output.h" @@ -94,7 +94,7 @@ static int pls_bproc_orted_make_dir(char *directory) pls_bproc_orted_delete_dir_tree(directory); } /* try to create it with proper mode */ - return(opal_os_create_dirpath(directory, my_mode)); + return(opal_os_dirpath_create(directory, my_mode)); } /** diff --git a/orte/runtime/orte_universe_exists.c b/orte/runtime/orte_universe_exists.c index 7b32d43653..7d27c684d1 100644 --- a/orte/runtime/orte_universe_exists.c +++ b/orte/runtime/orte_universe_exists.c @@ -39,6 +39,7 @@ #include "orte/util/sys_info.h" #include "orte/util/proc_info.h" #include "opal/util/os_path.h" +#include "opal/util/os_dirpath.h" #include "orte/util/session_dir.h" #include "orte/util/universe_setup_file_io.h" @@ -83,7 +84,7 @@ int orte_universe_search(opal_list_t *universe_list) { /* * Check to make sure we have access to this directory */ - if( ORTE_SUCCESS != (ret = orte_session_dir_check_dir(fulldirpath) )) { + if( ORTE_SUCCESS != (ret = opal_os_dirpath_access(fulldirpath, 0) )) { exit_status = ret; goto cleanup; } diff --git a/orte/tools/orte-clean/orte-clean.c b/orte/tools/orte-clean/orte-clean.c index 2ce33dfc31..c52b396c3f 100644 --- a/orte/tools/orte-clean/orte-clean.c +++ b/orte/tools/orte-clean/orte-clean.c @@ -159,7 +159,6 @@ main(int argc, char *argv[]) /* * Try to connect to the universe */ - if( orte_clean_globals.verbose ) { printf("orte_clean: Connecting to universe: %s\n", search_result->name); } @@ -174,6 +173,9 @@ main(int argc, char *argv[]) * If unable to connect to the universe, * clean it up! */ + if( orte_clean_globals.verbose ) { + printf("orte_clean: Cleaning the session directory for universe: %s\n", search_result->name); + } if( ORTE_SUCCESS != (ret = orte_clean_universe(search_result)) ){ exit_status = ret; goto cleanup; diff --git a/orte/util/session_dir.c b/orte/util/session_dir.c index 6a680dbd8b..d6b2b7f3e1 100644 --- a/orte/util/session_dir.c +++ b/orte/util/session_dir.c @@ -51,7 +51,7 @@ #include "orte/util/proc_info.h" #include "opal/util/output.h" #include "opal/util/os_path.h" -#include "opal/util/os_create_dirpath.h" +#include "opal/util/os_dirpath.h" #include "orte/mca/errmgr/errmgr.h" #include "orte/runtime/runtime.h" @@ -63,10 +63,7 @@ *******************************/ static int orte_create_dir(char *directory); -static void orte_dir_empty(char *pathname); -static void orte_dir_empty_all(char *pathname); - -static bool orte_is_empty(char *pathname); +static bool orte_dir_check_file(const char *root, const char *path); #ifdef __WINDOWS__ #define OMPI_DEFAULT_TMPDIR "C:\\TEMP" @@ -93,51 +90,17 @@ static int orte_create_dir(char *directory) /* Sanity check before creating the directory with the proper mode, * Make sure it doesn't exist already */ - if( ORTE_ERR_NOT_FOUND != (ret = orte_session_dir_check_dir(directory)) ) { - /* Failure because orte_session_dir_check_dir() indicated that either: + if( OPAL_ERR_NOT_FOUND != (ret = opal_os_dirpath_access(directory, my_mode)) ) { + /* Failure because opal_os_dirpath_access() indicated that either: * - The directory exists and we can access it (no need to create it again), - * return ORTE_SUCCESS, or - * - don't have access rights, return ORTE_ERROR + * return OPAL_SUCCESS, or + * - don't have access rights, return OPAL_ERROR */ return(ret); } /* The directory doesn't exist so create it */ else { - return(opal_os_create_dirpath(directory, my_mode)); - } -} - -/* - * Check that the directory: - * - exists - * - if exists, then we have permission to interact with it - */ -int orte_session_dir_check_dir(char *directory) -{ -#ifndef __WINDOWS__ - struct stat buf; - mode_t my_mode = S_IRWXU; /* at the least, I need to be able to do anything */ -#else - struct __stat64 buf; - mode_t my_mode = _S_IREAD | _S_IWRITE | _S_IEXEC; -#endif - -#ifndef __WINDOWS__ - if (0 == stat(directory, &buf)) { /* exists - check access */ -#else - if (0 == _stat64(directory, &buf)) { /* exist -- check */ -#endif - if ((buf.st_mode & my_mode) == my_mode) { /* okay, I can work here */ - return(ORTE_SUCCESS); - } - else { - /* Don't have access rights to the existing directory */ - return(ORTE_ERROR); - } - } - else { - /* We could not find the directory */ - return( ORTE_ERR_NOT_FOUND ); + return(opal_os_dirpath_create(directory, my_mode)); } } @@ -345,7 +308,7 @@ int orte_session_dir(bool create, /* This indicates if the prefix was set, and so if it fails then we * should try with the default prefixes.*/ bool dbl_check_prefix = false; - + if( NULL != prefix) dbl_check_prefix = true; @@ -404,7 +367,7 @@ int orte_session_dir(bool create, * if we are not creating, then just verify that the path is OK */ else { - if( ORTE_SUCCESS != (rtn = orte_session_dir_check_dir(fulldirpath) )) { + if( ORTE_SUCCESS != (rtn = opal_os_dirpath_access(fulldirpath, 0) )) { /* It is not valid so we give up and return an error */ return_code = rtn; @@ -556,11 +519,14 @@ orte_session_dir_cleanup(orte_jobid_t jobid) return ORTE_ERR_OUT_OF_RESOURCE; } - orte_dir_empty_all(job_session_dir); - orte_dir_empty(orte_process_info.universe_session_dir); - orte_dir_empty(tmp); + opal_os_dirpath_destroy(job_session_dir, + true, orte_dir_check_file); + opal_os_dirpath_destroy(orte_process_info.universe_session_dir, + false, orte_dir_check_file); + opal_os_dirpath_destroy(tmp, + false, orte_dir_check_file); - if (orte_is_empty(job_session_dir)) { + if (opal_os_dirpath_is_empty(job_session_dir)) { if (orte_debug_flag) { opal_output(0, "sess_dir_finalize: found job session dir empty - deleting"); } @@ -572,7 +538,7 @@ orte_session_dir_cleanup(orte_jobid_t jobid) goto CLEANUP; } - if (orte_is_empty(orte_process_info.universe_session_dir)) { + if (opal_os_dirpath_is_empty(orte_process_info.universe_session_dir)) { if (orte_debug_flag) { opal_output(0, "sess_dir_finalize: found univ session dir empty - deleting"); } @@ -584,7 +550,7 @@ orte_session_dir_cleanup(orte_jobid_t jobid) goto CLEANUP; } - if (orte_is_empty(tmp)) { + if (opal_os_dirpath_is_empty(tmp)) { if (orte_debug_flag) { opal_output(0, "sess_dir_finalize: found top session dir empty - deleting"); } @@ -648,12 +614,16 @@ orte_session_dir_finalize(orte_process_name_t *proc) return ORTE_ERR_OUT_OF_RESOURCE; } - orte_dir_empty(proc_session_dir); - orte_dir_empty(job_session_dir); - orte_dir_empty(orte_process_info.universe_session_dir); - orte_dir_empty(tmp); + opal_os_dirpath_destroy(proc_session_dir, + false, orte_dir_check_file); + opal_os_dirpath_destroy(job_session_dir, + false, orte_dir_check_file); + opal_os_dirpath_destroy(orte_process_info.universe_session_dir, + false, orte_dir_check_file); + opal_os_dirpath_destroy(tmp, + false, orte_dir_check_file); - if (orte_is_empty(proc_session_dir)) { + if (opal_os_dirpath_is_empty(proc_session_dir)) { if (orte_debug_flag) { opal_output(0, "sess_dir_finalize: found proc session dir empty - deleting"); } @@ -665,7 +635,7 @@ orte_session_dir_finalize(orte_process_name_t *proc) goto CLEANUP; } - if (orte_is_empty(job_session_dir)) { + if (opal_os_dirpath_is_empty(job_session_dir)) { if (orte_debug_flag) { opal_output(0, "sess_dir_finalize: found job session dir empty - deleting"); } @@ -677,7 +647,7 @@ orte_session_dir_finalize(orte_process_name_t *proc) goto CLEANUP; } - if (orte_is_empty(orte_process_info.universe_session_dir)) { + if (opal_os_dirpath_is_empty(orte_process_info.universe_session_dir)) { if (orte_debug_flag) { opal_output(0, "sess_dir_finalize: found univ session dir empty - deleting"); } @@ -689,7 +659,7 @@ orte_session_dir_finalize(orte_process_name_t *proc) goto CLEANUP; } - if (orte_is_empty(tmp)) { + if (opal_os_dirpath_is_empty(tmp)) { if (orte_debug_flag) { opal_output(0, "sess_dir_finalize: found top session dir empty - deleting"); } @@ -709,246 +679,18 @@ CLEANUP: return ORTE_SUCCESS; } -static void -orte_dir_empty(char *pathname) -{ -#ifndef __WINDOWS__ - DIR *dp; - struct dirent *ep; - char *filenm; -#ifndef HAVE_STRUCT_DIRENT_D_TYPE - int ret; - struct stat buf; -#endif +static bool +orte_dir_check_file(const char *root, const char *path) { - if (NULL == pathname) { /* protect against error */ - return; + /* + * Keep: + * - files starting with "output-" + * - universe contact (universe-setup.txt) + */ + if( (0 == strncmp(path, "output-", strlen("output-"))) || + (0 == strcmp(path, "universe-setup.txt"))) { + return false; } - dp = opendir(pathname); - if (NULL == dp) { - return; - } - - while (NULL != (ep = readdir(dp)) ) { - /* skip: - * - . and .. - * - directories - * - files starting with "output-" - * - universe contact (universe-setup.txt) - */ - if ((0 != strcmp(ep->d_name, ".")) && - (0 != strcmp(ep->d_name, "..")) && - (0 != strncmp(ep->d_name, "output-", strlen("output-"))) && - (0 != strcmp(ep->d_name, "universe-setup.txt"))) { - - filenm = opal_os_path(false, pathname, ep->d_name, NULL); - - /* make sure it's not a directory */ -#ifdef HAVE_STRUCT_DIRENT_D_TYPE - if (DT_DIR == ep->d_type) { - free(filenm); - continue; - } -#else /* have dirent.d_type */ - ret = stat(filenm, &buf); - if (ret < 0 || S_ISDIR(buf.st_mode)) { - free(filenm); - continue; - } -#endif /* have dirent.d_type */ - unlink(filenm); - free(filenm); - } - } - closedir(dp); -#else - bool empty = false; - char search_path[MAX_PATH]; - HANDLE file; - WIN32_FIND_DATA file_data; - TCHAR *file_name; - - if (NULL != pathname) { - strncpy(search_path, pathname, strlen(pathname)+1); - strncat (search_path, "\\*", 3); - file = FindFirstFile(search_path, &file_data); - - if (INVALID_HANDLE_VALUE == file) { - FindClose(&file_data); - return; - } - - do { - if ((0 != strcmp(file_data.cFileName, ".")) && - (0 != strcmp(file_data.cFileName, "..")) && - (!(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) && - (0 != strncmp(file_data.cFileName,"output-", strlen("output-"))) && - (0 != strcmp(file_data.cFileName,"universe-setup.txt-"))) { - - file_name = opal_os_path(false, pathname, file_data.cFileName, NULL); - DeleteFile(file_name); - - } - if (0 == FindNextFile(file, &file_data)) { - empty = true; - } - } while(!empty); - FindClose(&file_data); - } -#endif -} - - -static void -orte_dir_empty_all(char *pathname) -{ -#ifndef WIN32 - DIR *dp; - struct dirent *ep; - char *filenm; -#ifndef HAVE_STRUCT_DIRENT_D_TYPE - int ret; - struct stat buf; -#endif - int rc; - - if (NULL == pathname) { /* protect against error */ - return; - } - - dp = opendir(pathname); - if (NULL == dp) { - return; - } - - while (NULL != (ep = readdir(dp)) ) { - /* skip: - * - . and .. - * - directories - * - files starting with "output-" - * - universe contact (universe-setup.txt) - */ - if ((0 != strcmp(ep->d_name, ".")) && - (0 != strcmp(ep->d_name, "..")) && - (0 != strncmp(ep->d_name, "output-", strlen("output-"))) && - (0 != strcmp(ep->d_name, "universe-setup.txt"))) { - - filenm = opal_os_path(false, pathname, ep->d_name, NULL); - - /* is it a directory */ -#ifdef HAVE_STRUCT_DIRENT_D_TYPE - if (DT_DIR == ep->d_type) { - orte_dir_empty_all(filenm); - rmdir(filenm); - free(filenm); - continue; - } -#else /* have dirent.d_type */ - ret = stat(filenm, &buf); - if (ret < 0 || S_ISDIR(buf.st_mode)) { - orte_dir_empty_all(filenm); - rmdir(filenm); - free(filenm); - continue; - } -#endif /* have dirent.d_type */ - rc = unlink(filenm); - free(filenm); - } - } - closedir(dp); -#else - bool empty = false; - char search_path[MAX_PATH]; - HANDLE file; - WIN32_FIND_DATA file_data; - TCHAR *file_name; - - if (NULL != pathname) { - strncpy(search_path, pathname, strlen(pathname)+1); - strncat (search_path, "\\*", 3); - file = FindFirstFile(search_path, &file_data); - - if (INVALID_HANDLE_VALUE == file) { - FindClose(&file_data); - return; - } - - do { - if(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - orte_dir_empty_all(file_name); - } - if ((0 != strcmp(file_data.cFileName, ".")) && - (0 != strcmp(file_data.cFileName, "..")) && - (0 != strncmp(file_data.cFileName,"output-", strlen("output-"))) && - (0 != strcmp(file_data.cFileName,"universe-setup.txt-"))) { - - file_name = opal_os_path(false, pathname, file_data.cFileName, NULL); - DeleteFile(file_name); - - } - if (0 == FindNextFile(file, &file_data)) { - empty = true; - } - } while(!empty); - FindClose(&file_data); - } -#endif -} - - -/* tests if the directory is empty */ -static bool orte_is_empty(char *pathname) -{ -#ifndef __WINDOWS__ - DIR *dp; - struct dirent *ep; - if (NULL != pathname) { /* protect against error */ - dp = opendir(pathname); - 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 true; -#else - char search_path[MAX_PATH]; - HANDLE file; - WIN32_FIND_DATA file_data; - - if (NULL != pathname) { - strncpy(search_path, pathname, strlen(pathname)+1); - strncat (search_path, "\\*", 3); - - file = FindFirstFile(search_path, &file_data); - if (INVALID_HANDLE_VALUE == file) { - FindClose(&file_data); - return true; - } - - if (0 != strcmp(file_data.cFileName, ".") || 0 != strcmp(file_data.cFileName, "..")) { - FindClose(&file_data); - return false; - } - - while (0 != FindNextFile(file, &file_data)) { - if (0 != strcmp(file_data.cFileName, ".") || 0 != strcmp(file_data.cFileName, "..")) { - FindClose(&file_data); - return false; - } - } - } - - FindClose(&file_data); - return true; -#endif /* ifndef __WINDOWS__ */ } diff --git a/orte/util/session_dir.h b/orte/util/session_dir.h index 3a6b920bae..5714a4327b 100644 --- a/orte/util/session_dir.h +++ b/orte/util/session_dir.h @@ -140,18 +140,6 @@ OMPI_DECLSPEC int orte_session_dir_get_name(char **fulldirpath, char *batchid, char *univ, char *job, char *proc); -/* - * Check the session directory string passed - * to check if the session directory exists, and can be accessed - * - * @param directory 'fulldirpath' returned value from orte_session_dir_get_name() - * @retval ORTE_SUCCESS If the directory exists, and can be accessed - * @retval ORTE_ERR_NOT_FOUND If the directory does not exist - * @retval ORTE_ERROR If the directory cannot be accessed, but does exist - */ -OMPI_DECLSPEC int orte_session_dir_check_dir(char *directory); - - /** The orte_session_dir_finalize() function performs a cleanup of the * session directory tree. It first removes the session directory for * the calling process. It then checks to see if the job-level session diff --git a/test/util/opal_os_create_dirpath.c b/test/util/opal_os_create_dirpath.c index 9fc3c8027f..02e700334c 100644 --- a/test/util/opal_os_create_dirpath.c +++ b/test/util/opal_os_create_dirpath.c @@ -33,7 +33,7 @@ #include "opal/runtime/opal.h" #include "opal/constants.h" #include "opal/util/os_path.h" -#include "opal/util/os_create_dirpath.h" +#include "opal/util/os_dirpath.h" #ifdef __WINDOWS__ #define PATH_SEP "\\" @@ -89,7 +89,7 @@ static bool test1(void) /* Test trivial functionality. Program should return OPAL_ERROR when called with NULL path. */ - if (OPAL_ERROR != opal_os_create_dirpath(NULL, S_IRWXU)) + if (OPAL_ERROR != opal_os_dirpath_create(NULL, S_IRWXU)) return(false); return true; @@ -111,14 +111,14 @@ static bool test2(void) return(false); } - if (OPAL_ERROR == opal_os_create_dirpath(tmp, S_IRWXU)) { + if (OPAL_ERROR == opal_os_dirpath_create(tmp, S_IRWXU)) { rmdir(tmp); return(false); } chmod(tmp, S_IRUSR); - if (OPAL_ERROR == opal_os_create_dirpath(tmp, S_IRWXU)) { + if (OPAL_ERROR == opal_os_dirpath_create(tmp, S_IRWXU)) { rmdir(tmp); return(false); } @@ -148,7 +148,7 @@ static bool test3(void) } out = opal_os_path(true, a[0], a[1], a[2], NULL); - if (OPAL_ERROR == opal_os_create_dirpath(out, S_IRWXU)) { + if (OPAL_ERROR == opal_os_dirpath_create(out, S_IRWXU)) { out = opal_os_path(true, a[0], a[1], a[2], NULL); if (0 == stat(out, &buf)) rmdir(out); diff --git a/test/util/orte_session_dir.c b/test/util/orte_session_dir.c index f139fd359d..36c3a3cdc9 100644 --- a/test/util/orte_session_dir.c +++ b/test/util/orte_session_dir.c @@ -38,7 +38,7 @@ #include "orte/util/proc_info.h" #include "orte/util/sys_info.h" #include "opal/util/os_path.h" -#include "opal/util/os_create_dirpath.h" +#include "opal/util/os_dirpath.h" #include "orte/util/session_dir.h" #include "orte/util/proc_info.h" #include "orte/runtime/runtime.h"