Skip to content

Add C function to read token file #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion include/fdp/fdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down
28 changes: 26 additions & 2 deletions src/fdp_c_api.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include <string>
#include <utility>

#include <ghc/filesystem.hpp>

#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;
Expand Down Expand Up @@ -55,7 +58,7 @@ void FDP::delete_c_struct(FdpDataPipeline *data_pipeline) {
*/
template <typename Function, typename Return, typename... Args>
FdpError exception_to_err_code(Function &&function, Return &ret,
Args &&... args) {
Args &&...args) {
try {
ret = std::forward<Function>(function)(std::forward<Args>(args)...);
return FDP_ERR_NONE;
Expand All @@ -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 <typename Function, typename... Args>
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 {
Expand Down Expand Up @@ -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;
}
18 changes: 11 additions & 7 deletions test/test_c_api.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down