Skip to content

Commit

Permalink
ucm: fix Path condition - substitute Path and Mode fields
Browse files Browse the repository at this point in the history
The Path and Mode fields should be also substituted for
the runtime evaluation. See Fixes.

Fixes: #395
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
  • Loading branch information
perexg committed Apr 24, 2024
1 parent de52941 commit ef6463a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/ucm/ucm_cond.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ static int if_eval_path(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
{
const char *path, *mode = "";
int err, amode = F_OK;
char *s;

if (uc_mgr->conf_format < 4) {
uc_error("Path condition is supported in v4+ syntax");
Expand All @@ -292,27 +293,34 @@ static int if_eval_path(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
return -EINVAL;
}

if (strncasecmp(mode, "exist", 5) == 0) {
err = uc_mgr_get_substituted_value(uc_mgr, &s, mode);
if (err < 0)
return err;
if (strncasecmp(s, "exist", 5) == 0) {
amode = F_OK;
} else if (strcasecmp(mode, "read") == 0) {
} else if (strcasecmp(s, "read") == 0) {
amode = R_OK;
} else if (strcasecmp(mode, "write") == 0) {
} else if (strcasecmp(s, "write") == 0) {
amode = W_OK;
} else if (strcasecmp(mode, "exec") == 0) {
} else if (strcasecmp(s, "exec") == 0) {
amode = X_OK;
} else {
uc_error("Path unknown mode (If.Condition.Mode)");
uc_error("Path unknown mode '%s' (If.Condition.Mode)", s);
free(s);
return -EINVAL;
}
free(s);

err = uc_mgr_get_substituted_value(uc_mgr, &s, path);
if (err < 0)
return err;
#ifdef HAVE_EACCESS
if (eaccess(path, amode))
err = eaccess(path, amode);
#else
if (access(path, amode))
err = access(path, amode);
#endif
return 0;

return 1;
free(s);
return err ? 0 : 1;
}

static int if_eval(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval)
Expand Down

0 comments on commit ef6463a

Please sign in to comment.