Skip to content

Commit

Permalink
conf_environment refactor
Browse files Browse the repository at this point in the history
Some gcc versions don't like the vec.h/vec.c implementations of a vector, this removes the dependency on that, and reimplements conf_env functions in terms of argz instead of usa.
  • Loading branch information
trws committed Mar 2, 2016
1 parent c6ff212 commit 61a8145
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 767 deletions.
10 changes: 5 additions & 5 deletions src/cmd/flux.c
Expand Up @@ -167,12 +167,12 @@ int main (int argc, char *argv[])
if (optparse_hasopt (p, "trace-handle")) {
if (setenv ("FLUX_HANDLE_TRACE", "1", 1) < 0)
err_exit ("setenv");
flux_conf_environment_set (cf, "FLUX_HANDLE_TRACE", "1", "");
flux_conf_environment_set (cf, "FLUX_HANDLE_TRACE", "1", 0);
}
if (optparse_getopt (p, "uri", &opt)) {
if (setenv ("FLUX_URI", opt, 1) < 0)
err_exit ("setenv");
flux_conf_environment_set(cf, "FLUX_URI", opt, "");
flux_conf_environment_set(cf, "FLUX_URI", opt, 0);
}

optind = optparse_optind (p);
Expand Down Expand Up @@ -200,7 +200,7 @@ int main (int argc, char *argv[])
optparse_set_data (p, "flux_t", h);
} else {
if (flux_conf_load (cf) == 0) {
flux_conf_environment_set (cf, "FLUX_CONF_USEFILE", "1", "");
flux_conf_environment_set (cf, "FLUX_CONF_USEFILE", "1", 0);
} else if (errno != ENOENT)
err_exit ("%s", flux_conf_get_directory (cf));
}
Expand All @@ -210,7 +210,7 @@ int main (int argc, char *argv[])
*/
if (!optparse_getopt (p, "secdir", &secdir))
secdir = flux_conf_get_directory (cf);
flux_conf_environment_set (cf, "FLUX_SEC_DIRECTORY", secdir, "");
flux_conf_environment_set (cf, "FLUX_SEC_DIRECTORY", secdir, 0);

/* Add PATH to flux_conf_environment and prepend path to
* this executable if necessary.
Expand Down Expand Up @@ -332,7 +332,7 @@ void setup_path (flux_conf_t cf, const char *argv0)
/* If argv[0] was explicitly "flux" then assume PATH is already set */
if (strcmp (argv0, "flux") == 0)
return;
flux_conf_environment_from_env (cf, "PATH", "/bin:/usr/bin", ":");
flux_conf_environment_from_env (cf, "PATH", "/bin:/usr/bin", ':');
selfdir = dir_self ();
flux_conf_environment_push (cf, "PATH", selfdir);
free (selfdir);
Expand Down
130 changes: 94 additions & 36 deletions src/common/libflux/conf.c
Expand Up @@ -31,13 +31,12 @@
#include <unistd.h>
#include <stdlib.h>
#include <czmq.h>
#include <argz.h>

#include "src/common/libutil/log.h"
#include "src/common/libutil/xzmalloc.h"
#include "src/common/libutil/iterators.h"
#include "src/common/libutil/sds.h"
#include "src/common/libutil/vec.h"
#include "src/common/libutil/usa.h"


#include "conf.h"

Expand Down Expand Up @@ -96,15 +95,15 @@ static void initialize_environment (flux_conf_t cf)
{
// Defaults from the environment
flux_conf_environment_from_env (
cf, "LUA_CPATH", "", ";"); /* Lua replaces ;; with the default path */
cf, "LUA_CPATH", "", ';'); /* Lua replaces ;; with the default path */
flux_conf_environment_no_dedup_push_back (cf, "LUA_CPATH", ";;");
flux_conf_environment_from_env (
cf, "LUA_PATH", "", ";"); /* use a null separator to keep it intact */
cf, "LUA_PATH", "", ';'); /* use a null separator to keep it intact */
flux_conf_environment_no_dedup_push_back (cf, "LUA_PATH", ";;");
flux_conf_environment_from_env (cf, "PYTHONPATH", "", ":");
flux_conf_environment_from_env (cf, "FLUX_CONNECTOR_PATH", "", ":");
flux_conf_environment_from_env (cf, "FLUX_EXEC_PATH", "", ":");
flux_conf_environment_from_env (cf, "FLUX_MODULE_PATH", "", ":");
flux_conf_environment_from_env (cf, "PYTHONPATH", "", ':');
flux_conf_environment_from_env (cf, "FLUX_CONNECTOR_PATH", "", ':');
flux_conf_environment_from_env (cf, "FLUX_EXEC_PATH", "", ':');
flux_conf_environment_from_env (cf, "FLUX_MODULE_PATH", "", ':');

