/*------------------------------------------------------------------------- * Function: trav_print_visit_lnk * * Purpose: Callback for visiting link, when printing info * * Return: 0 on success, * -1 on failure *------------------------------------------------------------------------- */ static int trav_print_visit_lnk(const char *path, const H5L_info_t *linfo, void *udata) { trav_print_udata_t *print_udata = (trav_print_udata_t *)udata; /* Print appropriate information for the type of link */ switch(linfo->type) { case H5L_TYPE_SOFT: if(linfo->u.val_size > 0) { char *targbuf = (char*)HDmalloc(linfo->u.val_size + 1); if(targbuf) { if(H5Lget_val(print_udata->fid, path, targbuf, linfo->u.val_size + 1, H5P_DEFAULT) < 0) targbuf[0] = 0; printf(" %-10s %s -> %s\n", "link", path, targbuf); HDfree(targbuf); } } /* end if */ else printf(" %-10s %s ->\n", "link", path); break; case H5L_TYPE_EXTERNAL: if(linfo->u.val_size > 0) { char *targbuf = NULL; const char *filename = NULL; const char *objname = NULL; targbuf = (char*)HDmalloc(linfo->u.val_size + 1); if(targbuf) { if(H5Lget_val(print_udata->fid, path, targbuf, linfo->u.val_size + 1, H5P_DEFAULT) < 0) targbuf[0] = 0; if(H5Lunpack_elink_val(targbuf, linfo->u.val_size, NULL, &filename, &objname) >= 0) printf(" %-10s %s -> %s %s\n", "ext link", path, filename, objname); HDfree(targbuf); } } /* end if */ else printf(" %-10s %s ->\n", "ext link", path); break; case H5L_TYPE_HARD: /* Should be handled elsewhere */ return(-1); case H5L_TYPE_ERROR: case H5L_TYPE_MAX: default: printf(" %-10s %s -> ???\n", "unknown type of UD link", path); break; } /* end switch() */ return(0); } /* end trav_print_visit_lnk() */
//-------------------------------------------------------------------------- // Function: CommonFG::getLinkval ///\brief Returns the name of the object that the symbolic link points to. ///\param name - IN: Symbolic link to the object ///\param size - IN: Maximum number of characters of value to be returned ///\return Name of the object ///\exception H5::FileIException or H5::GroupIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- H5std_string CommonFG::getLinkval( const char* name, size_t size ) const { H5L_info_t linkinfo; char *value_C; // value in C string size_t val_size = size; H5std_string value = ""; herr_t ret_value; // if user doesn't provide buffer size, determine it if (size == 0) { ret_value = H5Lget_info(getLocId(), name, &linkinfo, H5P_DEFAULT); if( ret_value < 0 ) { throwException("getLinkval", "H5Lget_info to find buffer size failed"); } val_size = linkinfo.u.val_size; } // if link has value, retrieve the value, otherwise, return null string if (val_size > 0) { value_C = new char[val_size+1]; // temporary C-string for C API ret_value = H5Lget_val(getLocId(), name, value_C, val_size, H5P_DEFAULT); if( ret_value < 0 ) { throwException("getLinkval", "H5Lget_val failed"); } value = H5std_string(value_C); delete []value_C; } return(value); }
std::string H5SoftLink::getLinkValue() const { herr_t err; H5L_info_t info; char * buf = 0; std::string ret; err = H5Lget_info(getParent().getH5Id(), name.c_str(), &info, H5P_DEFAULT); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the link info")); } buf = new char[info.u.val_size]; err = H5Lget_val(getParent().getH5Id(), name.c_str(), static_cast<void *>(buf), info.u.val_size, H5P_DEFAULT); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the link target")); } ret = std::string(buf); delete[] buf; return ret; }
/*------------------------------------------------------------------------- * Function: trav_print_visit_lnk * * Purpose: Callback for visiting link, when printing info * * Return: 0 on success, -1 on failure * * Programmer: Quincey Koziol, [email protected] * * Date: September 6, 2007 * *------------------------------------------------------------------------- */ static int trav_print_visit_lnk(const char *path, const H5L_info_t *linfo, void *udata) { trav_print_udata_t *print_udata = (trav_print_udata_t *)udata; /* Print appropriate information for the type of link */ switch(linfo->type) { case H5L_TYPE_SOFT: if(linfo->u.val_size > 0) { char *targbuf = (char*)HDmalloc(linfo->u.val_size + 1); HDassert(targbuf); H5Lget_val(print_udata->fid, path, targbuf, linfo->u.val_size + 1, H5P_DEFAULT); printf(" %-10s %s -> %s\n", "link", path, targbuf); HDfree(targbuf); } /* end if */ else printf(" %-10s %s ->\n", "link", path); break; case H5L_TYPE_EXTERNAL: if(linfo->u.val_size > 0) { char *targbuf; const char *filename; const char *objname; targbuf = (char*)HDmalloc(linfo->u.val_size + 1); HDassert(targbuf); H5Lget_val(print_udata->fid, path, targbuf, linfo->u.val_size + 1, H5P_DEFAULT); H5Lunpack_elink_val(targbuf, linfo->u.val_size, NULL, &filename, &objname); printf(" %-10s %s -> %s %s\n", "ext link", path, filename, objname); HDfree(targbuf); } /* end if */ else printf(" %-10s %s ->\n", "ext link", path); break; default: printf(" %-10s %s -> ???\n", "unknown type of UD link", path); break; } /* end switch() */ return(0); } /* end trav_print_visit_lnk() */
int VsFilter::visitLinks(hid_t locId, const char* name, const H5L_info_t *linfo, void* opdata) { VsLog::debugLog() <<"VsFilter::visitLinks() - looking at object " <<name <<std::endl; switch (linfo->type) { case H5L_TYPE_HARD: { H5O_info_t objinfo; /* Stat the object */ if(H5Oget_info_by_name(locId, name, &objinfo, H5P_DEFAULT) < 0) { VsLog::errorLog() <<"VsFilter::visitLinks() - unable to open object with name " <<name <<std::endl; VsLog::errorLog() <<"VsFilter::visitLinks() - this object and all children will be dropped." <<std::endl; return 0; } switch(objinfo.type) { case H5O_TYPE_GROUP: return visitGroup( locId, name, opdata ); break; case H5O_TYPE_DATASET: return visitDataset( locId, name, opdata ); break; default: VsLog::debugLog() << "VsFilter::visitLinks: node '" << name << "' has an unknown type " << objinfo.type << std::endl; break; } } break; //end of case H5L_TYPE_HARD case H5L_TYPE_EXTERNAL: { char *targbuf = (char*) malloc( linfo->u.val_size ); if (H5Lget_val(locId, name, targbuf, linfo->u.val_size, H5P_DEFAULT) < 0) { VsLog::errorLog() <<"VsFilter::visitLinks() - unable to open external link with name " <<targbuf <<std::endl; VsLog::errorLog() <<"VsFilter::visitLinks() - this object and all children will be dropped." <<std::endl; return 0; } const char *filename; const char *targname; if (H5Lunpack_elink_val(targbuf, linfo->u.val_size, 0, &filename, &targname) < 0) { VsLog::errorLog() <<"VsFilter::visitLinks() - unable to open external file with name " <<filename <<std::endl; VsLog::errorLog() <<"VsFilter::visitLinks() - this object and all children will be dropped." <<std::endl; return 0; } VsLog::debugLog() << "VsFilter::visitLinks(): node '" << name << "' is an external link." << std::endl; VsLog::debugLog() << "VsFilter::visitLinks(): node '" << targname << "' is an external target group." << std::endl; free(targbuf); targbuf = NULL; // Get info of the linked object. H5O_info_t objinfo; #ifndef H5_USE_16_API hid_t obj_id = H5Oopen(locId, name, H5P_DEFAULT); #else hid_t obj_id = H5Oopen(locId, name); #endif if (obj_id < 0) { VsLog::errorLog() <<"VsFilter::visitLinks() - unable to get id for external object " <<name <<std::endl; VsLog::errorLog() <<"VsFilter::visitLinks() - this object and all children will be dropped." <<std::endl; return 0; } //Test-open the linked object if (H5Oget_info (obj_id, &objinfo) < 0) { VsLog::errorLog() <<"VsFilter::visitLinks() - unable to open external object " <<name <<std::endl; VsLog::errorLog() <<"VsFilter::visitLinks() - this object and all children will be dropped." <<std::endl; return 0; } //Close the linked object to release hdf5 id H5Oclose( obj_id ); //Finally, decide what to do depending on what type of object this is switch(objinfo.type) { case H5O_TYPE_GROUP: return visitGroup( locId, name, opdata ); break; case H5O_TYPE_DATASET: return visitDataset( locId, name, opdata ); break; default: VsLog::debugLog() << "VsFilter::visitLinks: node '" << name << "' has an unknown type " << objinfo.type << std::endl; break; } } break; //END OF CASE H5L_TYPE_EXTERNAL default: VsLog::debugLog() << "VsFilter::visitLinks: node '" << name << "' has an unknown object type " << linfo->type << std::endl; break; } return 0; }
/* * Class: hdf_hdf5lib_H5 * Method: H5Lget_value * Signature: (JLjava/lang/String;[Ljava/lang/String;J)I */ JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5Lget_1value (JNIEnv *env, jclass clss, jlong loc_id, jstring name, jobjectArray link_value, jlong access_id) { size_t buf_size; herr_t status; H5L_info_t infobuf; const char *lName; char *lValue; const char *file_name; const char *obj_name; jstring str; infobuf.type = -1; PIN_JAVA_STRING(name, lName); if (lName != NULL) { /* get the length of the link val */ status = H5Lget_info((hid_t)loc_id, lName, &infobuf, H5P_DEFAULT); if(status < 0) { h5libraryError(env); } /* end if */ else { buf_size = infobuf.u.val_size + 1;/* add extra space for the null terminator */ if(infobuf.type == H5L_TYPE_HARD) { h5JNIFatalError(env, "H5Lget_val: link is hard type"); } /* end if */ else { lValue = (char*)HDmalloc(sizeof(char) * buf_size); if (lValue == NULL) { h5outOfMemory(env, "H5Lget_val: malloc failed"); } /* end if */ else { status = H5Lget_val((hid_t)loc_id, lName, (void*)lValue, buf_size, (hid_t)access_id); if (status < 0) { h5libraryError(env); } /* end if */ else if(infobuf.type == H5L_TYPE_EXTERNAL) { status = H5Lunpack_elink_val(lValue, (size_t)infobuf.u.val_size, (unsigned*)NULL, &file_name, &obj_name); if (status < 0) { h5libraryError(env); } /* end if */ else { str = ENVPTR->NewStringUTF(ENVPAR obj_name); if (str == NULL) { h5JNIFatalError(env, "H5Lget_val: return string not created"); } /* end if */ else { ENVPTR->SetObjectArrayElement(ENVPAR link_value, 0, str); str = ENVPTR->NewStringUTF(ENVPAR file_name); if (str == NULL) { h5JNIFatalError(env, "H5Lget_val: return string not created"); } /* end if */ else { ENVPTR->SetObjectArrayElement(ENVPAR link_value, 1, str); } /* end else */ } /* end else */ } /* end else */ } /* end else if */ else { str = ENVPTR->NewStringUTF(ENVPAR lValue); if (str == NULL) { /* exception -- fatal JNI error */ h5JNIFatalError(env, "H5Lget_val: return string not created"); } /* end if */ else { ENVPTR->SetObjectArrayElement(ENVPAR link_value, 0, str); } /* end else */ } /* end else */ HDfree(lValue); } /* end else */ } /* end else */ } /* end else */ UNPIN_JAVA_STRING(name, lName); } return infobuf.type; } /* end Java_hdf_hdf5lib_H5_H5Lget_1val */
/*------------------------------------------------------------------------- * Function: H5tools_get_symlink_info * * Purpose: Get symbolic link (soft, external) info and its target object type (dataset, group, named datatype) and path, if exist * * Patameters: * - [IN] fileid : link file id * - [IN] linkpath : link path * - [OUT] link_info: returning target object info (h5tool_link_info_t) * * Return: * 2 : given pathname is object * 1 : Succed to get link info. * 0 : Detected as a dangling link * -1 : H5 API failed. * * NOTE: * link_info->trg_path must be freed out of this function * * Programmer: Jonathan Kim * * Date: Feb 8, 2010 *-------------------------------------------------------------------------*/ int H5tools_get_symlink_info(hid_t file_id, const char * linkpath, h5tool_link_info_t *link_info, hbool_t get_obj_type) { htri_t l_ret; H5O_info_t trg_oinfo; hid_t fapl = H5P_DEFAULT; hid_t lapl = H5P_DEFAULT; int ret = -1; /* init to fail */ /* init */ link_info->trg_type = H5O_TYPE_UNKNOWN; /* if path is root, return group type */ if(!HDstrcmp(linkpath,"/")) { link_info->trg_type = H5O_TYPE_GROUP; ret = 2; goto out; } /* check if link itself exist */ if(H5Lexists(file_id, linkpath, H5P_DEFAULT) <= 0) { if(link_info->opt.msg_mode == 1) parallel_print("Warning: link <%s> doesn't exist \n",linkpath); goto out; } /* end if */ /* get info from link */ if(H5Lget_info(file_id, linkpath, &(link_info->linfo), H5P_DEFAULT) < 0) { if(link_info->opt.msg_mode == 1) parallel_print("Warning: unable to get link info from <%s>\n",linkpath); goto out; } /* end if */ /* given path is hard link (object) */ if(link_info->linfo.type == H5L_TYPE_HARD) { ret = 2; goto out; } /* end if */ /* trg_path must be freed out of this function when finished using */ link_info->trg_path = (char*)HDcalloc(link_info->linfo.u.val_size, sizeof(char)); HDassert(link_info->trg_path); /* get link value */ if(H5Lget_val(file_id, linkpath, (void *)link_info->trg_path, link_info->linfo.u.val_size, H5P_DEFAULT) < 0) { if(link_info->opt.msg_mode == 1) parallel_print("Warning: unable to get link value from <%s>\n",linkpath); goto out; } /* end if */ /*----------------------------------------------------- * if link type is external link use different lapl to * follow object in other file */ if(link_info->linfo.type == H5L_TYPE_EXTERNAL) { if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) goto out; if(H5Pset_fapl_sec2(fapl) < 0) goto out; if((lapl = H5Pcreate(H5P_LINK_ACCESS)) < 0) goto out; if(H5Pset_elink_fapl(lapl, fapl) < 0) goto out; } /* end if */ /* Check for retrieving object info */ if(get_obj_type) { /*-------------------------------------------------------------- * if link's target object exist, get type */ /* check if target object exist */ l_ret = H5Oexists_by_name(file_id, linkpath, lapl); /* detect dangling link */ if(l_ret == FALSE) { ret = 0; goto out; } /* end if */ /* function failed */ else if(l_ret < 0) goto out; /* get target object info */ if(H5Oget_info_by_name(file_id, linkpath, &trg_oinfo, lapl) < 0) { if(link_info->opt.msg_mode == 1) parallel_print("Warning: unable to get object information for <%s>\n", linkpath); goto out; } /* end if */ /* check unknown type */ if(trg_oinfo.type < H5O_TYPE_GROUP || trg_oinfo.type >=H5O_TYPE_NTYPES) { if(link_info->opt.msg_mode == 1) parallel_print("Warning: target object of <%s> is unknown type\n", linkpath); goto out; } /* end if */ /* set target obj type to return */ link_info->trg_type = trg_oinfo.type; link_info->objno = trg_oinfo.addr; link_info->fileno = trg_oinfo.fileno; } /* end if */ else link_info->trg_type = H5O_TYPE_UNKNOWN; /* succeed */ ret = 1; out: if(fapl != H5P_DEFAULT) H5Pclose(fapl); if(lapl != H5P_DEFAULT) H5Pclose(lapl); return ret; } /* end H5tools_get_symlink_info() */