summaryrefslogtreecommitdiffstats
path: root/src/H5Dint.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/H5Dint.c')
-rw-r--r--src/H5Dint.c115
1 files changed, 112 insertions, 3 deletions
diff --git a/src/H5Dint.c b/src/H5Dint.c
index e48aabc..0e0843f 100644
--- a/src/H5Dint.c
+++ b/src/H5Dint.c
@@ -65,6 +65,8 @@ static herr_t H5D__init_type(H5F_t *file, const H5D_t *dset, hid_t type_id,
static herr_t H5D__init_space(H5F_t *file, const H5D_t *dset, const H5S_t *space);
static herr_t H5D__update_oh_info(H5F_t *file, hid_t dxpl_id, H5D_t *dset,
hid_t dapl_id);
+static herr_t H5D_build_extfile_prefix(const H5D_t *dset, hid_t dapl_id,
+ char **extfile_prefix);
static herr_t H5D__open_oid(H5D_t *dataset, hid_t dapl_id, hid_t dxpl_id);
static herr_t H5D__init_storage(const H5D_t *dataset, hbool_t full_overwrite,
hsize_t old_dim[], hid_t dxpl_id);
@@ -903,6 +905,84 @@ done:
} /* end H5D__update_oh_info() */
+/*--------------------------------------------------------------------------
+ * Function: H5D_build_extfile_prefix
+ *
+ * Purpose: Determine the external file prefix to be used and store
+ * it in extfile_prefix. Stores an empty string if no prefix
+ * should be used.
+ *
+ * Return: SUCCEED/FAIL
+ *
+ * Programmer: Steffen Kiess
+ * October 16, 2015
+ *--------------------------------------------------------------------------
+ */
+static herr_t
+H5D_build_extfile_prefix(const H5D_t *dset, hid_t dapl_id, char **extfile_prefix /*out*/)
+{
+ char *prefix = NULL; /* prefix used to look for the file */
+ char *extpath = NULL; /* absolute path of directory the HDF5 file is in */
+ size_t extpath_len; /* length of extpath */
+ size_t prefix_len; /* length of prefix */
+ size_t extfile_prefix_len; /* length of expanded prefix */
+ H5P_genplist_t *plist = NULL; /* Property list pointer */
+ herr_t ret_value = SUCCEED; /* Return value */
+
+
+ FUNC_ENTER_NOAPI_NOINIT
+
+ HDassert(dset);
+ HDassert(dset->oloc.file);
+
+ extpath = H5F_EXTPATH(dset->oloc.file);
+ HDassert(extpath);
+
+ /* XXX: Future thread-safety note - getenv is not required
+ * to be reentrant.
+ */
+ prefix = HDgetenv("HDF5_EXTFILE_PREFIX");
+
+ if(prefix == NULL || *prefix == '\0') {
+ /* Set prefix to value of H5D_ACS_EFILE_PREFIX_NAME property */
+ if(NULL == (plist = H5P_object_verify(dapl_id, H5P_DATASET_ACCESS)))
+ HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "can't find object for ID")
+ /* No error checking possible here */
+ prefix = (char *)H5P_peek_voidp(plist, H5D_ACS_EFILE_PREFIX_NAME);
+ } /* end if */
+
+ /* Prefix has to be checked for NULL / empty string again because the
+ * code above might have updated it.
+ */
+ if(prefix == NULL || *prefix == '\0' || HDstrcmp(prefix, ".") == 0) {
+ /* filename is interpreted as relative to the current directory,
+ * does not need to be expanded
+ */
+ if(NULL == (*extfile_prefix = (char *)H5MM_strdup("")))
+ HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
+ } /* end if */
+ else {
+ if (HDstrncmp(prefix, "${ORIGIN}", HDstrlen("${ORIGIN}")) == 0) {
+ /* Replace ${ORIGIN} at beginning of prefix by directory of HDF5 file */
+ extpath_len = HDstrlen(extpath);
+ prefix_len = HDstrlen(prefix);
+ extfile_prefix_len = extpath_len + prefix_len - HDstrlen("${ORIGIN}") + 1;
+
+ if(NULL == (*extfile_prefix = (char *)H5MM_malloc(extfile_prefix_len)))
+ HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "unable to allocate buffer")
+ HDsnprintf(*extfile_prefix, extfile_prefix_len, "%s%s", extpath, prefix + HDstrlen("${ORIGIN}"));
+ } /* end if */
+ else {
+ if(NULL == (*extfile_prefix = (char *)H5MM_strdup(prefix)))
+ HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
+ } /* end else */
+ } /* end else */
+
+done:
+ FUNC_LEAVE_NOAPI(ret_value)
+} /* H5D_build_extfile_prefix() */
+
+
/*-------------------------------------------------------------------------
* Function: H5D__create
*
@@ -1062,6 +1142,10 @@ H5D__create(H5F_t *file, hid_t type_id, const H5S_t *space, hid_t dcpl_id,
/* Indicate that the layout information was initialized */
layout_init = TRUE;
+ /* Set the external file prefix */
+ if(H5D_build_extfile_prefix(new_dset, dapl_id, &new_dset->shared->extfile_prefix) < 0)
+ HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, NULL, "unable to initialize external file prefix")
+
/* Add the dataset to the list of opened objects in the file */
if(H5FO_top_incr(new_dset->oloc.file, new_dset->oloc.addr) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTINC, NULL, "can't incr object ref. count")
@@ -1095,6 +1179,7 @@ done:
} /* end if */
if(new_dset->shared->dcpl_id != 0 && H5I_dec_ref(new_dset->shared->dcpl_id) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CANTDEC, NULL, "unable to decrement ref count on property list")
+ new_dset->shared->extfile_prefix = (char *)H5MM_xfree(new_dset->shared->extfile_prefix);
new_dset->shared = H5FL_FREE(H5D_shared_t, new_dset->shared);
} /* end if */
new_dset->oloc.file = NULL;
@@ -1125,7 +1210,8 @@ H5D_open(const H5G_loc_t *loc, hid_t dapl_id, hid_t dxpl_id)
{
H5D_shared_t *shared_fo = NULL;
H5D_t *dataset = NULL;
- H5D_t *ret_value; /* Return value */
+ char *extfile_prefix = NULL; /* Expanded external file prefix */
+ H5D_t *ret_value = NULL; /* Return value */
FUNC_ENTER_NOAPI(NULL)
@@ -1144,6 +1230,10 @@ H5D_open(const H5G_loc_t *loc, hid_t dapl_id, hid_t dxpl_id)
if(H5G_name_copy(&(dataset->path), loc->path, H5_COPY_SHALLOW) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTCOPY, NULL, "can't copy path")
+ /* Get the external file prefix */
+ if(H5D_build_extfile_prefix(dataset, dapl_id, &extfile_prefix) < 0)
+ HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, NULL, "unable to initialize external file prefix")
+
/* Check if dataset was already open */
if(NULL == (shared_fo = (H5D_shared_t *)H5FO_opened(dataset->oloc.file, dataset->oloc.addr))) {
/* Clear any errors from H5FO_opened() */
@@ -1163,6 +1253,12 @@ H5D_open(const H5G_loc_t *loc, hid_t dapl_id, hid_t dxpl_id)
/* We're the first dataset to use the the shared info */
dataset->shared->fo_count = 1;
+
+ /* Set the external file prefix */
+ dataset->shared->extfile_prefix = extfile_prefix;
+ /* Prevent string from being freed during done: */
+ extfile_prefix = NULL;
+
} /* end if */
else {
/* Point to shared info */
@@ -1171,6 +1267,12 @@ H5D_open(const H5G_loc_t *loc, hid_t dapl_id, hid_t dxpl_id)
/* Increment # of datasets using shared information */
shared_fo->fo_count++;
+ /* Check whether the external file prefix of the already open dataset
+ * matches the new external file prefix
+ */
+ if(HDstrcmp(extfile_prefix, dataset->shared->extfile_prefix) != 0)
+ HGOTO_ERROR(H5E_DATASET, H5E_CANTOPENOBJ, NULL, "new external file prefix does not match external file prefix of already open dataset")
+
/* Check if the object has been opened through the top file yet */
if(H5FO_top_count(dataset->oloc.file, dataset->oloc.addr) == 0) {
/* Open the object through this top file */
@@ -1187,11 +1289,15 @@ H5D_open(const H5G_loc_t *loc, hid_t dapl_id, hid_t dxpl_id)
ret_value = dataset;
done:
+ extfile_prefix = (char *)H5MM_xfree(extfile_prefix);
+
if(ret_value == NULL) {
/* Free the location--casting away const*/
if(dataset) {
- if(shared_fo == NULL) /* Need to free shared fo */
+ if(shared_fo == NULL && dataset->shared) { /* Need to free shared fo */
+ dataset->shared->extfile_prefix = (char *)H5MM_xfree(dataset->shared->extfile_prefix);
dataset->shared = H5FL_FREE(H5D_shared_t, dataset->shared);
+ }
H5O_loc_free(&(dataset->oloc));
H5G_name_free(&(dataset->path));
@@ -1452,11 +1558,14 @@ H5D_close(H5D_t *dataset)
#endif /* NDEBUG */
} /* end switch */ /*lint !e788 All appropriate cases are covered */
+ /* Free the external file prefix */
+ dataset->shared->extfile_prefix = (char *)H5MM_xfree(dataset->shared->extfile_prefix);
+
/*
* Release datatype, dataspace and creation property list -- there isn't
* much we can do if one of these fails, so we just continue.
*/
- free_failed = (unsigned)(H5I_dec_ref(dataset->shared->type_id) < 0 || H5S_close(dataset->shared->space) < 0 ||
+ free_failed |= (unsigned)(H5I_dec_ref(dataset->shared->type_id) < 0 || H5S_close(dataset->shared->space) < 0 ||
H5I_dec_ref(dataset->shared->dcpl_id) < 0);
/* Remove the dataset from the list of opened objects in the file */