Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit

Permalink
fix wresolver for #160 (mingw build)
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Raab committed Sep 12, 2015
1 parent d2081af commit edb4d50
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 45 deletions.
117 changes: 78 additions & 39 deletions src/plugins/wresolver/wresolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,34 @@ typedef struct _resolverHandles resolverHandles;

struct _resolverHandles
{
resolverHandle spec;
resolverHandle dir;
resolverHandle user;
resolverHandle system;
};

static resolverHandle * elektraGetResolverHandle(Plugin *handle, Key *parentKey)
{
resolverHandles *pks = elektraPluginGetData(handle);
if (!strncmp(keyName(parentKey), "user", 4)) return &pks->user;
else return &pks->system;
switch (keyGetNamespace(parentKey))
{
case KEY_NS_SPEC:
return &pks->spec;
case KEY_NS_DIR:
return &pks->dir;
case KEY_NS_USER:
return &pks->user;
case KEY_NS_SYSTEM:
return &pks->system;
case KEY_NS_PROC:
case KEY_NS_EMPTY:
case KEY_NS_NONE:
case KEY_NS_META:
case KEY_NS_CASCADING:
break;
}
// ELEKTRA_ASSERT(0 && "namespace not valid for resolving");
return 0;
}


Expand Down Expand Up @@ -111,13 +130,18 @@ static void escapePath(char *home)
}
}

static void elektraResolveSpec(resolverHandle *p)
static void elektraResolveSpec(resolverHandle *p, Key *errorKey)
{
char * system = getenv("ALLUSERSPROFILE");

if (!system) system = "";
if (!system)
{
system = "";
ELEKTRA_ADD_WARNING(90, errorKey, "could not get ALLUSERSPROFILE for spec, using /");
}
else escapePath(system);


if (p->path[0] == '/')
{
/* Use absolute path */
Expand Down Expand Up @@ -182,13 +206,15 @@ static void elektraResolveUser(resolverHandle *p, Key *warningsKey)
}
else
{
ELEKTRA_ADD_WARNING(90, warningsKey, "could not get home");
strcpy(home, "");
ELEKTRA_ADD_WARNING(90, warningsKey, "could not get home (CSIDL_PROFILE), using /");
}
# else
char * home = (char*) getenv("HOME");
if(!home)
{
ELEKTRA_ADD_WARNING(90, warningsKey, "could not get home");
home = "";
ELEKTRA_ADD_WARNING(90, warningsKey, "could not get home, using /");
}
# endif

Expand All @@ -197,11 +223,15 @@ static void elektraResolveUser(resolverHandle *p, Key *warningsKey)
strncat (p->filename, p->path, PATH_MAX);
}

static void elektraResolveSystem(resolverHandle *p)
static void elektraResolveSystem(resolverHandle *p, Key *errorKey)
{
char * system = getenv("ALLUSERSPROFILE");

if (!system) system = "";
if (!system)
{
system = "";
ELEKTRA_ADD_WARNING(90, errorKey, "could not get ALLUSERSPROFILE, using /");
}
else escapePath(system);

if (p->path[0] == '/')
Expand All @@ -224,31 +254,6 @@ static void elektraResolveSystem(resolverHandle *p)
return;
}

void elektraWresolveFileName(elektraNamespace ns, resolverHandle *p, Key *warningsKey)
{
switch (ns)
{
case KEY_NS_SPEC:
elektraResolveSpec(p);
break;
case KEY_NS_DIR:
elektraResolveDir(p, warningsKey);
break;
case KEY_NS_USER:
elektraResolveUser(p, warningsKey);
break;
case KEY_NS_SYSTEM:
elektraResolveSystem(p);
break;
case KEY_NS_PROC:
case KEY_NS_EMPTY:
case KEY_NS_NONE:
case KEY_NS_META:
case KEY_NS_CASCADING:
break;
}
}

int elektraWresolverOpen(Plugin *handle, Key *errorKey)
{
KeySet *resolverConfig = elektraPluginGetConfig(handle);
Expand All @@ -261,12 +266,31 @@ int elektraWresolverOpen(Plugin *handle, Key *errorKey)
}

resolverHandles *p = malloc(sizeof(resolverHandles));
resolverInit (&p->user, path);
resolverInit (&p->system, path);

