Skip to content

Commit

Permalink
Extract a shared internal rpmostree-hif.[ch]
Browse files Browse the repository at this point in the history
As we start to do more package things, extract common helper functions
around HifContext * that by default operates on the system root.

Some of these bits should go in libhif, but the immediate plan is to
iterate here, then push downwards later.
  • Loading branch information
cgwalters committed Apr 12, 2015
1 parent 5a9c2a0 commit 54e02cc
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 87 deletions.
2 changes: 2 additions & 0 deletions Makefile-rpm-ostree.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ librpmostree_la_SOURCES = \
src/libpriv/rpmostree-json-parsing.h \
src/libpriv/rpmostree-util.c \
src/libpriv/rpmostree-util.h \
src/libpriv/rpmostree-hif.c \
src/libpriv/rpmostree-hif.h \
src/libpriv/rpmostree-passwd-util.c \
src/libpriv/rpmostree-passwd-util.h \
src/libpriv/rpmostree-cleanup.h \
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

#include "config.h"

#include <libhif.h>
#include <libhif/hif-utils.h>

#include "rpmostree-hif.h"
#include "rpmostree-cache-builtins.h"
#include "rpmostree-util.h"
#include "rpmostree-cleanup.h"
Expand Down Expand Up @@ -59,63 +57,25 @@ rpmostree_cache_builtin_refresh (int argc, char **argv, GCancellable *cancellabl
if (!rpmostree_option_context_parse (context, option_entries, &argc, &argv, error))
goto out;

hifctx = hif_context_new ();
hif_context_set_http_proxy (hifctx, g_getenv ("http_proxy"));

if (!opt_reposdir)
opt_reposdir = "/etc/yum.repos.d";
hifctx = _rpmostree_libhif_get_default ();

hif_context_set_repo_dir (hifctx, opt_reposdir);
hif_context_set_cache_dir (hifctx, "/var/cache/rpm-ostree/metadata");
hif_context_set_solv_dir (hifctx, "/var/cache/rpm-ostree/solv");
hif_context_set_lock_dir (hifctx, "/run/rpm-ostree/lock");
if (opt_reposdir)
hif_context_set_repo_dir (hifctx, opt_reposdir);

hif_context_set_check_disk_space (hifctx, FALSE);
hif_context_set_check_transaction (hifctx, FALSE);

if (!hif_context_setup (hifctx, NULL, error))
if (!_rpmostree_libhif_setup (hifctx, cancellable, error))
return FALSE;

if (opt_enable_repos)
{
GPtrArray *sources = hif_context_get_sources (hifctx);
gs_unref_hashtable GHashTable *enabled_repos = g_hash_table_new (g_str_hash, g_str_equal);
char **strviter;
guint i;

for (strviter = opt_enable_repos; strviter && *strviter; strviter++)
g_hash_table_add (enabled_repos, *strviter);

for (i = 0; i < sources->len; i++)
{
HifSource *src = sources->pdata[i];
const char *repoid = hif_source_get_id (src);

if (!g_hash_table_contains (enabled_repos, repoid))
hif_source_set_enabled (src, HIF_SOURCE_ENABLED_NONE);
else
{
hif_source_set_enabled (src, HIF_SOURCE_ENABLED_PACKAGES);
g_hash_table_remove (enabled_repos, repoid);
}
}
_rpmostree_libhif_repos_disable_all (hifctx);

if (g_hash_table_size (enabled_repos) > 0)
for (strviter = opt_enable_repos; strviter && *strviter; strviter++)
{
GHashTableIter hashiter;
gpointer hkey, hvalue;
GString *err = g_string_new ("Repositories enabled but not found:");

g_hash_table_iter_init (&hashiter, enabled_repos);
while (g_hash_table_iter_next (&hashiter, &hkey, &hvalue))
{
g_string_append_c (err, ' ');
g_string_append (err, (char*)hkey);
}
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
err->str);
g_string_free (err, TRUE);
goto out;
const char *repoid = *strviter;
if (!hif_context_repo_enable (hifctx, repoid, error))
goto out;
}
}

Expand Down
File renamed without changes.
44 changes: 7 additions & 37 deletions src/app/rpmostree-compose-builtin-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <glib-unix.h>
#include <json-glib/json-glib.h>
#include <gio/gunixoutputstream.h>
#include <rpm/rpmsq.h>
#include <libhif.h>
#include <libhif/hif-utils.h>
#include <stdio.h>
Expand All @@ -33,6 +32,7 @@

#include "rpmostree-compose-builtins.h"
#include "rpmostree-util.h"
#include "rpmostree-hif.h"
#include "rpmostree-json-parsing.h"
#include "rpmostree-cleanup.h"
#include "rpmostree-treepkgdiff.h"
Expand Down Expand Up @@ -191,26 +191,11 @@ install_packages_in_root (RpmOstreeTreeComposeContext *self,
NULL);
gs_free char *ret_new_inputhash = NULL;

/* We can be control-c'd at any time */
#if BUILDOPT_HAVE_RPMSQ_SET_INTERRUPT_SAFETY
rpmsqSetInterruptSafety (FALSE);
#endif

