history: Fix printing refs

The history command seems to have been broken since it was changed to
use FlatpakDecomposed, since that type only works for app or runtime
refs, resulting in errors such as:
$ flatpak history
error: appstream2/x86_64 is not application or runtime

Fix this by making the logic a bit smarter, and don't let any one
invalid ref entry prevent the whole command from working.

Fixes #4332
This commit is contained in:
Phaedrus Leeds
2021-10-27 18:56:25 -07:00
parent 2f53c1ec51
commit e44b54856e
4 changed files with 31 additions and 22 deletions

View File

@@ -217,35 +217,43 @@ print_history (GPtrArray *dirs,
strcmp (columns[k].name, "arch") == 0 ||
strcmp (columns[k].name, "branch") == 0)
{
g_autoptr(FlatpakDecomposed) ref = NULL;
g_autofree char *ref_str = get_field (j, "REF", error);
g_autofree char *ref_str = NULL;
g_autofree char *value = NULL;
ref_str = get_field (j, "REF", error);
if (*error)
return FALSE;
if (ref_str && ref_str[0])
{
ref = flatpak_decomposed_new_from_ref (ref_str, error);
if (ref == NULL)
return FALSE;
}
if (ref_str && ref_str[0] &&
!flatpak_is_app_runtime_or_appstream_ref (ref_str) &&
g_strcmp0 (ref_str, OSTREE_REPO_METADATA_REF) != 0)
g_warning ("Unknown ref in history: %s", ref_str);
if (strcmp (columns[k].name, "ref") == 0)
flatpak_table_printer_add_column (printer, ref_str);
else
value = g_strdup (ref_str);
else if (strcmp (columns[k].name, "arch") == 0)
{
g_autofree char *value = NULL;
if (ref)
if (ref_str != NULL)
value = flatpak_get_arch_for_ref (ref_str);
}
else if (ref_str && ref_str[0] &&
(g_str_has_prefix (ref_str, "app/") ||
g_str_has_prefix (ref_str, "runtime/")))
{
g_autoptr(FlatpakDecomposed) ref = NULL;
ref = flatpak_decomposed_new_from_ref (ref_str, NULL);
if (ref == NULL)
g_warning ("Invalid ref in history: %s", ref_str);
else
{
if (strcmp (columns[k].name, "application") == 0)
value = flatpak_decomposed_dup_id (ref);
else if (strcmp (columns[k].name, "arch") == 0)
value = flatpak_decomposed_dup_arch (ref);
else
value = flatpak_decomposed_dup_branch (ref);
}
flatpak_table_printer_add_column (printer, value);
}
flatpak_table_printer_add_column (printer, value);
}
else if (strcmp (columns[k].name, "installation") == 0)
{

View File

@@ -15814,7 +15814,7 @@ flatpak_dir_delete_mirror_refs (FlatpakDir *self,
* remotes, but that would not cover the case of if a remote was
* deleted.
*/
if (is_flatpak_ref (c_r->ref_name) ||
if (flatpak_is_app_runtime_or_appstream_ref (c_r->ref_name) ||
g_strcmp0 (c_r->ref_name, OSTREE_REPO_METADATA_REF) == 0)
{
if (dry_run)

View File

@@ -43,6 +43,7 @@ char * flatpak_make_valid_id_prefix (const char *orig_id);
gboolean flatpak_id_has_subref_suffix (const char *id,
gssize id_len);
gboolean flatpak_is_app_runtime_or_appstream_ref (const char *ref);
char * flatpak_get_arch_for_ref (const char *ref);
const char *flatpak_get_compat_arch_reverse (const char *compat_arch);

View File

@@ -3173,8 +3173,8 @@ flatpak_repo_save_digested_summary_delta (OstreeRepo *repo,
}
static gboolean
is_flatpak_ref (const char *ref)
gboolean
flatpak_is_app_runtime_or_appstream_ref (const char *ref)
{
return
g_str_has_prefix (ref, "appstream/") ||
@@ -3289,7 +3289,7 @@ populate_commit_data_cache (OstreeRepo *repo,
VarVariantRef xa_data_v;
VarCacheDataRef xa_data;
if (!is_flatpak_ref (ref))
if (!flatpak_is_app_runtime_or_appstream_ref (ref))
continue;
commit_bytes = var_ref_info_peek_checksum (info, &commit_bytes_len);
@@ -4363,7 +4363,7 @@ generate_summary (OstreeRepo *repo,
if (!g_hash_table_contains (commits, rev))
continue; /* Filter out commit (by arch & subset) */
if (is_flatpak_ref (ref))
if (flatpak_is_app_runtime_or_appstream_ref (ref))
rev_data = g_hash_table_lookup (commit_data_cache, rev);
if (rev_data != NULL)
@@ -4804,7 +4804,7 @@ flatpak_repo_update (OstreeRepo *repo,
g_hash_table_add (arches, g_steal_pointer (&arch));
/* Add CommitData for flatpak refs that we didn't already pre-populate */
if (is_flatpak_ref (ref))
if (flatpak_is_app_runtime_or_appstream_ref (ref))
{
rev_data = g_hash_table_lookup (commit_data_cache, rev);
if (rev_data == NULL)