for (elektraNamespace i=KEY_NS_FIRST; i<=KEY_NS_LAST; ++i)
// switch is only present to forget no namespace and to get
// a warning whenever a new namespace is present.
// (also used below in close)
// In fact its linear code executed:
switch (KEY_NS_SPEC)
{
elektraWresolveFileName(i, &p->system, errorKey);
case KEY_NS_SPEC:
resolverInit (&p->spec, path);
elektraResolveSpec(&p->spec, errorKey);
case KEY_NS_DIR:
resolverInit (&p->dir, path);
elektraResolveDir(&p->dir, errorKey);
case KEY_NS_USER:
resolverInit (&p->user, path);
elektraResolveUser(&p->user, errorKey);
case KEY_NS_SYSTEM:
resolverInit (&p->system, path);
elektraResolveSystem(&p->system, errorKey);
case KEY_NS_PROC:
case KEY_NS_EMPTY:
case KEY_NS_NONE:
case KEY_NS_META:
case KEY_NS_CASCADING:
break;
}

elektraPluginSetData(handle, p);
Expand All @@ -280,8 +304,23 @@ int elektraWresolverClose(Plugin *handle, Key *errorKey ELEKTRA_UNUSED)

if (ps)
{
resolverClose(&ps->user);
resolverClose(&ps->system);
switch (KEY_NS_SPEC)
{
case KEY_NS_SPEC:
resolverClose(&ps->spec);
case KEY_NS_DIR:
resolverClose(&ps->dir);
case KEY_NS_USER:
resolverClose(&ps->user);
case KEY_NS_SYSTEM:
resolverClose(&ps->system);
case KEY_NS_PROC:
case KEY_NS_EMPTY:
case KEY_NS_NONE:
case KEY_NS_META:
case KEY_NS_CASCADING:
break;
}

free (ps);
elektraPluginSetData(handle, 0);
Expand Down
58 changes: 52 additions & 6 deletions tests/shell/check_resolver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,23 @@ check_resolver()
PLUGIN=`echo "$PLUGINS_NEWLINES" | grep -m 1 "resolver_.*_.*_$2.*"`
fi

if [ -z "$PLUGIN" ]
if [ "$2" = "w" ]
then
echo "No plugin matching $2 for namespace $1"
return
PLUGIN=wresolver
fi

[ -n "$PLUGIN" ]
exit_if_fail "No plugin matching $2 for namespace $1"

MOUNTPOINT=$1$ROOT_MOUNTPOINT

$KDB mount --resolver $PLUGIN $3 $MOUNTPOINT dump 1>/dev/null
succeed_if "could not mount root: $3 at $MOUNTPOINT with resolver $PLUGIN"
succeed_if "could not mount root using: $KDB mount --resolver $PLUGIN $3 $MOUNTPOINT dump"

FILE=`$KDB file -N $1 -n $ROOT_MOUNTPOINT 2> /dev/null`
echo "For $1 $2 $3 we got $FILE"
[ "x$FILE" = "x$4" ]
succeed_if "resolving of $MOUNTPOINT did not yield $4"
succeed_if "resolving of $MOUNTPOINT did not yield $4 but $FILE"

if [ "x$WRITE_TO_SYSTEM" = "xYES" ]; then
KEY=$ROOT_MOUNTPOINT/key
Expand Down Expand Up @@ -106,7 +108,51 @@ check_resolver system x app/config_file /etc/xdg/app/config_file
unset XDG_CONFIG_HOME
check_resolver system x app/config_file /etc/xdg/app/config_file

fi
OD=`pwd`
cd /tmp # hopefully no @KDB_DB_DIR@ is in /tmp
check_resolver dir x /a /tmp/a
check_resolver dir x /a/b /tmp/a/b
check_resolver dir x a /tmp/@KDB_DB_DIR@/a
check_resolver dir x a/b /tmp/@KDB_DB_DIR@/a/b
cd $OD

fi # end of XDG tests






if is_plugin_available wresolver
then

export ALLUSERSPROFILE="/C"
check_resolver spec w /app/config_file /C/app/config_file
check_resolver spec w app/config_file /C@CMAKE_INSTALL_PREFIX@/@KDB_DB_SPEC@/app/config_file
check_resolver system w /app/config_file /C/app/config_file
check_resolver system w app/config_file /C@KDB_DB_SYSTEM@/app/config_file
unset ALLUSERSPROFILE

export HOME="/D"
check_resolver user w /app/config_file /D//app/config_file
check_resolver user w app/config_file /D/app/config_file #@KDB_DB_USER@ not impl
unset HOME

OD=`pwd`
cd /tmp # hopefully no @KDB_DB_DIR@ is in /tmp
check_resolver dir w /a /tmp//a
check_resolver dir w /a/b /tmp//a/b
check_resolver dir w a /tmp/a #@KDB_DB_DIR@ not impl
check_resolver dir w a/b /tmp/a/b #@KDB_DB_DIR@ not impl
cd $OD

fi # end of wresolver tests







check_resolver system b x @KDB_DB_SYSTEM@/x
check_resolver system b x/a @KDB_DB_SYSTEM@/x/a
Expand Down

0 comments on commit edb4d50

Please sign in to comment.