diff options
Diffstat (limited to 'tools/h5repack/h5repack.c')
-rw-r--r-- | tools/h5repack/h5repack.c | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/tools/h5repack/h5repack.c b/tools/h5repack/h5repack.c index ab0ca66..9adb1af 100644 --- a/tools/h5repack/h5repack.c +++ b/tools/h5repack/h5repack.c @@ -237,6 +237,337 @@ int h5repack_addlayout(const char* str, return 0; } +/* Note: The below copy_named_datatype(), named_datatype_free(), copy_attr() + * were located in h5repack_copy.c as static prior to bugfix1726. + * Made shared functions as copy_attr() was needed in h5repack_refs.c. + * However copy_attr() may be obsoleted when H5Acopy is available and put back + * others to static in h5repack_copy.c. + */ +/*------------------------------------------------------------------------- +* 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. +* +* Programmer: Neil Fortner +* +* Date: April 14, 2009 +* +*------------------------------------------------------------------------- +*/ +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) + goto error; + + 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)))) + goto error; + 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) + { + HDassert(!dt_ret); + dt_ret = dt; + } /* end if */ + } /* end if */ + } /* 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)))) + goto error; + 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 = h5tools_get_native_type(type_in); + else + dt_ret->id_out = H5Tcopy(type_in); + if(dt_ret->id_out < 0) + goto error; + if(H5Tcommit_anon(fidout, dt_ret->id_out, H5P_DEFAULT, H5P_DEFAULT) < 0) + goto error; + } /* 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) + goto error; + + return(ret_value); + +error: + return(-1); +} /* end copy_named_datatype */ + + +/*------------------------------------------------------------------------- +* Function: named_datatype_free +* +* Purpose: Frees the stack of named datatypes. +* +* Programmer: Neil Fortner +* +* Date: April 14, 2009 +* +*------------------------------------------------------------------------- +*/ +int named_datatype_free(named_dt_t **named_dt_head_p, int ignore_err) +{ + named_dt_t *dt = *named_dt_head_p; + + while(dt) + { + /* Pop the datatype off the stack and free it */ + if(H5Tclose(dt->id_out) < 0 && !ignore_err) + goto error; + dt = dt->next; + HDfree(*named_dt_head_p); + *named_dt_head_p = dt; + } /* end while */ + + return 0; + +error: + return -1; +} /* end named_datatype_free */ + +/*------------------------------------------------------------------------- +* 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 +* +* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu +* +* Date: October, 28, 2003 +* +*------------------------------------------------------------------------- +*/ +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) +{ + 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; + + if(H5Oget_info(loc_in, &oinfo) < 0) + goto error; + + /*------------------------------------------------------------------------- + * copy all attributes + *------------------------------------------------------------------------- + */ + + for ( u = 0; u < (unsigned)oinfo.num_attrs; u++) + { + buf=NULL; + + /* 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) + goto error; + + /* get name */ + if (H5Aget_name( attr_id, (size_t)255, name ) < 0) + goto error; + + /* get the file datatype */ + if ((ftype_id = H5Aget_type( attr_id )) < 0 ) + goto error; + + /* Check if the datatype is committed */ + if((is_named = H5Tcommitted(ftype_id)) < 0) + goto error; + if(is_named) + { + hid_t fidout; + + /* Create out file id */ + if((fidout = H5Iget_file_id(loc_out)) < 0) + goto error; + + /* Copy named dt */ + if((wtype_id = copy_named_datatype(ftype_id, fidout, named_dt_head_p, + travt, options)) < 0) + { + H5Fclose(fidout); + goto error; + } /* end if */ + + if(H5Fclose(fidout) < 0) + goto error; + } /* end if */ + + /* get the dataspace handle */ + if ((space_id = H5Aget_space( attr_id )) < 0 ) + goto error; + + /* get dimensions */ + if ( (rank = H5Sget_simple_extent_dims(space_id, dims, NULL)) < 0 ) + goto error; + + nelmts=1; + for (j=0; j<rank; j++) + nelmts*=dims[j]; + + /* wtype_id will have already been set if using a named dtype */ + if(!is_named) + { + if (options->use_native==1) + wtype_id = h5tools_get_native_type(ftype_id); + else + wtype_id = H5Tcopy(ftype_id); + } /* end if */ + + if ((msize=H5Tget_size(wtype_id))==0) + goto error; + + /*------------------------------------------------------------------------- + * 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 + *------------------------------------------------------------------------- + */ + + if (H5T_REFERENCE==H5Tget_class(wtype_id)) + { + ; + } + else + { + /*------------------------------------------------------------------------- + * read to memory + *------------------------------------------------------------------------- + */ + + buf = (void *)HDmalloc((size_t)(nelmts * msize)); + if(buf == NULL) + { + error_msg("h5repack", "cannot read into memory\n" ); + goto error; + } + if(H5Aread(attr_id, wtype_id, buf) < 0) + goto error; + + /*------------------------------------------------------------------------- + * copy + *------------------------------------------------------------------------- + */ + + if((attr_out = H5Acreate2(loc_out, name, wtype_id, space_id, H5P_DEFAULT, H5P_DEFAULT)) < 0) + goto error; + if(H5Awrite(attr_out, wtype_id, buf) < 0) + goto error; + + /*close*/ + if(H5Aclose(attr_out) < 0) + goto error; + + + if(buf) + free(buf); + + } /*H5T_REFERENCE*/ + + + if(options->verbose) + printf(FORMAT_OBJ_ATTR, "attr", name); + + /*------------------------------------------------------------------------- + * close + *------------------------------------------------------------------------- + */ + + if (H5Tclose(ftype_id) < 0) goto error; + if (H5Tclose(wtype_id) < 0) goto error; + if (H5Sclose(space_id) < 0) goto error; + if (H5Aclose(attr_id) < 0) goto error; + + } /* u */ + + + return 0; + +error: + H5E_BEGIN_TRY { + H5Tclose(ftype_id); + H5Tclose(wtype_id); + H5Sclose(space_id); + H5Aclose(attr_id); + H5Aclose(attr_out); + if (buf) + free(buf); + } H5E_END_TRY; + return -1; +} /*------------------------------------------------------------------------- * Function: check_options |