NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
nxcdf_file.c File Reference

File state, markers, synchronization, and cleanup for nxCDF. More...

#include "nxcdf/nxcdf.h"
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "nxcdf_internal.h"

Functions

static int swmr_schema (NC_GRP_INFO_T *grp)
 Check the complete tree for schema excluded from SWMR writer sessions.
int nxcdf_swmr_validate (NC_FILE_INFO_T *h5, NXCDF_FILE_INFO_T *file)
 Preflight native serial SWMR without changing definitions or file bounds.
int nxcdf_swmr_activate (NC_FILE_INFO_T *h5, NXCDF_FILE_INFO_T *file)
 Activate a committed session without recursively entering enddef.
static int swmr_flush_group (NC_GRP_INFO_T *grp, int scales)
 Flush variable datasets or dimension scales throughout a group tree.
int nxcdf_swmr_flush (NC_FILE_INFO_T *h5, NXCDF_FILE_INFO_T *file)
 Publish all variable data before flushing dimension scales and the file.
static int swmr_file (int ncid, NC_FILE_INFO_T **h5p, NXCDF_FILE_INFO_T **filep)
 Resolve public SWMR IDs without casting another backend's private state.
int nc_start_swmr_write (int ncid)
 Start the writer session described by nc_start_swmr_write() in nep.h. See nep.h for parameter, return-value and lifecycle documentation.
int nc_swmr_flush (int ncid)
 Publish a writer session as specified by nc_swmr_flush() in nep.h. See nep.h for parameter, return-value and publication documentation.
static int swmr_refresh_group (NC_GRP_INFO_T *grp, int scales)
 Refresh variable datasets or dimension scales throughout a group tree.
int nxcdf_swmr_read_aliases (NC_GRP_INFO_T *grp)
 Collapse coordinate-variable alias handles to a single dataset id.
int nxcdf_swmr_refresh (NC_FILE_INFO_T *h5, NXCDF_FILE_INFO_T *file)
 Refresh all variable data before reloading dimension-scale extents.
int nc_swmr_refresh (int ncid)
 Refresh a reader session as specified by nc_swmr_refresh() in nep.h. See nep.h for parameter, return-value and visibility documentation.
void nxcdf_close_group_hdf (NC_GRP_INFO_T *grp)
 Recursively close the HDF5 objects belonging to a group.
void nxcdf_scrub_extended_atomic_data (NC_GRP_INFO_T *grp)
 Release attribute data and variable fill values that netcdf-c's teardown cannot reclaim.
int NXCDF_free_file (NXCDF_FILE_INFO_T *file)
 Close a NXCDF_FILE_INFO_T's HDF5 handles and free it.
int NXCDF_add_file (int ncid, const char *path, int mode, NXCDF_FILE_INFO_T **filep)
 Allocate per-file state and attach it to netcdf-c's open-file list.
int NXCDF_get_file (int ncid, NC_FILE_INFO_T **h5p, NXCDF_FILE_INFO_T **filep)
 Resolve both the shared and nxCDF-private file state for an ncid.
static int write_string_att (hid_t location, const char *name, const char *value)
 Write a null-terminated scalar HDF5 string attribute.
int NXCDF_write_markers (NXCDF_FILE_INFO_T *file)
 Write the hidden marker attributes identifying the file as nxCDF.
int NXCDF_read_markers (NXCDF_FILE_INFO_T *file)
 Read and validate the hidden marker attributes at open.
int NXCDF_sync (int ncid)
 Flush pending writes — the nc_sync() dispatch entry.
int NXCDF_close (int ncid, void *parameters)
 Flush, close, and release a file — the nc_close() dispatch entry.
int NXCDF_abort (int ncid)
 Abort a file — the nc_abort() dispatch entry.
int NXCDF_inq_format (int ncid, int *formatp)
 Report the public format class — the nc_inq_format() dispatch entry.
int NXCDF_inq_format_extended (int ncid, int *formatp, int *modep)
 Report the extended format and effective mode — the nc_inq_format_extended() dispatch entry.
static int commit_group_vars (NC_FILE_INFO_T *h5, NXCDF_FILE_INFO_T *file, NC_GRP_INFO_T *grp)
 Rebuild dirty variable datasets and mark them committed.
