Skip to content

Commit

Permalink
HDF5: Delete and re-create attribute when overwriting with diff. type
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 19, 2024
1 parent 269c5e2 commit f33f078
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/IO/HDF5/HDF5IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,8 +1694,7 @@ void HDF5IOHandlerImpl::writeAttribute(
"[HDF5] Internal error: Failed to get HDF5 datatype during attribute "
"write");
std::string name = parameters.name;
if (H5Aexists(node_id, name.c_str()) == 0)
{
auto create_attribute_anew = [&]() {
hid_t dataspace = getH5DataSpace(att);
VERIFY(
dataspace >= 0,
Expand All @@ -1717,14 +1716,41 @@ void HDF5IOHandlerImpl::writeAttribute(
status == 0,
"[HDF5] Internal error: Failed to close HDF5 dataspace during "
"attribute write");
}
else
};
if (H5Aexists(node_id, name.c_str()) != 0)
{
attribute_id = H5Aopen(node_id, name.c_str(), H5P_DEFAULT);
VERIFY(
node_id >= 0,
attribute_id >= 0,
"[HDF5] Internal error: Failed to open HDF5 attribute during "
"attribute write");
/*
* Only reuse the old attribute if it had the same type.
*/
hid_t type_id = H5Aget_type(attribute_id);
VERIFY(
type_id >= 0,
"[HDF5] Internal error: Failed to inquire HDF5 attribute type "
"during attribute write");
auto equal = H5Tequal(type_id, dataType);
VERIFY(
equal >= 0,
"[HDF5] Internal error: Failed to compare HDF5 attribute types "
"during attribute write");
if (equal == 0) // unequal
{
status = H5Adelete(node_id, name.c_str());
VERIFY(
status == 0,
"[HDF5] Internal error: Failed to delete previous HDF5 "
"attribute "
"during attribute write");
create_attribute_anew();
}
}
else
{
create_attribute_anew();
}

using DT = Datatype;
Expand Down

0 comments on commit f33f078

Please sign in to comment.