Exemple #1
0
/**
 * hif_db_remove_all:
 * @db: a #HifDb instance.
 * @package: A package to use as a reference
 * @error: A #GError, or %NULL
 *
 * Removes a all data value from the yumdb 'database' for a given package.
 *
 * Returns: %TRUE for success, %FALSE otherwise
 *
 * Since: 0.1.0
 **/
gboolean
hif_db_remove_all(HifDb *db, HifPackage *package, GError **error)
{
    HifDbPrivate *priv = GET_PRIVATE(db);
    const gchar *filename;
    g_autoptr(GDir) dir = NULL;
    g_autofree gchar *index_dir = NULL;
    g_autoptr(GFile) file_directory = NULL;

    g_return_val_if_fail(HIF_IS_DB(db), FALSE);
    g_return_val_if_fail(package != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

    if (!priv->enabled)
        return TRUE;

    /* get the folder */
    index_dir = hif_db_get_dir_for_package(db, package);
    if (index_dir == NULL) {
        g_set_error(error,
                    HIF_ERROR,
                    HIF_ERROR_FAILED,
                    "cannot create index for %s",
                    hif_package_get_package_id(package));
        return FALSE;
    }
    if (!g_file_test(index_dir, G_FILE_TEST_IS_DIR)) {
        g_debug("Nothing to delete in %s", index_dir);
        return TRUE;
    }

    /* open */
    dir = g_dir_open(index_dir, 0, error);
    if (dir == NULL)
        return FALSE;

    /* delete each one */
    filename = g_dir_read_name(dir);
    while (filename != NULL) {
        g_autofree gchar *index_file = NULL;
        g_autoptr(GFile) file_tmp = NULL;

        index_file = g_build_filename(index_dir, filename, NULL);
        file_tmp = g_file_new_for_path(index_file);

        /* delete, ignoring error */
        g_debug("deleting %s from %s", filename, index_dir);
        if (!g_file_delete(file_tmp, NULL, NULL))
            g_debug("failed to delete %s", filename);
        filename = g_dir_read_name(dir);
    }

    /* now delete the directory */
    file_directory = g_file_new_for_path(index_dir);
    return g_file_delete(file_directory, NULL, error);
}
Exemple #2
0
/**
 * hif_package_get_cost:
 * @pkg: a #HifPackage *instance.
 *
 * Returns the cost of the repo that provided the package.
 *
 * Returns: the cost, where higher is more expensive, default 1000
 *
 * Since: 0.1.0
 **/
guint
hif_package_get_cost(HifPackage *pkg)
{
    HifPackagePrivate *priv;
    priv = hif_package_get_priv(pkg);
    if (priv->repo == NULL) {
        g_warning("no repo for %s", hif_package_get_package_id(pkg));
        return G_MAXUINT;
    }
    return hif_repo_get_cost(priv->repo);
}
Exemple #3
0
/**
 * hif_db_get_string:
 * @db: a #HifDb instance.
 * @package: A package to use as a reference
 * @key: A key name to retrieve, e.g. "releasever"
 * @error: A #GError, or %NULL
 *
 * Gets a string value from the yumdb 'database'.
 *
 * Returns: An allocated value, or %NULL
 *
 * Since: 0.1.0
 **/
gchar *
hif_db_get_string(HifDb *db, HifPackage *package, const gchar *key, GError **error)
{
    gchar *value = NULL;
    g_autofree gchar *filename = NULL;
    g_autofree gchar *index_dir = NULL;

    g_return_val_if_fail(HIF_IS_DB(db), NULL);
    g_return_val_if_fail(package != NULL, NULL);
    g_return_val_if_fail(key != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);

    /* get file contents */
    index_dir = hif_db_get_dir_for_package(db, package);
    if (index_dir == NULL) {
        g_set_error(error,
                    HIF_ERROR,
                    HIF_ERROR_FAILED,
                    "cannot read index for %s",
                    hif_package_get_package_id(package));
        return NULL;
    }

    filename = g_build_filename(index_dir, key, NULL);

    /* check it exists */
    if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
        g_set_error(error,
                    HIF_ERROR,
                    HIF_ERROR_FAILED,
                    "%s key not found",
                    filename);
        return NULL;
    }

    /* get value */
    if (!g_file_get_contents(filename, &value, NULL, error))
        return NULL;
    return value;
}
Exemple #4
0
/**
 * hif_db_ensure_origin_pkg:
 * @db: a #HifDb instance.
 * @pkg: A package to set
 *
 * Sets the repo origin on a package if not already set.
 *
 * Since: 0.1.0
 */
void
hif_db_ensure_origin_pkg(HifDb *db, HifPackage *pkg)
{
    g_autoptr(GError) error = NULL;
    g_autofree gchar *tmp = NULL;

    /* already set */
    if (hif_package_get_origin(pkg) != NULL)
        return;
    if (!hif_package_installed(pkg))
        return;

    /* set from the database if available */
    tmp = hif_db_get_string(db, pkg, "from_repo", &error);
    if (tmp == NULL) {
        g_debug("no origin for %s: %s",
             hif_package_get_package_id(pkg),
             error->message);
    } else {
        hif_package_set_origin(pkg, tmp);
    }
}
Exemple #5
0
/**
 * hif_db_set_string:
 * @db: a #HifDb instance.
 * @package: A package to use as a reference
 * @key: Key name to save, e.g. "reason"
 * @value: Key data to save, e.g. "dep"
 * @error: A #GError, or %NULL
 *
 * Writes a data value to the yumdb 'database'.
 *
 * Returns: Returns: %TRUE for success, %FALSE otherwise
 *
 * Since: 0.1.0
 **/
gboolean
hif_db_set_string(HifDb *db,
           HifPackage *package,
           const gchar *key,
           const gchar *value,
           GError **error)
{
    HifDbPrivate *priv = GET_PRIVATE(db);
    g_autofree gchar *index_dir = NULL;
    g_autofree gchar *index_file = NULL;

    g_return_val_if_fail(HIF_IS_DB(db), FALSE);
    g_return_val_if_fail(package != NULL, FALSE);
    g_return_val_if_fail(key != NULL, FALSE);
    g_return_val_if_fail(value != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

    if (!priv->enabled)
        return TRUE;

    /* create the index directory */
    index_dir = hif_db_get_dir_for_package(db, package);
    if (index_dir == NULL) {
        g_set_error(error,
                    HIF_ERROR,
                    HIF_ERROR_FAILED,
                    "cannot create index for %s",
                    hif_package_get_package_id(package));
        return FALSE;
    }
    if (!hif_db_create_dir(index_dir, error))
        return FALSE;

    /* write the value */
    index_file = g_build_filename(index_dir, key, NULL);
    g_debug("writing %s to %s", value, index_file);
    return g_file_set_contents(index_file, value, -1, error);
}
Exemple #6
0
/**
 * hif_db_remove:
 * @db: a #HifDb instance.
 * @package: A package to use as a reference
 * @key: Key name to delete, e.g. "reason"
 * @error: A #GError, or %NULL
 *
 * Removes a data value from the yumdb 'database' for a given package.
 *
 * Returns: %TRUE for success, %FALSE otherwise
 *
 * Since: 0.1.0
 **/
gboolean
hif_db_remove(HifDb *db,
           HifPackage *package,
           const gchar *key,
           GError **error)
{
    HifDbPrivate *priv = GET_PRIVATE(db);
    g_autofree gchar *index_dir = NULL;
    g_autofree gchar *index_file = NULL;
    g_autoptr(GFile) file = NULL;

    g_return_val_if_fail(HIF_IS_DB(db), FALSE);
    g_return_val_if_fail(package != NULL, FALSE);
    g_return_val_if_fail(key != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

    if (!priv->enabled)
        return TRUE;

    /* create the index directory */
    index_dir = hif_db_get_dir_for_package(db, package);
    if (index_dir == NULL) {
        g_set_error(error,
                    HIF_ERROR,
                    HIF_ERROR_FAILED,
                    "cannot create index for %s",
                    hif_package_get_package_id(package));
        return FALSE;
    }

    /* delete the value */
    g_debug("deleting %s from %s", key, index_dir);
    index_file = g_build_filename(index_dir, key, NULL);
    file = g_file_new_for_path(index_file);
    return g_file_delete(file, NULL, error);
}