Skip to content

Commit

Permalink
Merge pull request #1103 from NetCDF-World-Domination-Council/ejh_fun…
Browse files Browse the repository at this point in the history
…c_cleanup3

Warning, coverity warning, error handling, documentation, and hdf5_set_log_level fixes, all on the path to the lazy var feature
  • Loading branch information
WardF authored Aug 20, 2018
2 parents 5539b50 + d59446c commit c59f43d
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 389 deletions.
4 changes: 4 additions & 0 deletions include/hdf5internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ typedef struct NC_HDF5_FILE_INFO
int rec_detach_scales(NC_GRP_INFO_T *grp, int dimid, hid_t dimscaleid);
int rec_reattach_scales(NC_GRP_INFO_T *grp, int dimid, hid_t dimscaleid);

/* These functions are internal to the libhdf5 directory. */
int nc4_detect_preserve_dimids(NC_GRP_INFO_T *grp, nc_bool_t *bad_coord_orderp);
int hdf5_set_log_level();

#endif /* _HDF5INTERNAL_ */
12 changes: 9 additions & 3 deletions include/nc4internal.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* Copyright 2005-2018 University Corporation for Atmospheric
Research/Unidata. */
/**
* @file This header file contains macros, types and prototypes used
* to build and manipulate the netCDF metadata model.
* @file
* @internal This header file contains macros, types and prototypes
* used to build and manipulate the netCDF metadata model.
*
* @author Ed Hartnett, Dennis Heimbigner, Ward Fisher
*/
Expand Down Expand Up @@ -77,6 +78,9 @@ typedef enum {NCNAT, NCVAR, NCDIM, NCATT, NCTYP, NCFLD, NCGRP} NC_SORT;
/** This is the number of netCDF atomic types. */
#define NUM_ATOMIC_TYPES (NC_MAX_ATOMIC_TYPE + 1)

/** Number of parameters needed for ZLIB filter. */
#define CD_NELEMS_ZLIB 1

/* Boolean type, to make the code easier to read */
typedef enum {NC_FALSE = 0, NC_TRUE = 1} nc_bool_t;

