Commit f68f76f1 authored by Philip Carns's avatar Philip Carns
Browse files

ability to attach targets specified in json

parent 380ebe45
...@@ -57,7 +57,8 @@ typedef struct { ...@@ -57,7 +57,8 @@ typedef struct {
#define BAKE_ERR_IO (-14) /* Back-end I/O error */ #define BAKE_ERR_IO (-14) /* Back-end I/O error */
#define BAKE_ERR_NOENT (-15) /* entry does not exist */ #define BAKE_ERR_NOENT (-15) /* entry does not exist */
#define BAKE_ERR_EXIST (-16) /* entry already exists */ #define BAKE_ERR_EXIST (-16) /* entry already exists */
#define BAKE_ERR_END (-17) /* End of valid bake error codes */ #define BAKE_ERR_NOMEM (-17) /* entry already exists */
#define BAKE_ERR_END (-18) /* End of valid bake error codes */
/** /**
* Print bake errors in human-friendly form * Print bake errors in human-friendly form
......
...@@ -1057,38 +1057,90 @@ static int setup_poolset(bake_provider_t provider) ...@@ -1057,38 +1057,90 @@ static int setup_poolset(bake_provider_t provider)
return BAKE_SUCCESS; return BAKE_SUCCESS;
} }
static int configure_targets(bake_provider_t provider, /* attach each target listed in the backend json block. This fn assumes
struct json_object* _config) * that backend has an array of strings called "targets"
*/
static int attach_targets(bake_provider_t provider,
const char* prefix,
struct json_object* backend)
{ {
struct json_object* val; bake_target_id_t tid;
int ret;
int i;
char** target_names = NULL;
int target_names_count = 0;
struct json_object* targets; struct json_object* targets;
struct json_object* _target; struct json_object* _target;
unsigned i = 0;
if (CONFIG_HAS(_config, "file_backend", val)) { if (CONFIG_HAS(backend, "targets", targets)) {
BAKE_TRACE(provider->mid, "checking file_backend object in json"); target_names_count = json_object_array_length(targets);
target_names = calloc(target_names_count, sizeof(*target_names));
if (!target_names) {
ret = BAKE_ERR_NOMEM;
goto error;
}
for (i = 0; i < target_names_count; i++) {
target_names[i] = malloc(256 * sizeof(char));
if (!target_names[i]) {
ret = BAKE_ERR_NOMEM;
goto error;
}
}
if (CONFIG_HAS(val, "targets", targets)) { json_array_foreach(targets, i, _target)
json_array_foreach(targets, i, _target) {
{ BAKE_TRACE(provider->mid, "target[%u]: %s", i,
BAKE_TRACE(provider->mid, "target: %s", json_object_get_string(_target));
json_object_get_string(_target)); snprintf(target_names[i], 256, "%s:%s", prefix,
json_object_get_string(_target));
}
/* Delete array from parent json. The array will be
* re-constructed as targets are attached.
*/
json_object_object_del(backend, "targets");
for (i = 0; i < target_names_count; i++) {
ret = bake_provider_attach_target(provider, target_names[i], &tid);
if (ret == BAKE_ERR_NOENT) {
/* TODO: target does not exist; try to create it */
assert(0);
} else if (ret != BAKE_SUCCESS) {
goto error;
} }
BAKE_INFO(provider->mid, "attached target %s", target_names[i]);
free(target_names[i]);
} }
/* TODO: walk target list and try to attach or create */ free(target_names);
} }
if (CONFIG_HAS(_config, "pmem_backend", val)) { return (0);
BAKE_TRACE(provider->mid, "checking pmem_backend object in json");
if (CONFIG_HAS(val, "targets", targets)) { error:
json_array_foreach(targets, i, _target) if (target_names) {
{ for (i = 0; i < target_names_count; i++) {
BAKE_TRACE(provider->mid, "target: %s", if (target_names[i]) free(target_names[i]);
json_object_get_string(_target));
}
} }
/* TODO: walk target list and try to attach or create */ free(target_names);
}
return (ret);
}
static int configure_targets(bake_provider_t provider,
struct json_object* _config)
{
struct json_object* val;
int ret;
if (CONFIG_HAS(_config, "file_backend", val)) {
BAKE_TRACE(provider->mid, "checking file_backend object in json");
ret = attach_targets(provider, "file", val);
if (ret != BAKE_SUCCESS) return (ret);
}
if (CONFIG_HAS(_config, "pmem_backend", val)) {
BAKE_TRACE(provider->mid, "checking pmem_backend object in json");
ret = attach_targets(provider, "pmem", val);
if (ret != BAKE_SUCCESS) return (ret);
} }
return (0); return (0);
......
...@@ -57,6 +57,9 @@ static char* bake_err_str(int ret) ...@@ -57,6 +57,9 @@ static char* bake_err_str(int ret)
case BAKE_ERR_EXIST: case BAKE_ERR_EXIST:
return "Entry already exists"; return "Entry already exists";
break; break;
case BAKE_ERR_NOMEM:
return "Out of memory";
break;
default: default:
return "Unknown error"; return "Unknown error";
break; break;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment