int xbps_init(struct xbps_handle *xhp) { struct utsname un; char cwd[PATH_MAX-1], *buf; const char *repodir, *native_arch; int rv; assert(xhp != NULL); /* get cwd */ if (getcwd(cwd, sizeof(cwd)) == NULL) return ENOTSUP; /* set conffile */ if (xhp->conffile[0] == '\0') { snprintf(xhp->conffile, sizeof(xhp->conffile), XBPS_CONF_DEF); } else { buf = strdup(xhp->conffile); snprintf(xhp->conffile, sizeof(xhp->conffile), "%s/%s", cwd, buf); free(buf); } /* Set rootdir */ if (xhp->rootdir[0] == '\0') { xhp->rootdir[0] = '/'; xhp->rootdir[1] = '\0'; } else if (xhp->rootdir[0] != '/') { buf = strdup(xhp->rootdir); snprintf(xhp->rootdir, sizeof(xhp->rootdir), "%s/%s", cwd, buf); free(buf); } /* parse configuration file */ xbps_dbg_printf(xhp, "%s\n", XBPS_RELVER); if ((rv = parse_file(xhp, cwd, xhp->conffile, false, false)) != 0) { xbps_dbg_printf(xhp, "Using built-in defaults\n"); } /* Set cachedir */ if (xhp->cachedir[0] == '\0') { snprintf(xhp->cachedir, sizeof(xhp->cachedir), "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", XBPS_CACHE_PATH); } else if (xhp->cachedir[0] != '/') { /* relative path */ buf = strdup(xhp->cachedir); snprintf(xhp->cachedir, sizeof(xhp->cachedir), "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); free(buf); } /* Set metadir */ if (xhp->metadir[0] == '\0') { snprintf(xhp->metadir, sizeof(xhp->metadir), "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", XBPS_META_PATH); } else if (xhp->metadir[0] != '/') { /* relative path */ buf = strdup(xhp->metadir); snprintf(xhp->metadir, sizeof(xhp->metadir), "%s/%s", strcmp(xhp->rootdir, "/") ? xhp->rootdir : "", buf); free(buf); } /* process virtualpkg.d dirs */ if ((rv = parse_dir(xhp, cwd, XBPS_SYS_VPKG_PATH, XBPS_VPKG_PATH, true)) != 0) return rv; /* process repo.d dirs */ if ((rv = parse_dir(xhp, cwd, XBPS_SYS_REPOD_PATH, XBPS_REPOD_PATH, false)) != 0) return rv; /* process preserve.d dirs */ if ((rv = parse_dir(xhp, cwd, XBPS_SYS_PRESERVED_PATH, XBPS_PRESERVED_PATH, false)) != 0) return rv; xhp->target_arch = getenv("XBPS_TARGET_ARCH"); if ((native_arch = getenv("XBPS_ARCH")) != NULL) { strlcpy(xhp->native_arch, native_arch, sizeof(xhp->native_arch)); } else { uname(&un); strlcpy(xhp->native_arch, un.machine, sizeof(xhp->native_arch)); } assert(xhp->native_arch); xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN, XBPS_FETCH_CACHECONN_HOST); 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, "syslog=%s\n", xhp->flags & XBPS_FLAG_DISABLE_SYSLOG ? "false" : "true"); xbps_dbg_printf(xhp, "Architecture: %s\n", xhp->native_arch); xbps_dbg_printf(xhp, "Target Architecture: %s\n", xhp->target_arch); if (xhp->flags & XBPS_FLAG_DEBUG) { for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) { xbps_array_get_cstring_nocopy(xhp->repositories, i, &repodir); xbps_dbg_printf(xhp, "Repository[%u]=%s\n", i, repodir); } } /* Going back to old working directory */ if (chdir(cwd) == -1) xbps_dbg_printf(xhp, "%s: cannot chdir to %s: %s\n", __func__, cwd, strerror(errno)); return 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; }