2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @internal Add provenance info for netcdf-4 files.
|
|
|
|
*
|
2018-12-07 05:34:40 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata See netcdf/COPYRIGHT file for copying
|
2017-12-05 03:21:14 +08:00
|
|
|
* and redistribution conditions.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2018-07-16 18:50:15 +08:00
|
|
|
|
2016-05-04 11:17:06 +08:00
|
|
|
#include "config.h"
|
2018-08-26 11:44:41 +08:00
|
|
|
#include "nc4internal.h"
|
2018-07-12 21:05:21 +08:00
|
|
|
#include "hdf5internal.h"
|
2019-03-10 11:35:57 +08:00
|
|
|
#include "nc_provenance.h"
|
2018-08-26 11:44:41 +08:00
|
|
|
#include "nclist.h"
|
|
|
|
#include "ncbytes.h"
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
/* Provide a hack to suppress the writing of _NCProperties attribute.
|
|
|
|
This is for creating a file without _NCProperties for testing purposes.
|
|
|
|
*/
|
|
|
|
#undef SUPPRESSNCPROPS
|
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
/* Various Constants */
|
|
|
|
#define NCPROPS_MAX_NAME 1024 /* max key name size */
|
|
|
|
#define NCPROPS_MAX_VALUE 1024 /* max value size */
|
2017-12-05 03:21:14 +08:00
|
|
|
#define HDF5_MAX_NAME 1024 /**< HDF5 max name. */
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
#define ESCAPECHARS "\\=|,"
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/** @internal Check NetCDF return code. */
|
2016-05-04 11:17:06 +08:00
|
|
|
#define NCHECK(expr) {if((expr)!=NC_NOERR) {goto done;}}
|
2017-12-05 03:21:14 +08:00
|
|
|
|
|
|
|
/** @internal Check HDF5 return code. */
|
2016-05-04 11:17:06 +08:00
|
|
|
#define HCHECK(expr) {if((expr)<0) {ncstat = NC_EHDFERR; goto done;}}
|
|
|
|
|
2019-04-19 04:23:20 +08:00
|
|
|
static int NC4_read_ncproperties(NC_FILE_INFO_T* h5, char** propstring);
|
|
|
|
static int NC4_write_ncproperties(NC_FILE_INFO_T* h5);
|
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
static int globalpropinitialized = 0;
|
2019-03-10 11:35:57 +08:00
|
|
|
static NC4_Provenance globalprovenance;
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
2018-08-26 11:44:41 +08:00
|
|
|
* @internal Initialize default provenance info
|
|
|
|
* This will only be used for newly created files
|
|
|
|
* or for opened files that do not contain an _NCProperties
|
|
|
|
* attribute.
|
2017-12-05 03:21:14 +08:00
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-05-04 11:17:06 +08:00
|
|
|
int
|
2018-08-26 11:44:41 +08:00
|
|
|
NC4_provenance_init(void)
|
2016-05-04 11:17:06 +08:00
|
|
|
{
|
|
|
|
int stat = NC_NOERR;
|
2018-08-26 11:44:41 +08:00
|
|
|
char* name = NULL;
|
|
|
|
char* value = NULL;
|
2016-05-04 11:17:06 +08:00
|
|
|
unsigned major,minor,release;
|
2019-03-10 11:35:57 +08:00
|
|
|
NCbytes* buffer = NULL; /* for constructing the global _NCProperties */
|
|
|
|
char printbuf[1024];
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
if(globalpropinitialized)
|
2019-02-19 20:19:37 +08:00
|
|
|
return stat;
|
2018-08-26 11:44:41 +08:00
|
|
|
|
|
|
|
/* Build _NCProperties info */
|
|
|
|
|
|
|
|
/* Initialize globalpropinfo */
|
2019-03-10 11:35:57 +08:00
|
|
|
memset((void*)&globalprovenance,0,sizeof(NC4_Provenance));
|
|
|
|
globalprovenance.version = NCPROPS_VERSION;
|
|
|
|
|
|
|
|
buffer = ncbytesnew();
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2019-04-19 04:23:20 +08:00
|
|
|
/* Insert version as first entry */
|
|
|
|
ncbytescat(buffer,NCPVERSION);
|
2019-03-10 11:35:57 +08:00
|
|
|
ncbytescat(buffer,"=");
|
|
|
|
|
|
|
|
snprintf(printbuf,sizeof(printbuf),"%d",globalprovenance.version);
|
|
|
|
ncbytescat(buffer,printbuf);
|
|
|
|
|
|
|
|
/* Insert the netcdf version */
|
|
|
|
ncbytesappend(buffer,NCPROPSSEP2);
|
|
|
|
ncbytescat(buffer,NCPNCLIB2);
|
|
|
|
ncbytescat(buffer,"=");
|
|
|
|
ncbytescat(buffer,PACKAGE_VERSION);
|
2018-12-07 05:34:40 +08:00
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
/* Insert the HDF5 as underlying storage format library */
|
2019-03-10 11:35:57 +08:00
|
|
|
ncbytesappend(buffer,NCPROPSSEP2);
|
|
|
|
ncbytescat(buffer,NCPHDF5LIB2);
|
|
|
|
ncbytescat(buffer,"=");
|
|
|
|
if((stat = NC4_hdf5get_libversion(&major,&minor,&release))) goto done;
|
|
|
|
snprintf(printbuf,sizeof(printbuf),"%1u.%1u.%1u",major,minor,release);
|
|
|
|
ncbytescat(buffer,printbuf);
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
#ifdef NCPROPERTIES_EXTRA
|
2020-11-15 06:07:08 +08:00
|
|
|
if(NCPROPERTIES_EXTRA != NULL && strlen(NCPROPERTIES_EXTRA) > 0)
|
2020-05-11 23:42:31 +08:00
|
|
|
{ const char* p;
|
2018-08-26 11:44:41 +08:00
|
|
|
/* Add any extra fields */
|
2019-03-10 11:35:57 +08:00
|
|
|
p = NCPROPERTIES_EXTRA;
|
|
|
|
if(p[0] == NCPROPSSEP2) p++; /* If leading separator */
|
|
|
|
ncbytesappend(buffer,NCPROPSSEP2);
|
|
|
|
ncbytescat(buffer,p);
|
2020-05-11 23:42:31 +08:00
|
|
|
}
|
2018-08-26 11:44:41 +08:00
|
|
|
#endif
|
2019-03-10 11:35:57 +08:00
|
|
|
ncbytesnull(buffer);
|
|
|
|
globalprovenance.ncproperties = ncbytesextract(buffer);
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2016-05-04 11:17:06 +08:00
|
|
|
done:
|
2019-03-10 11:35:57 +08:00
|
|
|
ncbytesfree(buffer);
|
2018-08-26 11:44:41 +08:00
|
|
|
if(name != NULL) free(name);
|
|
|
|
if(value != NULL) free(value);
|
|
|
|
if(stat == NC_NOERR)
|
|
|
|
globalpropinitialized = 1; /* avoid repeating it */
|
2016-05-04 11:17:06 +08:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:48:12 +08:00
|
|
|
/**
|
|
|
|
* @internal finalize default provenance info
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_provenance_finalize(void)
|
|
|
|
{
|
2019-03-10 11:35:57 +08:00
|
|
|
return NC4_clear_provenance(&globalprovenance);
|
2016-05-04 11:17:06 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
2019-03-10 11:35:57 +08:00
|
|
|
* Construct the provenance information for a newly created file.
|
2018-08-26 11:44:41 +08:00
|
|
|
* Note that creation of the _NCProperties attribute is deferred
|
|
|
|
* to the sync_netcdf4_file function.
|
|
|
|
*
|
|
|
|
* @param file Pointer to file object.
|
2017-12-05 03:21:14 +08:00
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
2018-08-26 11:44:41 +08:00
|
|
|
* [Note: other errors are reported via LOG()]
|
2017-12-05 03:21:14 +08:00
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-05-04 11:17:06 +08:00
|
|
|
int
|
2019-03-10 11:35:57 +08:00
|
|
|
NC4_new_provenance(NC_FILE_INFO_T* file)
|
2016-05-04 11:17:06 +08:00
|
|
|
{
|
|
|
|
int ncstat = NC_NOERR;
|
2019-03-10 11:35:57 +08:00
|
|
|
NC4_Provenance* provenance = NULL;
|
2018-08-26 11:44:41 +08:00
|
|
|
int superblock = -1;
|
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
LOG((5, "%s: ncid 0x%x", __func__, file->root_grp->hdr.id));
|
2018-11-17 06:31:37 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
assert(file->provenance.ncproperties == NULL); /* not yet defined */
|
2018-11-17 06:31:37 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
provenance = &file->provenance;
|
|
|
|
memset(provenance,0,sizeof(NC4_Provenance)); /* make sure */
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
/* Set the version */
|
|
|
|
provenance->version = globalprovenance.version;
|
|
|
|
|
|
|
|
/* Set the superblock number */
|
|
|
|
if((ncstat = NC4_hdf5get_superblock(file,&superblock))) goto done;
|
2018-08-26 11:44:41 +08:00
|
|
|
provenance->superblockversion = superblock;
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
if(globalprovenance.ncproperties != NULL) {
|
|
|
|
if((provenance->ncproperties = strdup(globalprovenance.ncproperties)) == NULL)
|
|
|
|
{ncstat = NC_ENOMEM; goto done;}
|
2016-05-04 11:17:06 +08:00
|
|
|
}
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2016-05-04 11:17:06 +08:00
|
|
|
done:
|
2018-08-26 11:44:41 +08:00
|
|
|
if(ncstat) {
|
2019-02-19 20:19:37 +08:00
|
|
|
LOG((0,"Could not create _NCProperties attribute"));
|
2019-03-10 11:35:57 +08:00
|
|
|
}
|
2018-08-26 11:44:41 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
2019-03-10 11:35:57 +08:00
|
|
|
* Construct the provenance information for an existing file.
|
2018-08-26 11:44:41 +08:00
|
|
|
*
|
|
|
|
* @param file Pointer to file object.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
2019-03-10 11:35:57 +08:00
|
|
|
* [Note: other errors are reported via LOG()]
|
2018-08-26 11:44:41 +08:00
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
int
|
2019-03-10 11:35:57 +08:00
|
|
|
NC4_read_provenance(NC_FILE_INFO_T* file)
|
2018-08-26 11:44:41 +08:00
|
|
|
{
|
|
|
|
int ncstat = NC_NOERR;
|
2019-03-10 11:35:57 +08:00
|
|
|
NC4_Provenance* provenance = NULL;
|
2018-08-26 11:44:41 +08:00
|
|
|
int superblock = -1;
|
2019-03-10 11:35:57 +08:00
|
|
|
char* propstring = NULL;
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
LOG((5, "%s: ncid 0x%x", __func__, file->root_grp->hdr.id));
|
2018-11-17 06:31:37 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
assert(file->provenance.version == 0); /* not yet defined */
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
provenance = &file->provenance;
|
|
|
|
memset(provenance,0,sizeof(NC4_Provenance)); /* make sure */
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
/* Set the superblock number */
|
|
|
|
if((ncstat = NC4_hdf5get_superblock(file,&superblock))) goto done;
|
|
|
|
provenance->superblockversion = superblock;
|
2016-08-08 23:24:19 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
/* Read the _NCProperties value from the file */
|
2020-01-09 06:18:31 +08:00
|
|
|
/* We do not return a size and assume the size is that upto the
|
|
|
|
first nul character */
|
2019-03-10 11:35:57 +08:00
|
|
|
if((ncstat = NC4_read_ncproperties(file,&propstring))) goto done;
|
|
|
|
provenance->ncproperties = propstring;
|
2019-04-19 00:53:16 +08:00
|
|
|
propstring = NULL;
|
2018-11-17 06:31:37 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
done:
|
|
|
|
nullfree(propstring);
|
|
|
|
if(ncstat) {
|
|
|
|
LOG((0,"Could not create _NCProperties attribute"));
|
|
|
|
}
|
2018-08-26 11:44:41 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
}
|
2016-08-08 23:24:19 +08:00
|
|
|
|
2019-04-19 04:23:20 +08:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* Add the provenance information to a newly created file.
|
|
|
|
*
|
|
|
|
* @param file Pointer to file object.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* [Note: other errors are reported via LOG()]
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2018-08-26 11:44:41 +08:00
|
|
|
int
|
2019-04-19 04:23:20 +08:00
|
|
|
NC4_write_provenance(NC_FILE_INFO_T* file)
|
|
|
|
{
|
|
|
|
int ncstat = NC_NOERR;
|
|
|
|
if((ncstat = NC4_write_ncproperties(file)))
|
|
|
|
goto done;
|
|
|
|
done:
|
|
|
|
return ncstat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HDF5 Specific attribute read/write of _NCProperties */
|
|
|
|
static int
|
2019-03-10 11:35:57 +08:00
|
|
|
NC4_read_ncproperties(NC_FILE_INFO_T* h5, char** propstring)
|
2018-08-26 11:44:41 +08:00
|
|
|
{
|
|
|
|
int retval = NC_NOERR;
|
|
|
|
hid_t hdf5grpid = -1;
|
|
|
|
hid_t attid = -1;
|
|
|
|
hid_t aspace = -1;
|
|
|
|
hid_t atype = -1;
|
|
|
|
hid_t ntype = -1;
|
|
|
|
char* text = NULL;
|
|
|
|
H5T_class_t t_class;
|
|
|
|
hsize_t size;
|
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
LOG((5, "%s", __func__));
|
2018-11-17 06:31:37 +08:00
|
|
|
|
2018-11-13 04:06:15 +08:00
|
|
|
hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid;
|
2016-08-08 23:24:19 +08:00
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
if(H5Aexists(hdf5grpid,NCPROPS) <= 0) { /* Does not exist */
|
2019-03-10 11:35:57 +08:00
|
|
|
/* File did not contain a _NCProperties attribute; leave empty */
|
2018-08-26 11:44:41 +08:00
|
|
|
goto done;
|
2016-08-08 23:24:19 +08:00
|
|
|
}
|
2018-08-26 11:44:41 +08:00
|
|
|
|
|
|
|
/* NCPROPS Attribute exists, make sure it is legitimate */
|
2020-09-26 00:41:37 +08:00
|
|
|
attid = H5Aopen_by_name(hdf5grpid, ".", NCPROPS, H5P_DEFAULT, H5P_DEFAULT);
|
2018-08-26 11:44:41 +08:00
|
|
|
assert(attid > 0);
|
|
|
|
aspace = H5Aget_space(attid);
|
|
|
|
atype = H5Aget_type(attid);
|
|
|
|
/* Verify atype and size */
|
|
|
|
t_class = H5Tget_class(atype);
|
|
|
|
if(t_class != H5T_STRING)
|
2019-02-19 20:19:37 +08:00
|
|
|
{retval = NC_EINVAL; goto done;}
|
2018-08-26 11:44:41 +08:00
|
|
|
size = H5Tget_size(atype);
|
|
|
|
if(size == 0)
|
2019-02-19 20:19:37 +08:00
|
|
|
{retval = NC_EINVAL; goto done;}
|
2018-08-26 11:44:41 +08:00
|
|
|
text = (char*)malloc(1+(size_t)size);
|
|
|
|
if(text == NULL)
|
2019-02-19 20:19:37 +08:00
|
|
|
{retval = NC_ENOMEM; goto done;}
|
2018-08-26 11:44:41 +08:00
|
|
|
if((ntype = H5Tget_native_type(atype, H5T_DIR_DEFAULT)) < 0)
|
2019-02-19 20:19:37 +08:00
|
|
|
{retval = NC_EHDFERR; goto done;}
|
2018-08-26 11:44:41 +08:00
|
|
|
if((H5Aread(attid, ntype, text)) < 0)
|
2019-02-19 20:19:37 +08:00
|
|
|
{retval = NC_EHDFERR; goto done;}
|
2018-08-26 11:44:41 +08:00
|
|
|
/* Make sure its null terminated */
|
|
|
|
text[(size_t)size] = '\0';
|
2019-03-10 11:35:57 +08:00
|
|
|
if(propstring) {*propstring = text; text = NULL;}
|
2018-08-26 11:44:41 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
if(ntype > 0 && H5Tclose(ntype) < 0) retval = NC_EHDFERR;
|
|
|
|
|
|
|
|
/* For certain errors, actually fail, else log that attribute was invalid and ignore */
|
2018-11-17 06:31:37 +08:00
|
|
|
if(retval != NC_NOERR) {
|
2019-02-19 20:19:37 +08:00
|
|
|
if(retval != NC_ENOMEM && retval != NC_EHDFERR) {
|
|
|
|
LOG((0,"Invalid _NCProperties attribute: ignored"));
|
|
|
|
retval = NC_NOERR;
|
|
|
|
}
|
2016-08-08 23:24:19 +08:00
|
|
|
}
|
2018-08-26 11:44:41 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2016-09-15 06:26:08 +08:00
|
|
|
|
2019-04-19 04:23:20 +08:00
|
|
|
static int
|
2018-08-26 11:44:41 +08:00
|
|
|
NC4_write_ncproperties(NC_FILE_INFO_T* h5)
|
|
|
|
{
|
2019-03-10 11:35:57 +08:00
|
|
|
#ifdef SUPPRESSNCPROPERTY
|
|
|
|
return NC_NOERR;
|
|
|
|
#else /*!SUPPRESSNCPROPERTY*/
|
2018-08-26 11:44:41 +08:00
|
|
|
int retval = NC_NOERR;
|
|
|
|
hid_t hdf5grpid = -1;
|
|
|
|
hid_t attid = -1;
|
|
|
|
hid_t aspace = -1;
|
|
|
|
hid_t atype = -1;
|
|
|
|
size_t len = 0;
|
2019-03-10 11:35:57 +08:00
|
|
|
NC4_Provenance* prov = &h5->provenance;
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
LOG((5, "%s", __func__));
|
2018-11-17 06:31:37 +08:00
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
/* If the file is read-only, return an error. */
|
|
|
|
if (h5->no_write)
|
2019-02-19 20:19:37 +08:00
|
|
|
{retval = NC_EPERM; goto done;}
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2018-11-13 04:06:15 +08:00
|
|
|
hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid;
|
2018-08-26 11:44:41 +08:00
|
|
|
|
|
|
|
if(H5Aexists(hdf5grpid,NCPROPS) > 0) /* Already exists, no overwrite */
|
2019-02-19 20:19:37 +08:00
|
|
|
goto done;
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
/* Build the property if we have legit value */
|
|
|
|
if(prov->ncproperties != NULL) {
|
|
|
|
/* 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(prov->ncproperties);
|
|
|
|
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;}
|
2021-04-23 06:16:05 +08:00
|
|
|
if ((attid = H5Acreate1(hdf5grpid, NCPROPS, atype, aspace, H5P_DEFAULT)) < 0)
|
2019-03-10 11:35:57 +08:00
|
|
|
{retval = NC_EFILEMETA; goto done;}
|
|
|
|
if (H5Awrite(attid, atype, prov->ncproperties) < 0)
|
|
|
|
{retval = NC_EFILEMETA; goto done;}
|
2018-11-17 06:31:37 +08:00
|
|
|
/* Verify */
|
|
|
|
#if 0
|
2019-02-19 20:19:37 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2018-11-17 06:31:37 +08:00
|
|
|
#endif
|
2019-03-10 11:35:57 +08:00
|
|
|
}
|
2018-11-17 06:31:37 +08:00
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
done:
|
|
|
|
/* 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:
|
2019-02-19 20:19:37 +08:00
|
|
|
break;
|
2018-08-26 11:44:41 +08:00
|
|
|
default:
|
2019-02-19 20:19:37 +08:00
|
|
|
LOG((0,"Invalid _NCProperties attribute"));
|
|
|
|
retval = NC_NOERR;
|
|
|
|
break;
|
2018-08-26 11:44:41 +08:00
|
|
|
}
|
|
|
|
return retval;
|
2019-03-10 11:35:57 +08:00
|
|
|
#endif /*!SUPPRESSNCPROPERTY*/
|
2016-08-08 23:24:19 +08:00
|
|
|
}
|
2018-08-26 11:44:41 +08:00
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
/**************************************************/
|
|
|
|
/* Utilities */
|
|
|
|
|
2018-08-26 11:44:41 +08:00
|
|
|
/* Debugging */
|
|
|
|
|
|
|
|
void
|
2019-03-10 11:35:57 +08:00
|
|
|
ncprintprovenance(NC4_Provenance* info)
|
2018-08-26 11:44:41 +08:00
|
|
|
{
|
2019-03-10 11:35:57 +08:00
|
|
|
fprintf(stderr,"[%p] version=%d superblockversion=%d ncproperties=|%s|\n",
|
|
|
|
info,
|
|
|
|
info->version,
|
|
|
|
info->superblockversion,
|
|
|
|
(info->ncproperties==NULL?"":info->ncproperties));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* Clear the NCPROVENANCE object; do not free it
|
|
|
|
* @param prov Pointer to provenance object
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_clear_provenance(NC4_Provenance* prov)
|
|
|
|
{
|
|
|
|
LOG((5, "%s", __func__));
|
|
|
|
|
|
|
|
if(prov == NULL) return NC_NOERR;
|
|
|
|
nullfree(prov->ncproperties);
|
|
|
|
memset(prov,0,sizeof(NC4_Provenance));
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Unused functions */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Parse file properties.
|
|
|
|
*
|
|
|
|
* @param text0 Text properties.
|
|
|
|
* @param pairs list of parsed (key,value) pairs
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
properties_parse(const char* text0, NClist* pairs)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
char* p;
|
|
|
|
char* q;
|
|
|
|
char* text = NULL;
|
|
|
|
|
|
|
|
if(text0 == NULL || strlen(text0) == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
text = strdup(text0);
|
|
|
|
if(text == NULL) return NC_ENOMEM;
|
|
|
|
|
|
|
|
/* For back compatibility with version 1, translate '|' -> ',' */
|
|
|
|
for(p=text;*p;p++) {
|
|
|
|
if(*p == NCPROPSSEP1)
|
|
|
|
*p = NCPROPSSEP2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Walk and fill in ncinfo */
|
|
|
|
p = text;
|
|
|
|
while(*p) {
|
|
|
|
char* name = p;
|
|
|
|
char* value = NULL;
|
|
|
|
char* next = NULL;
|
|
|
|
|
|
|
|
/* Delimit whole (key,value) pair */
|
|
|
|
q = locate(p,NCPROPSSEP2);
|
|
|
|
if(*q != '\0') /* Never go beyond the final nul term */
|
|
|
|
*q++ = '\0';
|
|
|
|
next = q;
|
|
|
|
/* split key and value */
|
|
|
|
q = locate(p,'=');
|
|
|
|
name = p;
|
|
|
|
*q++ = '\0';
|
|
|
|
value = q;
|
|
|
|
/* Set up p for next iteration */
|
|
|
|
p = next;
|
|
|
|
nclistpush(pairs,strdup(name));
|
|
|
|
nclistpush(pairs,strdup(value));
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if(text) free(text);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate a specific character and return its pointer
|
|
|
|
or EOS if not found
|
|
|
|
take \ escapes into account */
|
|
|
|
static char*
|
|
|
|
locate(char* p, char tag)
|
|
|
|
{
|
|
|
|
char* next;
|
|
|
|
int c;
|
|
|
|
assert(p != NULL);
|
|
|
|
for(next = p;(c = *next);next++) {
|
|
|
|
if(c == tag)
|
|
|
|
return next;
|
|
|
|
else if(c == '\\' && next[1] != '\0')
|
|
|
|
next++; /* skip escaped char */
|
|
|
|
}
|
|
|
|
return next; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Utility to transfer a string to a buffer with escaping */
|
|
|
|
static void
|
|
|
|
escapify(NCbytes* buffer, const char* s)
|
|
|
|
{
|
|
|
|
const char* p;
|
|
|
|
for(p=s;*p;p++) {
|
|
|
|
if(strchr(ESCAPECHARS,*p) != NULL)
|
|
|
|
ncbytesappend(buffer,'\\');
|
|
|
|
ncbytesappend(buffer,*p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Build _NCProperties attribute value.
|
|
|
|
*
|
|
|
|
* Convert a NCPROPINFO instance to a single string.
|
|
|
|
* Will always convert to current format
|
|
|
|
*
|
|
|
|
* @param version
|
|
|
|
* @param list Properties list
|
|
|
|
* @param spropp Pointer that gets properties string.
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EINVAL failed.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
build_propstring(int version, NClist* list, char** spropp)
|
|
|
|
{
|
|
|
|
int stat = NC_NOERR;
|
2018-08-26 11:44:41 +08:00
|
|
|
int i;
|
2019-03-10 11:35:57 +08:00
|
|
|
NCbytes* buffer = NULL;
|
|
|
|
char sversion[64];
|
|
|
|
|
|
|
|
LOG((5, "%s version=%d", __func__, version));
|
|
|
|
|
|
|
|
if(spropp != NULL) *spropp = NULL;
|
|
|
|
|
|
|
|
if(version == 0 || version > NCPROPS_VERSION) /* unknown case */
|
|
|
|
goto done;
|
|
|
|
if(list == NULL)
|
|
|
|
{stat = NC_EINVAL; goto done;}
|
|
|
|
|
|
|
|
if((buffer = ncbytesnew()) == NULL)
|
|
|
|
{stat = NC_ENOMEM; goto done;}
|
|
|
|
|
|
|
|
/* start with version */
|
|
|
|
ncbytescat(buffer,NCPVERSION);
|
|
|
|
ncbytesappend(buffer,'=');
|
|
|
|
/* Use current version */
|
|
|
|
snprintf(sversion,sizeof(sversion),"%d",NCPROPS_VERSION);
|
|
|
|
ncbytescat(buffer,sversion);
|
|
|
|
|
|
|
|
for(i=0;i<nclistlength(list);i+=2) {
|
|
|
|
char* value, *name;
|
|
|
|
name = nclistget(list,i);
|
|
|
|
if(name == NULL) continue;
|
|
|
|
value = nclistget(list,i+1);
|
|
|
|
ncbytesappend(buffer,NCPROPSSEP2); /* terminate last entry */
|
|
|
|
escapify(buffer,name);
|
|
|
|
ncbytesappend(buffer,'=');
|
|
|
|
escapify(buffer,value);
|
2018-12-07 05:34:40 +08:00
|
|
|
}
|
2019-03-10 11:35:57 +08:00
|
|
|
/* Force null termination */
|
|
|
|
ncbytesnull(buffer);
|
|
|
|
if(spropp) *spropp = ncbytesextract(buffer);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if(buffer != NULL) ncbytesfree(buffer);
|
|
|
|
return stat;
|
2018-08-26 11:44:41 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 11:35:57 +08:00
|
|
|
static int
|
|
|
|
properties_getversion(const char* propstring, int* versionp)
|
2018-08-26 11:44:41 +08:00
|
|
|
{
|
2019-03-10 11:35:57 +08:00
|
|
|
int ncstat = NC_NOERR;
|
|
|
|
int version = 0;
|
|
|
|
/* propstring should begin with "version=dddd" */
|
|
|
|
if(propstring == NULL || strlen(propstring) < strlen("version=") + strlen("1"))
|
2019-04-19 00:53:16 +08:00
|
|
|
{ncstat = NC_EINVAL; goto done;} /* illegal version */
|
2019-03-10 11:35:57 +08:00
|
|
|
if(memcmp(propstring,"version=",strlen("version=")) != 0)
|
2019-04-19 00:53:16 +08:00
|
|
|
{ncstat = NC_EINVAL; goto done;} /* illegal version */
|
2019-03-10 11:35:57 +08:00
|
|
|
propstring += strlen("version=");
|
|
|
|
/* get version */
|
|
|
|
version = atoi(propstring);
|
|
|
|
if(version < 0)
|
2019-04-19 00:53:16 +08:00
|
|
|
{ncstat = NC_EINVAL; goto done;} /* illegal version */
|
2019-03-10 11:35:57 +08:00
|
|
|
if(versionp) *versionp = version;
|
|
|
|
done:
|
|
|
|
return ncstat;
|
2018-08-26 11:44:41 +08:00
|
|
|
}
|
2019-03-10 11:35:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
2019-04-19 00:53:16 +08:00
|
|
|
* Construct the parsed provenance information
|
2019-03-10 11:35:57 +08:00
|
|
|
*
|
|
|
|
* @param prov Pointer to provenance object
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_ENOMEM
|
|
|
|
* @return ::NC_EINVAL
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_provenance(NC4_Provenance* prov)
|
|
|
|
{
|
|
|
|
int ncstat = NC_NOERR;
|
|
|
|
char *name = NULL;
|
|
|
|
char *value = NULL;
|
|
|
|
int version = 0;
|
|
|
|
NClist* list = NULL;
|
|
|
|
|
|
|
|
LOG((5, "%s: prov 0x%x", __func__, prov));
|
|
|
|
|
|
|
|
if(prov->ncproperty == NULL || strlen(prov->ncproperty) < strlen("version="))
|
|
|
|
{ncstat = NC_EINVAL; goto done;}
|
|
|
|
if((list = nclistnew()) == NULL)
|
|
|
|
{ncstat = NC_ENOMEM; goto done;}
|
|
|
|
|
|
|
|
/* Do we understand the version? */
|
|
|
|
if(prov->version > 0 && prov->version <= NCPROPS_VERSION) {/* recognized version */
|
|
|
|
if((ncstat=properties_parse(prov->ncproperty,list)))
|
|
|
|
goto done;
|
|
|
|
/* Remove version pair from properties list*/
|
|
|
|
if(nclistlength(list) < 2)
|
|
|
|
{ncstat = NC_EINVAL; goto done;} /* bad _NCProperties attribute */
|
|
|
|
/* Throw away the purported version=... */
|
|
|
|
nclistremove(list,0); /* version key */
|
|
|
|
nclistremove(list,0); /* version value */
|
|
|
|
|
|
|
|
/* Now, rebuild to the latest version */
|
|
|
|
switch (version) {
|
|
|
|
default: break; /* do nothing */
|
|
|
|
case 1: {
|
|
|
|
int i;
|
|
|
|
for(i=0;i<nclistlength(list);i+=2) {
|
|
|
|
char* newname = NULL;
|
|
|
|
name = nclistget(list,i);
|
|
|
|
if(name == NULL) continue; /* ignore */
|
|
|
|
if(strcmp(name,NCPNCLIB1) == 0)
|
|
|
|
newname = NCPNCLIB2; /* change name */
|
|
|
|
else if(strcmp(name,NCPHDF5LIB1) == 0)
|
|
|
|
newname = NCPHDF5LIB2;
|
|
|
|
else continue; /* ignore */
|
|
|
|
/* Do any rename */
|
|
|
|
nclistset(list,i,strdup(newname));
|
|
|
|
if(name) {free(name); name = NULL;}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
} /*switch*/
|
|
|
|
}
|
|
|
|
prov->properties = list;
|
|
|
|
list = NULL;
|
|
|
|
|
|
|
|
done:
|
|
|
|
nclistfreeall(list);
|
|
|
|
if(name != NULL) free(name);
|
|
|
|
if(value != NULL) free(value);
|
|
|
|
return ncstat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* Clear and Free the NC4_Provenance object
|
|
|
|
* @param prov Pointer to provenance object
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
NC4_free_provenance(NC4_Provenance* prov)
|
|
|
|
{
|
|
|
|
LOG((5, "%s", __func__));
|
|
|
|
|
|
|
|
if(prov == NULL) return NC_NOERR;
|
|
|
|
NC4_clear_provenance(prov);
|
|
|
|
free(prov);
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Utility to copy contents of the dfalt into an NCPROPINFO object */
|
|
|
|
static int
|
|
|
|
propinfo_default(NC4_Properties* dst, const NC4_Properties* dfalt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if(dst->properties == NULL) {
|
|
|
|
dst->properties = nclistnew();
|
|
|
|
if(dst->properties == NULL) return NC_ENOMEM;
|
|
|
|
}
|
|
|
|
dst->version = dfalt->version;
|
|
|
|
for(i=0;i<nclistlength(dfalt->properties);i++) {
|
|
|
|
char* s = nclistget(dfalt->properties,i);
|
|
|
|
s = strdup(s);
|
|
|
|
if(s == NULL) return NC_ENOMEM;
|
|
|
|
nclistpush(dst->properties,s);
|
|
|
|
}
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*0*/
|