diff --git a/include/fdp/fdp.h b/include/fdp/fdp.h index d46bf11..609a75f 100644 --- a/include/fdp/fdp.h +++ b/include/fdp/fdp.h @@ -118,7 +118,8 @@ FdpError fdp_link_read(FdpDataPipeline *data_pipeline, const char *data_product, * @return Error code */ FdpError fdp_link_write(FdpDataPipeline *data_pipeline, - const char *data_product, char *data_store_path, size_t data_store_path_len); + const char *data_product, char *data_store_path, + size_t data_store_path_len); /** * @brief Enumeration used to denote the different levels of logging. @@ -168,6 +169,21 @@ FdpLogLevel fdp_get_log_level(); */ int fdp_log(FdpLogLevel log_level, const char *msg); +/** + * @brief Read token from file. + * + * @param path The path to the token file. + * + * @param token Destination to store the token. The user must allocate + * sufficient space for the token. + * + * @param token_len Length of the character array used to store the token. + * + * @return Error code. Returns 0 if the token was read successfully, or a + * positive integer otherwise. + */ +FdpError fdp_read_token(const char *path, char *token, size_t token_len); + #ifdef __cplusplus } // close extern "C" diff --git a/src/fdp_c_api.cxx b/src/fdp_c_api.cxx index 078610e..b3ccffa 100644 --- a/src/fdp_c_api.cxx +++ b/src/fdp_c_api.cxx @@ -5,9 +5,12 @@ #include #include +#include + #include "fdp/exceptions.hxx" #include "fdp/fdp.h" #include "fdp/fdp.hxx" +#include "fdp/objects/metadata.hxx" #include "fdp/utilities/logging.hxx" namespace FDP = FairDataPipeline; @@ -55,7 +58,7 @@ void FDP::delete_c_struct(FdpDataPipeline *data_pipeline) { */ template FdpError exception_to_err_code(Function &&function, Return &ret, - Args &&... args) { + Args &&...args) { try { ret = std::forward(function)(std::forward(args)...); return FDP_ERR_NONE; @@ -82,7 +85,7 @@ FdpError exception_to_err_code(Function &&function, Return &ret, * @brief Companion to exception_to_err_code for non-returning functions. */ template -FdpError exception_to_err_code_void(Function &&function, Args &&... args) { +FdpError exception_to_err_code_void(Function &&function, Args &&...args) { int dummy; return exception_to_err_code( [&function, &args...]() -> int { @@ -323,3 +326,24 @@ int fdp_log(FdpLogLevel log_level, const char *msg) { } return 0; } + +// ========= +// utilities +// ========= + +FdpError fdp_read_token(const char *path, char *token, size_t token_len) { + std::string token_str; + FdpError err = exception_to_err_code(FDP::read_token, token_str, + ghc::filesystem::path(path)); + if (err) { + FDP::logger::get_logger()->error() << "Failed to read token file"; + return err; + } + if (token_str.size() >= token_len) { + FDP::logger::get_logger()->error() + << "Token array is too small to store token"; + return FDP_ERR_OTHER; + } + strncpy(token, token_str.c_str(), token_len); + return err; +} diff --git a/test/test_c_api.cxx b/test/test_c_api.cxx index d1ac2c7..f9637f3 100644 --- a/test/test_c_api.cxx +++ b/test/test_c_api.cxx @@ -37,10 +37,12 @@ TEST(CTest, link_read_write) { FdpDataPipeline *pipeline; fs::path config = fs::path(TESTDIR) / "data" / "write_csv.yaml"; fs::path script = fs::path(TESTDIR) / "test_script.sh"; - std::string token = - fdp::read_token(fs::path(home_dir()) / ".fair" / "registry" / "token"); + fs::path token_path = fs::path(home_dir()) / ".fair" / "registry" / "token"; + char token[BUFFER_SIZE]; + ASSERT_EQ(fdp_read_token(token_path.c_str(), token, BUFFER_SIZE), + FDP_ERR_NONE); ASSERT_EQ(fdp_init(&pipeline, config.string().c_str(), - script.string().c_str(), token.c_str()), + script.string().c_str(), token), FDP_ERR_NONE); // Test link write @@ -58,7 +60,7 @@ TEST(CTest, link_read_write) { ASSERT_EQ(fdp_finalise(&pipeline), FDP_ERR_NONE); config = fs::path(TESTDIR) / "data" / "read_csv.yaml"; ASSERT_EQ(fdp_init(&pipeline, config.string().c_str(), - script.string().c_str(), token.c_str()), + script.string().c_str(), token), FDP_ERR_NONE); // Test link read @@ -111,10 +113,12 @@ TEST(CTest, c_to_cpp) { FdpDataPipeline *c_pipeline; fs::path config = fs::path(TESTDIR) / "data" / "write_csv.yaml"; fs::path script = fs::path(TESTDIR) / "test_script.sh"; - std::string token = - fdp::read_token(fs::path(home_dir()) / ".fair" / "registry" / "token"); + fs::path token_path = fs::path(home_dir()) / ".fair" / "registry" / "token"; + char token[BUFFER_SIZE]; + ASSERT_EQ(fdp_read_token(token_path.c_str(), token, BUFFER_SIZE), + FDP_ERR_NONE); ASSERT_EQ(fdp_init(&c_pipeline, config.string().c_str(), - script.string().c_str(), token.c_str()), + script.string().c_str(), token), FDP_ERR_NONE); // Switch to C++ API