Пример #1
0
void validate_test(void)
{
	char *buf;
	unsigned int i;

	buf = "action = wlak";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);

	buf = "action = walk";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);

	buf = "action = run" " options { speed = 6 }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);

	buf = "action = jump" " options { speed = 2 name = 'Joe' }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);

	buf = "action = crawl" " options { speed = -2 name = 'Smith' }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);

	buf = "ip-address = { 0.0.0.0 , 1.2.3.4 , 192.168.0.254 , 10.0.0.255 , 20.30.40.256}";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
	buf = "ip-address = { 0.0.0.0 , 1.2.3.4 , 192.168.0.254 , 10.0.0.255 , 20.30.40.250}";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
	buf = "ip-address = { 1.2.3. }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);

	buf = "action = run" " multi_options { speeds = {1, 2, 3, 4, 5} }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
	for (i = 0; i < cfg_size(cfg, "multi_options"); i++) {
		cfg_t *multisec = cfg_getnsec(cfg, "multi_options", i);
		cfg_opt_t *speeds_opt = cfg_getopt(multisec, "speeds");

		fail_unless(speeds_opt != 0);
		fail_unless(speeds_opt->validcb == validate_speed);
	}

	buf = "action = run" " multi_options { speeds = {1, 2, 3, -4, 5} }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);

	buf = "action = run" " multi_options { speeds = {1, 2, 3, 4, 0} }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);

	buf = "action = run" " multi_options { options { xspeed = 3 } }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);

	buf = "action = run" " multi_options { options { xspeed = -3 } }";
	fail_unless(cfg_parse_buf(cfg, buf) == CFG_PARSE_ERROR);
}
Пример #2
0
int
xbps_init(struct xbps_handle *xhp)
{
    cfg_opt_t vpkg_opts[] = {
        CFG_STR_LIST(__UNCONST("targets"), NULL, CFGF_NONE),
        CFG_END()
    };
    cfg_opt_t opts[] = {
        /* Defaults if not set in configuration file */
        CFG_STR(__UNCONST("rootdir"), __UNCONST("/"), CFGF_NONE),
        CFG_STR(__UNCONST("cachedir"),
        __UNCONST(XBPS_CACHE_PATH), CFGF_NONE),
        CFG_INT(__UNCONST("FetchCacheConnections"),
        XBPS_FETCH_CACHECONN, CFGF_NONE),
        CFG_INT(__UNCONST("FetchCacheConnectionsPerHost"),
        XBPS_FETCH_CACHECONN_HOST, CFGF_NONE),
        CFG_INT(__UNCONST("FetchTimeoutConnection"),
        XBPS_FETCH_TIMEOUT, CFGF_NONE),
        CFG_INT(__UNCONST("TransactionFrequencyFlush"),
        XBPS_TRANS_FLUSH, CFGF_NONE),
        CFG_BOOL(__UNCONST("syslog"), true, CFGF_NONE),
        CFG_STR_LIST(__UNCONST("repositories"), NULL, CFGF_MULTI),
        CFG_STR_LIST(__UNCONST("PackagesOnHold"), NULL, CFGF_MULTI),
        CFG_SEC(__UNCONST("virtual-package"),
        vpkg_opts, CFGF_MULTI|CFGF_TITLE),
        CFG_FUNC(__UNCONST("include"), &cfg_include),
        CFG_END()
    };
    struct utsname un;
    int rv, cc, cch;
    bool syslog_enabled = false;

    assert(xhp != NULL);

    if (xhp->initialized)
        return 0;

    if (xhp->conffile == NULL)
        xhp->conffile = XBPS_CONF_DEF;

    /* parse configuration file */
    xhp->cfg = cfg_init(opts, CFGF_NOCASE);
    cfg_set_validate_func(xhp->cfg, "virtual-package", &cb_validate_virtual);

    if ((rv = cfg_parse(xhp->cfg, xhp->conffile)) != CFG_SUCCESS) {
        if (rv == CFG_FILE_ERROR) {
            /*
             * Don't error out if config file not found.
             * If a default repository is set, use it; otherwise
             * use defaults (no repos and no virtual packages).
             */
            if (errno != ENOENT)
                return rv;

            xhp->conffile = NULL;
            if (xhp->repository) {
                char *buf;

                buf = xbps_xasprintf("repositories = { %s }",
                                     xhp->repository);
                assert(buf);
                if ((rv = cfg_parse_buf(xhp->cfg, buf)) != 0)
                    return rv;
                free(buf);
            }
        } else if (rv == CFG_PARSE_ERROR) {
            /*
             * Parser error from configuration file.
             */
            return ENOTSUP;
        }
    }
    xbps_dbg_printf(xhp, "Configuration file: %s\n",
                    xhp->conffile ? xhp->conffile : "not found");
    /*
     * Respect client setting in struct xbps_handle for {root,cache}dir;
     * otherwise use values from configuration file or defaults if unset.
     */
    if (xhp->rootdir == NULL) {
        if (xhp->cfg == NULL)
            xhp->rootdir = "/";
        else
            xhp->rootdir = cfg_getstr(xhp->cfg, "rootdir");
    }
    if (xhp->cachedir == NULL) {
        if (xhp->cfg == NULL)
            xhp->cachedir = XBPS_CACHE_PATH;
        else
            xhp->cachedir = cfg_getstr(xhp->cfg, "cachedir");
    }
    if ((xhp->cachedir_priv = set_cachedir(xhp)) == NULL)
        return ENOMEM;
    xhp->cachedir = xhp->cachedir_priv;

    if ((xhp->metadir_priv = set_metadir(xhp)) == NULL)
        return ENOMEM;
    xhp->metadir = xhp->metadir_priv;

    uname(&un);
    xhp->un_machine = strdup(un.machine);
    assert(xhp->un_machine);

    if (xhp->cfg == NULL) {
        xhp->flags |= XBPS_FLAG_SYSLOG;
        xhp->fetch_timeout = XBPS_FETCH_TIMEOUT;
        xhp->transaction_frequency_flush = XBPS_TRANS_FLUSH;
        cc = XBPS_FETCH_CACHECONN;
        cch = XBPS_FETCH_CACHECONN_HOST;
    } else {
        if (cfg_getbool(xhp->cfg, "syslog"))
            xhp->flags |= XBPS_FLAG_SYSLOG;
        xhp->fetch_timeout = cfg_getint(xhp->cfg, "FetchTimeoutConnection");
        cc = cfg_getint(xhp->cfg, "FetchCacheConnections");
        cch = cfg_getint(xhp->cfg, "FetchCacheConnectionsPerHost");
        xhp->transaction_frequency_flush =
            cfg_getint(xhp->cfg, "TransactionFrequencyFlush");
    }
    if (xhp->flags & XBPS_FLAG_SYSLOG)
        syslog_enabled = true;

    xbps_fetch_set_cache_connection(cc, cch);

    xbps_dbg_printf(xhp, "Rootdir=%s\n", xhp->rootdir);
    xbps_dbg_printf(xhp, "Metadir=%s\n", xhp->metadir);
    xbps_dbg_printf(xhp, "Cachedir=%s\n", xhp->cachedir);
    xbps_dbg_printf(xhp, "FetchTimeout=%u\n", xhp->fetch_timeout);
    xbps_dbg_printf(xhp, "FetchCacheconn=%u\n", cc);
    xbps_dbg_printf(xhp, "FetchCacheconnHost=%u\n", cch);
    xbps_dbg_printf(xhp, "Syslog=%u\n", syslog_enabled);
    xbps_dbg_printf(xhp, "TransactionFrequencyFlush=%u\n",
                    xhp->transaction_frequency_flush);
    xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->un_machine);

    xhp->initialized = true;

    return 0;
}