Example #1
0
/**
 * dnf_package_array_download:
 * @packages: an array of packages.
 * @directory: destination directory, or %NULL for the cachedir.
 * @state: the #DnfState.
 * @error: a #GError or %NULL..
 *
 * Downloads an array of packages.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.1.0
 */
gboolean
dnf_package_array_download(GPtrArray *packages,
                const gchar *directory,
                DnfState *state,
                GError **error)
{
    DnfState *state_local;
    GHashTableIter hiter;
    gpointer key, value;
    guint i;
    g_autoptr(GHashTable) repo_to_packages = NULL;

    /* map packages to repos */
    repo_to_packages = g_hash_table_new_full(NULL, NULL, NULL, (GDestroyNotify)g_ptr_array_unref);
    for (i = 0; i < packages->len; i++) {
        DnfPackage *pkg = g_ptr_array_index(packages, i);
        DnfRepo *repo;
        GPtrArray *repo_packages;

        repo = dnf_package_get_repo(pkg);
        if (repo == NULL) {
            g_set_error_literal(error,
                                DNF_ERROR,
                                DNF_ERROR_INTERNAL_ERROR,
                                "package repo is unset");
            return FALSE;
        }
        repo_packages = g_hash_table_lookup(repo_to_packages, repo);
        if (repo_packages == NULL) {
            repo_packages = g_ptr_array_new();
            g_hash_table_insert(repo_to_packages, repo, repo_packages);
        }
        g_ptr_array_add(repo_packages, pkg);
    }

    /* set steps according to the number of repos we are going to download from */
    dnf_state_set_number_steps(state, g_hash_table_size(repo_to_packages));

    /* download all packages from each repo in one go */
    g_hash_table_iter_init(&hiter, repo_to_packages);
    while (g_hash_table_iter_next(&hiter, &key, &value)) {
        DnfRepo *repo = key;
        GPtrArray *repo_packages = value;

        state_local = dnf_state_get_child(state);
        if (!dnf_repo_download_packages(repo, repo_packages, directory, state_local, error))
            return FALSE;

        /* done */
        if (!dnf_state_done(state, error))
            return FALSE;
    }
    return TRUE;
}
Example #2
0
/**
 * dnf_package_download:
 * @pkg: a #DnfPackage *instance.
 * @directory: destination directory, or %NULL for the cachedir.
 * @state: the #DnfState.
 * @error: a #GError or %NULL..
 *
 * Downloads the package.
 *
 * Returns: the complete filename of the downloaded file
 *
 * Since: 0.1.0
 **/
gchar *
dnf_package_download(DnfPackage *pkg,
              const gchar *directory,
              DnfState *state,
              GError **error)
{
    DnfRepo *repo;
    repo = dnf_package_get_repo(pkg);
    if (repo == NULL) {
        g_set_error_literal(error,
                     DNF_ERROR,
                     DNF_ERROR_INTERNAL_ERROR,
                     "package repo is unset");
        return NULL;
    }
    return dnf_repo_download_package(repo, pkg, directory, state, error);
}
Example #3
0
/**
 * dnf_emit_package_list_filter:
 */
void
dnf_emit_package_list_filter (PkBackendJob *job,
			      PkBitfield filters,
			      GPtrArray *pkglist)
{
	DnfPackage *found;
	DnfPackage *pkg;
	guint i;
	g_autoptr(GHashTable) hash_cost = NULL;
	g_autoptr(GHashTable) hash_installed = NULL;

	/* if a package exists in multiple repos, show the one with the lowest
	 * cost of downloading */
	hash_cost = g_hash_table_new (g_str_hash, g_str_equal);
	for (i = 0; i < pkglist->len; i++) {
		pkg = g_ptr_array_index (pkglist, i);
		if (dnf_package_installed (pkg))
			continue;

		/* if the NEVRA does not already exist in the array, just add */
		found = g_hash_table_lookup (hash_cost,
					     dnf_package_get_nevra (pkg));
		if (found == NULL) {
			g_hash_table_insert (hash_cost,
					     (gpointer) dnf_package_get_nevra (pkg),
					     (gpointer) pkg);
			continue;
		}

		/* a lower cost package */
		if (dnf_package_get_cost (pkg) < dnf_package_get_cost (found)) {
			dnf_package_set_info (found, PK_INFO_ENUM_BLOCKED);
			g_hash_table_replace (hash_cost,
					      (gpointer) dnf_package_get_nevra (pkg),
					      (gpointer) pkg);
		} else {
			dnf_package_set_info (pkg, PK_INFO_ENUM_BLOCKED);
		}
	}

	/* add all the installed packages to a hash */
	hash_installed = g_hash_table_new (g_str_hash, g_str_equal);
	for (i = 0; i < pkglist->len; i++) {
		pkg = g_ptr_array_index (pkglist, i);
		if (!dnf_package_installed (pkg))
			continue;
		g_hash_table_insert (hash_installed,
				     (gpointer) dnf_package_get_nevra (pkg),
				     (gpointer) pkg);
	}

	/* anything remote in metadata-only mode needs to be unavailable */
	for (i = 0; i < pkglist->len; i++) {
		DnfRepo *src;
		pkg = g_ptr_array_index (pkglist, i);
		if (dnf_package_installed (pkg))
			continue;
		src = dnf_package_get_repo (pkg);
		if (src == NULL)
			continue;
		if (dnf_repo_get_enabled (src) != DNF_REPO_ENABLED_METADATA)
			continue;
		dnf_package_set_info (pkg, PK_INFO_ENUM_UNAVAILABLE);
	}

	for (i = 0; i < pkglist->len; i++) {
		pkg = g_ptr_array_index (pkglist, i);

		/* blocked */
		if ((PkInfoEnum) dnf_package_get_info (pkg) == PK_INFO_ENUM_BLOCKED)
			continue;

		/* GUI */
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_GUI) && !dnf_package_is_gui (pkg))
			continue;
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_GUI) && dnf_package_is_gui (pkg))
			continue;

		/* DEVELOPMENT */
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_DEVELOPMENT) && !dnf_package_is_devel (pkg))
			continue;
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_DEVELOPMENT) && dnf_package_is_devel (pkg))
			continue;

		/* DOWNLOADED */
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_DOWNLOADED) && !dnf_package_is_downloaded (pkg))
			continue;
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_DOWNLOADED) && dnf_package_is_downloaded (pkg))
			continue;

		/* if this package is available and the very same NEVRA is
		 * installed, skip this package */
		if (!dnf_package_installed (pkg)) {
			found = g_hash_table_lookup (hash_installed,
						     dnf_package_get_nevra (pkg));
			if (found != NULL)
				continue;
		}

		dnf_emit_package (job, PK_INFO_ENUM_UNKNOWN, pkg);
	}
}