summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/H5L.c114
-rw-r--r--src/H5Lpublic.h1
-rw-r--r--test/links.c7
3 files changed, 120 insertions, 2 deletions
diff --git a/src/H5L.c b/src/H5L.c
index 5927029..f8e7456 100644
--- a/src/H5L.c
+++ b/src/H5L.c
@@ -168,9 +168,14 @@ static herr_t H5L_delete_by_idx_cb(H5G_loc_t *grp_loc/*in*/, const char *name,
static herr_t H5L_move_cb(H5G_loc_t *grp_loc/*in*/, const char *name,
const H5O_link_t *lnk, H5G_loc_t *obj_loc, void *_udata/*in,out*/,
H5G_own_loc_t *own_loc/*out*/);
-static herr_t H5L_move_dest_cb(H5G_loc_t *grp_loc/*in*/,
- const char *name, const H5O_link_t *lnk, H5G_loc_t *obj_loc, void *_udata/*in,out*/,
+static herr_t H5L_move_dest_cb(H5G_loc_t *grp_loc/*in*/, const char *name,
+ const H5O_link_t *lnk, H5G_loc_t *obj_loc, void *_udata/*in,out*/,
H5G_own_loc_t *own_loc/*out*/);
+static herr_t H5L_exists_cb(H5G_loc_t *grp_loc/*in*/, const char *name,
+ const H5O_link_t *lnk, H5G_loc_t *obj_loc, void *_udata/*in,out*/,
+ H5G_own_loc_t *own_loc/*out*/);
+static htri_t H5L_exists(const H5G_loc_t *loc, const char *name, hid_t lapl_id,
+ hid_t dxpl_id);
static herr_t H5L_get_info_cb(H5G_loc_t *grp_loc/*in*/, const char *name,
const H5O_link_t *lnk, H5G_loc_t *obj_loc, void *_udata/*in,out*/,
H5G_own_loc_t *own_loc/*out*/);
@@ -832,6 +837,47 @@ done:
/*-------------------------------------------------------------------------
+ * Function: H5Lexists
+ *
+ * Purpose: Checks if a link of a given name exists in a group
+ *
+ * Return: Success: TRUE/FALSE
+ * Failure: Negative
+ *
+ * Programmer: Quincey Koziol
+ * Friday, March 16, 2007
+ *
+ *-------------------------------------------------------------------------
+ */
+herr_t
+H5Lexists(hid_t loc_id, const char *name, hid_t lapl_id)
+{
+ H5G_loc_t loc;
+ herr_t ret_value = SUCCEED;
+
+ FUNC_ENTER_API(H5Lexists, FAIL)
+
+ /* Check arguments */
+ if(H5G_loc(loc_id, &loc))
+ HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a location")
+ if(!name || !*name)
+ HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "no name specified")
+ if(H5P_DEFAULT == lapl_id)
+ lapl_id = H5P_LINK_ACCESS_DEFAULT;
+ else
+ if(TRUE != H5P_isa_class(lapl_id, H5P_LINK_ACCESS))
+ HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not link access property list ID")
+
+ /* Check for the existence of the link */
+ if((ret_value = H5L_exists(&loc, name, lapl_id, H5AC_ind_dxpl_id)) < 0)
+ HGOTO_ERROR(H5E_SYM, H5E_NOTFOUND, FAIL, "unable to get link info")
+
+done:
+ FUNC_LEAVE_API(ret_value)
+} /* end H5Lexists() */
+
+
+/*-------------------------------------------------------------------------
* Function: H5Lget_info
*
* Purpose: Gets metadata for a link.
@@ -2464,6 +2510,70 @@ done:
/*-------------------------------------------------------------------------
+ * Function: H5L_exists_cb
+ *
+ * Purpose: Callback for checking whether a link exists
+ *
+ * Return: Non-negative on success/Negative on failure
+ *
+ * Programmer: Quincey Koziol
+ * Friday, March 16 2007
+ *
+ *-------------------------------------------------------------------------
+ */
+static herr_t
+H5L_exists_cb(H5G_loc_t UNUSED *grp_loc/*in*/, const char UNUSED *name,
+ const H5O_link_t *lnk, H5G_loc_t UNUSED *obj_loc, void *_udata/*in,out*/,
+ H5G_own_loc_t *own_loc/*out*/)
+{
+ hbool_t *udata = (hbool_t *)_udata; /* User data passed in */
+
+ FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5L_exists_cb)
+
+ /* Check if the name in this group resolved to a valid link */
+ *udata = (lnk != NULL);
+
+ /* Indicate that this callback didn't take ownership of the group *
+ * location for the object */
+ *own_loc = H5G_OWN_NONE;
+
+ FUNC_LEAVE_NOAPI(SUCCEED)
+} /* end H5L_exists_cb() */
+
+
+/*-------------------------------------------------------------------------
+ * Function: H5L_exists
+ *
+ * Purpose: Returns whether a link exists in a group
+ *
+ * Return: Non-negative (TRUE/FALSE) on success/Negative on failure
+ *
+ * Programmer: Quincey Koziol
+ * Friday, March 16 2007
+ *
+ *-------------------------------------------------------------------------
+ */
+static htri_t
+H5L_exists(const H5G_loc_t *loc, const char *name, hid_t lapl_id, hid_t dxpl_id)
+{
+ hbool_t exists = FALSE; /* Whether the link exists in the group */
+ herr_t ret_value = SUCCEED; /* Return value */
+
+ FUNC_ENTER_NOAPI_NOINIT(H5L_exists)
+
+ /* Traverse the group hierarchy to locate the object to get info about */
+ if(H5G_traverse(loc, name, H5G_TARGET_SLINK|H5G_TARGET_UDLINK, H5L_exists_cb, &exists, lapl_id, dxpl_id) < 0)
+ HGOTO_ERROR(H5E_SYM, H5E_EXISTS, FAIL, "path doesn't exist")
+
+ /* Set return value */
+ ret_value = exists;
+
+done:
+ FUNC_LEAVE_NOAPI(ret_value)
+} /* H5L_exists() */
+
+
+/*-------------------------------------------------------------------------
* Function: H5L_get_info_cb
*
* Purpose: Callback for retrieving a link's metadata
diff --git a/src/H5Lpublic.h b/src/H5Lpublic.h
index 7faeb8e..2c9331f 100644
--- a/src/H5Lpublic.h
+++ b/src/H5Lpublic.h
@@ -152,6 +152,7 @@ H5_DLL herr_t H5Lget_val(hid_t loc_id, const char *name, void *buf/*out*/,
H5_DLL herr_t H5Lget_val_by_idx(hid_t loc_id, const char *group_name,
H5_index_t idx_type, H5_iter_order_t order, hsize_t n,
void *buf/*out*/, size_t size, hid_t lapl_id);
+H5_DLL htri_t H5Lexists(hid_t loc_id, const char *name, hid_t lapl_id);
H5_DLL herr_t H5Lget_info(hid_t loc_id, const char *name,
H5L_info_t *linfo /*out*/, hid_t lapl_id);
H5_DLL herr_t H5Lget_info_by_idx(hid_t loc_id, const char *group_name,
diff --git a/test/links.c b/test/links.c
index eb7b596..ff6a8b8 100644
--- a/test/links.c
+++ b/test/links.c
@@ -310,6 +310,8 @@ cklinks(hid_t fapl, hbool_t new_format)
puts(" expected file location.");
TEST_ERROR
}
+ if(H5Lexists(file, "d1", H5P_DEFAULT) != TRUE) TEST_ERROR
+ if(H5Lexists(file, "grp1/hard", H5P_DEFAULT) != TRUE) TEST_ERROR
/* Symbolic link */
if (H5Gget_objinfo(file, "grp1/soft", TRUE, &sb2) < 0) TEST_ERROR
@@ -330,6 +332,7 @@ cklinks(hid_t fapl, hbool_t new_format)
puts(" Soft link test failed. Wrong link value");
TEST_ERROR
}
+ if(H5Lexists(file, "grp1/soft", H5P_DEFAULT) != TRUE) TEST_ERROR
/* Dangling link */
H5E_BEGIN_TRY {
@@ -356,6 +359,7 @@ cklinks(hid_t fapl, hbool_t new_format)
puts(" Dangling link test failed. Wrong link value");
TEST_ERROR
}
+ if(H5Lexists(file, "grp1/dangle", H5P_DEFAULT) != TRUE) TEST_ERROR
/* Recursive link */
H5E_BEGIN_TRY {
@@ -383,6 +387,9 @@ cklinks(hid_t fapl, hbool_t new_format)
TEST_ERROR
}
+ /* Non-existant link */
+ if(H5Lexists(file, "foobar", H5P_DEFAULT) == TRUE) TEST_ERROR
+
/* Cleanup */
if (H5Fclose(file) < 0) TEST_ERROR
PASSED();