int cfg_validate_path(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) { LOG_TRACEME struct stat sb; if (!str_path_isabs(value)) { cfg_error(cfg, "Invalid absolute path for %s = '%s'", opt->name, value); return -1; } if (lstat(value, &sb) == -1) { cfg_error(cfg, "File does not exist for %s = '%s'", opt->name, value); return -1; } *(const char **) result = (const char *) value; return 0; }
static int str_path_isabs_t(void) { int i, res, rc = 0; char *buf; struct test { const char *str; int res; } T[] = { { "", 0 }, { " ", 0 }, { "abc", 0 }, { "/", 1 }, { "/////", 1 }, { "/a/b/c/d/e", 1 }, { "/a/b///d/e", 1 }, { "/a/../d/e", 0 }, { "/a/./d/e", 0 }, { "/a/\n/d/e", 0 }, }; int TS = sizeof(T) / sizeof(T[0]); for (i = 0; i < TS; i++) { buf = strdup(T[i].str); res = str_path_isabs(buf); free(buf); if (res != T[i].res) rc += log_error("[%s/%02d] E[%d] R[%d]", __FUNCTION__, i, T[i].res, res); } return rc; }