int NXCDF__enddef (int ncid, size_t h_minfree, size_t v_align, size_t v_minfree, size_t r_align)
 Materialize pending metadata and leave define mode — the nc_enddef() dispatch entry.
int NXCDF_redef (int ncid)
 Return the file to define mode — the nc_redef() dispatch entry.
int NXCDF_check_mode (int mode)
 Reject unsupported disk, memory, sharing and parallel modes.
int NXCDF_set_fill (int ncid, int fillmode, int *old_modep)
 Change the session default inherited by subsequently defined variables. Existing variables retain their fill properties; this does not enter define mode or persist a file-wide setting. Subgroup IDs address the same default.

Detailed Description

File state, markers, synchronization, and cleanup for nxCDF.

Author
Edward Hartnett
Date
2026-08-28

Function Documentation

◆ commit_group_vars()

int commit_group_vars ( NC_FILE_INFO_T * h5,
NXCDF_FILE_INFO_T * file,
NC_GRP_INFO_T * grp )
static

Rebuild dirty variable datasets and mark them committed.

Def-var setters only record their settings and flag the variable dcpl_dirty; _enddef performs the single rebuild per variable that the immutable HDF5 creation properties require. Afterwards every variable with a live dataset is marked var->created, so post-commit setter calls fail with NC_ELATEDEF, matching upstream.

Parameters
h5NetCDF-4 file state.
filenxCDF file state.
grpGroup to walk (recursively).
Returns
NC_NOERR on success, or a NetCDF error code.

◆ nc_start_swmr_write()

int nc_start_swmr_write ( int ncid)

Start the writer session described by nc_start_swmr_write() in nep.h. See nep.h for parameter, return-value and lifecycle documentation.

Start a single-writer/multiple-reader session on a native nxCDF file.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_swmr_flush()

int nc_swmr_flush ( int ncid)

Publish a writer session as specified by nc_swmr_flush() in nep.h. See nep.h for parameter, return-value and publication documentation.

Publish all datasets and dimension scales of an active SWMR writer.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nc_swmr_refresh()

int nc_swmr_refresh ( int ncid)

Refresh a reader session as specified by nc_swmr_refresh() in nep.h. See nep.h for parameter, return-value and visibility documentation.

Refresh a nxCDF SWMR reader session so newly published data becomes visible.

Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ NXCDF__enddef()

int NXCDF__enddef ( int ncid,
size_t h_minfree,
size_t v_align,
size_t v_minfree,
size_t r_align )

Materialize pending metadata and leave define mode — the nc_enddef() dispatch entry.

While a file is in define mode, nc_def_dim/nc_def_var/ nc_put_att only build the in-memory NC_*_INFO_T tree. _enddef is where it all becomes HDF5: NXCDF_write_types() and the metadata writer in nxcdf_meta.c create the committed datatypes, dimension scales, variable datasets and attributes, then the file flushes and switches to data mode. The alignment arguments exist in the dispatch signature for netcdf-c compatibility but are ignored.

Parameters
ncidNetCDF file identifier.
h_minfreeIgnored (reserved for future alignment control).
v_alignIgnored (reserved for future alignment control).
v_minfreeIgnored (reserved for future alignment control).
r_alignIgnored (reserved for future alignment control).
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_abort()

int NXCDF_abort ( int ncid)

Abort a file — the nc_abort() dispatch entry.

Dispatch-table entry behind nc_abort(): releases the file's resources; shares the close path.

For nxCDF abort and close share the release path; HDF5 simply discards unflushed state.

Parameters
ncidNetCDF file identifier.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_add_file()

int NXCDF_add_file ( int ncid,
const char * path,
int mode,
NXCDF_FILE_INFO_T ** filep )

Allocate per-file state and attach it to netcdf-c's open-file list.

Called by both NXCDF_create() and NXCDF_open(). netcdf-c's nc4_file_list_add creates the shared NC_FILE_INFO_T (with a root NC_GRP_INFO_T); this function then allocates the nxCDF-private NXCDF_FILE_INFO_T and hangs it on format_file_info, plus a NXCDF_GRP_INFO_T on the root group. New files start in define mode with hdfid/rootid unset.

Parameters
ncidNetCDF file identifier.
pathFile-system path.
modeEffective NetCDF mode.
filepDestination for the allocated state.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_check_mode()

int NXCDF_check_mode ( int mode)

Reject unsupported disk, memory, sharing and parallel modes.

