Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0



## [Unreleased]

### Fixed
* Fixed `HDF5IO::open` to return `Status::Failure` instead of throwing if HDF5 can't open or create the file and updated `HDF5IO::open`, `HDF5IO::openS3`, and `HDF5IO::openRemote` to print the HDF5 error if opening fails. (@claude, @oruebel, [#363](https://github.com/NeurodataWithoutBorders/aqnwb/pull/363))

## [0.5.0] - 2026-11-03

### Added
Expand Down
66 changes: 44 additions & 22 deletions src/io/hdf5/HDF5IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <numeric>
#include <stdexcept>
#include <system_error>
#include <vector>

#include "io/hdf5/HDF5IO.hpp"
Expand Down Expand Up @@ -142,19 +143,23 @@ Status HDF5IO::openS3(const std::string& aws_region,
copyField(secret_id, ros3_fa.secret_id, sizeof(ros3_fa.secret_id));
copyField(secret_key, ros3_fa.secret_key, sizeof(ros3_fa.secret_key));

FileAccPropList fapl = createFileAccessPropertyList(chunkCache);
const herr_t ros3Status = H5Pset_fapl_ros3(fapl.getId(), &ros3_fa);
if (ros3Status < 0) {
return Status::Failure;
}

// HDF5 throws if it can't open the file, e.g., if the URL is invalid or the
// file is not an HDF5 file
try {
FileAccPropList fapl = createFileAccessPropertyList(chunkCache);
if (H5Pset_fapl_ros3(fapl.getId(), &ros3_fa) < 0) {
std::cerr << "HDF5IO::openS3: unable to set the ROS3 driver for file '"
<< getFileName() << "'" << std::endl;
return Status::Failure;
}
m_file = std::make_unique<H5::H5File>(
getFileName(), H5F_ACC_RDONLY, FileCreatPropList::DEFAULT, fapl);
m_opened = true;
} catch (const H5::Exception&) {
} catch (const H5::Exception& e) {
std::cerr << "HDF5IO::openS3: error opening file '" << getFileName()
<< "': " << e.getDetailMsg() << std::endl;
return Status::Failure;
}
m_opened = true;

return Status::Success;
}
Expand All @@ -167,26 +172,33 @@ Status HDF5IO::openRemote(const ChunkCacheConfig& chunkCache)
return Status::Failure;
}

FileAccPropList fapl = createFileAccessPropertyList(chunkCache);
if (H5Pset_fapl_remfile(fapl.getId(), nullptr) < 0) {
return Status::Failure;
}

// HDF5 throws if it can't open the file, e.g., if the URL is invalid or the
// file is not an HDF5 file
try {
FileAccPropList fapl = createFileAccessPropertyList(chunkCache);
if (H5Pset_fapl_remfile(fapl.getId(), nullptr) < 0) {
std::cerr << "HDF5IO::openRemote: unable to set the remfile driver for "
"file '"
<< getFileName() << "'" << std::endl;
return Status::Failure;
}
m_file = std::make_unique<H5::H5File>(
getFileName(), H5F_ACC_RDONLY, FileCreatPropList::DEFAULT, fapl);
m_opened = true;
} catch (const H5::Exception&) {
} catch (const H5::Exception& e) {
std::cerr << "HDF5IO::openRemote: error opening file '" << getFileName()
<< "': " << e.getDetailMsg() << std::endl;
return Status::Failure;
}
m_opened = true;

return Status::Success;
}
#endif // AQNWB_HAVE_REMFILE_VFD

