H5Object & H5ReferenceData::getData(const unsigned int size, const unsigned int * index) const { char * cdata = static_cast<char *>(data) + offset; void ** ref = 0; unsigned int pos = 0; unsigned int cumprod = 1; H5O_info_t info; H5Object * hobj; hid_t obj; hid_t file; ssize_t ssize; char * name = 0; std::string _name; for (unsigned int i = 0; i < size; i++) { pos += cumprod * index[i]; } if (pos >= totalSize) { throw H5Exception(__LINE__, __FILE__, _("Invalid index.")); } cdata += pos * (stride ? stride : dataSize); file = getFile().getH5Id(); ref = &(((void **)cdata)[0]); obj = H5Rdereference(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref); if (obj < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open object at the given position.")); } ssize = H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, 0, 0); name = new char[ssize + 1]; H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, name, ssize + 1); _name = std::string(name); delete[] name; H5Oget_info(obj, &info); switch (info.type) { case H5O_TYPE_GROUP: hobj = new H5Group(getParent(), obj, _name); break; case H5O_TYPE_DATASET: hobj = new H5Dataset(getParent(), obj, _name); break; case H5O_TYPE_NAMED_DATATYPE: hobj = new H5Type(getParent(), obj, _name); break; case H5O_TYPE_UNKNOWN: default: H5Oclose(obj); throw H5Exception(__LINE__, __FILE__, _("Unknown HDF5 object")); } return *hobj; }
/************************************************************ Operator function. Prints the name and type of the object being examined. ************************************************************/ static herr_t op_func_v1(hid_t loc_id, const char *name, const H5L_info_t *info, void *operator_data) { H5O_info_t oinfo; herr_t status = 0; int *pDataSetId = (int*)operator_data; hid_t obj = H5Oopen(loc_id, name, H5P_DEFAULT); if (obj < 0) { return -1; } /* * Get type of the object and return only datasetId * through operator_data. */ status = H5Oget_info(obj, &oinfo); if (status < 0) { H5Oclose(obj); return -1; } if (oinfo.type == H5O_TYPE_DATASET) { *pDataSetId = obj; } else { H5Oclose(obj); } return 0; }
extern hid_t get_attribute_handle(hid_t parent, char *name) { char buf[MAX_ATTR_NAME+1]; int nattr, i, len; hid_t aid; H5O_info_t object_info; if (parent < 0) { debug3("PROFILE: parent is not HDF5 object"); return -1; } H5Oget_info(parent, &object_info); nattr = object_info.num_attrs; for (i = 0; (nattr>0) && (i<nattr); i++) { aid = H5Aopen_by_idx(parent, ".", H5_INDEX_NAME, H5_ITER_INC, i, H5P_DEFAULT, H5P_DEFAULT); // Get the name of the attribute. len = H5Aget_name(aid, MAX_ATTR_NAME, buf); if (len < MAX_ATTR_NAME) { if (strcmp(buf, name) == 0) { return aid; } } H5Aclose(aid); } debug3("PROFILE: failed to find HDF5 attribute=%s\n", name); return -1; }
//-------------------------------------------------------------------------- // Function: H5Object::getNumAttrs ///\brief Returns the number of attributes attached to this HDF5 object. ///\return Number of attributes ///\exception H5::AttributeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- int H5Object::getNumAttrs() const { H5O_info_t oinfo; /* Object info */ if(H5Oget_info(getId(), &oinfo) < 0) throw AttributeIException(inMemFunc("getNumAttrs"), "H5Oget_info failed"); else return( (int)oinfo.num_attrs ); }
/* * Class: hdf_hdf5lib_H5 * Method: H5Oget_info * Signature: (J)Lhdf/hdf5lib/structs/H5O_info_t; */ JNIEXPORT jobject JNICALL Java_hdf_hdf5lib_H5_H5Oget_1info (JNIEnv *env, jclass clss, jlong loc_id) { herr_t status = -1; H5O_info_t infobuf; jvalue args[12]; jobject hdrinfobuf; jobject ihinfobuf1; jobject ihinfobuf2; jobject ret_obj = NULL; status = H5Oget_info((hid_t)loc_id, &infobuf); if (status < 0) { h5libraryError(env); } /* end if */ else { args[0].i = (jint)infobuf.hdr.version; args[1].i = (jint)infobuf.hdr.nmesgs; args[2].i = (jint)infobuf.hdr.nchunks; args[3].i = (jint)infobuf.hdr.flags; args[4].j = (jlong)infobuf.hdr.space.total; args[5].j = (jlong)infobuf.hdr.space.meta; args[6].j = (jlong)infobuf.hdr.space.mesg; args[7].j = (jlong)infobuf.hdr.space.free; args[8].j = (jlong)infobuf.hdr.mesg.present; args[9].j = (jlong)infobuf.hdr.mesg.shared; CALL_CONSTRUCTOR("hdf/hdf5lib/structs/H5O_hdr_info_t", "(IIIIJJJJJJ)V", args); hdrinfobuf = ret_obj; args[0].j = (jlong)infobuf.meta_size.obj.index_size; args[1].j = (jlong)infobuf.meta_size.obj.heap_size; CALL_CONSTRUCTOR("hdf/hdf5lib/structs/H5_ih_info_t", "(JJ)V", args); ihinfobuf1 = ret_obj; args[0].j = (jlong)infobuf.meta_size.attr.index_size; args[1].j = (jlong)infobuf.meta_size.attr.heap_size; CALL_CONSTRUCTOR("hdf/hdf5lib/structs/H5_ih_info_t", "(JJ)V", args); ihinfobuf2 = ret_obj; args[0].j = (jlong)infobuf.fileno; args[1].j = (jlong)infobuf.addr; args[2].i = infobuf.type; args[3].i = (jint)infobuf.rc; args[4].j = (jlong)infobuf.num_attrs; args[5].j = infobuf.atime; args[6].j = infobuf.mtime; args[7].j = infobuf.ctime; args[8].j = infobuf.btime; args[9].l = hdrinfobuf; args[10].l = ihinfobuf1; args[11].l = ihinfobuf2; CALL_CONSTRUCTOR("hdf/hdf5lib/structs/H5O_info_t", "(JJIIJJJJJLhdf/hdf5lib/structs/H5O_hdr_info_t;Lhdf/hdf5lib/structs/H5_ih_info_t;Lhdf/hdf5lib/structs/H5_ih_info_t;)V", args); } return ret_obj; } /* end Java_hdf_hdf5lib_H5_H5Oget_1info */
herr_t H5Group::getLsInfo(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data) { H5O_info_t oinfo; herr_t err; hid_t obj; OpDataGetLs & opdata = *(OpDataGetLs *)op_data; switch (info->type) { case H5L_TYPE_SOFT: opdata.name->push_back(name); opdata.type->push_back("soft"); break; case H5L_TYPE_EXTERNAL: opdata.name->push_back(name); opdata.type->push_back("external"); break; case H5L_TYPE_HARD: obj = H5Oopen_by_addr(g_id, info->u.address); if (obj < 0) { return (herr_t) - 1; } err = H5Oget_info(obj, &oinfo); H5Oclose(obj); if (err < 0) { return (herr_t) - 1; } switch (oinfo.type) { case H5O_TYPE_GROUP: opdata.name->push_back(name); opdata.type->push_back("group"); break; case H5O_TYPE_DATASET: opdata.name->push_back(name); opdata.type->push_back("dataset"); break; case H5O_TYPE_NAMED_DATATYPE: opdata.name->push_back(name); opdata.type->push_back("type"); break; default: return (herr_t) - 1; } break; default: return (herr_t) - 1; } return (herr_t)0; }
/*------------------------------------------------------------------------- * Function: trav_fileinfo_add * * Purpose: Add a file addr & fileno to info struct * * Return: void * *------------------------------------------------------------------------- */ void trav_fileinfo_add(trav_info_t *info, hid_t loc_id) { H5O_info_t oinfo; size_t idx = info->nused - 1; if ( info->paths[idx].path && HDstrcmp(info->paths[idx].path, ".")) H5Oget_info_by_name(loc_id, info->paths[idx].path, &oinfo, H5P_DEFAULT); else H5Oget_info(loc_id, &oinfo); info->paths[idx].objno = oinfo.addr; info->paths[idx].fileno = oinfo.fileno; } /* end trav_fileinfo_add() */
herr_t H5Group::printLsInfo(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data) { H5O_info_t oinfo; herr_t err; H5Object * hobj = 0; hid_t obj = 0; OpDataPrintLs & opdata = *(OpDataPrintLs *)op_data; switch (info->type) { case H5L_TYPE_SOFT: hobj = new H5SoftLink(*opdata.parent, name); break; case H5L_TYPE_EXTERNAL: hobj = new H5ExternalLink(*opdata.parent, name); break; case H5L_TYPE_HARD: obj = H5Oopen(g_id, name, H5P_DEFAULT); err = H5Oget_info(obj, &oinfo); H5Oclose(obj); if (err < 0) { return (herr_t) - 1; } switch (oinfo.type) { case H5O_TYPE_GROUP: hobj = new H5Group(*opdata.parent, name); break; case H5O_TYPE_DATASET: hobj = new H5Dataset(*opdata.parent, name); break; case H5O_TYPE_NAMED_DATATYPE: hobj = new H5Type(*opdata.parent, name); break; default: return (herr_t) - 1; } break; default: return (herr_t) - 1; } hobj->printLsInfo(*opdata.os); delete hobj; return (herr_t)0; }
/****if* H5Of/H5Oget_info_c * NAME * H5Oget_info_c * PURPOSE * Calls H5Oget_info * INPUTS * object_id - Identifier for target object. * OUTPUTS * object_info - Buffer in which to return object information. * * RETURNS * 0 on success, -1 on failure * AUTHOR * M. Scot Breitenfeld * May 16, 2012 * SOURCE */ int_f h5oget_info_c (hid_t_f *object_id, H5O_info_t_f *object_info) /******/ { int_f ret_value = 0; /* Return value */ H5O_info_t Oinfo; /* * Call H5Oinfo_by_name function. */ if(H5Oget_info((hid_t)*object_id, &Oinfo) < 0) HGOTO_DONE(FAIL); ret_value = fill_h5o_info_t_f(Oinfo,object_info); done: return ret_value; }
/*------------------------------------------------------------------------- * Function: flush_verification * * Purpose: This function tries to open target object in the test file. * It compares the success of the open function to the expected * value, and succeeds if they are equal and fails if they differ. * * Note that full path to the object must be provided as the * obj_pathname argument. * * Return: 0 on Success, 1 on Failure * * Programmer: Mike McGreevy * July 16, 2010 * * Modifications: * *------------------------------------------------------------------------- */ herr_t flush_verification(const char * obj_pathname, const char * expected) { /* Variables */ hid_t oid,fid,status = 0; H5O_info_t oinfo; /* Try to open the testfile and then obj_pathname within the file */ H5E_BEGIN_TRY { fid = H5Fopen(FILENAME, H5F_ACC_SWMR_READ, H5P_DEFAULT); oid = H5Oopen(fid, obj_pathname, H5P_DEFAULT); status = H5Oget_info(oid, &oinfo); } H5E_END_TRY; /* Compare to expected result */ if (strcmp(expected, FLUSHED) == 0) { if ((oid < 0) || (status < 0)) { HDfprintf(stderr, "Error! %s should be on disk, but was NOT!\n", obj_pathname); PROCESS_ERROR; } /* end if */ } else if (strcmp(expected, NOT_FLUSHED) == 0) { if ((oid > 0) || (status > 0)) { HDfprintf(stderr, "Error! %s not expected to be flushed, but it was found on disk!\n", obj_pathname); PROCESS_ERROR; } /* end if */ } else { HDfprintf(stderr, "Error! Bad verification parameters. %s is an invalid expected outcome.\n", expected); PROCESS_ERROR; } /* end if */ /* Cleanup */ H5E_BEGIN_TRY { H5Oclose(oid); H5Fclose(fid); } H5E_END_TRY; return SUCCEED; error: return FAIL; } /* flush_verification */
std::vector<std::string> list_attributes(hdf5dataset& hdataset) { H5O_info_t info; H5Oget_info(hdataset.handle(), &info); std::size_t num_attr = info.num_attrs; std::vector<std::string> result(num_attr); const int MAX_LEN = 256; char attr_name[MAX_LEN]; for(std::size_t i = 0; i < num_attr; ++i) { hid_t attribute_id = H5Aopen_idx(hdataset.handle(), i); H5Aget_name(attribute_id, MAX_LEN, attr_name); result[i] = std::string(attr_name); H5Aclose(attribute_id); } return result; }
/* * To exercise the coding for the re-read of the object header for SWMR access. * When the object header is read in H5O_load() of H5Ocache.c, the library initially reads * 512 bytes for decoding, then reads the remaining bytes later if the object header is * greater than 512 bytes. For SWMR access, the read should be done all at one time. */ static herr_t test_ohdr_swmr(void) { hid_t fid = -1; /* File ID */ hid_t fapl = -1; /* File access property list */ hid_t did = -1; /* Dataset ID */ hid_t sid = -1; /* Dataspace ID */ hid_t plist = -1; /* Dataset creation property list */ size_t compact_size = 1024; /* The size of compact dataset */ int wbuf[1024]; /* Buffer for writing */ hsize_t dims[1]; /* Dimension sizes */ unsigned int n = 0, u; /* Locatl index variable */ H5O_info_t obj_info; /* Information for the object */ TESTING("exercise the coding for the re-read of the object header for SWMR access"); /* File access property list */ if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) FAIL_STACK_ERROR /* Set to use latest library format */ if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) FAIL_STACK_ERROR /* Initialize data */ for(u = 0; u < compact_size; u++) wbuf[u] = n++; /* Create the file with the latest format (ensure version 2 object header for SWMR) */ if((fid = H5Fcreate(FILE_OHDR_SWMR, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR /* Create a small data space for compact dataset */ dims[0] = (hsize_t)compact_size; if((sid = H5Screate_simple(1, dims, NULL)) < 0) FAIL_STACK_ERROR /* Create property list for compact dataset creation */ if((plist = H5Pcreate(H5P_DATASET_CREATE)) < 0) FAIL_STACK_ERROR /* Set the layout for the compact dataset */ if(H5Pset_layout(plist, H5D_COMPACT) < 0) FAIL_STACK_ERROR /* Create a compact dataset */ if((did = H5Dcreate2(fid, DSET_NAME, H5T_NATIVE_INT, sid, H5P_DEFAULT, plist, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Write to the compact dataset */ if(H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf) < 0) FAIL_STACK_ERROR /* Close the dataset */ if(H5Dclose(did) < 0) FAIL_STACK_ERROR /* Close the file */ if(H5Fclose(fid) < 0) FAIL_STACK_ERROR /* Open the file for SWMR write and latest format */ if((fid = H5Fopen(FILE_OHDR_SWMR, H5F_ACC_RDWR|H5F_ACC_SWMR_WRITE, fapl)) < 0) FAIL_STACK_ERROR /* Open the compact dataset */ if((did = H5Dopen2(fid, DSET_NAME, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Get the object information */ if(H5Oget_info(did, &obj_info) < 0) FAIL_STACK_ERROR /* The size of object header should be greater than the speculative read size of 512 */ /* This will exercise the coding for the re-read of the object header for SWMR access */ if(obj_info.hdr.space.total < 512) TEST_ERROR; /* Close the dataset */ if(H5Dclose(did) < 0) FAIL_STACK_ERROR /* Close the file */ if(H5Fclose(fid) < 0) FAIL_STACK_ERROR /* Close the dataspace */ if(H5Sclose(sid) < 0) FAIL_STACK_ERROR /* Close the dataset creation property list */ if(H5Pclose(plist) < 0) FAIL_STACK_ERROR /* Close the file access property list */ if(H5Pclose(fapl) < 0) FAIL_STACK_ERROR /* Remove the test file */ if(HDremove(FILE_OHDR_SWMR) < 0) FAIL_STACK_ERROR PASSED(); return 0; error: H5E_BEGIN_TRY { H5Fclose(fid); H5Dclose(did); H5Sclose(sid); H5Pclose(plist); H5Pclose(fapl); HDremove(FILE_OHDR_SWMR); } H5E_END_TRY; return -1; } /* test_ohdr_swmr() */
u::logic HDF5FormatLib::GetHDF5StructFile(QString filePathJson1, QString filePathHdf) { hid_t file; herr_t status; H5O_info_t infobuf; struct opdata od; filePathJson = filePathJson1; try { FILE *pfile1; pfile1 = fopen(filePathJson1.toStdString().c_str(), "w"); if (!pfile1) { throw GenericExc(QObject::tr("Ошибка открытия json файла")); } fprintf(pfile1, "%s", "{\n"); fclose(pfile1); file = H5Fopen (filePathHdf.toStdString().c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); if (file < 0) { throw GenericExc(QObject::tr("Ошибка открытия файла")); } status = H5Oget_info (file, &infobuf); if (status < 0) { throw GenericExc(QObject::tr("Ошибка получения информации о файле")); } od.recurs = 0; od.prev = NULL; od.addr = infobuf.addr; status = H5Literate (file, H5_INDEX_NAME, H5_ITER_NATIVE, NULL, op_func, (void *) &od); if (status < 0) { throw GenericExc(QObject::tr("Ошибка получения структуры файла")); } status = H5Fclose (file); if (status < 0) { throw GenericExc(QObject::tr("Ошибка закрытия файла")); } pfile1 = fopen(filePathJson.toStdString().c_str(), "a"); if (!pfile1) { throw GenericExc(QObject::tr("Ошибка открытия json файла")); } fprintf(pfile1, "%s", "}"); fclose(pfile1); return true; } catch (const GenericExc& exc) { m_errDescription = exc.GetWhat(); return false; } catch (...) { m_errDescription = QObject::tr("Неизвестная ошибка при получении структуры данных"); return false; } }
/*------------------------------------------------------------------------- * Function: build_match_list_attrs * * Purpose: get list of matching attribute name from obj1 and obj2 * * Note: * Find common attribute; the algorithm for search is referred from * build_match_list() in h5diff.c . * * Parameter: * table_out [OUT] : return the list * * Programmer: Jonathan Kim * * Date: March 15, 2011 *------------------------------------------------------------------------*/ static herr_t build_match_list_attrs(hid_t loc1_id, hid_t loc2_id, table_attrs_t ** table_out, diff_opt_t *options) { H5O_info_t oinfo1, oinfo2; /* Object info */ hid_t attr1_id=-1; /* attr ID */ hid_t attr2_id=-1; /* attr ID */ size_t curr1 = 0; size_t curr2 = 0; unsigned infile[2]; char name1[ATTR_NAME_MAX]; char name2[ATTR_NAME_MAX]; int cmp; unsigned i; table_attrs_t *table_lp = NULL; if(H5Oget_info(loc1_id, &oinfo1) < 0) goto error; if(H5Oget_info(loc2_id, &oinfo2) < 0) goto error; table_attrs_init( &table_lp ); /*-------------------------------------------------- * build the list */ while(curr1 < oinfo1.num_attrs && curr2 < oinfo2.num_attrs) { /*------------------ * open attribute1 */ if((attr1_id = H5Aopen_by_idx(loc1_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr1, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* get name */ if(H5Aget_name(attr1_id, (size_t)ATTR_NAME_MAX, name1) < 0) goto error; /*------------------ * open attribute2 */ if((attr2_id = H5Aopen_by_idx(loc2_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr2, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* get name */ if(H5Aget_name(attr2_id, (size_t)ATTR_NAME_MAX, name2) < 0) goto error; /* criteria is string compare */ cmp = HDstrcmp(name1, name2); if(cmp == 0) { infile[0] = 1; infile[1] = 1; table_attr_mark_exist(infile, name1, table_lp); curr1++; curr2++; } else if(cmp < 0) { infile[0] = 1; infile[1] = 0; table_attr_mark_exist(infile, name1, table_lp); table_lp->nattrs_only1++; curr1++; } else { infile[0] = 0; infile[1] = 1; table_attr_mark_exist(infile, name2, table_lp); table_lp->nattrs_only2++; curr2++; } /* close for next turn */ H5Aclose(attr1_id); attr1_id = -1; H5Aclose(attr2_id); attr2_id = -1; } /* end while */ /* list1 did not end */ infile[0] = 1; infile[1] = 0; while(curr1 < oinfo1.num_attrs) { /*------------------ * open attribute1 */ if((attr1_id = H5Aopen_by_idx(loc1_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr1, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* get name */ if(H5Aget_name(attr1_id, (size_t)ATTR_NAME_MAX, name1) < 0) goto error; table_attr_mark_exist(infile, name1, table_lp); table_lp->nattrs_only1++; curr1++; /* close for next turn */ H5Aclose(attr1_id); attr1_id = -1; } /* list2 did not end */ infile[0] = 0; infile[1] = 1; while(curr2 < oinfo2.num_attrs) { /*------------------ * open attribute2 */ if((attr2_id = H5Aopen_by_idx(loc2_id, ".", H5_INDEX_NAME, H5_ITER_INC, (hsize_t)curr2, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* get name */ if(H5Aget_name(attr2_id, (size_t)ATTR_NAME_MAX, name2) < 0) goto error; table_attr_mark_exist(infile, name2, table_lp); table_lp->nattrs_only2++; curr2++; /* close for next turn */ H5Aclose(attr2_id); } /*------------------------------------------------------ * print the list */ if(options->m_verbose_level == 2) { /* if '-v2' is detected */ parallel_print(" obj1 obj2\n"); parallel_print(" --------------------------------------\n"); for(i = 0; i < (unsigned int) table_lp->nattrs; i++) { char c1, c2; c1 = (table_lp->attrs[i].exist[0]) ? 'x' : ' '; c2 = (table_lp->attrs[i].exist[1]) ? 'x' : ' '; parallel_print("%5c %6c %-15s\n", c1, c2, table_lp->attrs[i].name); } /* end for */ } if(options->m_verbose_level >= 1) { parallel_print("Attributes status: %d common, %d only in obj1, %d only in obj2\n", table_lp->nattrs - table_lp->nattrs_only1 - table_lp->nattrs_only2, table_lp->nattrs_only1, table_lp->nattrs_only2 ); } *table_out = table_lp; return 0; error: if (0 < attr1_id) H5Aclose(attr1_id); if (0 < attr2_id) H5Aclose(attr2_id); return -1; }
/*------------------------------------------------------------------------- * Function: main * * Purpose: H5O_mtime_decode() test. * * Return: Success: * * Failure: * * Programmer: Robb Matzke * Thursday, July 30, 1998 * * Modifications: * Added checks for old and new modification time messages * in pre-created datafiles (generated with gen_old_mtime.c and * gen_new_mtime.c). * Quincey Koziol * Friday, January 3, 2003 * *------------------------------------------------------------------------- */ int main(void) { hid_t fapl, file, space, dset; hsize_t size[1] = {2}; time_t now; struct tm *tm; H5O_info_t oi1, oi2; signed char buf1[32], buf2[32]; char filename[1024]; h5_reset(); fapl = h5_fileaccess(); TESTING("modification time messages"); /* Create the file, create a dataset, then close the file */ h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if((space = H5Screate_simple(1, size, NULL)) < 0) TEST_ERROR; if((dset = H5Dcreate2(file, "dset", H5T_NATIVE_SCHAR, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; now = HDtime(NULL); if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Sclose(space) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* * Open the file and get the modification time. We'll test the * H5Oget_info() arguments too: being able to stat something without * knowing its name. */ h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if((file = H5Fopen(filename, H5F_ACC_RDONLY, fapl)) < 0) TEST_ERROR; if(H5Oget_info_by_name(file, "dset", &oi1, H5P_DEFAULT) < 0) TEST_ERROR; if((dset = H5Dopen2(file, "dset", H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Oget_info(dset, &oi2) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Compare addresses & times from the two ways of calling H5Oget_info() */ if(oi1.addr != oi2.addr || oi1.mtime != oi2.mtime) { H5_FAILED(); puts(" Calling H5Oget_info() with the dataset ID returned"); puts(" different values than calling it with a file and dataset"); puts(" name."); goto error; } /* Compare times -- they must be within 60 seconds of one another */ if(0 == oi1.mtime) { SKIPPED(); puts(" The modification time could not be decoded on this OS."); puts(" Modification times will be mantained in the file but"); puts(" cannot be queried on this system. See H5O_mtime_decode()."); return 0; } else if(HDfabs(HDdifftime(now, oi1.mtime)) > 60.0) { H5_FAILED(); tm = HDlocaltime(&(oi1.mtime)); HDstrftime((char*)buf1, sizeof buf1, "%Y-%m-%d %H:%M:%S", tm); tm = HDlocaltime(&now); HDstrftime((char*)buf2, sizeof buf2, "%Y-%m-%d %H:%M:%S", tm); printf(" got: %s\n ans: %s\n", buf1, buf2); goto error; } PASSED(); /* Check opening existing file with old-style modification time information * and make certain that the time is correct */ TESTING("accessing old modification time messages"); { char testfile[512]=""; char *srcdir = HDgetenv("srcdir"); if(srcdir && ((HDstrlen(srcdir) + strlen(TESTFILE1) + 1) < sizeof(testfile))){ HDstrcpy(testfile, srcdir); HDstrcat(testfile, "/"); } HDstrcat(testfile, TESTFILE1); file = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT); if(file >= 0){ if(H5Oget_info_by_name(file, "/Dataset1", &oi1, H5P_DEFAULT) < 0) TEST_ERROR; if(oi1.mtime != MTIME1) { H5_FAILED(); /* If this fails, examine H5Omtime.c. Modification time is very * system dependant (e.g., on Windows DST must be hardcoded). */ puts(" Old modification time incorrect"); goto error; } if(H5Fclose(file) < 0) TEST_ERROR; } else { H5_FAILED(); printf("***cannot open the pre-created old modification test file (%s)\n", testfile); goto error; } /* end else */ } PASSED(); /* Check opening existing file with new-style modification time information * and make certain that the time is correct */ TESTING("accessing new modification time messages"); { char testfile[512]=""; char *srcdir = HDgetenv("srcdir"); if(srcdir && ((HDstrlen(srcdir) + strlen(TESTFILE2) + 1) < sizeof(testfile))){ HDstrcpy(testfile, srcdir); HDstrcat(testfile, "/"); } HDstrcat(testfile, TESTFILE2); file = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT); if(file >= 0){ if(H5Oget_info_by_name(file, "/Dataset1", &oi2, H5P_DEFAULT) < 0) TEST_ERROR; if(oi2.mtime != MTIME2) { H5_FAILED(); puts(" Modification time incorrect."); goto error; } if(H5Fclose(file) < 0) TEST_ERROR; } else { H5_FAILED(); printf("***cannot open the pre-created old modification test file (%s)\n", testfile); goto error; } /* end else */ } PASSED(); /* All looks good */ puts("All modification time tests passed."); h5_cleanup(FILENAME, fapl); return 0; /* Something broke */ error: return 1; }
/*------------------------------------------------------------------------- * Function: refresh_verification * * Purpose: This function opens the specified object, and checks to see * that is does not have any attributes on it. It then sends * a signal to the main process, which will flush the object * (putting an attribute on the object on disk). This function * will then refresh the object, and verify that it has picked * up the new metadata reflective of the added attribute. * * Return: 0 on Success, 1 on Failure * * Programmer: Mike McGreevy * July 16, 2010 * * Modifications: * *------------------------------------------------------------------------- */ herr_t refresh_verification(const char * obj_pathname) { /* Variables */ hid_t oid,fid,status = 0; H5O_info_t flushed_oinfo; H5O_info_t refreshed_oinfo; /* Open Object */ if ((fid = H5Fopen(FILENAME, H5F_ACC_SWMR_READ, H5P_DEFAULT)) < 0) PROCESS_ERROR; if ((oid = H5Oopen(fid, obj_pathname, H5P_DEFAULT)) < 0) PROCESS_ERROR; /* Get Object info */ if ((status = H5Oget_info(oid, &flushed_oinfo)) < 0) PROCESS_ERROR; /* Make sure there are no attributes on the object. This is just a sanity check to ensure we didn't erroneously flush the attribute before starting the verification. */ if (flushed_oinfo.num_attrs != 0) PROCESS_ERROR; /* Send Signal to MAIN PROCESS indicating that it can go ahead and modify the object. */ send_signal(SIGNAL_BETWEEN_PROCESSES_1, NULL, NULL); /* Wait for Signal from MAIN PROCESS indicating that it's modified the object and we can run verification now. */ if (wait_for_signal(SIGNAL_BETWEEN_PROCESSES_2) < 0) PROCESS_ERROR; /* Get object info again. This will NOT reflect what's on disk, only what's in the cache. Thus, all values will be unchanged from above, despite newer information being on disk. */ if ((status = H5Oget_info(oid, &refreshed_oinfo)) < 0) PROCESS_ERROR; /* Verify that before doing a refresh, getting the object info returns stale information. (i.e., unchanged from above, despite new info on disk). */ if (flushed_oinfo.addr != refreshed_oinfo.addr) PROCESS_ERROR; if (flushed_oinfo.type != refreshed_oinfo.type) PROCESS_ERROR; if (flushed_oinfo.hdr.version != refreshed_oinfo.hdr.version) PROCESS_ERROR; if (flushed_oinfo.hdr.flags != refreshed_oinfo.hdr.flags) PROCESS_ERROR; if (flushed_oinfo.num_attrs != refreshed_oinfo.num_attrs) PROCESS_ERROR; if (flushed_oinfo.hdr.nmesgs != refreshed_oinfo.hdr.nmesgs) PROCESS_ERROR; if (flushed_oinfo.hdr.nchunks != refreshed_oinfo.hdr.nchunks) PROCESS_ERROR; if (flushed_oinfo.hdr.space.total != refreshed_oinfo.hdr.space.total) PROCESS_ERROR; /* Refresh object */ /* The H5*refresh function called depends on which object we are trying * to refresh. (MIKE: add desired refresh call as parameter so adding new * test cases is easy). */ if ((strcmp(obj_pathname, D1) == 0) || (strcmp(obj_pathname, D2) == 0)) { if (H5Drefresh(oid) < 0) PROCESS_ERROR; } /* end if */ else if ((strcmp(obj_pathname, G1) == 0) || (strcmp(obj_pathname, G2) == 0)) { if (H5Grefresh(oid) < 0) PROCESS_ERROR; } /* end if */ else if ((strcmp(obj_pathname, T1) == 0) || (strcmp(obj_pathname, T2) == 0)) { if (H5Trefresh(oid) < 0) PROCESS_ERROR; } /* end if */ else if ((strcmp(obj_pathname, D3) == 0) || (strcmp(obj_pathname, G3) == 0) || (strcmp(obj_pathname, T3) == 0)) { if (H5Orefresh(oid) < 0) PROCESS_ERROR; } /* end if */ else { HDfprintf(stdout, "Error. %s is an unrecognized object.\n", obj_pathname); PROCESS_ERROR; } /* end else */ /* Get object info. This should now accurately reflect the refreshed object on disk. */ if ((status = H5Oget_info(oid, &refreshed_oinfo)) < 0) PROCESS_ERROR; /* Confirm following attributes are the same: */ if (flushed_oinfo.addr != refreshed_oinfo.addr) PROCESS_ERROR; if (flushed_oinfo.type != refreshed_oinfo.type) PROCESS_ERROR; if (flushed_oinfo.hdr.version != refreshed_oinfo.hdr.version) PROCESS_ERROR; if (flushed_oinfo.hdr.flags != refreshed_oinfo.hdr.flags) PROCESS_ERROR; /* Confirm following attributes are different */ if (flushed_oinfo.num_attrs == refreshed_oinfo.num_attrs) PROCESS_ERROR; if (flushed_oinfo.hdr.nmesgs == refreshed_oinfo.hdr.nmesgs) PROCESS_ERROR; if (flushed_oinfo.hdr.nchunks == refreshed_oinfo.hdr.nchunks) PROCESS_ERROR; if (flushed_oinfo.hdr.space.total == refreshed_oinfo.hdr.space.total) PROCESS_ERROR; /* Close objects */ if (H5Oclose(oid) < 0) PROCESS_ERROR; if (H5Fclose(fid) < 0) PROCESS_ERROR; /* Return */ return SUCCEED; error: return FAIL; } /* refresh_verification */
unsigned int LocID::referenceCount() const { H5O_info_t oInfo; HErr res = H5Oget_info(hid, &oInfo); res.check("LocID:referenceCount: Coud not get object info"); return oInfo.rc; }
int main (void) { hid_t file, dataset; /* File and dataset identifiers */ hid_t fid; /* Dataspace identifier */ hid_t attr1, attr2, attr3; /* Attribute identifiers */ hid_t attr; hid_t aid1, aid2, aid3; /* Attribute dataspace identifiers */ hid_t atype, atype_mem; /* Attribute type */ H5T_class_t type_class; hsize_t fdim[] = {SIZE}; hsize_t adim[] = {ADIM1, ADIM2}; /* Dimensions of the first attribute */ float matrix[ADIM1][ADIM2]; /* Attribute data */ herr_t ret; /* Return value */ H5O_info_t oinfo; /* Object info */ unsigned i, j; /* Counters */ char string_out[80]; /* Buffer to read string attribute back */ int point_out; /* Buffer to read scalar attribute back */ /* * Data initialization. */ int vector[] = {1, 2, 3, 4, 5, 6, 7}; /* Dataset data */ int point = 1; /* Value of the scalar attribute */ char string[] = "ABCD"; /* Value of the string attribute */ for (i=0; i < ADIM1; i++) { /* Values of the array attribute */ for (j=0; j < ADIM2; j++) matrix[i][j] = -1.; } /* * Create a file. */ file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create the dataspace for the dataset in the file. */ fid = H5Screate(H5S_SIMPLE); ret = H5Sset_extent_simple(fid, RANK, fdim, NULL); /* * Create the dataset in the file. */ dataset = H5Dcreate2(file, "Dataset", H5T_NATIVE_INT, fid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Write data to the dataset. */ ret = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL , H5S_ALL, H5P_DEFAULT, vector); /* * Create dataspace for the first attribute. */ aid1 = H5Screate(H5S_SIMPLE); ret = H5Sset_extent_simple(aid1, ARANK, adim, NULL); /* * Create array attribute. */ attr1 = H5Acreate2(dataset, ANAME, H5T_NATIVE_FLOAT, aid1, H5P_DEFAULT, H5P_DEFAULT); /* * Write array attribute. */ ret = H5Awrite(attr1, H5T_NATIVE_FLOAT, matrix); /* * Create scalar attribute. */ aid2 = H5Screate(H5S_SCALAR); attr2 = H5Acreate2(dataset, "Integer attribute", H5T_NATIVE_INT, aid2, H5P_DEFAULT, H5P_DEFAULT); /* * Write scalar attribute. */ ret = H5Awrite(attr2, H5T_NATIVE_INT, &point); /* * Create string attribute. */ aid3 = H5Screate(H5S_SCALAR); atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 5); H5Tset_strpad(atype,H5T_STR_NULLTERM); attr3 = H5Acreate2(dataset, ANAMES, atype, aid3, H5P_DEFAULT, H5P_DEFAULT); /* * Write string attribute. */ ret = H5Awrite(attr3, atype, string); /* * Close attribute and file dataspaces, and datatype. */ ret = H5Sclose(aid1); ret = H5Sclose(aid2); ret = H5Sclose(aid3); ret = H5Sclose(fid); ret = H5Tclose(atype); /* * Close the attributes. */ ret = H5Aclose(attr1); ret = H5Aclose(attr2); ret = H5Aclose(attr3); /* * Close the dataset. */ ret = H5Dclose(dataset); /* * Close the file. */ ret = H5Fclose(file); /* * Reopen the file. */ file = H5Fopen(H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT); /* * Open the dataset. */ dataset = H5Dopen2(file, "Dataset", H5P_DEFAULT); /* * Attach to the scalar attribute using attribute name, then read and * display its value. */ attr = H5Aopen(dataset, "Integer attribute", H5P_DEFAULT); ret = H5Aread(attr, H5T_NATIVE_INT, &point_out); printf("The value of the attribute \"Integer attribute\" is %d \n", point_out); ret = H5Aclose(attr); /* * Find string attribute by iterating through all attributes */ ret = H5Oget_info(dataset, &oinfo); for(i = 0; i < (unsigned)oinfo.num_attrs; i++) { attr = H5Aopen_by_idx(dataset, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t)i, H5P_DEFAULT, H5P_DEFAULT); atype = H5Aget_type(attr); type_class = H5Tget_class(atype); if (type_class == H5T_STRING) { atype_mem = H5Tget_native_type(atype, H5T_DIR_ASCEND); ret = H5Aread(attr, atype_mem, string_out); printf("Found string attribute; its index is %d , value = %s \n", i, string_out); ret = H5Tclose(atype_mem); } ret = H5Aclose(attr); ret = H5Tclose(atype); } /* * Get attribute info using iteration function. */ ret = H5Aiterate2(dataset, H5_INDEX_NAME, H5_ITER_INC, NULL, attr_info, NULL); /* * Close the dataset and the file. */ H5Dclose(dataset); H5Fclose(file); return 0; }
/*------------------------------------------------------------------------- * Function: copy_named_datatype * * Purpose: Copies the specified datatype anonymously, and returns an open * id for that datatype in the output file. The first time this * is called it scans every named datatype in travt into a * private stack, afterwards it simply scans that stack. The id * returned must be closed after it is no longer needed. * named_datatype_free must be called before the program exits * to free the stack. *------------------------------------------------------------------------- */ hid_t copy_named_datatype(hid_t type_in, hid_t fidout, named_dt_t **named_dt_head_p, trav_table_t *travt, pack_opt_t *options) { named_dt_t *dt = *named_dt_head_p; /* Stack pointer */ named_dt_t *dt_ret = NULL; /* Datatype to return */ H5O_info_t oinfo; /* Object info of input dtype */ hid_t ret_value = -1; /* The identifier of the named dtype in the out file */ if (H5Oget_info(type_in, &oinfo) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Oget_info failed"); if (*named_dt_head_p) { /* Stack already exists, search for the datatype */ while (dt && dt->addr_in != oinfo.addr) dt = dt->next; dt_ret = dt; } else { /* Create the stack */ size_t i; for (i = 0; i < travt->nobjs; i++) { if (travt->objs[i].type == H5TRAV_TYPE_NAMED_DATATYPE) { /* Push onto the stack */ if (NULL == (dt = (named_dt_t *)HDmalloc(sizeof(named_dt_t)))) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "buffer allocation failed failed"); dt->next = *named_dt_head_p; *named_dt_head_p = dt; /* Update the address and id */ dt->addr_in = travt->objs[i].objno; dt->id_out = -1; /* Check if this type is the one requested */ if (oinfo.addr == dt->addr_in) { dt_ret = dt; } /* end if */ } /* end if */ } /* end for */ } /* end else */ /* Handle the case that the requested datatype was not found. This is * possible if the datatype was committed anonymously in the input file. */ if (!dt_ret) { /* Push the new datatype onto the stack */ if (NULL == (dt_ret = (named_dt_t *)HDmalloc(sizeof(named_dt_t)))) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "buffer allocation failed failed"); dt_ret->next = *named_dt_head_p; *named_dt_head_p = dt_ret; /* Update the address and id */ dt_ret->addr_in = oinfo.addr; dt_ret->id_out = -1; } /* end if */ /* If the requested datatype does not yet exist in the output file, copy it * anonymously */ if (dt_ret->id_out < 0) { if (options->use_native == 1) dt_ret->id_out = H5Tget_native_type(type_in, H5T_DIR_DEFAULT); else dt_ret->id_out = H5Tcopy(type_in); if (dt_ret->id_out < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tget_native_type-H5Tcopy failed"); if (H5Tcommit_anon(fidout, dt_ret->id_out, H5P_DEFAULT, H5P_DEFAULT) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tcommit_anon failed"); } /* end if */ /* Set return value */ ret_value = dt_ret->id_out; /* Increment the ref count on id_out, because the calling function will try to close it */ if(H5Iinc_ref(ret_value) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Iinc_ref failed"); done: return ret_value; } /* end copy_named_datatype */
/*------------------------------------------------------------------------- * Function: copy_attr * * Purpose: copy attributes located in LOC_IN, which is obtained either from * loc_id = H5Gopen2( fid, name); * loc_id = H5Dopen2( fid, name); * loc_id = H5Topen2( fid, name); * * Return: 0, ok, -1 no *------------------------------------------------------------------------- */ int copy_attr(hid_t loc_in, hid_t loc_out, named_dt_t **named_dt_head_p, trav_table_t *travt, pack_opt_t *options) { int ret_value = 0; hid_t attr_id = -1; /* attr ID */ hid_t attr_out = -1; /* attr ID */ hid_t space_id = -1; /* space ID */ hid_t ftype_id = -1; /* file type ID */ hid_t wtype_id = -1; /* read/write type ID */ size_t msize; /* size of type */ void *buf = NULL; /* data buffer */ hsize_t nelmts; /* number of elements in dataset */ int rank; /* rank of dataset */ htri_t is_named; /* Whether the datatype is named */ hsize_t dims[H5S_MAX_RANK];/* dimensions of dataset */ char name[255]; H5O_info_t oinfo; /* object info */ int j; unsigned u; hbool_t is_ref = 0; H5T_class_t type_class = -1; if (H5Oget_info(loc_in, &oinfo) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Oget_info failed"); /*------------------------------------------------------------------------- * copy all attributes *------------------------------------------------------------------------- */ for (u = 0; u < (unsigned) oinfo.num_attrs; u++) { /* open attribute */ if ((attr_id = H5Aopen_by_idx(loc_in, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t) u, H5P_DEFAULT, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aopen_by_idx failed"); /* get name */ if (H5Aget_name(attr_id, (size_t) 255, name) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Pclose failed"); /* get the file datatype */ if ((ftype_id = H5Aget_type(attr_id)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aget_type failed"); /* Check if the datatype is committed */ if ((is_named = H5Tcommitted(ftype_id)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tcommitted failed"); if (is_named && travt) { hid_t fidout = -1; /* Create out file id */ if ((fidout = H5Iget_file_id(loc_out)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Iget_file_id failed"); /* Copy named dt */ if ((wtype_id = copy_named_datatype(ftype_id, fidout, named_dt_head_p, travt, options)) < 0) { H5Fclose(fidout); HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "copy_named_datatype failed"); } /* end if */ if (H5Fclose(fidout) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Fclose failed"); } /* end if */ else { if (options->use_native == 1) wtype_id = H5Tget_native_type(ftype_id, H5T_DIR_DEFAULT); else wtype_id = H5Tcopy(ftype_id); } /* end else */ /* get the dataspace handle */ if ((space_id = H5Aget_space(attr_id)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aget_space failed"); /* get dimensions */ if ((rank = H5Sget_simple_extent_dims(space_id, dims, NULL)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Sget_simple_extent_dims failed"); nelmts = 1; for (j = 0; j < rank; j++) nelmts *= dims[j]; if ((msize = H5Tget_size(wtype_id)) == 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tget_size failed"); /*------------------------------------------------------------------------- * object references are a special case. We cannot just copy the buffers, * but instead we recreate the reference. * This is done on a second sweep of the file that just copies the referenced * objects at copy_refs_attr() *------------------------------------------------------------------------- */ type_class = H5Tget_class(wtype_id); is_ref = (type_class == H5T_REFERENCE); if (type_class == H5T_VLEN || type_class == H5T_ARRAY) { hid_t base_type = -1; base_type = H5Tget_super(ftype_id); is_ref = (is_ref || (H5Tget_class(base_type) == H5T_REFERENCE)); if (H5Tclose(base_type) < 0) H5TOOLS_INFO(H5E_tools_min_id_g, "H5Tclose base_type failed"); } if (type_class == H5T_COMPOUND) { int nmembers = H5Tget_nmembers(wtype_id); for (j = 0; j < nmembers; j++) { hid_t mtid = H5Tget_member_type(wtype_id, (unsigned)j); H5T_class_t mtclass = H5Tget_class(mtid); if (H5Tclose(mtid) < 0) H5TOOLS_INFO(H5E_tools_min_id_g, "H5Tclose mtid failed"); if (mtclass == H5T_REFERENCE) { is_ref = 1; break; } } /* for (j=0; i<nmembers; j++) */ } /* if (type_class == H5T_COMPOUND) */ if (!is_ref) { /*------------------------------------------------------------------------- * read to memory *------------------------------------------------------------------------- */ buf = (void *)HDmalloc((size_t)(nelmts * msize)); if (buf == NULL) { HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "HDmalloc failed"); } /* end if */ if (H5Aread(attr_id, wtype_id, buf) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aread failed"); /*------------------------------------------------------------------------- * copy *------------------------------------------------------------------------- */ if ((attr_out = H5Acreate2(loc_out, name, wtype_id, space_id, H5P_DEFAULT, H5P_DEFAULT)) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Acreate2 failed on ,%s>", name); if (H5Awrite(attr_out, wtype_id, buf) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Awrite failed"); /*close*/ if (H5Aclose(attr_out) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aclose failed"); /* Check if we have VL data and string in the attribute's datatype that must * be reclaimed */ if (TRUE == h5tools_detect_vlen(wtype_id)) H5Dvlen_reclaim(wtype_id, space_id, H5P_DEFAULT, buf); HDfree(buf); buf = NULL; } /*H5T_REFERENCE*/ if (options->verbose) printf(FORMAT_OBJ_ATTR, "attr", name); /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ if (H5Sclose(space_id) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Sclose failed"); space_id = -1; if (H5Tclose(wtype_id) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tclose failed"); wtype_id = -1; if (H5Tclose(ftype_id) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Tclose failed"); ftype_id = -1; if (H5Aclose(attr_id) < 0) HGOTO_ERROR(FAIL, H5E_tools_min_id_g, "H5Aclose failed"); attr_id = -1; } /* for u */ done: H5E_BEGIN_TRY { if (buf) { /* Check if we have VL data and string in the attribute's datatype that must * be reclaimed */ if (TRUE == h5tools_detect_vlen(wtype_id)) H5Dvlen_reclaim(wtype_id, space_id, H5P_DEFAULT, buf); /* Free buf */ HDfree(buf); } /* end if */ H5Aclose(attr_out); H5Sclose(space_id); H5Tclose(wtype_id); H5Tclose(ftype_id); H5Aclose(attr_id); } H5E_END_TRY; return ret_value; } /* end copy_attr() */
void H5ReferenceData::printData(std::ostream & os, const unsigned int pos, const unsigned int indentLevel) const { char * cdata = static_cast<char *>(data) + offset + pos * (stride ? stride : dataSize); void ** ref = &(((void **)cdata)[0]); hid_t file = getFile().getH5Id(); hid_t obj = H5Rdereference(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref); if (obj < 0) { os << "NULL"; return; } ssize_t size = H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, 0, 0); char * name = 0; H5O_info_t info; if (size != -1) { name = new char[size + 1]; H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, name, size + 1); if (datasetReference == H5R_OBJECT) { H5Oget_info(obj, &info); H5Oclose(obj); switch (info.type) { case H5O_TYPE_GROUP: os << "GROUP "; break; case H5O_TYPE_DATASET: os << "DATASET "; break; case H5O_TYPE_NAMED_DATATYPE: os << "DATATYPE "; break; case H5O_TYPE_UNKNOWN: default: if (size != -1) { delete[] name; } throw H5Exception(__LINE__, __FILE__, _("Unknown HDF5 object")); } os << (haddr_t)(*ref) << " " << name; } else { hid_t space = H5Rget_region(file, H5R_DATASET_REGION, ref); hssize_t npoints = H5Sget_select_elem_npoints(space); hsize_t ndims = H5Sget_simple_extent_dims(space, 0, 0); H5Oclose(obj); os << "DATASET " << name << " {"; if (npoints >= 0) { const hsize_t N = ndims * npoints; hsize_t * buf = new hsize_t[N]; herr_t err = H5Sget_select_elem_pointlist(space, 0, npoints, buf); for (hssize_t i = 0; i < (hssize_t)N; i += ndims) { os << "("; for (unsigned int j = 0; j < ndims - 1; j++) { os << buf[i + j] << ","; } os << buf[i + ndims - 1] << ")"; if (i != N - ndims) { os << ", "; } else { os << "}"; } } delete[] buf; } else { hssize_t nblocks = H5Sget_select_hyper_nblocks(space); if (nblocks >= 0) { const hsize_t N = 2 * ndims * nblocks; hsize_t * buf = new hsize_t[N]; herr_t err = H5Sget_select_hyper_blocklist(space, 0, nblocks, buf); for (hssize_t i = 0; i < (hssize_t)N; i += 2 * ndims) { os << "("; for (unsigned int j = 0; j < ndims - 1; j++) { os << buf[i + j] << ","; } os << buf[i + ndims - 1] << ")-("; for (unsigned int j = 0; j < ndims - 1; j++) { os << buf[i + ndims + j] << ","; } os << buf[i + 2 * ndims - 1] << ")"; if (i != N - 2 * ndims) { os << ", "; } else { os << "}"; } } delete[] buf; } } H5Sclose(space); } delete[] name; } }
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; }
hsize_t diff_attr(hid_t loc1_id, hid_t loc2_id, const char *path1, const char *path2, diff_opt_t *options) { hid_t attr1_id=-1; /* attr ID */ hid_t attr2_id=-1; /* attr ID */ hid_t space1_id=-1; /* space ID */ hid_t space2_id=-1; /* space ID */ hid_t ftype1_id=-1; /* file data type ID */ hid_t ftype2_id=-1; /* file data type ID */ hid_t mtype1_id=-1; /* memory data type ID */ hid_t mtype2_id=-1; /* memory data type ID */ size_t msize1; /* memory size of memory type */ size_t msize2; /* memory size of memory type */ void *buf1=NULL; /* data buffer */ void *buf2=NULL; /* data buffer */ hsize_t nelmts1; /* number of elements in dataset */ int rank1; /* rank of dataset */ int rank2; /* rank of dataset */ hsize_t dims1[H5S_MAX_RANK];/* dimensions of dataset */ hsize_t dims2[H5S_MAX_RANK];/* dimensions of dataset */ char name1[512]; char name2[512]; char np1[512]; char np2[512]; H5O_info_t oinfo1, oinfo2; /* Object info */ unsigned u; /* Local index variable */ hsize_t nfound = 0; hsize_t nfound_total = 0; int j; if(H5Oget_info(loc1_id, &oinfo1) < 0) goto error; if(H5Oget_info(loc2_id, &oinfo2) < 0) goto error; if(oinfo1.num_attrs != oinfo2.num_attrs) return 1; for( u = 0; u < (unsigned)oinfo1.num_attrs; u++) { /* reset buffers for every attribute, we might goto out and call free */ buf1 = NULL; buf2 = NULL; /* open attribute */ if((attr1_id = H5Aopen_by_idx(loc1_id, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t)u, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* get name */ if(H5Aget_name(attr1_id, 255, name1) < 0) goto error; /* use the name on the first file to open the second file */ H5E_BEGIN_TRY { if((attr2_id = H5Aopen(loc2_id, name1, H5P_DEFAULT)) < 0) goto error; } H5E_END_TRY; /* get name */ if(H5Aget_name(attr2_id, 255, name2) < 0) goto error; /* get the datatypes */ if ((ftype1_id = H5Aget_type(attr1_id)) < 0) goto error; if ((ftype2_id = H5Aget_type(attr2_id)) < 0) goto error; if ((mtype1_id = h5tools_get_native_type(ftype1_id))<0) goto error; if ((mtype2_id = h5tools_get_native_type(ftype2_id))<0) goto error; if ((msize1 = H5Tget_size(mtype1_id))==0) goto error; if ((msize2 = H5Tget_size(mtype2_id))==0) goto error; /* get the dataspace */ if ((space1_id = H5Aget_space(attr1_id)) < 0) goto error; if ((space2_id = H5Aget_space(attr2_id)) < 0) goto error; /* get dimensions */ if ( (rank1 = H5Sget_simple_extent_dims(space1_id, dims1, NULL)) < 0 ) goto error; if ( (rank2 = H5Sget_simple_extent_dims(space2_id, dims2, NULL)) < 0 ) goto error; /*------------------------------------------------------------------------- * check for comparable TYPE and SPACE *------------------------------------------------------------------------- */ if ( msize1 != msize2 || diff_can_type(ftype1_id, ftype2_id, rank1, rank2, dims1, dims2, NULL, NULL, name1, name2, options, 0)!=1) { if (H5Tclose(ftype1_id)<0) goto error; if (H5Tclose(ftype2_id)<0) goto error; if (H5Sclose(space1_id)<0) goto error; if (H5Sclose(space2_id)<0) goto error; if (H5Aclose(attr1_id)<0) goto error; if (H5Aclose(attr2_id)<0) goto error; if (H5Tclose(mtype1_id)<0) goto error; if (H5Tclose(mtype2_id)<0) goto error; continue; } /*------------------------------------------------------------------------- * read *------------------------------------------------------------------------- */ nelmts1=1; for (j=0; j<rank1; j++) nelmts1*=dims1[j]; buf1=(void *) HDmalloc((unsigned)(nelmts1*msize1)); buf2=(void *) HDmalloc((unsigned)(nelmts1*msize2)); if ( buf1==NULL || buf2==NULL){ parallel_print( "cannot read into memory\n" ); goto error; } if (H5Aread(attr1_id,mtype1_id,buf1)<0) goto error; if (H5Aread(attr2_id,mtype2_id,buf2)<0) goto error; /* format output string */ sprintf(np1,"%s of <%s>",name1,path1); sprintf(np2,"%s of <%s>",name2,path2); /*------------------------------------------------------------------------- * array compare *------------------------------------------------------------------------- */ /* always print name */ if (options->m_verbose) { do_print_objname ("attribute", np1, np2); nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1, options, np1, np2, mtype1_id, attr1_id, attr2_id); print_found(nfound); } /* check first if we have differences */ else { if (options->m_quiet==0) { /* shut up temporarily */ options->m_quiet=1; nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1, options, np1, np2, mtype1_id, attr1_id, attr2_id); /* print again */ options->m_quiet=0; if (nfound) { do_print_objname ("attribute", np1, np2); nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1, options, np1, np2, mtype1_id, attr1_id, attr2_id); print_found(nfound); } /*if*/ } /*if*/ /* in quiet mode, just count differences */ else { nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1, options, np1, np2, mtype1_id, attr1_id, attr2_id); } /*else quiet */ } /*else verbose */ /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ if (H5Tclose(ftype1_id)<0) goto error; if (H5Tclose(ftype2_id)<0) goto error; if (H5Sclose(space1_id)<0) goto error; if (H5Sclose(space2_id)<0) goto error; if (H5Aclose(attr1_id)<0) goto error; if (H5Aclose(attr2_id)<0) goto error; if (H5Tclose(mtype1_id)<0) goto error; if (H5Tclose(mtype2_id)<0) goto error; if (buf1) HDfree(buf1); if (buf2) HDfree(buf2); nfound_total += nfound; } /* u */ return nfound_total; error: H5E_BEGIN_TRY { H5Tclose(ftype1_id); H5Tclose(ftype2_id); H5Tclose(mtype1_id); H5Tclose(mtype2_id); H5Sclose(space1_id); H5Sclose(space2_id); H5Aclose(attr1_id); H5Aclose(attr2_id); if (buf1) HDfree(buf1); if (buf2) HDfree(buf2); } H5E_END_TRY; options->err_stat=1; return nfound_total; }
/*------------------------------------------------------------------------- * Function: find_objs_cb * * Purpose: Callback to find objects, committed types and store them in tables * * Return: Success: SUCCEED * * Failure: FAIL * * Programmer: Ruey-Hsia Li * * Modifications: * *------------------------------------------------------------------------- */ static herr_t find_objs_cb(const char *name, const H5O_info_t *oinfo, const char *already_seen, void *op_data) { find_objs_t *info = (find_objs_t*)op_data; herr_t ret_value = 0; switch(oinfo->type) { case H5O_TYPE_GROUP: if(NULL == already_seen) add_obj(info->group_table, oinfo->addr, name, TRUE); break; case H5O_TYPE_DATASET: if(NULL == already_seen) { hid_t dset; /* Add the dataset to the list of objects */ add_obj(info->dset_table, oinfo->addr, name, TRUE); /* Check for a dataset that uses a named datatype */ if((dset = H5Dopen2(info->fid, name, H5P_DEFAULT)) >= 0) { hid_t type = H5Dget_type(dset); if(H5Tcommitted(type) > 0) { H5O_info_t type_oinfo; H5Oget_info(type, &type_oinfo); if(search_obj(info->type_table, type_oinfo.addr) == NULL) add_obj(info->type_table, type_oinfo.addr, name, FALSE); } /* end if */ H5Tclose(type); H5Dclose(dset); } /* end if */ else ret_value = FAIL; } /* end if */ break; case H5O_TYPE_NAMED_DATATYPE: if(NULL == already_seen) { obj_t *found_obj; if((found_obj = search_obj(info->type_table, oinfo->addr)) == NULL) add_obj(info->type_table, oinfo->addr, name, TRUE); else { /* Use latest version of name */ HDfree(found_obj->objname); found_obj->objname = HDstrdup(name); /* Mark named datatype as having valid name */ found_obj->recorded = TRUE; } /* end else */ } /* end if */ break; default: break; } /* end switch */ return ret_value; }