HighFive icon indicating copy to clipboard operation
HighFive copied to clipboard

Review the code for checking if a link exists.

Open 1uc opened this issue 2 years ago • 0 comments

The code in question is:

template <typename Derivate>
inline bool NodeTraits<Derivate>::_exist(const std::string& node_name, bool raise_errors) const {
    SilenceHDF5 silencer{};
    const auto val =
        H5Lexists(static_cast<const Derivate*>(this)->getId(), node_name.c_str(), H5P_DEFAULT);
    if (val < 0) {
        if (raise_errors) {
            HDF5ErrMapper::ToException<GroupException>("Invalid link for exist()");
        } else {
            return false;
        }
    }

    // The root path always exists, but H5Lexists return 0 or 1
    // depending of the version of HDF5, so always return true for it
    // We had to call H5Lexists anyway to check that there are no errors
    return (node_name == "/") ? true : (val > 0);
}

template <typename Derivate>
inline bool NodeTraits<Derivate>::exist(const std::string& group_path) const {
    // When there are slashes, first check everything is fine
    // so that subsequent errors are only due to missing intermediate groups
    if (group_path.find('/') != std::string::npos) {
        _exist("/");  // Shall not throw under normal circumstances
        // Unless "/" (already checked), verify path exists (not throwing errors)
        return (group_path == "/") ? true : _exist(group_path, false);
    }
    return _exist(group_path);
}

which goes against the recommendations of HDF5, as to how we should check if a nested link exists. I'd like to also be able to check the existence of attributes. Here is the documentation of H5Lexists: https://docs.hdfgroup.org/hdf5/v1_12/group___h5_l.html#ga171be6e41dc1a464edc402df0ebdf801

which includes a pseudo-algorithm for correctly checking existence of nested links.

1uc avatar May 03 '23 06:05 1uc