// Build paths
flux_conf_environment_push (cf, "FLUX_CONNECTOR_PATH", CONNECTOR_PATH);
Expand All @@ -123,7 +122,7 @@ const char *flux_conf_get_directory (flux_conf_t cf)
void flux_conf_set_directory (flux_conf_t cf, const char *path)
{
free (cf->confdir);
flux_conf_environment_set (cf, "FLUX_CONF_DIRECTORY", path, "");
flux_conf_environment_set (cf, "FLUX_CONF_DIRECTORY", path, ':');
cf->confdir = xstrdup (path);
}

Expand Down Expand Up @@ -290,38 +289,82 @@ const char *flux_conf_next (flux_conf_itr_t itr)
return item;
}

struct env_item {
char *argz;
size_t argz_len;
char sep;
_Bool clean;
char *str_cache;
};

struct env_item * new_env_item(){
return calloc (1, sizeof(struct env_item));
}
void free_env_item(struct env_item *i)
{
free (i->argz);
free (i->str_cache);
free (i);
}

char *find_env_item(struct env_item *i, const char *s)
{
char *entry = 0;
while ((entry = argz_next (i->argz, i->argz_len, entry))) {
if (!strcmp(entry, s))
return entry;
}
return NULL;
}

static const char *stringify_env_item (struct env_item *item)
{
if (!item)
return NULL;
if (!item->argz)
return "";
if (item->clean)
return item->str_cache;

free(item->str_cache);
item->str_cache = malloc(item->argz_len);
memcpy(item->str_cache, item->argz, item->argz_len);
argz_stringify(item->str_cache, item->argz_len, item->sep);

return item->str_cache;
}

const char *flux_conf_environment_get (flux_conf_t cf, const char *key)
{
return usa_get_joined (zhash_lookup (cf->environment, key));
struct env_item *item = zhash_lookup (cf->environment, key);
return stringify_env_item(item);
}

static void flux_conf_environment_set_inner (flux_conf_t cf,
const char *key,
const sds value,
const char *separator)
const char *value,
char separator)
{
unique_string_array_t *item = usa_new ();
usa_set_separator (item, separator);
struct env_item *item = new_env_item();
item->clean = false;
if (sdslen (item->sep))
usa_split_and_push (item, value, false);
else
usa_push (item, value);
item->sep = separator;
zhash_update (cf->environment, key, (void *)item);
zhash_freefn (cf->environment, key, (zhash_free_fn *)usa_destroy);
zhash_freefn (cf->environment, key, (zhash_free_fn *)free_env_item);

flux_conf_environment_push_back(cf, key, value);
}

void flux_conf_environment_set (flux_conf_t cf,
const char *key,
const char *value,
const char *separator)
char separator)
{
flux_conf_environment_set_inner (cf, key, sdsnew (value), separator);
flux_conf_environment_set_inner (cf, key, value, separator);
}

void flux_conf_environment_unset (flux_conf_t cf, const char *key)
{
flux_conf_environment_set_inner (cf, key, sdsempty (), "");
flux_conf_environment_set_inner (cf, key, NULL, 0);
}

