1
1

Everything related to file and path management. Now we have a full

range of OS friendly path management functions, such as opal_basename
opal_dirname. They should always be used instead of basename and
dirname. There are several functions which allow us to create paths
that are compatible with the OS.
The OPAL_ENV_SEP define should be used (instead of ':') when a env
variable is splitted.

This commit was SVN r11336.
Этот коммит содержится в:
George Bosilca 2006-08-22 23:25:13 +00:00
родитель 2fa412fcd0
Коммит a28b025150
14 изменённых файлов: 230 добавлений и 144 удалений

Просмотреть файл

@ -23,14 +23,6 @@
#ifndef OMPI_STDINT_H
#define OMPI_STDINT_H 1
#include "opal_config.h"
#ifdef HAVE_STDINT_H
#include <stdint.h>
#else
/*
* Include what we can and define what is missing.
*/
@ -195,7 +187,5 @@ typedef unsigned long long uintptr_t;
#endif /* ifndef SIZE_MAX */
#endif /* HAVE_STDINT_H */
#endif /* OMPI_STDINT_H */

Просмотреть файл

@ -215,11 +215,7 @@ static void find_dyn_components(const char *path, const char *type_name,
dir = path_to_use;
if (NULL != dir) {
do {
#ifdef __WINDOWS__
end = strchr(dir, ';');
#else
end = strchr(dir, ':');
#endif
end = strchr(dir, OPAL_ENV_SEP);
if (NULL != end) {
*end = '\0';
}
@ -273,7 +269,7 @@ static void find_dyn_components(const char *path, const char *type_name,
*/
static int save_filename(const char *filename, lt_ptr data)
{
int len, prefix_len, total_len;
size_t len, prefix_len, total_len;
char *prefix;
const char *basename;
component_file_item_t *component_file;
@ -332,7 +328,7 @@ static int save_filename(const char *filename, lt_ptr data)
static int open_component(component_file_item_t *target_file,
opal_list_t *found_components)
{
int len, show_errors, param;
int show_errors, param;
lt_dlhandle component_handle;
mca_base_component_t *component_struct;
char *struct_name, *err;
@ -340,6 +336,7 @@ static int open_component(component_file_item_t *target_file,
opal_list_item_t *cur;
mca_base_component_list_item_t *mitem;
dependency_item_t *ditem;
size_t len;
opal_output_verbose(40, 0, "mca: base: component_find: examining dyanmic %s MCA component \"%s\"",
target_file->type, target_file->name, NULL);
@ -392,8 +389,8 @@ static int open_component(component_file_item_t *target_file,
opal_output(0, "mca: base: component_find: unable to open %s %s: %s (ignored)",
target_file->type, target_file->name, err);
}
opal_output_verbose(40, 0, "mca: base: component_find: unable to open: %s (ignored)",
err, NULL);
opal_output_verbose(40, 0, "mca: base: component_find: unable to open %s: %s (ignored)",
target_file->filename, err, NULL);
free(err);
target_file->status = FAILED_TO_LOAD;
free_dependency_list(&dependencies);
@ -486,7 +483,7 @@ static int check_ompi_info(component_file_item_t *target_file,
opal_list_t *dependencies,
opal_list_t *found_components)
{
int len;
size_t len;
FILE *fp;
char *depname;
char buffer[BUFSIZ], *p;
@ -583,7 +580,7 @@ static int check_dependency(char *line, component_file_item_t *target_file,
/* Ensure that this was a valid dependency statement */
type = line;
name = strchr(line, ':');
name = strchr(line, OPAL_ENV_SEP);
if (NULL == name) {
return OPAL_ERR_OUT_OF_RESOURCE;
}

Просмотреть файл

@ -54,9 +54,9 @@ static void parse_verbose(char *e, opal_output_stream_t *lds);
int mca_base_open(void)
{
int param_index;
char *value;
char *value, *home;
opal_output_stream_t lds;
char hostname[64];
char hostname[MAXHOSTNAMELEN];
if (!mca_base_opened) {
mca_base_opened = true;
@ -65,8 +65,14 @@ int mca_base_open(void)
}
/* Register some params */
#if !defined(__WINDOWS__)
home = getenv("HOME");
asprintf(&value, "%s:%s"OPAL_PATH_SEP".openmpi"OPAL_PATH_SEP"components", OPAL_PKGLIBDIR, home);
#else
home = getenv("USERPROFILE");
asprintf(&value, "%s;%s"OPAL_PATH_SEP".openmpi"OPAL_PATH_SEP"components", OPAL_PKGLIBDIR, home);
#endif /* !defined(__WINDOWS__) */
asprintf(&value, "%s:~/.openmpi/components", OPAL_PKGLIBDIR);
mca_base_param_component_path =
mca_base_param_reg_string_name("mca", "component_path",
"Path where to look for Open MPI and ORTE components",
@ -94,7 +100,7 @@ int mca_base_open(void)
} else {
set_defaults(&lds);
}
gethostname(hostname, sizeof(hostname));
gethostname(hostname, MAXHOSTNAMELEN);
asprintf(&lds.lds_prefix, "[%s:%05d] ", hostname, getpid());
opal_output_reopen(0, &lds);
opal_output_verbose(5, 0, "mca: base: opening components");
@ -189,7 +195,7 @@ static void parse_verbose(char *e, opal_output_stream_t *lds)
else if (strncasecmp(ptr, "level", 5) == 0) {
lds->lds_verbose_level = 0;
if (ptr[5] == ':')
if (ptr[5] == OPAL_ENV_SEP)
lds->lds_verbose_level = atoi(ptr + 6);
}

Просмотреть файл

@ -22,18 +22,49 @@
#include <string.h>
#include "opal/util/basename.h"
#include "opal/util/os_path.h"
#include "opal/constants.h"
/**
* Return a pointer into the original string where the last PATH delimiter
* was found. It does not modify the original string. Moreover, it does not
* scan the full string, but only the part allowed by the specified number
* of characters.
* If the last character on the string is a path separator, it will be skipped.
*/
static inline char* opal_find_last_path_separator( const char* filename, size_t n )
{
char* p = (char*)filename + n;
/* First skip the latest separators */
for( ; p != filename; p-- ) {
#if defined(__WINDOWS__)
if( (*p != '\\') && (*p != '/') )
break;
#else
if( *p != OPAL_PATH_SEP )
break;
#endif /* defined(__WINDOWS__) */
}
for( ; p != filename; p-- ) {
#if defined(__WINDOWS__)
if( (*p == '\\') || (*p == '/') )
return p;
#else
if( *p == OPAL_PATH_SEP )
return p;
#endif /* defined(__WINDOWS__) */
}
return NULL; /* nothing found inside the filename */
}
char *opal_basename(const char *filename)
{
size_t i;
char *tmp, *ret = NULL;
#ifdef __WINDOWS__
const char sep = '\\';
#else
const char sep = '/';
#endif
const char sep = OPAL_PATH_SEP[0];
/* Check for the bozo case */
@ -92,5 +123,34 @@ char *opal_basename(const char *filename)
}
ret = strdup(ret + 1);
free(tmp);
return ret;
return opal_make_filename_os_friendly(ret);
}
char* opal_dirname(const char* filename)
{
#if defined(HAVE_DIRNAME)
return dirname(filename);
#else
const char* p = opal_find_last_path_separator(filename, strlen(filename));
for( ; p != filename; p-- ) {
if( (*p == '\\') || (*p == '/') ) {
/* If there are several delimiters remove them all */
for( --p; p != filename; p-- ) {
if( (*p != '\\') && (*p != '/') ) {
p++;
break;
}
}
if( p != filename ) {
char* ret = (char*)malloc( p - filename + 1 );
strncpy_s( ret, (p - filename + 1), filename, p - filename );
ret[p - filename] = '\0';
return opal_make_filename_os_friendly(ret);
}
break; /* return the duplicate of "." */
}
}
return _strdup(".");
#endif /* defined(HAVE_DIRNAME) */
}

Просмотреть файл

@ -63,7 +63,44 @@ extern "C" {
*
* G:\ returns "G:"
*/
char *opal_basename(const char* filename);
OPAL_DECLSPEC char *opal_basename(const char* filename);
/**
* Return the dirname of a filename.
*
* @param filename The filename to examine
*
* @returns A string containing the dirname, or NULL if there is an error
*
* The contents of the \em filename parameter are unchanged. This
* function returns a new string containing the dirname of the
* filename (which must be eventually freed by the caller), or
* NULL if there is an error. Trailing "/" characters in the
* filename do not count, unless it is in the only part of the
* filename (e.g., "/" or "C:\").
*
* This function will do the Right Things on POSIX and
* Windows-based operating systems. For example:
*
* foo.txt returns "foo.txt"
*
* /foo/bar/baz returns "/foo/bar"
*
* /yow.c returns "/"
*
* / returns ""
*
* C:\foo\bar\baz returns "C:\foo\bar"
*
* D:foo.txt returns "D:"
*
* E:\yow.c returns "E:"
*
* F: returns ""
*
* G:\ returns ""
*/
OPAL_DECLSPEC char *opal_dirname(const char* filename);
#if defined(c_plusplus) || defined(__cplusplus)
}

Просмотреть файл

@ -27,6 +27,9 @@
#include "opal/util/opal_environ.h"
#include "opal/constants.h"
#if !defined(__WINDOWS__)
extern char** environ;
#endif /* !defined(__WINDOWS__) */
/*
* Merge two environ-like char arrays, ensuring that there are no
@ -82,7 +85,6 @@ char **opal_environ_merge(char **minor, char **major)
return ret;
}
/*
* Portable version of setenv(), allowing editing of any environ-like
* array
@ -93,7 +95,6 @@ int opal_setenv(const char *name, const char *value, bool overwrite,
int i;
char *newvalue, *compare;
size_t len;
extern char **environ;
/* Make the new value */
@ -108,7 +109,7 @@ int opal_setenv(const char *name, const char *value, bool overwrite,
/* Check the bozo case */
if (NULL == env) {
if( NULL == env ) {
return OPAL_ERR_BAD_PARAM;
} else if (NULL == *env) {
i = 0;
@ -117,8 +118,7 @@ int opal_setenv(const char *name, const char *value, bool overwrite,
}
/* If this is the "environ" array, use putenv */
if (*env == environ) {
if( *env == environ ) {
/* THIS IS POTENTIALLY A MEMORY LEAK! But I am doing it
because so that we don't violate the law of least
astonishmet for OPAL developers (i.e., those that don't

Просмотреть файл

@ -39,14 +39,7 @@
#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;
static const char path_sep[] = OPAL_PATH_SEP;
int opal_os_dirpath_create(const char *path, const mode_t mode)
{
@ -172,7 +165,7 @@ int opal_os_dirpath_destroy(const char *path,
int rc, exit_status = OPAL_SUCCESS;
bool is_dir = false;
#ifndef WIN32
#ifndef __WINDOWS__
DIR *dp;
struct dirent *ep;
char *filenm;
@ -269,7 +262,6 @@ int opal_os_dirpath_destroy(const char *path,
*/
closedir(dp);
#else
bool empty = false;
char search_path[MAX_PATH];
HANDLE file;
WIN32_FIND_DATA file_data;
@ -280,11 +272,11 @@ int opal_os_dirpath_destroy(const char *path,
}
strncpy(search_path, path, strlen(path)+1);
strncat (search_path, "\\*", 3);
strncat (search_path, OPAL_PATH_SEP"*", 3);
file = FindFirstFile(search_path, &file_data);
if (INVALID_HANDLE_VALUE == file) {
FindClose(&file_data);
FindClose(file);
return OPAL_ERROR;
}
@ -333,17 +325,9 @@ int opal_os_dirpath_destroy(const char *path,
else {
DeleteFile(file_name);
}
/*
* Are we done yet?
*/
if (0 == FindNextFile(file, &file_data)) {
empty = true;
}
} while(!empty);
} while( 0 != FindNextFile(file, &file_data) );
FindClose(&file_data);
FindClose(file);
#endif
cleanup:
@ -384,6 +368,7 @@ bool opal_os_dirpath_is_empty(const char *path ) {
char search_path[MAX_PATH];
HANDLE file;
WIN32_FIND_DATA file_data;
bool found = false;
if (NULL != path) {
strncpy(search_path, path, strlen(path)+1);
@ -391,25 +376,19 @@ bool opal_os_dirpath_is_empty(const char *path ) {
file = FindFirstFile(search_path, &file_data);
if (INVALID_HANDLE_VALUE == file) {
FindClose(&file_data);
return true;
goto cleanup;
}
if (0 != strcmp(file_data.cFileName, ".") || 0 != strcmp(file_data.cFileName, "..")) {
FindClose(&file_data);
return false;
}
while (0 != FindNextFile(file, &file_data)) {
do {
if (0 != strcmp(file_data.cFileName, ".") || 0 != strcmp(file_data.cFileName, "..")) {
FindClose(&file_data);
return false;
found = true;
goto cleanup;
}
}
} while (0 != FindNextFile(file, &file_data));
}
FindClose(&file_data);
return true;
cleanup:
FindClose(file);
return found;
#endif /* ifndef __WINDOWS__ */
}

Просмотреть файл

@ -49,6 +49,10 @@
#include <sys/stat.h>
#endif
#if defined(c_plusplus) || defined(__cplusplus)
extern "C" {
#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
@ -115,4 +119,8 @@ OPAL_DECLSPEC int opal_os_dirpath_destroy(const char *path,
bool recursive,
opal_os_dirpath_destroy_callback_fn_t cbfunc);
#if defined(c_plusplus) || defined(__cplusplus)
}
#endif
#endif

Просмотреть файл

@ -34,20 +34,13 @@
#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;
static const char *path_sep = OPAL_PATH_SEP;
char *opal_os_path(bool relative, ...)
{
va_list ap, ap1;
char *element, *path;
int num_elements, total_length;
size_t num_elements, total_length;
va_start(ap, relative);
va_start(ap1, relative);
@ -112,5 +105,5 @@ char *opal_os_path(bool relative, ...)
va_end(ap);
va_end(ap1);
return(path);
return opal_make_filename_os_friendly(path);
}

Просмотреть файл

@ -38,13 +38,17 @@
*
*/
#ifndef OPAL_OS_PATH_H
#define OPAL_OS_PATH_H
#include "opal_config.h"
#include <stdio.h>
#include <stdarg.h>
#ifndef OPAL_OS_PATH_H
#define OPAL_OS_PATH_H
#if defined(c_plusplus) || defined(__cplusplus)
extern "C" {
#endif
/**
* @param relative A boolean that specifies if the path name is to be constructed
@ -63,4 +67,40 @@
*/
OPAL_DECLSPEC char *opal_os_path(bool relative, ...);
/**
* Convert the path to be OS friendly. On UNIX this function will
* be empty, when on Windows it will convert all '/' to '\\' and
* eventually remove the '/cygdrive/' from the beginning of the
* path (if the configure was runned under Cygwin).
*/
#if defined(__WINDOWS__)
static inline char* opal_make_filename_os_friendly( char* filename )
{
char* p = filename;
size_t length;
if( NULL == filename )
return NULL;
length = strlen(filename);
if( strncmp( filename, "/cygdrive/", 10 ) == 0 ) {
memmove( filename + 1, filename + 10, length - 10 );
filename[0] = filename[1];
filename[1] = ':';
filename[length - 10 + 1] = '\0';
}
for( ; *p != '\0'; p++ ) {
if( *p == '/' )
*p = '\\';
}
return filename;
}
#else
#define opal_make_filename_os_friendly(PATH) (PATH)
#endif /* defined(__WINDOWS__) */
#if defined(c_plusplus) || defined(__cplusplus)
}
#endif
#endif /* OPAL_OS_PATH_H */

Просмотреть файл

@ -24,22 +24,14 @@
#endif
#include "opal/util/path.h"
#include "opal/util/os_path.h"
#include "opal/util/argv.h"
/*
* PATH environment variable separator
*/
#ifdef __WINDOWS__
#define PATHENVSEP ';'
#else
#define PATHENVSEP ':'
#endif
#include "opal/util/output.h"
static void path_env_load(char *path, int *pargc, char ***pargv);
static char *path_access(char *fname, char *path, int mode);
static char *list_env_get(char *var, char **list);
/**
* Locates a file with certain permissions
*/
@ -53,7 +45,7 @@ char *opal_path_find(char *fname, char **pathv, int mode, char **envv)
/* If absolute path is given, return it without searching. */
if ('/' == *fname) {
if (OPAL_PATH_SEP[0] == *fname) {
return path_access(fname, "", mode);
}
@ -68,15 +60,14 @@ char *opal_path_find(char *fname, char **pathv, int mode, char **envv)
while (pathv[i] && NULL == fullpath) {
/* Replace environment variable at the head of the string. */
if ('$' == *pathv[i]) {
delimit = strchr(pathv[i], '/');
delimit = strchr(pathv[i], OPAL_PATH_SEP[0]);
if (delimit) {
*delimit = '\0';
}
env = list_env_get(pathv[i]+1, envv);
if (delimit) {
*delimit = '/';
*delimit = OPAL_PATH_SEP[0];
}
if (NULL != env) {
if (!delimit) {
@ -98,7 +89,7 @@ char *opal_path_find(char *fname, char **pathv, int mode, char **envv)
}
i++;
}
return fullpath;
return opal_make_filename_os_friendly(fullpath);
}
/*
@ -170,19 +161,9 @@ static char *path_access(char *fname, char *path, int mode)
char *fullpath;
/* Allocate space for the full pathname. */
fullpath = (char*) malloc(strlen(path) + strlen(fname) + 2);
if (NULL == fullpath) {
fullpath = opal_os_path( false, path, fname, NULL );
if( NULL == fullpath )
return NULL;
}
if (strlen(path) > 0) {
strcpy(fullpath, path);
strcat(fullpath, "/");
strcat(fullpath, fname);
} else {
strcpy(fullpath, fname);
}
/* Get status on the full path name to check for existance. Then
check the permissions. */
@ -222,7 +203,7 @@ static void path_env_load(char *path, int *pargc, char ***pargv)
/* Locate the delimiter. */
for (p = path; *p && (*p != PATHENVSEP); ++p) {
for (p = path; *p && (*p != OPAL_ENV_SEP); ++p) {
continue;
}
@ -258,7 +239,7 @@ static void path_env_load(char *path, int *pargc, char ***pargv)
*/
static char *list_env_get(char *var, char **list)
{
int n;
size_t n;
if (NULL != list) {
n = strlen(var);

Просмотреть файл

@ -29,27 +29,35 @@ static __inline char* getenv (const char *name)
{
int ret;
char *buffer;
DWORD length = GetEnvironmentVariable( (LPCSTR)((void*)name), NULL, 0 );
DWORD length = GetEnvironmentVariable( (LPCSTR)name, NULL, 0 );
if( 0 == length ) return NULL;
buffer = (char *)malloc(sizeof(char) * length);
ret = GetEnvironmentVariable((LPCSTR)((void*)name), (LPSTR)((void*)buffer), length);
ret = GetEnvironmentVariable((LPCSTR)name, (LPSTR)buffer, length);
return (ret > 0) ? buffer: NULL;
}
static __inline int setenv (const char *name, const char *value, int rewrite) {
static __inline int setenv (const char *name, const char *value, int rewrite)
{
int ret;
if( 0 == rewrite ) {
DWORD length = 0;
if( 0 == (length = GetEnvironmentVariable( (LPCSTR)name, NULL, length )) ) {
if( ERROR_ENVVAR_NOT_FOUND == GetLastError() ) { /* do not exist */
return 0;
}
}
}
/* just push it back to the windows thingy */
int ret = SetEnvironmentVariable ((LPCSTR)((void*)name), (LPCSTR)((void*)value));
ret = SetEnvironmentVariable ((LPCSTR)name, (LPCSTR)value);
return (0 != ret)? 1: 0;
}
static __inline unsigned int sleep(unsigned int seconds) {
/* microsoft sleep is in milliseconds. Note: interrupt beaviour has
not yet been handled */
Sleep(seconds * 100);
/* Allow interruptions */
SleepEx(seconds * 100, TRUE);
return 0;
}
@ -59,18 +67,16 @@ static __inline size_t sysconf(int option) {
SYSTEM_INFO sys_info;
/* hardcoded on windows ... The maximum limit seems to be 2048 but
* it requires a call to _setmaxstdio.
*/
if( _SC_OPEN_MAX == option )
return 512;
if( _SC_OPEN_MAX == option ) {
return _getmaxstdio();
}
GetSystemInfo(&sys_info);
if (_SC_PAGESIZE == option){
return (size_t)sys_info.dwPageSize;
}
printf( "This functionality is not supported: line: %d\tfile: %s\n",
__LINE__, __FILE__);
__LINE__, __FILE__ );
abort();
return 0;
}

Просмотреть файл

@ -27,17 +27,4 @@ static __inline int getpagesize(void)
return (int)sys_info.dwPageSize;
}
/**
* Case insensitive comparaison of strings.
*/
static __inline int strncasecmp( const char *s1, const char *s2, int n)
{
return _strnicmp( s1, s2, (size_t)n );
}
static __inline int strcasecmp(char *s1, char *s2)
{
return _stricmp(s1, s2);
}
#endif

Просмотреть файл

@ -116,13 +116,15 @@ typedef unsigned int uint;
#define close(FD) _close((FD))
#define unlink(PATH) _unlink((PATH))
#define dup2(OLDFD, NEWFD) _dup2((OLDFD), (NEWFD))
#define write(FD, BUF, SIZE) _write((FD), (BUF), (SIZE))
#define read(FD, BUF, SIZE) _read((FD), (BUF), (SIZE))
#define write(FD, BUF, SIZE) _write((FD), (BUF), (unsigned int)(SIZE))
#define read(FD, BUF, SIZE) _read((FD), (BUF), (unsigned int)(SIZE))
#define fileno(FD) _fileno((FD))
#define isatty(FD) _isatty((FD))
#define execvp _execvp
#define pipe(array_fd) _pipe(array_fd, 1024, O_BINARY )
#define S_ISDIR(STAT_MODE) ((STAT_MODE) & _S_IFDIR)
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#define UINT32_MAX _UI32_MAX
#define INT32_MAX _I32_MAX