int NC4_put_propattr(NC_HDF5_FILE_INFO_T* h5) { int ncstat = NC_NOERR; H5T_class_t t_class; size_t size; hid_t grp = -1; hid_t exists = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; herr_t herr = 0; char* text = NULL; /* Get root group */ grp = h5->root_grp->hdf_grpid; /* get root group */ /* See if the NCPROPS attribute exists */ if(H5Aexists(grp,NCPROPS) == 0) { /* Does not exist */ ncstat = NC4_buildpropinfo(&h5->fileinfo->propattr,&text); if(text == NULL || ncstat != NC_NOERR) { goto done; } herr = -1; /* Create a datatype to refer to. */ HCHECK((atype = H5Tcopy(H5T_C_S1))); HCHECK((H5Tset_cset(atype, H5T_CSET_ASCII))); HCHECK((H5Tset_size(atype, strlen(text)+1))); /*keep nul term */ HCHECK((aspace = H5Screate(H5S_SCALAR))); HCHECK((attid = H5Acreate(grp, NCPROPS, atype, aspace, H5P_DEFAULT))); HCHECK((H5Awrite(attid, atype, text))); herr = 0; } done: if(ncstat != NC_NOERR) { if(text != NULL) { free(text); text = NULL; } } if(attid >= 0) HCHECK((H5Aclose(attid))); if(aspace >= 0) HCHECK((H5Sclose(aspace))); if(atype >= 0) HCHECK((H5Tclose(atype))); return ncstat; }
int NC4_write_ncproperties(NC_FILE_INFO_T* h5) { int retval = NC_NOERR; hid_t hdf5grpid = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; char* text = NULL; size_t len = 0; LOG((3, "%s", __func__)); /* If the file is read-only, return an error. */ if (h5->no_write) {retval = NC_EPERM; goto done;} hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid; if(H5Aexists(hdf5grpid,NCPROPS) > 0) /* Already exists, no overwrite */ goto done; /* Build the attribute string */ if((retval = NC4_buildpropinfo(&h5->provenance->propattr,&text))) goto done; /* Build the HDF5 string type */ if ((atype = H5Tcopy(H5T_C_S1)) < 0) {retval = NC_EHDFERR; goto done;} if (H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) {retval = NC_EHDFERR; goto done;} if(H5Tset_cset(atype, H5T_CSET_ASCII) < 0) {retval = NC_EHDFERR; goto done;} len = strlen(text); if(H5Tset_size(atype, len) < 0) {retval = NC_EFILEMETA; goto done;} /* Create NCPROPS attribute */ if((aspace = H5Screate(H5S_SCALAR)) < 0) {retval = NC_EFILEMETA; goto done;} if ((attid = H5Acreate(hdf5grpid, NCPROPS, atype, aspace, H5P_DEFAULT)) < 0) {retval = NC_EFILEMETA; goto done;} if (H5Awrite(attid, atype, text) < 0) {retval = NC_EFILEMETA; goto done;} /* Verify */ #if 0 { hid_t spacev, typev; hsize_t dsize, tsize; typev = H5Aget_type(attid); spacev = H5Aget_space(attid); dsize = H5Aget_storage_size(attid); tsize = H5Tget_size(typev); fprintf(stderr,"dsize=%lu tsize=%lu\n",(unsigned long)dsize,(unsigned long)tsize); } #endif done: if(text != NULL) free(text); /* Close out the HDF5 objects */ if(attid > 0 && H5Aclose(attid) < 0) retval = NC_EHDFERR; if(aspace > 0 && H5Sclose(aspace) < 0) retval = NC_EHDFERR; if(atype > 0 && H5Tclose(atype) < 0) retval = NC_EHDFERR; /* For certain errors, actually fail, else log that attribute was invalid and ignore */ switch (retval) { case NC_ENOMEM: case NC_EHDFERR: case NC_EPERM: case NC_EFILEMETA: case NC_NOERR: break; default: LOG((0,"Invalid _NCProperties attribute")); retval = NC_NOERR; break; } return retval; }