@@ -338,6 +338,47 @@ HDF5IOHandlerImpl::createDataset(Writable* writable,
338338 H5P_DEFAULT);
339339 VERIFY (node_id >= 0 , " [HDF5] Internal error: Failed to open HDF5 group during dataset creation" );
340340
341+ if ( m_handler->m_backendAccess == Access::APPEND )
342+ {
343+ // The dataset might already exist in the file from a previous run
344+ // We delete it, otherwise we could not create it again with
345+ // possibly different parameters.
346+ // This is inefficient, as only the link to the dataset will be
347+ // removed, but not the actual dataset
348+ // But such is life if overwriting an iteration in Append mode
349+ H5G_info_t group_info;
350+ herr_t status = H5Gget_info ( node_id, &group_info );
351+ VERIFY (
352+ status == 0 ,
353+ " [HDF5] Internal error: Failed to get HDF5 group info for " +
354+ concrete_h5_file_position ( writable ) +
355+ " during dataset creation" );
356+ for ( hsize_t i = 0 ; i < group_info.nlinks ; ++i )
357+ {
358+ if ( H5G_DATASET != H5Gget_objtype_by_idx ( node_id, i ) )
359+ {
360+ continue ;
361+ }
362+ ssize_t name_length =
363+ H5Gget_objname_by_idx ( node_id, i, nullptr , 0 );
364+ std::vector< char > charbuffer ( name_length + 1 );
365+ H5Gget_objname_by_idx (
366+ node_id, i, charbuffer.data (), name_length + 1 );
367+ if ( std::strncmp (
368+ name.c_str (), charbuffer.data (), name_length + 1 ) !=
369+ 0 )
370+ {
371+ continue ;
372+ }
373+ status = H5Ldelete ( node_id, name.c_str (), H5P_DEFAULT );
374+ VERIFY (
375+ status == 0 ,
376+ " [HDF5] Internal error: Failed to delete old dataset from "
377+ " group for overwriting." );
378+ break ;
379+ }
380+ }
381+
341382 Datatype d = parameters.dtype ;
342383 if ( d == Datatype::UNDEFINED )
343384 {
0 commit comments