From 2f57b404b1c1c376316ca0c6bb5e789e2066298f Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Tue, 23 Mar 2021 16:53:03 +0100 Subject: [PATCH] ucm: add Path condition type Check for a file presence and mode. Modes: exists, read, write, exec Example: If.0 { Condition { Type Path Mode read Path "/etc/alsa/something" True { ... } } } Signed-off-by: Jaroslav Kysela --- src/ucm/ucm_cond.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/ucm/ucm_cond.c b/src/ucm/ucm_cond.c index cf6da6fd..59d1a155 100644 --- a/src/ucm/ucm_cond.c +++ b/src/ucm/ucm_cond.c @@ -269,6 +269,47 @@ static int if_eval_control_exists(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval return 1; } +static int if_eval_path(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval) +{ + const char *path, *mode = NULL; + int err, amode = F_OK; + + if (uc_mgr->conf_format < 4) { + uc_error("Path condition is supported in v4+ syntax"); + return -EINVAL; + } + + err = get_string(eval, "Path", &path); + if (err < 0) { + uc_error("Path error (If.Condition.Path)"); + return -EINVAL; + } + + err = get_string(eval, "Mode", &mode); + if (err < 0 && err != -ENOENT) { + uc_error("Path error (If.Condition.Mode)"); + return -EINVAL; + } + + if (strncasecmp(mode, "exist", 5) == 0) { + amode = F_OK; + } else if (strcasecmp(mode, "read") == 0) { + amode = R_OK; + } else if (strcasecmp(mode, "write") == 0) { + amode = W_OK; + } else if (strcasecmp(mode, "exec") == 0) { + amode = X_OK; + } else { + uc_error("Path unknown mode (If.Condition.Mode)"); + return -EINVAL; + } + + if (eaccess(path, amode)) + return 0; + + return 1; +} + static int if_eval(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval) { const char *type; @@ -297,6 +338,9 @@ static int if_eval(snd_use_case_mgr_t *uc_mgr, snd_config_t *eval) if (strcmp(type, "RegexMatch") == 0) return if_eval_regex_match(uc_mgr, eval); + if (strcmp(type, "Path") == 0) + return if_eval_path(uc_mgr, eval); + uc_error("unknown If.Condition.Type"); return -EINVAL; } -- 2.47.1