2018-01-31 23:44:33 +08:00
|
|
|
/* Copyright 2003-2018, University Corporation for Atmospheric
|
|
|
|
* Research. See COPYRIGHT file for copying and redistribution
|
|
|
|
* conditions. */
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @internal This file is part of netcdf-4, a netCDF-like interface
|
|
|
|
* for HDF5, or a HDF5 backend for netCDF, depending on your point of
|
|
|
|
* view.
|
|
|
|
*
|
|
|
|
* This file handles the nc4 attribute functions.
|
|
|
|
*
|
|
|
|
* Remember that with atts, type conversion can take place when
|
|
|
|
* writing them, and when reading them.
|
|
|
|
*
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
#include "nc.h"
|
2018-03-17 01:46:18 +08:00
|
|
|
#include "nc4internal.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
#include "nc4dispatch.h"
|
2010-08-05 10:44:59 +08:00
|
|
|
#include "ncdispatch.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
int nc4typelen(nc_type type);
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
2018-01-31 23:44:33 +08:00
|
|
|
* @internal Get special informatation about the attribute.
|
2017-12-05 03:21:14 +08:00
|
|
|
*
|
|
|
|
* @param h5 Pointer to HDF5 file info struct.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param filetypep Pointer that gets type of the attribute data in
|
|
|
|
* file.
|
|
|
|
* @param mem_type Type of attribute data in memory.
|
|
|
|
* @param lenp Pointer that gets length of attribute array.
|
|
|
|
* @param attnump Pointer that gets the attribute number.
|
|
|
|
* @param data Attribute data.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
2018-01-31 23:44:33 +08:00
|
|
|
* @return ::NC_ERANGE Data conversion out of range.
|
2017-12-05 03:21:14 +08:00
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nc4_get_att_special(NC_HDF5_FILE_INFO_T* h5, const char* name,
|
|
|
|
nc_type* filetypep, nc_type mem_type, size_t* lenp,
|
2018-03-17 01:46:18 +08:00
|
|
|
int* attnump, void* data)
|
2017-12-05 03:21:14 +08:00
|
|
|
{
|
|
|
|
/* Fail if asking for att id */
|
|
|
|
if(attnump)
|
|
|
|
return NC_EATTMETA;
|
|
|
|
|
|
|
|
if(strcmp(name,NCPROPS)==0) {
|
|
|
|
char* propdata = NULL;
|
|
|
|
int stat = NC_NOERR;
|
|
|
|
int len;
|
|
|
|
if(h5->fileinfo->propattr.version == 0)
|
|
|
|
return NC_ENOTATT;
|
|
|
|
if(mem_type == NC_NAT) mem_type = NC_CHAR;
|
|
|
|
if(mem_type != NC_CHAR)
|
|
|
|
return NC_ECHAR;
|
|
|
|
if(filetypep) *filetypep = NC_CHAR;
|
|
|
|
stat = NC4_buildpropinfo(&h5->fileinfo->propattr, &propdata);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
|
|
|
len = strlen(propdata);
|
|
|
|
if(lenp) *lenp = len;
|
|
|
|
if(data) strncpy((char*)data,propdata,len+1);
|
|
|
|
free(propdata);
|
|
|
|
} else if(strcmp(name,ISNETCDF4ATT)==0
|
|
|
|
|| strcmp(name,SUPERBLOCKATT)==0) {
|
|
|
|
unsigned long long iv = 0;
|
|
|
|
if(filetypep) *filetypep = NC_INT;
|
|
|
|
if(lenp) *lenp = 1;
|
|
|
|
if(strcmp(name,SUPERBLOCKATT)==0)
|
|
|
|
iv = (unsigned long long)h5->fileinfo->superblockversion;
|
|
|
|
else /* strcmp(name,ISNETCDF4ATT)==0 */
|
|
|
|
iv = NC4_isnetcdf4(h5);
|
|
|
|
if(mem_type == NC_NAT) mem_type = NC_INT;
|
|
|
|
if(data)
|
|
|
|
switch (mem_type) {
|
|
|
|
case NC_BYTE: *((char*)data) = (char)iv; break;
|
|
|
|
case NC_SHORT: *((short*)data) = (short)iv; break;
|
|
|
|
case NC_INT: *((int*)data) = (int)iv; break;
|
|
|
|
case NC_UBYTE: *((unsigned char*)data) = (unsigned char)iv; break;
|
|
|
|
case NC_USHORT: *((unsigned short*)data) = (unsigned short)iv; break;
|
|
|
|
case NC_UINT: *((unsigned int*)data) = (unsigned int)iv; break;
|
|
|
|
case NC_INT64: *((long long*)data) = (long long)iv; break;
|
|
|
|
case NC_UINT64: *((unsigned long long*)data) = (unsigned long long)iv; break;
|
|
|
|
default:
|
|
|
|
return NC_ERANGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
|
|
|
* @internal Get or put attribute metadata from our linked list of
|
|
|
|
* file info. Always locate the attribute by name, never by attnum.
|
|
|
|
* The mem_type is ignored if data=NULL.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param xtype Pointer that gets (file) type of attribute.
|
|
|
|
* @param mem_type The type of attribute data in memory.
|
|
|
|
* @param lenp Pointer that gets length of attribute array.
|
|
|
|
* @param attnum Pointer that gets the index number of this attribute.
|
|
|
|
* @param data Pointer that gets attribute data.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2018-01-31 23:44:33 +08:00
|
|
|
static int
|
|
|
|
nc4_get_att(int ncid, int varid, const char *name, nc_type *xtype,
|
|
|
|
nc_type mem_type, size_t *lenp, int *attnum, void *data)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2018-01-31 23:44:33 +08:00
|
|
|
NC *nc;
|
2010-06-03 21:24:43 +08:00
|
|
|
NC_GRP_INFO_T *grp;
|
|
|
|
NC_HDF5_FILE_INFO_T *h5;
|
2013-04-24 05:50:07 +08:00
|
|
|
NC_ATT_INFO_T *att = NULL;
|
2010-06-03 21:24:43 +08:00
|
|
|
int my_attnum = -1;
|
|
|
|
int need_to_convert = 0;
|
|
|
|
int range_error = NC_NOERR;
|
2014-10-02 07:04:56 +08:00
|
|
|
void *bufr = NULL;
|
2010-06-03 21:24:43 +08:00
|
|
|
size_t type_size;
|
|
|
|
char norm_name[NC_MAX_NAME + 1];
|
|
|
|
int i;
|
2018-01-31 23:44:33 +08:00
|
|
|
int retval;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
if (attnum)
|
2010-06-03 21:24:43 +08:00
|
|
|
my_attnum = *attnum;
|
|
|
|
|
2014-10-02 07:04:56 +08:00
|
|
|
LOG((3, "%s: ncid 0x%x varid %d name %s attnum %d mem_type %d",
|
2017-12-04 22:07:45 +08:00
|
|
|
__func__, ncid, varid, name, my_attnum, mem_type));
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Find info for this file, group, and h5 info. */
|
|
|
|
if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
|
|
|
|
return retval;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2016-10-13 02:33:17 +08:00
|
|
|
/* Check varid */
|
|
|
|
if (varid != NC_GLOBAL) {
|
2018-03-17 01:46:18 +08:00
|
|
|
NC_VAR_INFO_T* var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
|
|
|
|
if(var == NULL)
|
2017-12-04 22:07:45 +08:00
|
|
|
return NC_ENOTVAR;
|
2018-03-17 01:46:18 +08:00
|
|
|
assert(var->hdr.id == varid);
|
2016-10-13 02:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name == NULL)
|
2017-12-04 22:07:45 +08:00
|
|
|
BAIL(NC_EBADNAME);
|
2016-10-13 02:33:17 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Normalize name. */
|
|
|
|
if ((retval = nc4_normalize_name(name, norm_name)))
|
2014-02-14 22:07:14 +08:00
|
|
|
BAIL(retval);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* If this is one of the reserved atts, use nc_get_att_special. */
|
|
|
|
if (nc->ext_ncid == ncid && varid == NC_GLOBAL) {
|
2018-03-17 01:46:18 +08:00
|
|
|
const NC_reservedatt* ra = NC_findreserved(norm_name);
|
|
|
|
if(ra != NULL && (ra->flags & NAMEONLYFLAG))
|
|
|
|
return nc4_get_att_special(h5, norm_name, xtype, mem_type, lenp, attnum, data);
|
2017-12-04 22:07:45 +08:00
|
|
|
}
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Find the attribute, if it exists. */
|
|
|
|
if ((retval = nc4_find_grp_att(grp, varid, norm_name, my_attnum, &att)))
|
|
|
|
return retval;
|
2014-10-02 07:04:56 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* If mem_type is NC_NAT, it means we want to use the attribute's
|
|
|
|
* file type as the mem type as well. */
|
|
|
|
if (mem_type == NC_NAT)
|
2014-02-12 07:12:08 +08:00
|
|
|
mem_type = att->nc_typeid;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* If the attribute is NC_CHAR, and the mem_type isn't, or vice
|
|
|
|
* versa, that's a freakish attempt to convert text to
|
|
|
|
* numbers. Some pervert out there is trying to pull a fast one!
|
2018-01-31 23:44:33 +08:00
|
|
|
* Send him an NC_ECHAR error. */
|
|
|
|
if (data && att->len)
|
|
|
|
if ((att->nc_typeid == NC_CHAR && mem_type != NC_CHAR) ||
|
|
|
|
(att->nc_typeid != NC_CHAR && mem_type == NC_CHAR))
|
|
|
|
BAIL(NC_ECHAR); /* take that, you freak! */
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Copy the info. */
|
|
|
|
if (lenp)
|
|
|
|
*lenp = att->len;
|
|
|
|
if (xtype)
|
2014-02-12 07:12:08 +08:00
|
|
|
*xtype = att->nc_typeid;
|
2016-05-04 11:17:06 +08:00
|
|
|
if (attnum) {
|
2018-03-17 01:46:18 +08:00
|
|
|
*attnum = att->hdr.id;
|
2016-05-04 11:17:06 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Zero len attributes are easy to read! */
|
|
|
|
if (!att->len)
|
2014-02-14 22:07:14 +08:00
|
|
|
BAIL(NC_NOERR);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Later on, we will need to know the size of this type. */
|
2018-03-17 01:46:18 +08:00
|
|
|
if ((retval = nc4_get_typelen_mem(h5, mem_type, &type_size)))
|
2014-02-14 22:07:14 +08:00
|
|
|
BAIL(retval);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* We may have to convert data. Treat NC_CHAR the same as
|
|
|
|
* NC_UBYTE. If the mem_type is NAT, don't try any conversion - use
|
|
|
|
* the attribute's type. */
|
2014-02-12 07:12:08 +08:00
|
|
|
if (data && att->len && mem_type != att->nc_typeid &&
|
2010-06-03 21:24:43 +08:00
|
|
|
mem_type != NC_NAT &&
|
2014-10-02 07:04:56 +08:00
|
|
|
!(mem_type == NC_CHAR &&
|
2017-12-04 22:07:45 +08:00
|
|
|
(att->nc_typeid == NC_UBYTE || att->nc_typeid == NC_BYTE)))
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
if (!(bufr = malloc((size_t)(att->len * type_size))))
|
2017-12-04 22:07:45 +08:00
|
|
|
BAIL(NC_ENOMEM);
|
2014-02-14 22:07:14 +08:00
|
|
|
need_to_convert++;
|
2014-10-02 07:04:56 +08:00
|
|
|
if ((retval = nc4_convert_type(att->data, bufr, att->nc_typeid,
|
2017-12-04 22:07:45 +08:00
|
|
|
mem_type, (size_t)att->len, &range_error,
|
2018-01-31 23:44:33 +08:00
|
|
|
NULL, (h5->cmode & NC_CLASSIC_MODEL), 0, 0)))
|
2017-12-04 22:07:45 +08:00
|
|
|
BAIL(retval);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* For strict netcdf-3 rules, ignore erange errors between UBYTE
|
|
|
|
* and BYTE types. */
|
|
|
|
if ((h5->cmode & NC_CLASSIC_MODEL) &&
|
2017-12-04 22:07:45 +08:00
|
|
|
(att->nc_typeid == NC_UBYTE || att->nc_typeid == NC_BYTE) &&
|
|
|
|
(mem_type == NC_UBYTE || mem_type == NC_BYTE) &&
|
|
|
|
range_error)
|
|
|
|
range_error = 0;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufr = att->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the caller wants data, copy it for him. If he hasn't
|
Corrected "BAIL" macros to avoid infinite loop when logging is disabled and an
error occurs after an "exit:" label.
Corrected a dozen Coverity errors (mainly allocation issues, along with a few
other things):
711711, 711802, 711803, 711905, 970825, 996123, 996124, 1025787,
1047274, 1130013, 1130014, 1139538
Refactored internal fill-value code to correctly handle string types, and
especially to allow NULL pointers and null strings (ie. "") to be
distinguished. The code now avoids partially aliasing the two together
(which only happened on the 'write' side of things and wasn't reflected on
the 'read' side, adding to the previous confusion).
Probably still weak on handling fill-values of variable-length and compound
datatypes.
Refactored the recursive metadata reads a bit more, to process HDF5 named
datatypes and datasets immediately, avoiding chewing up memory for those
types of objects, etc.
Finished uncommenting and updating the nc_test4/tst_fills2.c code (as I'm
proceeding alphabetically through the nc_test4 code files).
2013-12-29 15:12:43 +08:00
|
|
|
allocated enough memory for it, he will burn in segmentation
|
2010-06-03 21:24:43 +08:00
|
|
|
fault hell, writhing with the agony of undiscovered memory
|
|
|
|
bugs! */
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
if (att->vldata)
|
|
|
|
{
|
2017-12-04 22:07:45 +08:00
|
|
|
size_t base_typelen;
|
|
|
|
hvl_t *vldest = data;
|
|
|
|
NC_TYPE_INFO_T *type;
|
2014-02-12 07:12:08 +08:00
|
|
|
|
2014-02-14 22:07:14 +08:00
|
|
|
/* Get the type object for the attribute's type */
|
2017-12-04 22:07:45 +08:00
|
|
|
if ((retval = nc4_find_type(h5, att->nc_typeid, &type)))
|
|
|
|
BAIL(retval);
|
2014-02-14 22:07:14 +08:00
|
|
|
|
|
|
|
/* Retrieve the size of the base type */
|
2018-03-17 01:46:18 +08:00
|
|
|
if ((retval = nc4_get_typelen_mem(h5, type->u.v.base_nc_typeid, &base_typelen)))
|
2014-02-14 22:07:14 +08:00
|
|
|
BAIL(retval);
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
for (i = 0; i < att->len; i++)
|
|
|
|
{
|
|
|
|
vldest[i].len = att->vldata[i].len;
|
|
|
|
if (!(vldest[i].p = malloc(vldest[i].len * base_typelen)))
|
|
|
|
BAIL(NC_ENOMEM);
|
|
|
|
memcpy(vldest[i].p, att->vldata[i].p, vldest[i].len * base_typelen);
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
else if (att->stdata)
|
|
|
|
{
|
2017-12-04 22:07:45 +08:00
|
|
|
for (i = 0; i < att->len; i++)
|
|
|
|
{
|
2013-07-11 04:09:31 +08:00
|
|
|
/* Check for NULL pointer for string (valid in HDF5) */
|
|
|
|
if(att->stdata[i])
|
|
|
|
{
|
2017-12-04 22:07:45 +08:00
|
|
|
if (!(((char **)data)[i] = strdup(att->stdata[i])))
|
|
|
|
BAIL(NC_ENOMEM);
|
2013-07-11 04:09:31 +08:00
|
|
|
}
|
|
|
|
else
|
2017-12-04 22:07:45 +08:00
|
|
|
((char **)data)[i] = att->stdata[i];
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-31 23:44:33 +08:00
|
|
|
memcpy(data, bufr, (size_t)(att->len * type_size));
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
exit:
|
2014-02-12 07:12:08 +08:00
|
|
|
if (need_to_convert)
|
|
|
|
free(bufr);
|
2010-06-03 21:24:43 +08:00
|
|
|
if (range_error)
|
2014-02-14 22:07:14 +08:00
|
|
|
retval = NC_ERANGE;
|
|
|
|
return retval;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
2018-01-31 23:44:33 +08:00
|
|
|
* @internal Learn about an att. All the nc4 nc_inq_ functions just
|
|
|
|
* call nc4_get_att to get the metadata on an attribute.
|
2017-12-04 22:07:45 +08:00
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
2018-01-31 23:44:33 +08:00
|
|
|
* @param xtypep Pointer that gets type of attribute.
|
|
|
|
* @param lenp Pointer that gets length of attribute data array.
|
2017-12-04 22:07:45 +08:00
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2018-01-31 23:44:33 +08:00
|
|
|
int
|
|
|
|
NC4_inq_att(int ncid, int varid, const char *name, nc_type *xtypep,
|
|
|
|
size_t *lenp)
|
|
|
|
{
|
|
|
|
LOG((2, "%s: ncid 0x%x varid %d name %s", __func__, ncid, varid, name));
|
|
|
|
return nc4_get_att(ncid, varid, name, xtypep, NC_NAT, lenp, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Learn an attnum, given a name.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param attnump Pointer that gets the attribute index number.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_inq_attid(int ncid, int varid, const char *name, int *attnump)
|
|
|
|
{
|
|
|
|
LOG((2, "%s: ncid 0x%x varid %d name %s", __func__, ncid, varid, name));
|
|
|
|
return nc4_get_att(ncid, varid, name, NULL, NC_NAT, NULL, attnump, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Given an attnum, find the att's name.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param attnum The index number of the attribute.
|
|
|
|
* @param name Pointer that gets name of attrribute.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_inq_attname(int ncid, int varid, int attnum, char *name)
|
|
|
|
{
|
|
|
|
NC *nc;
|
|
|
|
NC_ATT_INFO_T *att;
|
|
|
|
NC_HDF5_FILE_INFO_T *h5;
|
|
|
|
int retval = NC_NOERR;
|
|
|
|
|
|
|
|
LOG((2, "nc_inq_attname: ncid 0x%x varid %d attnum %d",
|
|
|
|
ncid, varid, attnum));
|
|
|
|
|
|
|
|
/* Find metadata. */
|
|
|
|
if (!(nc = nc4_find_nc_file(ncid,NULL)))
|
|
|
|
return NC_EBADID;
|
|
|
|
|
|
|
|
/* get netcdf-4 metadata */
|
|
|
|
h5 = NC4_DATA(nc);
|
|
|
|
assert(h5);
|
|
|
|
|
|
|
|
/* Handle netcdf-4 files. */
|
|
|
|
if ((retval = nc4_find_nc_att(ncid, varid, NULL, attnum, &att)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Get the name. */
|
|
|
|
if (name)
|
2018-03-17 01:46:18 +08:00
|
|
|
strcpy(name, att->hdr.name);
|
2018-01-31 23:44:33 +08:00
|
|
|
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
/**
|
|
|
|
* @internal Get the attribute list for either a varid or NC_GLOBAL
|
|
|
|
*
|
|
|
|
* @param grp Group
|
|
|
|
* @param varid Variable ID | NC_BLOGAL
|
|
|
|
* @param varp Pointer into which to return created NC_VAR_INFO_T instance
|
|
|
|
*
|
|
|
|
* @return Attribute list | NULL
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
static NCindex*
|
|
|
|
getattlist(NC_GRP_INFO_T* grp, int varid, NC_VAR_INFO_T** varp)
|
|
|
|
{
|
|
|
|
if (varid == NC_GLOBAL) {
|
|
|
|
if(varp) *varp = NULL;
|
|
|
|
return grp->att;
|
|
|
|
} else {
|
|
|
|
NC_VAR_INFO_T* var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
|
|
|
|
if (!var) return NULL;
|
|
|
|
assert(var->hdr.id == varid);
|
|
|
|
if(varp) *varp = var;
|
|
|
|
return var->att;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/**
|
|
|
|
* @internal I think all atts should be named the exact same thing, to
|
|
|
|
* avoid confusion!
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param newname New name for attribute.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_rename_att(int ncid, int varid, const char *name, const char *newname)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2018-01-31 23:44:33 +08:00
|
|
|
NC *nc;
|
2014-10-02 07:04:56 +08:00
|
|
|
NC_GRP_INFO_T *grp;
|
2010-06-03 21:24:43 +08:00
|
|
|
NC_HDF5_FILE_INFO_T *h5;
|
|
|
|
NC_VAR_INFO_T *var = NULL;
|
2018-03-17 01:46:18 +08:00
|
|
|
NC_ATT_INFO_T *att;
|
|
|
|
NCindex *list;
|
2018-01-31 23:44:33 +08:00
|
|
|
char norm_newname[NC_MAX_NAME + 1], norm_name[NC_MAX_NAME + 1];
|
|
|
|
hid_t datasetid = 0;
|
|
|
|
int retval = NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
if (!name || !newname)
|
|
|
|
return NC_EINVAL;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
LOG((2, "nc_rename_att: ncid 0x%x varid %d name %s newname %s",
|
|
|
|
ncid, varid, name, newname));
|
2017-02-04 02:19:39 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* If the new name is too long, that's an error. */
|
|
|
|
if (strlen(newname) > NC_MAX_NAME)
|
|
|
|
return NC_EMAXNAME;
|
2017-02-04 02:19:39 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Find info for this file, group, and h5 info. */
|
|
|
|
if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
|
|
|
|
return retval;
|
|
|
|
assert(h5 && grp && h5);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* If the file is read-only, return an error. */
|
|
|
|
if (h5->no_write)
|
2017-12-04 22:07:45 +08:00
|
|
|
return NC_EPERM;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Check and normalize the name. */
|
|
|
|
if ((retval = nc4_check_name(newname, norm_newname)))
|
|
|
|
return retval;
|
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
/* Is new name in use? */
|
|
|
|
list = getattlist(grp,varid,&var);
|
|
|
|
if(list == NULL)
|
|
|
|
return NC_ENOTVAR;
|
|
|
|
|
|
|
|
att = (NC_ATT_INFO_T*)ncindexlookup(list,norm_newname);
|
|
|
|
if(att != NULL)
|
2018-01-31 23:44:33 +08:00
|
|
|
return NC_ENAMEINUSE;
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Normalize name and find the attribute. */
|
|
|
|
if ((retval = nc4_normalize_name(name, norm_name)))
|
2016-10-13 02:33:17 +08:00
|
|
|
return retval;
|
2018-03-17 01:46:18 +08:00
|
|
|
|
|
|
|
att = (NC_ATT_INFO_T*)ncindexlookup(list,norm_name);
|
2018-01-31 23:44:33 +08:00
|
|
|
if (!att)
|
|
|
|
return NC_ENOTATT;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* If we're not in define mode, new name must be of equal or
|
|
|
|
less size, if complying with strict NC3 rules. */
|
2018-03-17 01:46:18 +08:00
|
|
|
if (!(h5->flags & NC_INDEF) && strlen(norm_newname) > strlen(att->hdr.name) &&
|
2018-01-31 23:44:33 +08:00
|
|
|
(h5->cmode & NC_CLASSIC_MODEL))
|
|
|
|
return NC_ENOTINDEFINE;
|
2016-10-13 02:33:17 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Delete the original attribute, if it exists in the HDF5 file. */
|
|
|
|
if (att->created)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2018-01-31 23:44:33 +08:00
|
|
|
if (varid == NC_GLOBAL)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2018-03-17 01:46:18 +08:00
|
|
|
if (H5Adelete(grp->hdf_grpid, att->hdr.name) < 0)
|
2018-01-31 23:44:33 +08:00
|
|
|
return NC_EHDFERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2018-01-31 23:44:33 +08:00
|
|
|
else
|
2017-12-04 22:07:45 +08:00
|
|
|
{
|
2018-01-31 23:44:33 +08:00
|
|
|
if ((retval = nc4_open_var_grp2(grp, varid, &datasetid)))
|
|
|
|
return retval;
|
2018-03-17 01:46:18 +08:00
|
|
|
if (H5Adelete(datasetid, att->hdr.name) < 0)
|
2018-01-31 23:44:33 +08:00
|
|
|
return NC_EHDFERR;
|
2017-12-04 22:07:45 +08:00
|
|
|
}
|
2018-01-31 23:44:33 +08:00
|
|
|
att->created = NC_FALSE;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Copy the new name into our metadata. */
|
2018-03-17 01:46:18 +08:00
|
|
|
if(att->hdr.name) free(att->hdr.name);
|
|
|
|
if (!(att->hdr.name = strdup(norm_newname)))
|
2018-01-31 23:44:33 +08:00
|
|
|
return NC_ENOMEM;
|
2018-03-17 01:46:18 +08:00
|
|
|
att->hdr.hashkey = NC_hashmapkey(att->hdr.name,strlen(att->hdr.name)); /* Fix hash key */
|
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
att->dirty = NC_TRUE;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
/* Rehash the attribute list so that the new name is used */
|
|
|
|
if(!ncindexrebuild(list))
|
|
|
|
return NC_EINTERNAL;
|
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Mark attributes on variable dirty, so they get written */
|
|
|
|
if(var)
|
|
|
|
var->attr_dirty = NC_TRUE;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/**
|
|
|
|
* @internal Delete an att. Rub it out. Push the button on
|
|
|
|
* it. Liquidate it. Bump it off. Take it for a one-way
|
|
|
|
* ride. Terminate it.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute to delete.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_del_att(int ncid, int varid, const char *name)
|
|
|
|
{
|
|
|
|
NC *nc;
|
|
|
|
NC_GRP_INFO_T *grp;
|
|
|
|
NC_VAR_INFO_T *var;
|
2018-03-17 01:46:18 +08:00
|
|
|
NC_HDF5_FILE_INFO_T *h5;
|
|
|
|
NC_ATT_INFO_T *att;
|
|
|
|
NCindex* attlist = NULL;
|
2018-01-31 23:44:33 +08:00
|
|
|
hid_t locid = 0, datasetid = 0;
|
|
|
|
int retval = NC_NOERR;
|
2018-03-17 01:46:18 +08:00
|
|
|
int i;
|
|
|
|
size_t deletedid;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
if (!name)
|
|
|
|
return NC_EINVAL;
|
|
|
|
|
|
|
|
LOG((2, "nc_del_att: ncid 0x%x varid %d name %s",
|
|
|
|
ncid, varid, name));
|
|
|
|
|
|
|
|
/* Find info for this file, group, and h5 info. */
|
|
|
|
if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
assert(h5 && grp);
|
|
|
|
|
|
|
|
/* If the file is read-only, return an error. */
|
|
|
|
if (h5->no_write)
|
|
|
|
return NC_EPERM;
|
|
|
|
|
|
|
|
/* If it's not in define mode, forget it. */
|
|
|
|
if (!(h5->flags & NC_INDEF))
|
|
|
|
{
|
|
|
|
if (h5->cmode & NC_CLASSIC_MODEL)
|
|
|
|
return NC_ENOTINDEFINE;
|
|
|
|
if ((retval = NC4_redef(ncid)))
|
|
|
|
BAIL(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get either the global or a variable attribute list. Also figure
|
|
|
|
out the HDF5 location it's attached to. */
|
2018-03-17 01:46:18 +08:00
|
|
|
attlist = getattlist(grp,varid,&var);
|
|
|
|
if(attlist == NULL)
|
|
|
|
return NC_ENOTVAR;
|
2018-01-31 23:44:33 +08:00
|
|
|
if (varid == NC_GLOBAL)
|
|
|
|
locid = grp->hdf_grpid;
|
2018-03-17 01:46:18 +08:00
|
|
|
else if (var->created)
|
|
|
|
locid = var->hdf_datasetid;
|
2018-01-31 23:44:33 +08:00
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
/* Now find the attribute by name */
|
|
|
|
att = (NC_ATT_INFO_T*)ncindexlookup(attlist,name);
|
2018-01-31 23:44:33 +08:00
|
|
|
|
|
|
|
/* If att is NULL, we couldn't find the attribute. */
|
|
|
|
if (!att)
|
|
|
|
BAIL_QUIET(NC_ENOTATT);
|
|
|
|
|
|
|
|
/* Delete it from the HDF5 file, if it's been created. */
|
|
|
|
if (att->created)
|
|
|
|
{
|
|
|
|
assert(locid);
|
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
if(H5Adelete(locid, att->hdr.name) < 0)
|
2018-01-31 23:44:33 +08:00
|
|
|
BAIL(NC_EATTMETA);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
deletedid = att->hdr.id;
|
|
|
|
|
|
|
|
/* Remove this attribute in this list */
|
2018-01-31 23:44:33 +08:00
|
|
|
if ((retval = nc4_att_list_del(attlist, att)))
|
|
|
|
BAIL(retval);
|
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
/* Renumber all attributes with higher indices. */
|
|
|
|
for(i=0;i<ncindexsize(attlist);i++) {
|
|
|
|
NC_ATT_INFO_T* a = (NC_ATT_INFO_T*)ncindexith(attlist,i);
|
|
|
|
if(a == NULL) continue;
|
|
|
|
if(a->hdr.id > deletedid) a->hdr.id--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rebuild the index */
|
|
|
|
if(!ncindexrebuild(attlist))
|
|
|
|
BAIL(NC_EINTERNAL);
|
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
exit:
|
|
|
|
if (datasetid > 0) H5Dclose(datasetid);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Write an attribute to a netCDF-4/HDF5 file, converting
|
|
|
|
* data type if necessary.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param file_type Type of the attribute data in file.
|
|
|
|
* @param len Number of elements in attribute array.
|
|
|
|
* @param data Attribute data.
|
2018-03-17 01:46:18 +08:00
|
|
|
* @param mem_type Type of data in memory.
|
2018-01-31 23:44:33 +08:00
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Variable not found.
|
|
|
|
* @return ::NC_EBADNAME Name contains illegal characters.
|
|
|
|
* @return ::NC_ENAMEINUSE Name already in use.
|
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_put_att(int ncid, int varid, const char *name, nc_type file_type,
|
|
|
|
size_t len, const void *data, nc_type mem_type)
|
|
|
|
{
|
|
|
|
NC *nc;
|
|
|
|
NC_GRP_INFO_T *grp;
|
|
|
|
NC_HDF5_FILE_INFO_T *h5;
|
|
|
|
NC_VAR_INFO_T *var = NULL;
|
2018-03-17 01:46:18 +08:00
|
|
|
NCindex* attlist = NULL;
|
|
|
|
NC_ATT_INFO_T* att;
|
2018-01-31 23:44:33 +08:00
|
|
|
char norm_name[NC_MAX_NAME + 1];
|
|
|
|
nc_bool_t new_att = NC_FALSE;
|
|
|
|
int retval = NC_NOERR, range_error = 0;
|
|
|
|
size_t type_size;
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find info for this file, group, and h5 info. */
|
|
|
|
if ((ret = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
|
|
|
|
return ret;
|
|
|
|
assert(nc && grp && h5);
|
|
|
|
|
|
|
|
/* Find att, if it exists. (Must check varid first or nc_test will
|
|
|
|
* break.) */
|
2018-03-17 01:46:18 +08:00
|
|
|
attlist = getattlist(grp,varid,&var);
|
|
|
|
if(attlist == NULL)
|
|
|
|
return NC_ENOTVAR;
|
2018-01-31 23:44:33 +08:00
|
|
|
|
|
|
|
/* The length needs to be positive (cast needed for braindead
|
|
|
|
systems with signed size_t). */
|
|
|
|
if((unsigned long) len > X_INT_MAX)
|
|
|
|
return NC_EINVAL;
|
|
|
|
|
|
|
|
/* Check name before LOG statement. */
|
|
|
|
if (!name || strlen(name) > NC_MAX_NAME)
|
|
|
|
return NC_EBADNAME;
|
|
|
|
|
|
|
|
LOG((1, "%s: ncid 0x%x varid %d name %s file_type %d mem_type %d len %d",
|
|
|
|
__func__, ncid, varid, name, file_type, mem_type, len));
|
|
|
|
|
|
|
|
/* If len is not zero, then there must be some data. */
|
|
|
|
if (len && !data)
|
|
|
|
return NC_EINVAL;
|
|
|
|
|
|
|
|
/* If the file is read-only, return an error. */
|
|
|
|
if (h5->no_write)
|
|
|
|
return NC_EPERM;
|
|
|
|
|
|
|
|
/* Check and normalize the name. */
|
|
|
|
if ((retval = nc4_check_name(name, norm_name)))
|
|
|
|
return retval;
|
|
|
|
|
2018-03-17 01:46:18 +08:00
|
|
|
/* Check that a reserved att name is not being used improperly */
|
|
|
|
const NC_reservedatt* ra = NC_findreserved(name);
|
|
|
|
if(ra != NULL) {
|
|
|
|
/* case 1: grp=root, varid==NC_GLOBAL, flags & READONLYFLAG */
|
|
|
|
if (nc->ext_ncid == ncid && varid == NC_GLOBAL && grp->parent == NULL
|
|
|
|
&& (ra->flags & READONLYFLAG))
|
|
|
|
return NC_ENAMEINUSE;
|
|
|
|
/* case 2: grp=NA, varid!=NC_GLOBAL, flags & DIMSCALEFLAG */
|
|
|
|
if (varid != NC_GLOBAL && (ra->flags & DIMSCALEFLAG))
|
|
|
|
return NC_ENAMEINUSE;
|
2018-01-31 23:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See if there is already an attribute with this name. */
|
2018-03-17 01:46:18 +08:00
|
|
|
att = (NC_ATT_INFO_T*)ncindexlookup(attlist,norm_name);
|
2018-01-31 23:44:33 +08:00
|
|
|
|
|
|
|
LOG((1, "%s: ncid 0x%x varid %d name %s file_type %d mem_type %d len %d",
|
|
|
|
__func__, ncid, varid, name, file_type, mem_type, len));
|
|
|
|
|
|
|
|
if (!att)
|
|
|
|
{
|
|
|
|
/* If this is a new att, require define mode. */
|
|
|
|
if (!(h5->flags & NC_INDEF))
|
|
|
|
{
|
|
|
|
if (h5->cmode & NC_CLASSIC_MODEL)
|
|
|
|
return NC_EINDEFINE;
|
|
|
|
if ((retval = NC4_redef(ncid)))
|
|
|
|
BAIL(retval);
|
|
|
|
}
|
|
|
|
new_att = NC_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* For an existing att, if we're not in define mode, the len
|
|
|
|
must not be greater than the existing len for classic model. */
|
|
|
|
if (!(h5->flags & NC_INDEF) &&
|
|
|
|
len * nc4typelen(file_type) > (size_t)att->len * nc4typelen(att->nc_typeid))
|
|
|
|
{
|
|
|
|
if (h5->cmode & NC_CLASSIC_MODEL)
|
|
|
|
return NC_EINDEFINE;
|
|
|
|
if ((retval = NC4_redef(ncid)))
|
|
|
|
BAIL(retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We must have two valid types to continue. */
|
|
|
|
if (file_type == NC_NAT || mem_type == NC_NAT)
|
|
|
|
return NC_EBADTYPE;
|
|
|
|
|
|
|
|
/* Get information about this type. */
|
2018-03-17 01:46:18 +08:00
|
|
|
if ((retval = nc4_get_typelen_mem(h5, file_type, &type_size)))
|
2018-01-31 23:44:33 +08:00
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* No character conversions are allowed. */
|
|
|
|
if (file_type != mem_type &&
|
|
|
|
(file_type == NC_CHAR || mem_type == NC_CHAR ||
|
|
|
|
file_type == NC_STRING || mem_type == NC_STRING))
|
|
|
|
return NC_ECHAR;
|
|
|
|
|
|
|
|
/* For classic mode file, only allow atts with classic types to be
|
|
|
|
* created. */
|
|
|
|
if (h5->cmode & NC_CLASSIC_MODEL && file_type > NC_DOUBLE)
|
|
|
|
return NC_ESTRICTNC3;
|
|
|
|
|
|
|
|
/* Add to the end of the attribute list, if this att doesn't
|
2010-06-03 21:24:43 +08:00
|
|
|
already exist. */
|
|
|
|
if (new_att)
|
|
|
|
{
|
|
|
|
LOG((3, "adding attribute %s to the list...", norm_name));
|
2018-03-17 01:46:18 +08:00
|
|
|
if ((ret = nc4_att_list_add(attlist, norm_name, &att)))
|
2018-01-31 23:44:33 +08:00
|
|
|
BAIL (ret);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now fill in the metadata. */
|
2014-02-12 07:12:08 +08:00
|
|
|
att->dirty = NC_TRUE;
|
|
|
|
att->nc_typeid = file_type;
|
2013-07-11 04:09:31 +08:00
|
|
|
|
|
|
|
/* If this att has vlen or string data, release it before we lose the length value. */
|
|
|
|
if (att->stdata)
|
|
|
|
{
|
|
|
|
for (i = 0; i < att->len; i++)
|
|
|
|
if(att->stdata[i])
|
2017-12-04 22:07:45 +08:00
|
|
|
free(att->stdata[i]);
|
2013-07-11 04:09:31 +08:00
|
|
|
free(att->stdata);
|
|
|
|
att->stdata = NULL;
|
|
|
|
}
|
|
|
|
if (att->vldata)
|
|
|
|
{
|
|
|
|
for (i = 0; i < att->len; i++)
|
2017-12-04 22:07:45 +08:00
|
|
|
nc_free_vlen(&att->vldata[i]);
|
2013-07-11 04:09:31 +08:00
|
|
|
free(att->vldata);
|
|
|
|
att->vldata = NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
att->len = len;
|
|
|
|
|
|
|
|
/* If this is the _FillValue attribute, then we will also have to
|
2014-11-24 23:36:58 +08:00
|
|
|
* copy the value to the fill_vlue pointer of the NC_VAR_INFO_T
|
2010-06-03 21:24:43 +08:00
|
|
|
* struct for this var. (But ignore a global _FillValue
|
|
|
|
* attribute). */
|
2018-03-17 01:46:18 +08:00
|
|
|
if (!strcmp(att->hdr.name, _FillValue) && varid != NC_GLOBAL)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
|
2012-08-17 02:31:48 +08:00
|
|
|
/* Fill value must be same type and have exactly one value */
|
2018-03-17 01:46:18 +08:00
|
|
|
if (att->nc_typeid != var->type_info->hdr.id)
|
2017-12-04 22:07:45 +08:00
|
|
|
return NC_EBADTYPE;
|
2012-08-17 02:31:48 +08:00
|
|
|
if (att->len != 1)
|
2017-12-04 22:07:45 +08:00
|
|
|
return NC_EINVAL;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* If we already wrote to the dataset, then return an error. */
|
|
|
|
if (var->written_to)
|
2017-12-04 22:07:45 +08:00
|
|
|
return NC_ELATEFILL;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Get the length of the veriable data type. */
|
2018-03-17 01:46:18 +08:00
|
|
|
if ((retval = nc4_get_typelen_mem(grp->nc4_info, var->type_info->hdr.id,
|
2016-06-11 07:03:08 +08:00
|
|
|
&type_size)))
|
2017-12-04 22:07:45 +08:00
|
|
|
return retval;
|
2014-10-02 07:04:56 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Already set a fill value? Now I'll have to free the old
|
|
|
|
* one. Make up your damn mind, would you? */
|
|
|
|
if (var->fill_value)
|
2017-12-04 22:07:45 +08:00
|
|
|
{
|
|
|
|
if (var->type_info->nc_type_class == NC_VLEN)
|
|
|
|
{
|
|
|
|
if ((retval = nc_free_vlen(var->fill_value)))
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
else if (var->type_info->nc_type_class == NC_STRING)
|
|
|
|
{
|
|
|
|
if (*(char **)var->fill_value)
|
|
|
|
free(*(char **)var->fill_value);
|
|
|
|
}
|
|
|
|
free(var->fill_value);
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Determine the size of the fill value in bytes. */
|
2014-02-12 07:12:08 +08:00
|
|
|
if (var->type_info->nc_type_class == NC_VLEN)
|
2017-12-04 22:07:45 +08:00
|
|
|
size = sizeof(hvl_t);
|
2014-02-12 07:12:08 +08:00
|
|
|
else if (var->type_info->nc_type_class == NC_STRING)
|
2017-12-04 22:07:45 +08:00
|
|
|
size = sizeof(char *);
|
2010-06-03 21:24:43 +08:00
|
|
|
else
|
2017-12-04 22:07:45 +08:00
|
|
|
size = type_size;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-01-31 23:44:33 +08:00
|
|
|
/* Allocate space for the fill value. */
|
Corrected "BAIL" macros to avoid infinite loop when logging is disabled and an
error occurs after an "exit:" label.
Corrected a dozen Coverity errors (mainly allocation issues, along with a few
other things):
711711, 711802, 711803, 711905, 970825, 996123, 996124, 1025787,
1047274, 1130013, 1130014, 1139538
Refactored internal fill-value code to correctly handle string types, and
especially to allow NULL pointers and null strings (ie. "") to be
distinguished. The code now avoids partially aliasing the two together
(which only happened on the 'write' side of things and wasn't reflected on
the 'read' side, adding to the previous confusion).
Probably still weak on handling fill-values of variable-length and compound
datatypes.
Refactored the recursive metadata reads a bit more, to process HDF5 named
datatypes and datasets immediately, avoiding chewing up memory for those
types of objects, etc.
Finished uncommenting and updating the nc_test4/tst_fills2.c code (as I'm
proceeding alphabetically through the nc_test4 code files).
2013-12-29 15:12:43 +08:00
|
|
|
if (!(var->fill_value = calloc(1, size)))
|
2017-12-04 22:07:45 +08:00
|
|
|
return NC_ENOMEM;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Copy the fill_value. */
|
2018-03-17 01:46:18 +08:00
|
|
|
LOG((4, "Copying fill value into metadata for variable %s", var->hdr.name));
|
2014-02-12 07:12:08 +08:00
|
|
|
if (var->type_info->nc_type_class == NC_VLEN)
|
2017-12-04 22:07:45 +08:00
|
|
|
{
|
|
|
|
nc_vlen_t *in_vlen = (nc_vlen_t *)data, *fv_vlen = (nc_vlen_t *)(var->fill_value);
|
Corrected "BAIL" macros to avoid infinite loop when logging is disabled and an
error occurs after an "exit:" label.
Corrected a dozen Coverity errors (mainly allocation issues, along with a few
other things):
711711, 711802, 711803, 711905, 970825, 996123, 996124, 1025787,
1047274, 1130013, 1130014, 1139538
Refactored internal fill-value code to correctly handle string types, and
especially to allow NULL pointers and null strings (ie. "") to be
distinguished. The code now avoids partially aliasing the two together
(which only happened on the 'write' side of things and wasn't reflected on
the 'read' side, adding to the previous confusion).
Probably still weak on handling fill-values of variable-length and compound
datatypes.
Refactored the recursive metadata reads a bit more, to process HDF5 named
datatypes and datasets immediately, avoiding chewing up memory for those
types of objects, etc.
Finished uncommenting and updating the nc_test4/tst_fills2.c code (as I'm
proceeding alphabetically through the nc_test4 code files).
2013-12-29 15:12:43 +08:00
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
fv_vlen->len = in_vlen->len;
|
|
|
|
if (!(fv_vlen->p = malloc(size * in_vlen->len)))
|
2016-06-11 07:03:08 +08:00
|
|
|
return NC_ENOMEM;
|
2017-12-04 22:07:45 +08:00
|
|
|
memcpy(fv_vlen->p, in_vlen->p, in_vlen->len * size);
|
|
|
|
}
|
2014-02-12 07:12:08 +08:00
|
|
|
else if (var->type_info->nc_type_class == NC_STRING)
|
2017-12-04 22:07:45 +08:00
|
|
|
{
|
2018-01-31 23:44:33 +08:00
|
|
|
if (*(char **)data)
|
2017-12-04 22:07:45 +08:00
|
|
|
{
|
|
|
|
if (!(*(char **)(var->fill_value) = malloc(strlen(*(char **)data) + 1)))
|
|
|
|
return NC_ENOMEM;
|
|
|
|
strcpy(*(char **)var->fill_value, *(char **)data);
|
|
|
|
}
|
|
|
|
else
|
Corrected "BAIL" macros to avoid infinite loop when logging is disabled and an
error occurs after an "exit:" label.
Corrected a dozen Coverity errors (mainly allocation issues, along with a few
other things):
711711, 711802, 711803, 711905, 970825, 996123, 996124, 1025787,
1047274, 1130013, 1130014, 1139538
Refactored internal fill-value code to correctly handle string types, and
especially to allow NULL pointers and null strings (ie. "") to be
distinguished. The code now avoids partially aliasing the two together
(which only happened on the 'write' side of things and wasn't reflected on
the 'read' side, adding to the previous confusion).
Probably still weak on handling fill-values of variable-length and compound
datatypes.
Refactored the recursive metadata reads a bit more, to process HDF5 named
datatypes and datasets immediately, avoiding chewing up memory for those
types of objects, etc.
Finished uncommenting and updating the nc_test4/tst_fills2.c code (as I'm
proceeding alphabetically through the nc_test4 code files).
2013-12-29 15:12:43 +08:00
|
|
|
*(char **)var->fill_value = NULL;
|
2017-12-04 22:07:45 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
else
|
2017-12-04 22:07:45 +08:00
|
|
|
memcpy(var->fill_value, data, type_size);
|
2015-11-12 02:32:12 +08:00
|
|
|
|
2014-12-01 22:52:53 +08:00
|
|
|
/* Indicate that the fill value was changed, if the variable has already
|
|
|
|
* been created in the file, so the dataset gets deleted and re-created. */
|
|
|
|
if (var->created)
|
|
|
|
var->fill_val_changed = NC_TRUE;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the attribute data, if there is any. VLENs and string
|
|
|
|
* arrays have to be handled specially. */
|
2018-01-31 23:44:33 +08:00
|
|
|
if (att->len)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2014-02-12 07:12:08 +08:00
|
|
|
nc_type type_class; /* Class of attribute's type */
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2014-02-12 07:12:08 +08:00
|
|
|
/* Get class for this type. */
|
|
|
|
if ((retval = nc4_get_typeclass(h5, file_type, &type_class)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
assert(data);
|
|
|
|
if (type_class == NC_VLEN)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2014-02-12 07:12:08 +08:00
|
|
|
const hvl_t *vldata1;
|
2017-12-04 22:07:45 +08:00
|
|
|
NC_TYPE_INFO_T *type;
|
|
|
|
size_t base_typelen;
|
2014-02-14 22:07:14 +08:00
|
|
|
|
|
|
|
/* Get the type object for the attribute's type */
|
2017-12-04 22:07:45 +08:00
|
|
|
if ((retval = nc4_find_type(h5, file_type, &type)))
|
|
|
|
BAIL(retval);
|
2014-02-14 22:07:14 +08:00
|
|
|
|
|
|
|
/* Retrieve the size of the base type */
|
2018-03-17 01:46:18 +08:00
|
|
|
if ((retval = nc4_get_typelen_mem(h5, type->u.v.base_nc_typeid, &base_typelen)))
|
2014-02-14 22:07:14 +08:00
|
|
|
BAIL(retval);
|
2014-02-12 07:12:08 +08:00
|
|
|
|
|
|
|
vldata1 = data;
|
2014-10-02 07:04:56 +08:00
|
|
|
if (!(att->vldata = (nc_vlen_t*)malloc(att->len * sizeof(hvl_t))))
|
|
|
|
BAIL(NC_ENOMEM);
|
2014-02-12 07:12:08 +08:00
|
|
|
for (i = 0; i < att->len; i++)
|
|
|
|
{
|
|
|
|
att->vldata[i].len = vldata1[i].len;
|
2014-02-14 22:07:14 +08:00
|
|
|
if (!(att->vldata[i].p = malloc(base_typelen * att->vldata[i].len)))
|
2014-02-12 07:12:08 +08:00
|
|
|
BAIL(NC_ENOMEM);
|
2014-02-14 22:07:14 +08:00
|
|
|
memcpy(att->vldata[i].p, vldata1[i].p, base_typelen * att->vldata[i].len);
|
2014-02-12 07:12:08 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2014-02-12 07:12:08 +08:00
|
|
|
else if (type_class == NC_STRING)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2017-12-04 22:07:45 +08:00
|
|
|
LOG((4, "copying array of NC_STRING"));
|
|
|
|
if (!(att->stdata = malloc(sizeof(char *) * att->len))) {
|
|
|
|
BAIL(NC_ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are overwriting an existing attribute,
|
|
|
|
specifically an NC_CHAR, we need to clean up
|
|
|
|
the pre-existing att->data. */
|
|
|
|
if (!new_att && att->data) {
|
|
|
|
free(att->data);
|
|
|
|
att->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < att->len; i++)
|
|
|
|
{
|
2014-02-12 07:12:08 +08:00
|
|
|
if(NULL != ((char **)data)[i]) {
|
2017-12-04 22:07:45 +08:00
|
|
|
LOG((5, "copying string %d of size %d", i, strlen(((char **)data)[i]) + 1));
|
|
|
|
if (!(att->stdata[i] = strdup(((char **)data)[i])))
|
|
|
|
BAIL(NC_ENOMEM);
|
2015-11-12 02:32:12 +08:00
|
|
|
}
|
|
|
|
else
|
2017-12-04 22:07:45 +08:00
|
|
|
att->stdata[i] = ((char **)data)[i];
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2014-02-12 07:12:08 +08:00
|
|
|
else
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2014-02-12 07:12:08 +08:00
|
|
|
/* [Re]allocate memory for the attribute data */
|
|
|
|
if (!new_att)
|
|
|
|
free (att->data);
|
|
|
|
if (!(att->data = malloc(att->len * type_size)))
|
|
|
|
BAIL(NC_ENOMEM);
|
|
|
|
|
|
|
|
/* Just copy the data, for non-atomic types */
|
|
|
|
if (type_class == NC_OPAQUE || type_class == NC_COMPOUND || type_class == NC_ENUM)
|
|
|
|
memcpy(att->data, data, len * type_size);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Data types are like religions, in that one can convert. */
|
2014-10-02 07:04:56 +08:00
|
|
|
if ((retval = nc4_convert_type(data, att->data, mem_type, file_type,
|
|
|
|
len, &range_error, NULL,
|
2018-01-31 23:44:33 +08:00
|
|
|
(h5->cmode & NC_CLASSIC_MODEL), 0, 0)))
|
2014-02-12 07:12:08 +08:00
|
|
|
BAIL(retval);
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-12 07:12:08 +08:00
|
|
|
att->dirty = NC_TRUE;
|
|
|
|
att->created = NC_FALSE;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2014-11-24 23:36:58 +08:00
|
|
|
/* Mark attributes on variable dirty, so they get written */
|
|
|
|
if(var)
|
2017-12-04 22:07:45 +08:00
|
|
|
var->attr_dirty = NC_TRUE;
|
2014-11-24 23:36:58 +08:00
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
exit:
|
2010-06-03 21:24:43 +08:00
|
|
|
/* If there was an error return it, otherwise return any potential
|
|
|
|
range error value. If none, return NC_NOERR as usual.*/
|
2014-10-02 07:04:56 +08:00
|
|
|
if (retval)
|
2010-06-03 21:24:43 +08:00
|
|
|
return retval;
|
|
|
|
if (range_error)
|
|
|
|
return NC_ERANGE;
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
|
|
|
* @internal Get an attribute.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param value Pointer that gets attribute data.
|
|
|
|
* @param memtype The type the data should be converted to as it is read.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
int
|
|
|
|
NC4_get_att(int ncid, int varid, const char *name, void *value, nc_type memtype)
|
|
|
|
{
|
2018-01-31 23:44:33 +08:00
|
|
|
return nc4_get_att(ncid, varid, name, NULL, memtype, NULL, NULL, value);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|