Skip to content

Commit

Permalink
Add the concept of metadata-only software sources
Browse files Browse the repository at this point in the history
We might want to return search results for repos that are disabled, for instance
the google chrome repo could be installed by default, but disabled pending
user consent.

We still want to show the application if the user explictly searches for
'google chrome' (which requires the ahead-of-time metadata download) but with
a step to enable the repo before the action can proceed.

By changing the boolean 'enabled' parameter to a bitfield we can preserve the
old ABI and also add the idea of a metadata-only enabled state.
  • Loading branch information
hughsie committed Nov 26, 2014
1 parent 4800a28 commit f754f58
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 27 deletions.
1 change: 1 addition & 0 deletions data/tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_files = \
yum.repos.d/redhat.repo \
yum.repos.d/bumblebee.repo

EXTRA_DIST = $(test_files)
Expand Down
4 changes: 4 additions & 0 deletions data/tests/yum.repos.d/redhat.repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[redhat]
name=Red Hat
baseurl=http://www.redhat.com/
enabled=0
11 changes: 7 additions & 4 deletions libhif/hif-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ hif_context_copy_vendor_cache (HifContext *context, GError **error)
_cleanup_free_ gchar *path = NULL;
_cleanup_free_ gchar *path_vendor = NULL;
src = g_ptr_array_index (priv->sources, i);
if (!hif_source_get_enabled (src))
if (hif_source_get_enabled (src) == HIF_SOURCE_ENABLED_NONE)
continue;

