diff options
Diffstat (limited to 'src/H5PL.c')
-rw-r--r-- | src/H5PL.c | 346 |
1 files changed, 299 insertions, 47 deletions
@@ -4,13 +4,12 @@ * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * - * the files COPYING and Copyright.html. COPYING can be found at the root * - * of the source code distribution tree; Copyright.html can be found at the * - * root level of an installed copy of the electronic document set and is * - * linked from the top-level documents page. It can also be found at * - * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have access * - * to either file, you may request a copy from help@hdfgroup.org. * + * the COPYING file, which can be found at the root of the source code * + * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. * + * If you do not have access to either file, you may request a copy from * + * help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +#define H5PL_PACKAGE /*suppress error about including H5PLpkg */ /****************/ /* Module Setup */ @@ -26,15 +25,33 @@ #include "H5private.h" /* Generic Functions */ #include "H5Eprivate.h" /* Error handling */ #include "H5MMprivate.h" /* Memory management */ -#include "H5PLprivate.h" /* Plugin */ +#include "H5PLpkg.h" /* Plugin */ #include "H5Zprivate.h" /* Filter pipeline */ /****************/ /* Local Macros */ /****************/ - -#define H5PL_MAX_PATH_NUM 16 +#ifdef H5_HAVE_WIN32_API +#define H5PL_EXPAND_ENV_VAR { \ + long bufCharCount; \ + char *tempbuf; \ + if(NULL == (tempbuf = (char *)H5MM_malloc(H5PL_EXPAND_BUFFER_SIZE))) \ + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for expanded path") \ + if((bufCharCount = ExpandEnvironmentStringsA(dl_path, tempbuf, H5PL_EXPAND_BUFFER_SIZE)) > H5PL_EXPAND_BUFFER_SIZE) { \ + tempbuf = (char *)H5MM_xfree(tempbuf); \ + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "expanded path is too long") \ + } \ + if(bufCharCount == 0) { \ + tempbuf = (char *)H5MM_xfree(tempbuf); \ + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "failed to expand path") \ + } \ + dl_path = (char *)H5MM_xfree(dl_path); \ + dl_path = tempbuf; \ + } +#else +#define H5PL_EXPAND_ENV_VAR +#endif /* H5_HAVE_WIN32_API */ /****************************/ /* Macros for supporting @@ -73,7 +90,7 @@ #define H5PL_CLR_ERROR /* maximum size for expanding env vars */ -#define EXPAND_BUFFER_SIZE 32767 +#define H5PL_EXPAND_BUFFER_SIZE 32767 typedef const void *(__cdecl *H5PL_get_plugin_info_t)(void); @@ -86,7 +103,7 @@ typedef const void *(__cdecl *H5PL_get_plugin_info_t)(void); #define H5PL_HANDLE void * /* Get a handle to a plugin library. Windows: TEXT macro handles Unicode strings */ -#define H5PL_OPEN_DLIB(S) dlopen(S, RTLD_NOW) +#define H5PL_OPEN_DLIB(S) dlopen(S, RTLD_LAZY) /* Get the address of a symbol in dynamic library */ #define H5PL_GET_LIB_FUNC(H,N) dlsym(H,N) @@ -95,7 +112,7 @@ typedef const void *(__cdecl *H5PL_get_plugin_info_t)(void); #define H5PL_CLOSE_LIB(H) dlclose(H) /* Clear error */ -#define H5PL_CLR_ERROR dlerror() +#define H5PL_CLR_ERROR HERROR(H5E_PLUGIN, H5E_CANTGET, "can't dlopen:%s", dlerror()) typedef const void *(*H5PL_get_plugin_info_t)(void); #endif /* H5_HAVE_WIN32_API */ @@ -212,10 +229,13 @@ H5PLset_loading_state(unsigned int plugin_type) { char *preload_path; herr_t ret_value = SUCCEED; /* Return value */ + FUNC_ENTER_API(FAIL) H5TRACE1("e", "Iu", plugin_type); + /* change the bit value of the requested plugin type(s) */ H5PL_plugin_g = plugin_type; + /* check if special ENV variable is set and disable all plugin types */ if(NULL != (preload_path = HDgetenv("HDF5_PLUGIN_PRELOAD"))) { /* Special symbol "::" means no plugin during data reading. */ @@ -243,12 +263,14 @@ herr_t H5PLget_loading_state(unsigned int *plugin_type) { herr_t ret_value = SUCCEED; /* Return value */ + FUNC_ENTER_API(FAIL) H5TRACE1("e", "*Iu", plugin_type); if(plugin_type) *plugin_type = H5PL_plugin_g; - done: + +done: FUNC_LEAVE_API(ret_value) } /* end H5PLget_loading_state() */ @@ -375,6 +397,258 @@ done: /*------------------------------------------------------------------------- + * Function: H5PLappend + * + * Purpose: Insert a plugin path at the end of the list. + * + * Return: Non-negative or success. + * + *------------------------------------------------------------------------- + */ +herr_t +H5PLappend(const char *plugin_path) +{ + herr_t ret_value = SUCCEED; /* Return value */ + char *dl_path = NULL; + + FUNC_ENTER_API(FAIL) + H5TRACE1("e", "*s", plugin_path); + if(H5PL_num_paths_g == H5PL_MAX_PATH_NUM) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "too many directories in path for table") + if(NULL == plugin_path) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "no path provided") + if(NULL == (dl_path = H5MM_strdup(plugin_path))) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path") + + H5PL_EXPAND_ENV_VAR + + H5PL_path_table_g[H5PL_num_paths_g] = dl_path; + H5PL_num_paths_g++; + +done: + FUNC_LEAVE_API(ret_value) +} /* end H5PLappend() */ + + +/*------------------------------------------------------------------------- + * Function: H5PLprepend + * + * Purpose: Insert a plugin path at the beginning of the list. + * + * Return: Non-negative or success. + * + *------------------------------------------------------------------------- + */ +herr_t +H5PLprepend(const char *plugin_path) +{ + herr_t ret_value = SUCCEED; /* Return value */ + char *dl_path = NULL; + unsigned int plindex; + + FUNC_ENTER_API(FAIL) + H5TRACE1("e", "*s", plugin_path); + if(H5PL_num_paths_g == H5PL_MAX_PATH_NUM) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "too many directories in path for table") + if(NULL == plugin_path) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "no path provided") + if(NULL == (dl_path = H5MM_strdup(plugin_path))) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path") + + H5PL_EXPAND_ENV_VAR + + for (plindex = (unsigned int)H5PL_num_paths_g; plindex > 0; plindex--) + H5PL_path_table_g[plindex] = H5PL_path_table_g[plindex - 1]; + H5PL_path_table_g[0] = dl_path; + H5PL_num_paths_g++; + +done: + FUNC_LEAVE_API(ret_value) +} /* end H5PLprepend() */ + + +/*------------------------------------------------------------------------- + * Function: H5PLreplace + * + * Purpose: Replace the path at the specified index. + * + * Return: Non-negative or success. + * + *------------------------------------------------------------------------- + */ +herr_t +H5PLreplace(const char *plugin_path, unsigned int index) +{ + herr_t ret_value = SUCCEED; /* Return value */ + char *dl_path = NULL; + + FUNC_ENTER_API(FAIL) + H5TRACE2("e", "*sIu", plugin_path, index); + if(NULL == plugin_path) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "no path provided") + if(index >= H5PL_MAX_PATH_NUM) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "index path out of bounds for table") + if(NULL == (dl_path = H5MM_strdup(plugin_path))) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path") + + H5PL_EXPAND_ENV_VAR + + if(H5PL_path_table_g[index]) + H5PL_path_table_g[index] = (char *)H5MM_xfree(H5PL_path_table_g[index]); + H5PL_path_table_g[index] = dl_path; + +done: + FUNC_LEAVE_API(ret_value) +} /* end H5PLreplace() */ + + +/*------------------------------------------------------------------------- + * Function: H5PLinsert + * + * Purpose: Insert a plugin path at the specified index, moving other paths after the index. + * + * Return: Non-negative or success. + * + *------------------------------------------------------------------------- + */ +herr_t +H5PLinsert(const char *plugin_path, unsigned int index) +{ + herr_t ret_value = SUCCEED; /* Return value */ + char *dl_path = NULL; + unsigned int plindex; + + FUNC_ENTER_API(FAIL) + H5TRACE2("e", "*sIu", plugin_path, index); + if(H5PL_num_paths_g == H5PL_MAX_PATH_NUM) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "too many directories in path for table") + if(NULL == plugin_path) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "no path provided") + if(index >= H5PL_MAX_PATH_NUM) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "index path out of bounds for table") + if(NULL == (dl_path = H5MM_strdup(plugin_path))) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path") + + H5PL_EXPAND_ENV_VAR + + for(plindex = (unsigned int)H5PL_num_paths_g; plindex > index; plindex--) + H5PL_path_table_g[plindex] = H5PL_path_table_g[plindex - 1]; + H5PL_path_table_g[index] = dl_path; + H5PL_num_paths_g++; + +done: + FUNC_LEAVE_API(ret_value) +} /* end H5PLinsert() */ + + +/*------------------------------------------------------------------------- + * Function: H5PLremove + * + * Purpose: Remove the plugin path at the specifed index and compacting the list. + * + * Return: Non-negative or success. + * + *------------------------------------------------------------------------- + */ +herr_t +H5PLremove(unsigned int index) +{ + herr_t ret_value = SUCCEED; /* Return value */ + unsigned int plindex; + + FUNC_ENTER_API(FAIL) + H5TRACE1("e", "Iu", index); + if(H5PL_num_paths_g == 0) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "no directories in table") + if(index >= H5PL_MAX_PATH_NUM) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "index path out of bounds for table") + if(NULL == H5PL_path_table_g[index]) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "no directory path at index") + H5PL_path_table_g[index] = (char *)H5MM_xfree(H5PL_path_table_g[index]); + + H5PL_num_paths_g--; + for(plindex = index; plindex < (unsigned int)H5PL_num_paths_g; plindex++) + H5PL_path_table_g[plindex] = H5PL_path_table_g[plindex + 1]; + H5PL_path_table_g[H5PL_num_paths_g] = NULL; + +done: + FUNC_LEAVE_API(ret_value) +} /* end H5PLremove() */ + + +/*------------------------------------------------------------------------- + * Function: H5PLget + * + * Purpose: Query the plugin path at the specified index. + * + * Return: Success: The length of path. + * + * If `pathname' is non-NULL then write up to `size' bytes into that + * buffer and always return the length of the pathname. + * Otherwise `size' is ignored and the function does not store the pathname, + * just returning the number of characters required to store the pathname. + * If an error occurs then the buffer pointed to by `pathname' (NULL or non-NULL) + * is unchanged and the function returns a negative value. + * If a zero is returned for the name's length, then there is no pathname + * associated with the index. + * + *------------------------------------------------------------------------- + */ +ssize_t +H5PLget(unsigned int index, char *pathname/*out*/, size_t size) +{ + ssize_t ret_value = 0; /* Return value */ + size_t len = 0; /* Length of pathname */ + char *dl_path = NULL; + + FUNC_ENTER_API(FAIL) + H5TRACE3("Zs", "Iuxz", index, pathname, size); + if(H5PL_num_paths_g == 0) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "no directories in table") + if(index >= H5PL_MAX_PATH_NUM) + HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "index path out of bounds for table") + if(NULL == (dl_path = H5PL_path_table_g[index])) + HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "no directory path at index") + len = HDstrlen(dl_path); + if(pathname) { + HDstrncpy(pathname, dl_path, MIN((size_t)(len + 1), size)); + if((size_t)len >= size) + pathname[size - 1] = '\0'; + } /* end if */ + + /* Set return value */ + ret_value = (ssize_t)len; + +done: + FUNC_LEAVE_API(ret_value) +} /* end H5PLget() */ + + +/*------------------------------------------------------------------------- + * Function: H5PLsize + * + * Purpose: Query the size of the current list of plugin paths. + * + * Return: Plugin path size + * + *------------------------------------------------------------------------- + */ +herr_t +H5PLsize(unsigned int *listsize) +{ + herr_t ret_value = SUCCEED; /* Return value */ + + FUNC_ENTER_API(FAIL) + H5TRACE1("e", "*Iu", listsize); + + *listsize = (unsigned int)H5PL_num_paths_g; + +done: + FUNC_LEAVE_API(ret_value) +} /* end H5PLsize() */ + + +/*------------------------------------------------------------------------- * Function: H5PL__init_path_table * * Purpose: Initialize the path table. @@ -401,24 +675,13 @@ H5PL__init_path_table(void) */ origin_dl_path = HDgetenv("HDF5_PLUGIN_PATH"); if(NULL == origin_dl_path) - dl_path = HDstrdup(H5PL_DEFAULT_PATH); + dl_path = H5MM_strdup(H5PL_DEFAULT_PATH); else - dl_path = HDstrdup(origin_dl_path); + dl_path = H5MM_strdup(origin_dl_path); if(NULL == dl_path) HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path") -#ifdef H5_HAVE_WIN32_API - else { /* Expand windows env var*/ - long bufCharCount; - char tempbuf[EXPAND_BUFFER_SIZE]; - if((bufCharCount = ExpandEnvironmentStrings(dl_path, tempbuf, EXPAND_BUFFER_SIZE)) > EXPAND_BUFFER_SIZE) - HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "expanded path is too long") - if(bufCharCount == 0) - HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "failed to expand path") - dl_path = (char *)H5MM_xfree(dl_path); - dl_path = H5MM_strdup(tempbuf); - } -#endif H5_HAVE_WIN32_API + H5PL_EXPAND_ENV_VAR /* Put paths in the path table. They are separated by ":" */ dir = HDstrtok(dl_path, H5PL_PATH_SEPARATOR); @@ -426,7 +689,7 @@ H5PL__init_path_table(void) /* Check for too many directories in path */ if(H5PL_num_paths_g == H5PL_MAX_PATH_NUM) HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "too many directories in path for table") - if(NULL == (H5PL_path_table_g[H5PL_num_paths_g] = HDstrdup(dir))) + if(NULL == (H5PL_path_table_g[H5PL_num_paths_g] = H5MM_strdup(dir))) HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for path") H5PL_num_paths_g++; dir = HDstrtok(NULL, H5PL_PATH_SEPARATOR); @@ -508,14 +771,9 @@ H5PL__find(H5PL_type_t plugin_type, int type_id, char *dir, const void **info) /* Attempt to open the dynamic library as a filter library */ if((found_in_dir = H5PL__open(plugin_type, pathname, type_id, info)) < 0) HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed") - if(found_in_dir) { - /* Indicate success */ - HGOTO_DONE(TRUE) - } /* end if */ - else { - HDassert(pathname); - pathname = (char *)H5MM_xfree(pathname); - } + if(found_in_dir) + HGOTO_DONE(TRUE) /* Indicate success */ + pathname = (char *)H5MM_xfree(pathname); } /* end if */ } /* end while */ @@ -523,8 +781,7 @@ done: if(dirp) if(HDclosedir(dirp) < 0) HDONE_ERROR(H5E_FILE, H5E_CLOSEERROR, FAIL, "can't close directory: %s", HDstrerror(errno)) - if(pathname) - pathname = (char *)H5MM_xfree(pathname); + pathname = (char *)H5MM_xfree(pathname); FUNC_LEAVE_NOAPI(ret_value) } /* end H5PL__find() */ @@ -565,14 +822,9 @@ H5PL__find(H5PL_type_t plugin_type, int type_id, char *dir, const void **info) if((found_in_dir = H5PL__open(plugin_type, pathname, type_id, info)) < 0) HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "search in directory failed") - if(found_in_dir) { - /* Indicate success */ - HGOTO_DONE(TRUE) - } /* end if */ - else { - HDassert(pathname); - pathname = (char *)H5MM_xfree(pathname); - } + if(found_in_dir) + HGOTO_DONE(TRUE) /* Indicate success */ + pathname = (char *)H5MM_xfree(pathname); } /* end if */ } while(FindNextFileA(hFind, &fdFile)); /* Find the next file. */ @@ -707,7 +959,7 @@ H5PL__search_table(H5PL_type_t plugin_type, int type_id, const void **info) const H5Z_class2_t *plugin_info; if(NULL == (get_plugin_info = (H5PL_get_plugin_info_t)H5PL_GET_LIB_FUNC((H5PL_table_g[i]).handle, "H5PLget_plugin_info"))) - HGOTO_ERROR(H5E_DATATYPE, H5E_CANTGET, FAIL, "can't get function for H5PLget_plugin_info") + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTGET, FAIL, "can't get function for H5PLget_plugin_info") if(NULL == (plugin_info = (const H5Z_class2_t *)(*get_plugin_info)())) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTGET, FAIL, "can't get plugin info") |