0

Thank you in advance for your time.

I have a rather simple question regarding the best/easiest way to clone (I.e. Make a copy in disk) of an HDF5 file. For example, there is an HDF5 file: file.h5. I would like to copy the contents of this file into file_BU.h5. The reason for this is that there is a possibility my job that writes to the file will be killed partway through a write (to multiple datasets), and I would like to have a backup in between each "generation" of writes so I can recover the most recent good state.

Of course I could use normal file copy code such as Copy a file in a sane, safe and efficient way (or a naive system( "cp file.h5 file_BU.h5" ) ). However, this is an HDF5 specific question -- I'm wondering if there is a function to clone() an HDF5 file. Example code snippet below:

H5::H5File file;
file = H5::H5File( "file.h5", H5F_ACC_TRUNC );
//TODO Write datasets etc. to file
std::string file2name = "file_BU.h5";

//This is the thing I would like to do:
file.clone( file2name );
file.close();

//file_BU.h5 and file.h5 are now identical on disk...
Community
  • 1
  • 1
rveale
  • 66
  • 1
  • 10
  • 1
    What reasons are speaking against making a copy at the file-system level? The only advantage of parsing and re-formatting the file I can see is that to some degree, it verifies the integrity of the file. But it will also be slower and probably not easier. – 5gon12eder Feb 15 '16 at 08:32
  • Thanks for the good question. No reasons other than there might be an HDF5 library function to do it that will work from the H5::H5File, instead of having to know the string of the filename it's open from... – rveale Feb 15 '16 at 11:07
  • Why don't you read your file using `std::fstream` to an `std::string` as binary and then write it again to another file? Why does this have to be HDF5 specific at all? Are you willing to do this just for fun? – The Quantum Physicist Feb 16 '16 at 13:38

1 Answers1

0

I haven't checked the whole doc, but closest to what You are looking for seems to be https://www.hdfgroup.org/HDF5/doc/RM/RM_H5O.html#Object-Copy , but You obviously need to write some function to copy the whole thing.
You can check the rest of available functions here https://www.hdfgroup.org/HDF5/doc/RM/RM_H5Front.html#LowLevelAPIs and I would also look at the source of h5copy command line tool.

kakk11
  • 898
  • 8
  • 21