/* does the repo already exist */
Expand Down Expand Up @@ -1498,7 +1498,7 @@ hif_context_update (HifContext *context, const gchar *name, GError **error)
static gboolean
hif_context_repo_set_data (HifContext *context,
const gchar *repo_id,
gboolean enabled,
HifSourceEnabled enabled,
GError **error)
{
HifContextPrivate *priv = GET_PRIVATE (context);
Expand Down Expand Up @@ -1548,7 +1548,9 @@ hif_context_repo_enable (HifContext *context,
const gchar *repo_id,
GError **error)
{
return hif_context_repo_set_data (context, repo_id, TRUE, error);
return hif_context_repo_set_data (context, repo_id,
HIF_SOURCE_ENABLED_PACKAGES |
HIF_SOURCE_ENABLED_METADATA, error);
}

/**
Expand All @@ -1570,7 +1572,8 @@ hif_context_repo_disable (HifContext *context,
const gchar *repo_id,
GError **error)
{
return hif_context_repo_set_data (context, repo_id, FALSE, error);
return hif_context_repo_set_data (context, repo_id,
HIF_SOURCE_ENABLED_NONE, error);
}

/**
Expand Down
32 changes: 18 additions & 14 deletions libhif/hif-repos.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ hif_repos_add_media (HifRepos *repos,

/* create read-only location */
source = hif_source_new (priv->context);
hif_source_set_enabled (source, TRUE);
hif_source_set_enabled (source, HIF_SOURCE_ENABLED_PACKAGES);
hif_source_set_gpgcheck (source, TRUE);
hif_source_set_kind (source, HIF_SOURCE_KIND_MEDIA);
hif_source_set_cost (source, 100);
Expand Down Expand Up @@ -334,30 +334,34 @@ hif_repos_source_parse_id (HifRepos *repos,
GError **error)
{
HifReposPrivate *priv = GET_PRIVATE (repos);
gboolean has_enabled;
gboolean is_enabled;
HifSourceEnabled enabled = 0;
gboolean ret = TRUE;
gchar *tmp;
guint64 val;
guint cost;
_cleanup_object_unref_ HifSource *source;

/* enabled isn't a required key */
has_enabled = g_key_file_has_key (keyfile,
id,
"enabled",
NULL);
if (has_enabled) {
is_enabled = g_key_file_get_boolean (keyfile,
id,
"enabled",
NULL);
if (g_key_file_has_key (keyfile, id, "enabled", NULL)) {
if (g_key_file_get_boolean (keyfile, id, "enabled", NULL))
enabled |= HIF_SOURCE_ENABLED_PACKAGES;
} else {
is_enabled = TRUE;
enabled |= HIF_SOURCE_ENABLED_PACKAGES;
}

/* enabled_metadata isn't a required key */
if (g_key_file_has_key (keyfile, id, "enabled_metadata", NULL)) {
if (g_key_file_get_boolean (keyfile, id, "enabled_metadata", NULL))
enabled |= HIF_SOURCE_ENABLED_METADATA;
} else {
_cleanup_free_ gchar *basename = NULL;
basename = g_path_get_basename (filename);
if (g_strcmp0 (basename, "redhat.repo") == 0)
enabled |= HIF_SOURCE_ENABLED_METADATA;
}

source = hif_source_new (priv->context);
hif_source_set_enabled (source, is_enabled);
hif_source_set_enabled (source, enabled);
hif_source_set_kind (source, HIF_SOURCE_KIND_REMOTE);
cost = g_key_file_get_integer (keyfile, id, "cost", NULL);
if (cost != 0)
Expand Down
10 changes: 8 additions & 2 deletions libhif/hif-sack.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,23 @@ hif_sack_add_sources (HySack sack,
/* count the enabled sources */
for (i = 0; i < sources->len; i++) {
src = g_ptr_array_index (sources, i);
if (hif_source_get_enabled (src))
if (hif_source_get_enabled (src) != HIF_SOURCE_ENABLED_NONE)
cnt++;
}

/* add each repo */
hif_state_set_number_steps (state, cnt);
for (i = 0; i < sources->len; i++) {
src = g_ptr_array_index (sources, i);
if (!hif_source_get_enabled (src))
if (hif_source_get_enabled (src) == HIF_SOURCE_ENABLED_NONE)
continue;

/* only allow metadata-only sources if FLAG_UNAVAILABLE is set */
if (hif_source_get_enabled (src) == HIF_SOURCE_ENABLED_METADATA) {
if ((flags & HIF_SACK_ADD_FLAG_UNAVAILABLE) == 0)
continue;
}

state_local = hif_state_get_child (state);
ret = hif_sack_add_source (sack,
src,
Expand Down
2 changes: 2 additions & 0 deletions libhif/hif-sack.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @HIF_SACK_ADD_FLAG_FILELISTS: Add the filelists
* @HIF_SACK_ADD_FLAG_UPDATEINFO: Add the updateinfo
* @HIF_SACK_ADD_FLAG_REMOTE: Use remote sources
* @HIF_SACK_ADD_FLAG_UNAVAILABLE: Add sources that are unavailable
*
* The error code.
**/
Expand All @@ -46,6 +47,7 @@ typedef enum {
HIF_SACK_ADD_FLAG_FILELISTS = 1,
HIF_SACK_ADD_FLAG_UPDATEINFO = 2,
HIF_SACK_ADD_FLAG_REMOTE = 4,
HIF_SACK_ADD_FLAG_UNAVAILABLE = 8,
/*< private >*/
HIF_SACK_ADD_FLAG_LAST
} HifSackAddFlags;
Expand Down
6 changes: 6 additions & 0 deletions libhif/hif-self-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,12 @@ hif_repos_func (void)
src = hif_repos_get_source_by_id (repos, "bumblebee", &error);
g_assert_no_error (error);
g_assert (src != NULL);

/* load repos that should be metadata enabled automatically */
src = hif_repos_get_source_by_id (repos, "redhat", &error);
g_assert_no_error (error);
g_assert (src != NULL);
g_assert_cmpint (hif_source_get_enabled (src), ==, HIF_SOURCE_ENABLED_METADATA);
}

static void
Expand Down
16 changes: 11 additions & 5 deletions libhif/hif-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
typedef struct _HifSourcePrivate HifSourcePrivate;
struct _HifSourcePrivate
{
gboolean enabled;
HifSourceEnabled enabled;
gboolean gpgcheck;
guint cost;
gchar *filename; /* /etc/yum.repos.d/updates.repo */
Expand Down Expand Up @@ -253,11 +253,11 @@ hif_source_get_description (HifSource *source)
*
* Gets if the source is enabled.
*
* Returns: %TRUE if enabled
* Returns: %HIF_SOURCE_ENABLED_PACKAGES if enabled
*
* Since: 0.1.0
**/
gboolean
HifSourceEnabled
hif_source_get_enabled (HifSource *source)
{
HifSourcePrivate *priv = GET_PRIVATE (source);
Expand Down Expand Up @@ -508,6 +508,7 @@ void
hif_source_set_filename (HifSource *source, const gchar *filename)
{
HifSourcePrivate *priv = GET_PRIVATE (source);

g_free (priv->filename);
priv->filename = g_strdup (filename);
}
Expand Down Expand Up @@ -556,10 +557,15 @@ hif_source_set_packages_tmp (HifSource *source, const gchar *packages_tmp)
* Since: 0.1.0
**/
void
hif_source_set_enabled (HifSource *source, gboolean enabled)
hif_source_set_enabled (HifSource *source, HifSourceEnabled enabled)
{
HifSourcePrivate *priv = GET_PRIVATE (source);

priv->enabled = enabled;

/* packages implies metadata */
if (priv->enabled & HIF_SOURCE_ENABLED_PACKAGES)
priv->enabled |= HIF_SOURCE_ENABLED_METADATA;
}

/**
Expand Down Expand Up @@ -777,7 +783,7 @@ hif_source_check (HifSource *source,
/* has the media repo vanished? */
if (priv->kind == HIF_SOURCE_KIND_MEDIA &&
!g_file_test (priv->location, G_FILE_TEST_EXISTS)) {
priv->enabled = FALSE;
priv->enabled = HIF_SOURCE_ENABLED_NONE;
return TRUE;
}

Expand Down
20 changes: 18 additions & 2 deletions libhif/hif-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ typedef enum {
HIF_SOURCE_KIND_LAST
} HifSourceKind;

/**
* HifSourceEnabled:
* @HIF_SOURCE_ENABLED_NONE: Source is disabled
* @HIF_SOURCE_ENABLED_PACKAGES: Source is fully enabled
* @HIF_SOURCE_ENABLED_METADATA: Only source metadata is enabled
*
* How enabled is the source.
**/
typedef enum {
HIF_SOURCE_ENABLED_NONE = 0,
HIF_SOURCE_ENABLED_PACKAGES = 1,
HIF_SOURCE_ENABLED_METADATA = 2,
/*< private >*/
HIF_SOURCE_ENABLED_LAST
} HifSourceEnabled;

GType hif_source_get_type (void);
HifSource *hif_source_new (HifContext *context);

Expand All @@ -101,7 +117,7 @@ const gchar *hif_source_get_id (HifSource *source);
const gchar *hif_source_get_location (HifSource *source);
const gchar *hif_source_get_filename (HifSource *source);
const gchar *hif_source_get_packages (HifSource *source);
gboolean hif_source_get_enabled (HifSource *source);
HifSourceEnabled hif_source_get_enabled (HifSource *source);
guint hif_source_get_cost (HifSource *source);
HifSourceKind hif_source_get_kind (HifSource *source);
gboolean hif_source_get_gpgcheck (HifSource *source);
Expand All @@ -126,7 +142,7 @@ void hif_source_set_packages (HifSource *source,
void hif_source_set_packages_tmp (HifSource *source,
const gchar *packages_tmp);
void hif_source_set_enabled (HifSource *source,
gboolean enabled);
HifSourceEnabled enabled);
void hif_source_set_cost (HifSource *source,
guint cost);
void hif_source_set_kind (HifSource *source,
Expand Down

0 comments on commit f754f58

Please sign in to comment.