OSDN Git Service

ucm: add Path condition type
authorJaroslav Kysela <perex@perex.cz>
Tue, 23 Mar 2021 15:53:03 +0000 (16:53 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 23 Mar 2021 16:16:14 +0000 (17:16 +0100)
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 <perex@perex.cz>
src/ucm/ucm_cond.c

index cf6da6f..59d1a15 100644 (file)
@@ -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;
 }