/* Apparently there's only one process-global macro context;
* realistically, we're going to have to refactor all of the RPM
* stuff to a subprocess.
*/
hifctx = hif_context_new ();
hif_context_set_http_proxy (hifctx, g_getenv ("http_proxy"));

hifctx = _rpmostree_libhif_get_default ();
hif_context_set_install_root (hifctx, gs_file_get_path_cached (yumroot));

hif_context_set_cache_dir (hifctx, cachedir);
hif_context_set_solv_dir (hifctx, solvdir);
hif_context_set_lock_dir (hifctx, lockdir);
hif_context_set_check_disk_space (hifctx, FALSE);
hif_context_set_check_transaction (hifctx, FALSE);

hif_context_set_repo_dir (hifctx, gs_file_get_path_cached (contextdir));

{ JsonNode *install_langs_n =
Expand All @@ -235,28 +220,21 @@ install_packages_in_root (RpmOstreeTreeComposeContext *self,
}
}

if (!hif_context_setup (hifctx, cancellable, error))
if (!_rpmostree_libhif_setup (hifctx, cancellable, error))
goto out;

/* Forcibly override rpm/librepo SIGINT handlers. We always operate
* in a fully idempotent/atomic mode, and can be killed at any time.
*/
signal (SIGINT, SIG_DFL);
signal (SIGTERM, SIG_DFL);

/* Bind the json \"repos\" member to the hif state, which looks at the
* enabled= member of the repos file. By default we forcibly enable
* only repos which are specified, ignoring the enabled= flag.
*/
{
GPtrArray *sources;
JsonArray *enable_repos = NULL;
gs_unref_hashtable GHashTable *enabled_repo_names =
g_hash_table_new (g_str_hash, g_str_equal);
guint i;
guint n;

sources = hif_context_get_sources (hifctx);
_rpmostree_libhif_repos_disable_all (hifctx);

if (!json_object_has_member (treedata, "repos"))
{
Expand All @@ -273,17 +251,9 @@ install_packages_in_root (RpmOstreeTreeComposeContext *self,
const char *reponame = _rpmostree_jsonutil_array_require_string_element (enable_repos, i, error);
if (!reponame)
goto out;
g_hash_table_add (enabled_repo_names, (char*)reponame);
}

for (i = 0; i < sources->len; i++)
{
HifSource *src = g_ptr_array_index (sources, i);

if (!g_hash_table_lookup (enabled_repo_names, hif_source_get_id (src)))
hif_source_set_enabled (src, HIF_SOURCE_ENABLED_NONE);
else
hif_source_set_enabled (src, HIF_SOURCE_ENABLED_PACKAGES);

if (!hif_context_repo_enable (hifctx, reponame, error))
goto out;
}
}

Expand Down
85 changes: 85 additions & 0 deletions src/libpriv/rpmostree-hif.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2014 Colin Walters <walters@verbum.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/

#include "config.h"

#include <glib-unix.h>
#include <rpm/rpmsq.h>
#include <gio/gunixoutputstream.h>

#include "rpmostree-hif.h"
#include "rpmostree-cleanup.h"

HifContext *
_rpmostree_libhif_get_default (void)
{
HifContext *hifctx;

/* We can be control-c'd at any time */
#if BUILDOPT_HAVE_RPMSQ_SET_INTERRUPT_SAFETY
rpmsqSetInterruptSafety (FALSE);
#endif

hifctx = hif_context_new ();

hif_context_set_http_proxy (hifctx, g_getenv ("http_proxy"));

hif_context_set_repo_dir (hifctx, "/etc/yum.repos.d");
hif_context_set_cache_dir (hifctx, "/var/cache/rpm-ostree/metadata");
hif_context_set_solv_dir (hifctx, "/var/cache/rpm-ostree/solv");
hif_context_set_lock_dir (hifctx, "/run/rpm-ostree/lock");

hif_context_set_check_disk_space (hifctx, FALSE);
hif_context_set_check_transaction (hifctx, FALSE);

return hifctx;
}

gboolean
_rpmostree_libhif_setup (HifContext *context,
GCancellable *cancellable,
GError **error)
{
if (!hif_context_setup (context, cancellable, error))
return FALSE;

/* Forcibly override rpm/librepo SIGINT handlers. We always operate
* in a fully idempotent/atomic mode, and can be killed at any time.
*/
signal (SIGINT, SIG_DFL);
signal (SIGTERM, SIG_DFL);

return TRUE;
}

void
_rpmostree_libhif_repos_disable_all (HifContext *context)
{
GPtrArray *sources;
guint i;

sources = hif_context_get_sources (context);
for (i = 0; i < sources->len; i++)
{
HifSource *src = sources->pdata[i];

hif_source_set_enabled (src, HIF_SOURCE_ENABLED_NONE);
}
}
33 changes: 33 additions & 0 deletions src/libpriv/rpmostree-hif.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2015 Colin Walters <walters@verbum.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the licence or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/

#pragma once

#include <gio/gio.h>
#include <libhif.h>
#include <libhif/hif-utils.h>

HifContext *_rpmostree_libhif_get_default (void);

gboolean _rpmostree_libhif_setup (HifContext *context,
GCancellable *cancellable,
GError **error);

void _rpmostree_libhif_repos_disable_all (HifContext *context);

0 comments on commit 54e02cc

Please sign in to comment.