|
| 1 | +/* Copyright 2022, University Corporation for Atmospheric Research. |
| 2 | + * See COPYRIGHT file for copying and redistribution conditions. */ |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * @internal This function selects the best HDF5 file format options |
| 6 | + * to create netCDF-4 files that can be read and written by older |
| 7 | + * library versions. |
| 8 | + * |
| 9 | + * Format compatibility is transient, not baked in to an HDF5 file |
| 10 | + * at creation time. Therefore the desired compatibilty options |
| 11 | + * must be selected every time a file is opened for writing. |
| 12 | + * |
| 13 | + * This function should be called before every call to create a new |
| 14 | + * netCDF-4 file, or to open an existing netCDF-4 file for writing. |
| 15 | + * This function has no effect when opening a file for read only. |
| 16 | + * |
| 17 | + * This function should work correctly with all HDF5 library versions |
| 18 | + * from 1.8.0 through 1.13.0 and beyond, with no further changes. |
| 19 | + * This assumes that HDF5 versioning controls remain consistent |
| 20 | + * into the future. |
| 21 | + * |
| 22 | + * The basic functionality is to select the traditional HDF5 v1.8 |
| 23 | + * format compatibility, whenever possible. The less desirable |
| 24 | + * v1.6 compatibily is selected in a few strange cases when it is |
| 25 | + * not possible to select v1.8. |
| 26 | + * |
| 27 | + * Files created or updated with v1.10 and higher compatibility are |
| 28 | + * not legal netCDF-4 format, as of 2022 January. They are not |
| 29 | + * readable by any netCDF library version linked with any HDF5 v1.8 |
| 30 | + * or older library version. However, it is possible for advanced |
| 31 | + * or experimental software to deliberately override these default |
| 32 | + * format settings, to create advanced format files for special |
| 33 | + * purposes. |
| 34 | + * |
| 35 | + * Files created with v1.6 compatibility have superblock version 0. |
| 36 | + * Files created with v1.8 compatibility have superblock version 2. |
| 37 | + * Files created with v1.10 compatibility have superblock version 3, |
| 38 | + * and are avoided by default. Et cetera. |
| 39 | + * |
| 40 | + * The superblock version is locked in when a file is first created. |
| 41 | + * It is then possible to get a mix of v1.6 and v1.8 internal |
| 42 | + * object versions, when an existing netCDF-4 file is modified by |
| 43 | + * a different software version than the one that originally |
| 44 | + * created the file. Mixed-object files of this nature are common |
| 45 | + * and do not suffer any serious problems. |
| 46 | + * |
| 47 | + * See netcdf-c github issues #250 and #951 for more details about |
| 48 | + * the rationale and evolution of netCDF-4 format compatibility. |
| 49 | + * |
| 50 | + * See HDF5 documentation for H5Pset_libver_bounds and related RFC's, |
| 51 | + * for more details about HDF5 file object versioning. |
| 52 | + * |
| 53 | + * @author Dave Allured, NOAA/PSL/CIRES @date 2022 January 11 |
| 54 | + */ |
| 55 | + |
| 56 | +#include "config.h" |
| 57 | +#include "hdf5internal.h" |
| 58 | + |
| 59 | +/** |
| 60 | + * @internal Function to set HDF5 file access options for backward |
| 61 | + * format compatibility. Call this before every call to H5Fcreate |
| 62 | + * or H5Fopen. |
| 63 | + * |
| 64 | + * @param fapl_id Identifier for valid file access property list to |
| 65 | + * be used in the next call to H5Fcreate or H5Fopen. |
| 66 | + * |
| 67 | + * @return ::NC_NOERR No error. |
| 68 | + * @return ::NC_EHDFERR HDF5 returned error. |
| 69 | + * |
| 70 | + * @author Dave Allured, NOAA/PSL/CIRES @date 2022 January 11 |
| 71 | + */ |
| 72 | +int |
| 73 | +hdf5set_format_compatibility(hid_t fapl_id) |
| 74 | +{ |
| 75 | +#if H5_VERSION_GE(1,10,2) |
| 76 | + /* lib versions 1.10.2 and higher */ |
| 77 | + if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_V18, H5F_LIBVER_LATEST) < 0) |
| 78 | + |
| 79 | +#else |
| 80 | +#if H5_VERSION_GE(1,10,0) |
| 81 | + /* lib versions 1.10.0, 1.10.1 */ |
| 82 | + if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_EARLIEST, H5F_LIBVER_LATEST) < 0) |
| 83 | + |
| 84 | +#else |
| 85 | + /* all HDF5 1.8 lib versions */ |
| 86 | + if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) |
| 87 | +#endif |
| 88 | +#endif |
| 89 | + return NC_EHDFERR; /* failure exit */ |
| 90 | + |
| 91 | + return NC_NOERR; /* normal exit */ |
| 92 | +} |
0 commit comments