Skip to content

Commit

Permalink
AXL: add configuration call
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaas80 committed Oct 1, 2020
1 parent a799cd9 commit ae4e148
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 1 deletion.
87 changes: 87 additions & 0 deletions src/axl.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
Expand Down Expand Up @@ -309,6 +310,92 @@ int AXL_Finalize (void)
return rc;
}


/** Set a AXL config parameters */
kvtree* AXL_Config(const int id, const kvtree* config)
{
kvtree* retval = (kvtree*)(config);

static const char* known_options[] = {
AXL_KEY_CONFIG_FILE_BUF_SIZE,
AXL_KEY_CONFIG_DEBUG,
AXL_KEY_CONFIG_MKDIR,
AXL_KEY_CONFIG_COPY_METADATA,
NULL
};

/* TODO: implement getting configuration options back */
if (config == NULL) {
return NULL;
}

/* TODO: implement transfer specific options */
if (id != -1) {
return NULL;
}

if (config != NULL) {
/* read out all options we know about */
/* TODO: this could be turned into a list of structs */
unsigned long ul;
if (kvtree_util_get_bytecount(config,
AXL_KEY_CONFIG_FILE_BUF_SIZE, &ul) == KVTREE_SUCCESS)
{
axl_file_buf_size = (size_t) ul;
if (axl_file_buf_size != ul) {
char* value;
kvtree_util_get_str(config, AXL_KEY_CONFIG_FILE_BUF_SIZE, &value);
AXL_ERR("Value '%s' passed for %s exceeds int range",
value, AXL_KEY_CONFIG_FILE_BUF_SIZE
);
retval = NULL;
}
}

kvtree_util_get_int(config, AXL_KEY_CONFIG_DEBUG, &axl_debug);

kvtree_util_get_int(config, AXL_KEY_CONFIG_MKDIR, &axl_make_directories);

kvtree_util_get_int(config,
AXL_KEY_CONFIG_COPY_METADATA, &axl_copy_metadata);

/* report all unknown options (typos?) */
const kvtree_elem* elem;
for (elem = kvtree_elem_first(config);
elem != NULL;
elem = kvtree_elem_next(elem))
{
/* must be only one level deep, ie plain kev = value */
const kvtree* elem_hash = kvtree_elem_hash(elem);
assert(kvtree_size(elem_hash) == 1);

const kvtree* kvtree_first_elem_hash =
kvtree_elem_hash(kvtree_elem_first(elem_hash));
assert(kvtree_size(kvtree_first_elem_hash) == 0);

/* check against known options */
const char** opt;
int found = 0;
for (opt = known_options; *opt != NULL; opt++) {
if (strcmp(*opt, kvtree_elem_key(elem)) == 0) {
found = 1;
break;
}
}
if (! found) {
AXL_ERR("Unknown configuration parameter '%s' with value '%s'",
kvtree_elem_key(elem),
kvtree_elem_key(kvtree_elem_first(kvtree_elem_hash(elem)))
);
retval = NULL;
}
}
}

return retval;
}


/* Create a transfer handle (used for 0+ files)
* Type specifies a particular method to use
* Name is a user/application provided string
Expand Down
24 changes: 24 additions & 0 deletions src/axl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ extern "C" {
* \ingroup axl
* \brief asynchronous transfer library */

/** AXL configuration options */
#define AXL_KEY_CONFIG_FILE_BUF_SIZE "FILE_BUF_SIZE"
#define AXL_KEY_CONFIG_DEBUG "DEBUG"
#define AXL_KEY_CONFIG_MKDIR "MKDIR"
#define AXL_KEY_CONFIG_COPY_METADATA "COPY_METADATA"

/** Supported AXL transfer methods
* Note that DW, BBAPI, and CPPR must be found at compile time */
typedef enum {
Expand Down Expand Up @@ -82,6 +88,24 @@ int AXL_Finalize (void);
__AXL_Create(type, name, GET_ARG0(__VA_ARGS__))
int __AXL_Create (axl_xfer_t xtype, const char* name, const char* state_file);

/**
* Get/set AXL configuration values.
*
* config: The new configuration. Global variables are in top level of
* the tree, and per-ID values are subtrees. If config=NULL,
* then return a kvtree with all the configuration values (globals
* and all per-ID trees).
*
* Return value: If config != NULL, then return config on success. If
* config=NULL (you're querying the config) then return
* a new kvtree on success. Return NULL on any failures.
*/
typedef struct kvtree_struct kvtree;
kvtree* AXL_Config(
const int id, /** [IN] - transfer to act on, -1 for defaults */
const kvtree* config /** [IN] - kvtree of options */
);

/** Add a file to an existing transfer handle */
int AXL_Add (int id, const char* source, const char* destination);

Expand Down
2 changes: 1 addition & 1 deletion src/axl_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/*
* A list of pointers to kvtrees, indexed by AXL ID.
*/
kvtree** axl_kvtrees;
extern kvtree** axl_kvtrees;

/* current debug level for AXL library,
* set in AXL_Init used in axl_dbg */
Expand Down
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
###############
# Build tests
###############
INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR})

