diff --git a/CHANGELOG.md b/CHANGELOG.md index b95aca8e7..90feec41a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/io/hdf5/HDF5IO.cpp b/src/io/hdf5/HDF5IO.cpp index bdbdaef7d..00b6bf865 100644 --- a/src/io/hdf5/HDF5IO.cpp +++ b/src/io/hdf5/HDF5IO.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "io/hdf5/HDF5IO.hpp" @@ -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( 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; } @@ -167,18 +172,24 @@ 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( 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; } @@ -186,7 +197,8 @@ Status HDF5IO::openRemote(const ChunkCacheConfig& chunkCache) 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); @@ -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; @@ -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( - 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( + 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; diff --git a/src/io/hdf5/HDF5IO.hpp b/src/io/hdf5/HDF5IO.hpp index b37b1bf5d..f0e6f1707 100644 --- a/src/io/hdf5/HDF5IO.hpp +++ b/src/io/hdf5/HDF5IO.hpp @@ -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; @@ -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); @@ -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 = "", @@ -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 diff --git a/tests/testHDF5IO.cpp b/tests/testHDF5IO.cpp index 109a8e901..75c5671ca 100644 --- a/tests/testHDF5IO.cpp +++ b/tests/testHDF5IO.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -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(-1)) == Status::Failure); + REQUIRE_FALSE(hdf5io.isOpen()); + } } TEST_CASE("createGroup", "[hdf5io]")