static void flux_conf_environment_push_inner (flux_conf_t cf,
Expand All @@ -333,17 +376,30 @@ static void flux_conf_environment_push_inner (flux_conf_t cf,
if (!value || strlen (value) == 0)
return;

unique_string_array_t *item = zhash_lookup (cf->environment, key);
struct env_item *item = zhash_lookup (cf->environment, key);
if (!item)
item = usa_new ();
item = new_env_item();

if (split) {
usa_split_and_push (item, value, before);
char *split_value = NULL;
size_t split_value_len = 0;
argz_create_sep (value, item->sep, &split_value, &split_value_len);
char *entry = 0;
while((entry = argz_next (split_value, split_value_len, entry))) {
if ((!strlen(entry)) || find_env_item(item, entry))
continue;
if (before) {
argz_insert (&item->argz, &item->argz_len, item->argz, entry);
} else {
argz_add(&item->argz, &item->argz_len, entry);
}
}
free(split_value);
} else {
if (before) {
usa_push (item, value);
argz_insert (&item->argz, &item->argz_len, item->argz, value);
} else {
usa_push_back (item, value);
argz_add (&item->argz, &item->argz_len, value);
}
}
}
Expand Down Expand Up @@ -379,7 +435,7 @@ void flux_conf_environment_no_dedup_push_back (flux_conf_t cf,
void flux_conf_environment_from_env (flux_conf_t cf,
const char *key,
const char *default_base,
const char *separator)
char separator)
{
const char *env = getenv (key);
if (!env)
Expand All @@ -389,19 +445,21 @@ void flux_conf_environment_from_env (flux_conf_t cf,

void flux_conf_environment_set_separator (flux_conf_t cf,
const char *key,
const char *separator)
char separator)
{
usa_set_separator (zhash_lookup (cf->environment, key), separator);
struct env_item *item = zhash_lookup (cf->environment, key);
if (item)
item->sep = separator;
}

const char *flux_conf_environment_first (flux_conf_t cf)
{
return usa_get_joined (zhash_first (cf->environment));
return stringify_env_item (zhash_first (cf->environment));
}

const char *flux_conf_environment_next (flux_conf_t cf)
{
return usa_get_joined (zhash_next (cf->environment));
return stringify_env_item (zhash_next (cf->environment));
}

const char *flux_conf_environment_cursor (flux_conf_t cf)
Expand All @@ -412,11 +470,11 @@ const char *flux_conf_environment_cursor (flux_conf_t cf)
void flux_conf_environment_apply (flux_conf_t cf)
{
const char *key, *value;
unique_string_array_t *item;
struct env_item *item;
FOREACH_ZHASH (cf->environment, key, item)
{
value = usa_get_joined (item);
if (sdslen ((sds)value)) {
value = stringify_env_item (item);
if (value) {
if (setenv (key, value, 1) < 0)
err_exit ("setenv: %s=%s", key, value);
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/common/libflux/conf.h
Expand Up @@ -145,7 +145,7 @@ void flux_conf_environment_no_dedup_push_back (flux_conf_t cf,
void flux_conf_environment_set (flux_conf_t cf,
const char *key,
const char *value,
const char *separator);
char separator);
/**
* @brief Explicitly unset the environment variable "key," this will cause the
* variable to be cleared when this environment spec is applied.
Expand All @@ -167,7 +167,7 @@ void flux_conf_environment_unset (flux_conf_t cf, const char *key);
void flux_conf_environment_from_env (flux_conf_t cf,
const char *key,
const char *default_base,
const char *separator);
char separator);
/**
* @brief Set the separator for a given environment variable.
*
Expand All @@ -178,7 +178,7 @@ void flux_conf_environment_from_env (flux_conf_t cf,
*/
void flux_conf_environment_set_separator (flux_conf_t cf,
const char *key,
const char *separator);
char separator);

/**
* @brief Get the value of a given environment variable by name.
Expand Down
4 changes: 0 additions & 4 deletions src/common/libutil/Makefile.am
Expand Up @@ -51,10 +51,6 @@ libutil_la_SOURCES = \
sds.c \
sds.h \
sdsalloc.h \
vec.c \
vec.h \
usa.c \
usa.h \
iterators.h \
macros.h \
sha1.h \
Expand Down

0 comments on commit 61a8145

Please sign in to comment.