Validate unsupported create/open flags; return NC_EINVAL if present.

Parameters
modeNetCDF create/open flags, including the UDF selector.
Returns
NC_NOERR for supported flags, otherwise NC_EINVAL.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ NXCDF_close()

int NXCDF_close ( int ncid,
void * parameters )

Flush, close, and release a file — the nc_close() dispatch entry.

Dispatch-table entry behind nc_close(): ends define mode if needed, flushes, closes all HDF5 objects and frees state.

A file still in define mode is _enddef'd first (materializing its pending metadata), then all HDF5 handles are closed recursively, the file is flushed if writable, and the state is freed and removed from netcdf-c's open-file list.

Parameters
ncidNetCDF file identifier.
parametersOptional close parameters; currently ignored.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ nxcdf_close_group_hdf()

void nxcdf_close_group_hdf ( NC_GRP_INFO_T * grp)

Recursively close the HDF5 objects belonging to a group.

At close time every open hid_t under a group must be released: the group's own NXCDF_GRP_INFO_T::hdf_group (skipping the root, whose handle is closed separately via rootid), each variable's dataset hid_t, each dimension's scale hid_t, then all children. HDF5 calls are wrapped in H5E_BEGIN_TRY/H5E_END_TRY so a partially-closed tree does not print spurious error stacks.

Parameters
grpGroup.

◆ NXCDF_free_file()

int NXCDF_free_file ( NXCDF_FILE_INFO_T * file)

Close a NXCDF_FILE_INFO_T's HDF5 handles and free it.

Companion to NXCDF_add_file(): closes the root group and the HDF5 file, then releases the path and the struct. Safe to call with NULL. The H5Fget_obj_count log line is a leak check — anything but 1 (the file itself) after H5Fclose means an object was left open.

Parameters
fileState to release; may be NULL.
Returns
NC_NOERR or NC_EHDFERR if an HDF5 close fails.

◆ NXCDF_get_file()

int NXCDF_get_file ( int ncid,
NC_FILE_INFO_T ** h5p,
NXCDF_FILE_INFO_T ** filep )

Resolve both the shared and nxCDF-private file state for an ncid.

Almost every dispatch entry begins here: nc4_find_grp_h5 maps the ncid to netcdf-c's NC_FILE_INFO_T, and format_file_info then yields the NXCDF_FILE_INFO_T. A missing private record means the ncid names a non-nxCDF file.

Parameters
ncidNetCDF file identifier.
h5pOptional destination for common NetCDF-4 state.
filepOptional destination for nxCDF state.
Returns
NC_NOERR on success, or NC_EBADID for invalid state.

◆ NXCDF_inq_format()

int NXCDF_inq_format ( int ncid,
int * formatp )

Report the public format class — the nc_inq_format() dispatch entry.

Dispatch-table entry behind nc_inq_format(): reports NC_FORMAT_NETCDF4 — nxCDF files are in the NetCDF-4/HDF5 family.

nxCDF files report as NC_FORMAT_NETCDF4: they are HDF5 files in the NetCDF-4 family, distinguishable only through nc_inq_format_extended().

Parameters
ncidNetCDF file identifier.
formatpOptional destination for NC_FORMAT_NETCDF4.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_inq_format_extended()

int NXCDF_inq_format_extended ( int ncid,
int * formatp,
int * modep )

Report the extended format and effective mode — the nc_inq_format_extended() dispatch entry.

Dispatch-table entry behind nc_inq_format_extended(): reports NC_FORMATX_NXCDF and the effective mode flags.

Returns NC_FORMATX_NXCDF plus the mode the file actually runs with: the create/open flags plus NC_NXCDF and, for files carrying the _NxcdfModel marker, NC_NETCDF4_MODEL.

Parameters
ncidNetCDF file identifier.
formatpOptional destination for NC_FORMATX_NXCDF.
modepOptional destination for the effective open/create mode.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_read_markers()

int NXCDF_read_markers ( NXCDF_FILE_INFO_T * file)

Read and validate the hidden marker attributes at open.

Sets file->backend_marked when _NxcdfBackend matches "nxCDF/1.0", and restores netcdf4_model/NC_NETCDF4_MODEL when _NxcdfModel is present. A file without the backend marker is treated as a plain HDF5/netCDF-4 file and opened anyway — nxCDF can read upstream netCDF-4 files.