LIST(APPEND axl_test_srcs
axl_cp.c
Expand All @@ -10,8 +11,10 @@ LIST(APPEND axl_test_srcs
# SET(CMAKE_BUILD_TYPE Debug)

ADD_EXECUTABLE(axl_cp ${axl_test_srcs})
ADD_EXECUTABLE(test_config test_config.c)

TARGET_LINK_LIBRARIES(axl_cp axl)
TARGET_LINK_LIBRARIES(test_config axl)

################
# Add tests to ctest
Expand Down Expand Up @@ -43,6 +46,8 @@ IF(BBAPI_FOUND)
ADD_TEST(bbapi_resume_test test_axl.sh -n 300 -c 3 -U bbapi)
ENDIF(BBAPI_FOUND)

ADD_TEST(test_config test_config)

####################
# make a verbose "test" target named "check"
####################
Expand Down
120 changes: 120 additions & 0 deletions test/test_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <stdio.h>
#include <stdlib.h>
#include "axl.h"
#include "axl_internal.h"

#include "kvtree.h"
#include "kvtree_util.h"

int
main(void) {
int rc;
kvtree* axl_config_values = kvtree_new();

size_t old_axl_file_buf_size = axl_file_buf_size;
int old_axl_debug = axl_debug;
int old_axl_make_directories = axl_make_directories;
int old_axl_copy_metadata = axl_copy_metadata;

rc = AXL_Init();
if (rc != AXL_SUCCESS) {
printf("AXL_Init() failed (error %d)\n", rc);
return rc;
}

/* check AXL configuration settings */
rc = kvtree_util_set_bytecount(axl_config_values,
AXL_KEY_CONFIG_FILE_BUF_SIZE,
old_axl_file_buf_size + 1);
if (rc != KVTREE_SUCCESS) {
printf("kvtree_util_set_bytecount failed (error %d)\n", rc);
return rc;
}
rc = kvtree_util_set_int(axl_config_values, AXL_KEY_CONFIG_DEBUG,
!old_axl_debug);
if (rc != KVTREE_SUCCESS) {
printf("kvtree_util_set_int failed (error %d)\n", rc);
return rc;
}

printf("Configuring AXL (first set of options)...\n");
if (AXL_Config(-1, axl_config_values) == NULL) {
printf("AXL_Config() failed\n");
return EXIT_FAILURE;
}

/* check that options were set */

if (axl_file_buf_size != old_axl_file_buf_size + 1) {
printf("AXL_Config() failed to set %s: %lu != %lu\n",
AXL_KEY_CONFIG_FILE_BUF_SIZE, (long unsigned)axl_file_buf_size,
(long unsigned)(old_axl_file_buf_size + 1));
return EXIT_FAILURE;
}

if (axl_debug != !old_axl_debug) {
printf("AXL_Config() failed to set %s: %d != %d\n",
AXL_KEY_CONFIG_DEBUG, axl_debug, !old_axl_debug);
return EXIT_FAILURE;
}

/* set remainder of options */
kvtree_delete(&axl_config_values);
axl_config_values = kvtree_new();

rc = kvtree_util_set_int(axl_config_values, AXL_KEY_CONFIG_MKDIR,
!old_axl_make_directories);
if (rc != KVTREE_SUCCESS) {
printf("kvtree_util_set_int failed (error %d)\n", rc);
return rc;
}
rc = kvtree_util_set_int(axl_config_values, AXL_KEY_CONFIG_COPY_METADATA,
!old_axl_copy_metadata);
if (rc != KVTREE_SUCCESS) {
printf("kvtree_util_set_int failed (error %d)\n", rc);
return rc;
}

printf("Configuring AXL (second set of options)...\n");
if (AXL_Config(-1, axl_config_values) == NULL) {
printf("AXL_Config() failed\n");
return EXIT_FAILURE;
}

/* check all options once more */

if (axl_file_buf_size != old_axl_file_buf_size + 1) {
printf("AXL_Config() failed to set %s: %lu != %lu\n",
AXL_KEY_CONFIG_FILE_BUF_SIZE, (long unsigned)axl_file_buf_size,
(long unsigned)(old_axl_file_buf_size + 1));
return EXIT_FAILURE;
}

if (axl_debug != !old_axl_debug) {
printf("AXL_Config() failed to set %s: %d != %d\n",
AXL_KEY_CONFIG_DEBUG, axl_debug, !old_axl_debug);
return EXIT_FAILURE;
}

if (axl_make_directories != !old_axl_make_directories) {
printf("AXL_Config() failed to set %s: %d != %d\n",
AXL_KEY_CONFIG_MKDIR, axl_make_directories,
!old_axl_make_directories);
return EXIT_FAILURE;
}

if (axl_copy_metadata != !old_axl_copy_metadata) {
printf("AXL_Config() failed to set %s: %d != %d\n",
AXL_KEY_CONFIG_COPY_METADATA, axl_copy_metadata,
!old_axl_copy_metadata);
return EXIT_FAILURE;
}

rc = AXL_Finalize();
if (rc != AXL_SUCCESS) {
printf("AXL_Finalize() failed (error %d)\n", rc);
return rc;
}

return AXL_SUCCESS;
}

0 comments on commit ae4e148

Please sign in to comment.