Esempio n. 1
0
/*
 * Returns -1 on error, 0 if transfer was not necessary (local/remote
 * size and/or mtime match) and 1 if downloaded successfully.
 */
int HIDDEN
xbps_repo_sync(struct xbps_handle *xhp, const char *uri)
{
	const char *arch, *fetchstr = NULL;
	char *repodata, *lrepodir, *uri_fixedp;
	int rv = 0;

	assert(uri != NULL);

	/* ignore non remote repositories */
	if (!xbps_repository_is_remote(uri))
		return 0;

	uri_fixedp = xbps_get_remote_repo_string(uri);
	if (uri_fixedp == NULL)
		return -1;

	if (xhp->target_arch)
		arch = xhp->target_arch;
	else
		arch = xhp->native_arch;

	/*
	 * Full path to repository directory to store the plist
	 * index file.
	 */
	lrepodir = xbps_xasprintf("%s/%s", xhp->metadir, uri_fixedp);
	free(uri_fixedp);
	/*
	 * Create repodir in metadir.
	 */
	if ((rv = xbps_mkpath(lrepodir, 0755)) == -1) {
		if (errno != EEXIST) {
			xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
			    errno, NULL, "[reposync] failed "
			    "to create repodir `%s': %s", lrepodir,
			strerror(errno));
			free(lrepodir);
			return rv;
		}
	}
	if (chdir(lrepodir) == -1) {
		xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL, errno, NULL,
		    "[reposync] failed to change dir to repodir `%s': %s",
		    lrepodir, strerror(errno));
		free(lrepodir);
		return -1;
	}
	free(lrepodir);
	/*
	 * Remote repository plist index full URL.
	 */
	repodata = xbps_xasprintf("%s/%s-repodata", uri, arch);

	/* reposync start cb */
	xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC, 0, repodata, NULL);
	/*
	 * Download plist index file from repository.
	 */
	if ((rv = xbps_fetch_file(xhp, repodata, NULL)) == -1) {
		/* reposync error cb */
		fetchstr = xbps_fetch_error_string();
		xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
		    fetchLastErrCode != 0 ? fetchLastErrCode : errno, NULL,
		    "[reposync] failed to fetch file `%s': %s",
		    repodata, fetchstr ? fetchstr : strerror(errno));
	} else if (rv == 1)
		rv = 0;

	free(repodata);

	return rv;
}
Esempio n. 2
0
static int
download_binpkgs(struct xbps_handle *xhp, prop_object_iterator_t iter)
{
    prop_object_t obj;
    const char *pkgver, *repoloc, *filen, *trans;
    const char *pkgname, *version, *fetchstr;
    char *binfile;
    int rv = 0;

    while ((obj = prop_object_iterator_next(iter)) != NULL) {
        prop_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
        if ((strcmp(trans, "remove") == 0) ||
                (strcmp(trans, "configure") == 0))
            continue;

        prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
        prop_dictionary_get_cstring_nocopy(obj, "version", &version);
        prop_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
        assert(repoloc != NULL);
        prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
        assert(pkgver != NULL);
        prop_dictionary_get_cstring_nocopy(obj, "filename", &filen);
        assert(filen != NULL);

        binfile = xbps_path_from_repository_uri(xhp, obj, repoloc);
        if (binfile == NULL) {
            rv = EINVAL;
            break;
        }
        /*
         * If downloaded package is in cachedir continue.
         */
        if (access(binfile, R_OK) == 0) {
            free(binfile);
            continue;
        }
        /*
         * Create cachedir.
         */
        if (xbps_mkpath(xhp->cachedir, 0755) == -1) {
            xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL,
                              errno, pkgname, version,
                              "%s: [trans] cannot create cachedir `%s': %s",
                              pkgver, xhp->cachedir, strerror(errno));
            free(binfile);
            rv = errno;
            break;
        }
        xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD,
                          0, pkgname, version,
                          "Downloading binary package `%s' (from `%s')...",
                          filen, repoloc);
        /*
         * Fetch binary package.
         */
        rv = xbps_fetch_file(xhp, binfile, xhp->cachedir, false, NULL);
        if (rv == -1) {
            fetchstr = xbps_fetch_error_string();
            xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL,
                              fetchLastErrCode != 0 ? fetchLastErrCode : errno,
                              pkgname, version,
                              "%s: [trans] failed to download binary package "
                              "`%s' from `%s': %s", pkgver, filen, repoloc,
                              fetchstr ? fetchstr : strerror(errno));
            free(binfile);
            break;
        }
        rv = 0;
        free(binfile);
    }
    prop_object_iterator_reset(iter);

    return rv;
}