Parameters
fileOpen nxCDF state.
Returns
NC_NOERR on success, or NC_EFILEMETA for invalid markers.

◆ NXCDF_redef()

int NXCDF_redef ( int ncid)

Return the file to define mode — the nc_redef() dispatch entry.

Flips define_mode and netcdf-c's NC_INDEF flag; the next _enddef writes any newly defined metadata to HDF5.

Parameters
ncidNetCDF file identifier.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ nxcdf_scrub_extended_atomic_data()

void nxcdf_scrub_extended_atomic_data ( NC_GRP_INFO_T * grp)

Release attribute data and variable fill values that netcdf-c's teardown cannot reclaim.

nc4_file_list_del() reclaims every att->data and var->fill_value through NC_reclaim_data(), which routes any type id above NC_MAX_ATOMIC_TYPE to nc4_find_type(). The NEP extended atomic types (NC_FLOAT16 through the ids below NC_FIRSTUSERTYPEID) are not registered user types, so the lookup fails and the recursive teardown aborts early, leaking the whole group/var/att tree. Their payloads are flat malloc'd blocks — no nested allocations — so they are released here directly and the attribute's type id is rewritten to NC_BYTE so upstream's reclaim pass skips it.

Must run after all attribute/fill metadata has been materialized to HDF5 and immediately before nc4_file_list_del().

Parameters
grpRoot group to scrub; recurses into children.

◆ NXCDF_set_fill()

int NXCDF_set_fill ( int ncid,
int fillmode,
int * old_modep )

Change the session default inherited by subsequently defined variables. Existing variables retain their fill properties; this does not enter define mode or persist a file-wide setting. Subgroup IDs address the same default.

Set the session fill default for subsequently defined variables.

Parameters
ncidFile or group identifier.
fillmodeNC_FILL or NC_NOFILL.
old_modepOptional destination for the previous session default.
Returns
NC_NOERR, NC_EBADID, NC_EPERM for read-only files, or NC_EINVAL.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nxcdf_swmr_activate()

int nxcdf_swmr_activate ( NC_FILE_INFO_T * h5,
NXCDF_FILE_INFO_T * file )

Activate a committed session without recursively entering enddef.

Parameters
h5Shared NetCDF file metadata in data mode.
filenxCDF session state.
Returns
NC_NOERR or a validation/HDF5 error; active stays false on failure.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nxcdf_swmr_flush()

int nxcdf_swmr_flush ( NC_FILE_INFO_T * h5,
NXCDF_FILE_INFO_T * file )

Publish all variable data before flushing dimension scales and the file.

Parameters
h5Shared NetCDF metadata.
fileActive writable SWMR file.
Returns
NC_NOERR or NC_EHDFERR; publication is not transactional.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nxcdf_swmr_read_aliases()

int nxcdf_swmr_read_aliases ( NC_GRP_INFO_T * grp)

Collapse coordinate-variable alias handles to a single dataset id.

Two open handles to the same HDF5 object defeat SWMR data visibility in a reader: reads through either handle stay frozen at the open-time snapshot even after H5Drefresh. A coordinate variable holds such a pair — the dimension's scale handle and the variable's own handle — so a reader keeps only the variable's handle and points the dimension record at it. The surviving handle must be closed only once; nxcdf_close_group_hdf() already tolerates an identifier that was closed twice.

Parameters
grpSubtree to walk, in metadata index order.
Returns
NC_NOERR or NC_EHDFERR.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nxcdf_swmr_refresh()

int nxcdf_swmr_refresh ( NC_FILE_INFO_T * h5,
NXCDF_FILE_INFO_T * file )

Refresh all variable data before reloading dimension-scale extents.

Whole-file reader refresh behind nc_swmr_refresh(). The order matters: a coordinate variable's scale is refreshed through the variable's own dataset handle after the alias collapse, so variables are refreshed first and the scale pass only needs to recover extents. Re-running nxcdf_swmr_read_aliases() first is defensive — a session cache tuning call (nxcdf_reopen_var_cache) closes and reopens the alias pair, which would silently restore the poisonous duplicate handles.

Parameters
h5Shared NetCDF metadata.
fileRead-only SWMR reader file.
Returns
NC_NOERR or NC_EHDFERR.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ nxcdf_swmr_validate()