Status HDF5IO::open()
{
if (std::filesystem::exists(getFileName())) {
std::error_code ec; // use the non-throwing overload of exists
if (std::filesystem::exists(getFileName(), ec)) {
return open(FileMode::ReadWrite);
} else {
return open(FileMode::Overwrite);
Expand All @@ -206,14 +218,13 @@ Status HDF5IO::open(FileMode mode, const ChunkCacheConfig& chunkCache)
return Status::Failure;
}

if (!std::filesystem::exists(getFileName())) {
std::error_code ec; // use the non-throwing overload of exists
if (!std::filesystem::exists(getFileName(), ec)) {
if (mode == FileMode::ReadWrite || mode == FileMode::ReadOnly) {
return Status::Failure;
}
}

FileAccPropList fapl = createFileAccessPropertyList(chunkCache);

switch (mode) {
case FileMode::Overwrite:
accFlags = H5F_ACC_TRUNC;
Expand All @@ -225,11 +236,22 @@ Status HDF5IO::open(FileMode mode, const ChunkCacheConfig& chunkCache)
accFlags = H5F_ACC_RDONLY | H5F_ACC_SWMR_READ;
break;
default:
throw std::invalid_argument("Invalid file mode");
std::cerr << "HDF5IO::open: invalid file mode for file '" << getFileName()
<< "'" << std::endl;
return Status::Failure;
}

m_file = std::make_unique<H5::H5File>(
getFileName(), accFlags, FileCreatPropList::DEFAULT, fapl);
// HDF5 throws if it can't open or create the file, e.g., if the file is not
// an HDF5 file or the directory does not exist or is not writable
try {
FileAccPropList fapl = createFileAccessPropertyList(chunkCache);
m_file = std::make_unique<H5::H5File>(
getFileName(), accFlags, FileCreatPropList::DEFAULT, fapl);
} catch (const H5::Exception& e) {
std::cerr << "HDF5IO::open: error opening file '" << getFileName()
<< "': " << e.getDetailMsg() << std::endl;
return Status::Failure;
}
m_opened = true;

return Status::Success;
Expand Down
26 changes: 20 additions & 6 deletions src/io/hdf5/HDF5IO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,18 @@ class HDF5IO : public BaseIO

/**
* @brief Opens an existing file or creates a new file for writing.
* @return The status of the file opening operation.
* @return The status of the file opening operation (Status::Failure if HDF5
* can't open or create the file, see open(FileMode, const
* ChunkCacheConfig&)).
*/
Status open() override;

/**
* @brief Opens an existing file or creates a new file for writing.
* @param mode Access mode to use when opening the file.
* @return The status of the file opening operation.
* @return The status of the file opening operation (Status::Failure if HDF5
* can't open or create the file, see open(FileMode, const
* ChunkCacheConfig&)).
*/
Status open(FileMode mode) override;

Expand All @@ -115,8 +119,12 @@ class HDF5IO : public BaseIO
* custom raw data chunk cache.
* @param mode Access mode to use when opening the file.
* @param chunkCache Chunk cache used for each dataset of the file.
* @return The status of the file opening operation (Status::Failure if the
* chunk cache configuration is invalid).
* @return The status of the file opening operation. Status::Failure if the
* file is already open, the chunk cache configuration or the mode is
* invalid, the file does not exist in ReadWrite or ReadOnly mode, or HDF5
* can't open or create the file (e.g., if the file is not an HDF5 file or
* the directory does not exist or is not writable). HDF5 errors are caught
* and printed to std::cerr.
*/
Status open(FileMode mode, const ChunkCacheConfig& chunkCache);

Expand All @@ -139,7 +147,10 @@ class HDF5IO : public BaseIO
* @param secret_id The AWS access key ID. May be empty.
* @param secret_key The AWS secret access key. May be empty.
* @param chunkCache Chunk cache used for each dataset of the file.
* @return The status of the file opening operation.
* @return The status of the file opening operation. Status::Failure if the
* file is already open, the chunk cache configuration is invalid, or HDF5
* can't open the file (e.g., if the URL is invalid or the file is not an
* HDF5 file). HDF5 errors are caught and printed to std::cerr.
*/
Status openS3(const std::string& aws_region = "",
const std::string& secret_id = "",
Expand All @@ -162,7 +173,10 @@ class HDF5IO : public BaseIO
* read-ahead (see https://github.com/catalystneuro/remfile-cpp).
*
* @param chunkCache Chunk cache used for each dataset of the file.
* @return The status of the file opening operation.
* @return The status of the file opening operation. Status::Failure if the
* file is already open, the chunk cache configuration is invalid, or HDF5
* can't open the file (e.g., if the URL is invalid or the file is not an
* HDF5 file). HDF5 errors are caught and printed to std::cerr.
*/
Status openRemote(const ChunkCacheConfig& chunkCache = ChunkCacheConfig());
#endif // AQNWB_HAVE_REMFILE_VFD
Expand Down
38 changes: 38 additions & 0 deletions tests/testHDF5IO.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include <filesystem>
#include <fstream>
#include <future>
#include <iostream>
#include <limits>
Expand Down Expand Up @@ -172,6 +173,43 @@ TEST_CASE("open - hdf5 file modes", "[hdf5io]")
REQUIRE(hdf5io.open(IO::FileMode::ReadOnly) == Status::Failure);
REQUIRE_FALSE(hdf5io.isOpen());
}

SECTION("Open a file that is not an HDF5 file")
{
const std::string textFileName = getTestFilePath("test_open_not_hdf5.h5");
{
std::ofstream textFile(textFileName);
textFile << "not an HDF5 file";
}

for (const auto mode : {IO::FileMode::ReadWrite, IO::FileMode::ReadOnly}) {
IO::HDF5::HDF5IO hdf5io(textFileName);
REQUIRE(hdf5io.open(mode) == Status::Failure);
REQUIRE_FALSE(hdf5io.isOpen());
}
std::filesystem::remove(textFileName);
}

SECTION("Create a file in a non-existent directory")
{
const std::string missingDirFileName =
getTestFilePath("missing_directory/test_open_modes.h5");
REQUIRE_FALSE(std::filesystem::exists(
std::filesystem::path(missingDirFileName).parent_path()));

IO::HDF5::HDF5IO hdf5io(missingDirFileName);
REQUIRE(hdf5io.open(IO::FileMode::Overwrite) == Status::Failure);
REQUIRE_FALSE(hdf5io.isOpen());
// The IO can still be closed after a failed open
REQUIRE(hdf5io.close() == Status::Success);
}

SECTION("Open with an invalid file mode")
{
IO::HDF5::HDF5IO hdf5io(fileName);
REQUIRE(hdf5io.open(static_cast<IO::FileMode>(-1)) == Status::Failure);
REQUIRE_FALSE(hdf5io.isOpen());
}
}

TEST_CASE("createGroup", "[hdf5io]")
Expand Down
Loading