Expand Down Expand Up @@ -371,6 +375,8 @@ int nc4_type_free(NC_TYPE_INFO_T *type);
int nc4_nc4f_list_add(NC *nc, const char *path, int mode);
void nc4_file_list_del(NC *nc);
int nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims, NC_VAR_INFO_T **var);
int nc4_var_list_add2(NC_GRP_INFO_T* grp, const char* name, NC_VAR_INFO_T **var);
int nc4_var_set_ndims(NC_VAR_INFO_T *var, int ndims);
int nc4_var_list_del(NC_GRP_INFO_T* grp, NC_VAR_INFO_T *var);
int nc4_var_free(NC_VAR_INFO_T *var);
int nc4_dim_list_add(NC_GRP_INFO_T* grp, const char* name, size_t len, int assignedid, NC_DIM_INFO_T **dim);
Expand All @@ -386,7 +392,7 @@ int nc4_field_list_add(NC_TYPE_INFO_T* parent, const char *name,
int nc4_att_list_add(NCindex* list, const char* name, NC_ATT_INFO_T **att);
int nc4_att_list_del(NCindex* list, NC_ATT_INFO_T *att);
int nc4_att_free(NC_ATT_INFO_T *att);
int nc4_grp_list_add(NC_GRP_INFO_T *parent, char *name, NC_GRP_INFO_T **grp);
int nc4_grp_list_add(NC_FILE_INFO_T *h5, NC_GRP_INFO_T *parent, char *name, NC_GRP_INFO_T **grp);
int nc4_build_root_grp(NC_FILE_INFO_T* h5);
int nc4_rec_grp_del(NC_GRP_INFO_T *grp);
int nc4_enum_member_add(NC_TYPE_INFO_T *type, size_t size,
Expand Down
108 changes: 71 additions & 37 deletions libhdf5/hdf5attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
* Research. See COPYRIGHT file for copying and redistribution
* conditions. */
/**
* @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 HDF5 attributes.
* @file
* @internal This file handles HDF5 attributes.
*
* @author Ed Hartnett
*/

#include "config.h"
#include "hdf5internal.h"

int nc4typelen(nc_type type);

/**
* @internal Get the attribute list for either a varid or NC_GLOBAL
*
Expand Down Expand Up @@ -75,6 +70,13 @@ getattlist(NC_GRP_INFO_T *grp, int varid, NC_VAR_INFO_T **varp,
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @return ::NC_EMAXNAME New name too long.
* @return ::NC_EPERM File is read-only.
* @return ::NC_ENAMEINUSE New name already in use.
* @return ::NC_ENOTINDEFINE Classic model file not in define mode.
* @return ::NC_EHDFERR HDF error.
* @return ::NC_ENOMEM Out of memory.
* @return ::NC_EINTERNAL Could not rebuild list.
* @author Ed Hartnett
*/
int
Expand Down Expand Up @@ -184,45 +186,49 @@ NC4_rename_att(int ncid, int varid, const char *name, const char *newname)
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @author Ed Hartnett
* @return ::NC_ENOTATT Attribute not found.
* @return ::NC_EINVAL No name provided.
* @return ::NC_EPERM File is read only.
* @return ::NC_ENOTINDEFINE Classic model not in define mode.
* @return ::NC_EINTERNAL Could not rebuild list.
* @author Ed Hartnett, Dennis Heimbigner
*/
int
NC4_del_att(int ncid, int varid, const char *name)
{
NC *nc;
NC_GRP_INFO_T *grp;
NC_VAR_INFO_T *var;
NC_FILE_INFO_T *h5;
NC_ATT_INFO_T *att;
NCindex* attlist = NULL;
hid_t locid = 0, datasetid = 0;
hid_t locid = 0;
int i;
size_t deletedid;
int retval;

/* Name must be provided. */
if (!name)
return NC_EINVAL;

LOG((2, "nc_del_att: ncid 0x%x varid %d name %s",
ncid, varid, name));
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)))
if ((retval = nc4_find_grp_h5(ncid, &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 file is not in define mode, return error for classic model
* files, otherwise switch to define mode. */
if (!(h5->flags & NC_INDEF))
{
if (h5->cmode & NC_CLASSIC_MODEL)
return NC_ENOTINDEFINE;
if ((retval = NC4_redef(ncid)))
BAIL(retval);
return retval;
}

/* Get either the global or a variable attribute list. */
Expand All @@ -235,43 +241,71 @@ NC4_del_att(int ncid, int varid, const char *name)
else if (var->created)
locid = var->hdf_datasetid;

/* Now find the attribute by name */
att = (NC_ATT_INFO_T*)ncindexlookup(attlist,name);

/* If att is NULL, we couldn't find the attribute. */
if (!att)
BAIL_QUIET(NC_ENOTATT);
/* Now find the attribute by name. */
if (!(att = (NC_ATT_INFO_T*)ncindexlookup(attlist, name)))
return NC_ENOTATT;

/* Delete it from the HDF5 file, if it's been created. */
if (att->created)
{
assert(locid);

if(H5Adelete(locid, att->hdr.name) < 0)
BAIL(NC_EATTMETA);
if (H5Adelete(locid, att->hdr.name) < 0)
return NC_EATTMETA;
}


deletedid = att->hdr.id;

/* Remove this attribute in this list */
if ((retval = nc4_att_list_del(attlist, att)))
BAIL(retval);
return retval;

/* 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--;
for (i = 0; i < ncindexsize(attlist); i++)
{
NC_ATT_INFO_T *a;
if (!(a = (NC_ATT_INFO_T *)ncindexith(attlist, i)))
continue;
if (a->hdr.id > deletedid)
a->hdr.id--;
}

/* rebuild the index */
if(!ncindexrebuild(attlist))
BAIL(NC_EINTERNAL);
/* Rebuild the index. */
if (!ncindexrebuild(attlist))
return NC_EINTERNAL;

exit:
if (datasetid > 0) H5Dclose(datasetid);
return retval;
return NC_NOERR;
}

/**
* @internal This will return the length of a netcdf atomic data type
* in bytes.
*
* @param type A netcdf atomic type.
*
* @return Type size in bytes, or -1 if type not found.
* @author Ed Hartnett
*/
static int
nc4typelen(nc_type type)
{
switch(type){
case NC_BYTE:
case NC_CHAR:
case NC_UBYTE:
return 1;
case NC_USHORT:
case NC_SHORT:
return 2;
case NC_FLOAT:
case NC_INT:
case NC_UINT:
return 4;
case NC_DOUBLE:
case NC_INT64:
case NC_UINT64:
return 8;
}
return -1;
}

/**
Expand Down
33 changes: 17 additions & 16 deletions libhdf5/hdf5create.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,16 @@ nc4_create_file(const char *path, int cmode, size_t initialsz,
H5P_CRT_ORDER_INDEXED)) < 0)
BAIL(NC_EHDFERR);

/* Create the file. */
#ifdef HDF5_HAS_COLL_METADATA_OPS
H5Pset_all_coll_metadata_ops(fapl_id, 1 );
H5Pset_coll_metadata_write(fapl_id, 1);
/* If HDF5 supports collective metadata operations, turn them
* on. This is only relevant for parallel I/O builds of HDF5. */
if (H5Pset_all_coll_metadata_ops(fapl_id, 1) < 0)
BAIL(NC_EHDFERR);
if (H5Pset_coll_metadata_write(fapl_id, 1) < 0)
BAIL(NC_EHDFERR);
#endif

if(nc4_info->mem.inmemory) {
#if 0
if(nc4_info->mem.memio.size == 0)
nc4_info->memio.size = DEFAULT_CREATE_MEMSIZE; /* last ditch fix */
if(nc4_info->memio.memory == NULL) { /* last ditch fix */
nc4_info->memio.memory = malloc(nc4_info->memio.size);
if(nc4_info->memio.memory == NULL)
BAIL(NC_ENOMEM);
}
assert(nc4_info->memio.size > 0 && nc4_info->memio.memory != NULL);
#endif
retval = NC4_create_image_file(nc4_info,initialsz);
if(retval)
BAIL(retval);
Expand Down Expand Up @@ -251,9 +244,11 @@ nc4_create_file(const char *path, int cmode, size_t initialsz,
if (comm_duped) MPI_Comm_free(&nc4_info->comm);
if (info_duped) MPI_Info_free(&nc4_info->info);
#endif
if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
if(!nc4_info) return retval;
nc4_close_netcdf4_file(nc4_info,1,0); /* treat like abort */
if (fapl_id != H5P_DEFAULT)
H5Pclose(fapl_id);
if (!nc4_info)
return retval;
nc4_close_netcdf4_file(nc4_info, 1, 0); /* treat like abort */
return retval;
}

Expand Down Expand Up @@ -292,6 +287,12 @@ NC4_create(const char* path, int cmode, size_t initialsz, int basepe,
if (!nc4_hdf5_initialized)
nc4_hdf5_initialize();

#ifdef LOGGING
/* If nc logging level has changed, see if we need to turn on
* HDF5's error messages. */
hdf5_set_log_level();
#endif /* LOGGING */

/* Check the cmode for validity. */
if((cmode & ILLEGAL_CREATE_FLAGS) != 0)
{res = NC_EINVAL; goto done;}
Expand Down
Loading

0 comments on commit c59f43d

Please sign in to comment.