int nxcdf_swmr_validate ( NC_FILE_INFO_T * h5,
NXCDF_FILE_INFO_T * file )

Preflight native serial SWMR without changing definitions or file bounds.

Parameters
h5Shared NetCDF file metadata.
filenxCDF session state.
Returns
NC_NOERR, NC_EPERM, NC_EINVAL, NC_ENOTBUILT or NC_EHDFERR.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ NXCDF_sync()

int NXCDF_sync ( int ncid)

Flush pending writes — the nc_sync() dispatch entry.

Dispatch-table entry behind nc_sync(): flushes a writable file to storage.

Read-only files are a no-op.

Parameters
ncidNetCDF file identifier.
Returns
NC_NOERR on success, or a NetCDF error code.

◆ NXCDF_write_markers()

int NXCDF_write_markers ( NXCDF_FILE_INFO_T * file)

Write the hidden marker attributes identifying the file as nxCDF.

Every nxCDF file gets _NxcdfBackend = "nxCDF/1.0" on the root group — this is what NXCDF_read_markers checks at open to distinguish nxCDF files from upstream netCDF-4/HDF5 files. Files created with NC_NETCDF4_MODEL additionally get the integer _NxcdfModel marker so the compatibility mode survives reopen. See nxCDF Architecture.

Parameters
fileOpen writable nxCDF state.
Returns
NC_NOERR on success, or NC_EHDFERR.

◆ swmr_file()

int swmr_file ( int ncid,
NC_FILE_INFO_T ** h5p,
NXCDF_FILE_INFO_T ** filep )
static

Resolve public SWMR IDs without casting another backend's private state.

Shared ncid validation for nc_start_swmr_write(), nc_swmr_flush() and nc_swmr_refresh(). NC_check_id accepts a file or group ncid; the dispatch-table comparison then proves the file really is nxCDF before any NXCDF_FILE_INFO_T fields are dereferenced, so ids belonging to upstream netcdf-4 or another UDF get a clean NC_ENOTNC4 instead of a bad cast.

Parameters
ncidFile or group ID.
h5pDestination for shared metadata.
filepDestination for nxCDF state.
Returns
NC_NOERR, NC_EBADID or NC_ENOTNC4.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ swmr_flush_group()

int swmr_flush_group ( NC_GRP_INFO_T * grp,
int scales )
static

Flush variable datasets or dimension scales throughout a group tree.

Parameters
grpSubtree to flush, in metadata index order.
scalesNonzero selects scales, zero selects variables.
Returns
NC_NOERR or NC_EHDFERR. Alias handles are deliberately not closed.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ swmr_refresh_group()

int swmr_refresh_group ( NC_GRP_INFO_T * grp,
int scales )
static

Refresh variable datasets or dimension scales throughout a group tree.

Mirrors swmr_flush_group on the reader side, and is called twice by nxcdf_swmr_refresh(): once with scales zero for the variable index, once nonzero for the dimension index. Variable datasets get a bare H5Drefresh, which brings the handle's view of extent and raw data up to the writer's last publication. Dimension-scale datasets are additionally polled for their current extent, which is reloaded into the cached dim->len that nc_inq_dimlen() reports — the metadata tree is otherwise untouched, since a SWMR writer cannot create new objects. Read paths reacquire the dataspace per call, so no further per-variable state needs resetting here.

Parameters
grpSubtree to refresh, in metadata index order.
scalesNonzero selects scales, zero selects variables.
Returns
NC_NOERR or NC_EHDFERR.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ swmr_schema()

int swmr_schema ( NC_GRP_INFO_T * grp)
static

Check the complete tree for schema excluded from SWMR writer sessions.

Parameters
grpRoot of the subtree to validate.
Returns
NC_NOERR, or NC_ENOTBUILT for user types or dynamic/reference variables.
Author
Edward Hartnett, Intelligent Data Design, Inc.

◆ write_string_att()

int write_string_att ( hid_t location,
const char * name,
const char * value )
static

Write a null-terminated scalar HDF5 string attribute.

Helper for NXCDF_write_markers(): creates a scalar dataspace and a fixed-size, null-terminated string type sized to the value, then writes it. All HDF5 handles are closed on both paths.

Parameters
locationHDF5 location identifier.
nameName.
valueData buffer.
Returns
NC_NOERR on success, or NC_EHDFERR.