Support the --installation parameter for the flatpak list command

Also, refactor code that finds and prints the references with flatpak list,
as this needed to change a bit when considering multiple installations, as
the current code was not flexible enough for dealing with that.
This commit is contained in:
Mario Sanchez Prada
2016-12-16 19:17:37 +00:00
parent 7846195da5
commit 7d6197e3aa

View File

@@ -37,11 +37,13 @@ static gboolean opt_user;
static gboolean opt_system;
static gboolean opt_runtime;
static gboolean opt_app;
static char *opt_installation;
static char *opt_arch;
static GOptionEntry options[] = {
{ "user", 0, 0, G_OPTION_ARG_NONE, &opt_user, N_("Show user installations"), NULL },
{ "system", 0, 0, G_OPTION_ARG_NONE, &opt_system, N_("Show system-wide installations"), NULL },
{ "installation", 0, 0, G_OPTION_ARG_STRING, &opt_installation, N_("Show a specific system-wide installation"), NULL },
{ "show-details", 'd', 0, G_OPTION_ARG_NONE, &opt_show_details, N_("Show arches and branches"), NULL },
{ "runtime", 0, 0, G_OPTION_ARG_NONE, &opt_runtime, N_("List installed runtimes"), NULL },
{ "app", 0, 0, G_OPTION_ARG_NONE, &opt_app, N_("List installed applications"), NULL },
@@ -49,6 +51,34 @@ static GOptionEntry options[] = {
{ NULL }
};
/* Associates a flatpak installation's directory with
* the list of references for apps and runtimes */
typedef struct
{
FlatpakDir *dir;
GStrv app_refs;
GStrv runtime_refs;
} RefsData;
static RefsData *
refs_data_new (FlatpakDir *dir, const GStrv app_refs, const GStrv runtime_refs)
{
RefsData *refs_data = g_new0 (RefsData, 1);
refs_data->dir = g_object_ref (dir);
refs_data->app_refs = g_strdupv ((char **) app_refs);
refs_data->runtime_refs = g_strdupv ((char **) runtime_refs);
return refs_data;
}
static void
refs_data_free (RefsData *refs_data)
{
g_object_unref (refs_data->dir);
g_strfreev (refs_data->app_refs);
g_strfreev (refs_data->runtime_refs);
g_free (refs_data);
}
static char **
join_strv (char **a, char **b)
{
@@ -75,171 +105,157 @@ join_strv (char **a, char **b)
}
static gboolean
print_installed_refs (gboolean app, gboolean runtime, gboolean print_system, gboolean print_user, const char *arch, GCancellable *cancellable, GError **error)
find_refs_for_dir (FlatpakDir *dir, GStrv *apps, GStrv *runtimes, GCancellable *cancellable, GError **error)
{
g_autofree char *last = NULL;
g_auto(GStrv) system = NULL;
g_auto(GStrv) system_app = NULL;
g_auto(GStrv) system_runtime = NULL;
g_auto(GStrv) user = NULL;
g_auto(GStrv) user_app = NULL;
g_auto(GStrv) user_runtime = NULL;
g_autoptr(FlatpakDir) user_dir = NULL;
g_autoptr(FlatpakDir) system_dir = NULL;
int s, u;
if (print_user)
if (flatpak_dir_ensure_repo (dir, cancellable, NULL))
{
user_dir = flatpak_dir_get_user ();
if (flatpak_dir_ensure_repo (user_dir, cancellable, NULL))
{
if (app && !flatpak_dir_list_refs (user_dir, "app", &user_app, cancellable, error))
return FALSE;
if (runtime && !flatpak_dir_list_refs (user_dir, "runtime", &user_runtime, cancellable, error))
return FALSE;
}
}
if (print_system)
{
system_dir = flatpak_dir_get_system_default ();
if (flatpak_dir_ensure_repo (system_dir, cancellable, NULL))
{
if (app && !flatpak_dir_list_refs (system_dir, "app", &system_app, cancellable, error))
return FALSE;
if (runtime && !flatpak_dir_list_refs (system_dir, "runtime", &system_runtime, cancellable, error))
return FALSE;
}
}
FlatpakTablePrinter *printer = flatpak_table_printer_new ();
user = join_strv (user_app, user_runtime);
system = join_strv (system_app, system_runtime);
for (s = 0, u = 0; system[s] != NULL || user[u] != NULL; )
{
char *ref, *partial_ref;
g_auto(GStrv) parts = NULL;
const char *repo = NULL;
gboolean is_user;
FlatpakDir *dir = NULL;
g_autoptr(GVariant) deploy_data = NULL;
if (system[s] == NULL)
is_user = TRUE;
else if (user[u] == NULL)
is_user = FALSE;
else if (strcmp (system[s], user[u]) <= 0)
is_user = FALSE;
else
is_user = TRUE;
if (is_user)
{
ref = user[u++];
dir = user_dir;
}
else
{
ref = system[s++];
dir = system_dir;
}
parts = g_strsplit (ref, "/", -1);
partial_ref = strchr (ref, '/') + 1;
if (arch != NULL && strcmp (arch, parts[1]) != 0)
continue;
deploy_data = flatpak_dir_get_deploy_data (dir, ref, cancellable, error);
if (deploy_data == NULL)
if (apps != NULL && !flatpak_dir_list_refs (dir, "app", apps, cancellable, error))
return FALSE;
if (runtimes != NULL && !flatpak_dir_list_refs (dir, "runtime", runtimes, cancellable, error))
return FALSE;
}
repo = flatpak_deploy_data_get_origin (deploy_data);
return TRUE;
}
if (opt_show_details)
static gboolean
print_table_for_refs (gboolean print_apps, GPtrArray* refs_array, const char *arch, GCancellable *cancellable, GError **error)
{
FlatpakTablePrinter *printer = flatpak_table_printer_new ();
int i;
for (i = 0; i < refs_array->len; i++)
{
RefsData *refs_data = NULL;
FlatpakDir *dir = NULL;
g_auto(GStrv) dir_refs = NULL;
g_autofree char *last = NULL;
int j;
refs_data = (RefsData *) g_ptr_array_index (refs_array, i);
dir = refs_data->dir;
dir_refs = join_strv (refs_data->app_refs, refs_data->runtime_refs);
for (j = 0; dir_refs[j] != NULL; j++)
{
const char *active = flatpak_deploy_data_get_commit (deploy_data);
const char *alt_id = flatpak_deploy_data_get_alt_id (deploy_data);
g_autofree char *latest = NULL;
g_autofree char *size_s = NULL;
guint64 size = 0;
g_autofree const char **subpaths = NULL;
char *ref, *partial_ref;
g_auto(GStrv) parts = NULL;
const char *repo = NULL;
g_autoptr(GVariant) deploy_data = NULL;
latest = flatpak_dir_read_latest (dir, repo, ref, NULL, NULL, NULL);
if (latest)
ref = dir_refs[j];
parts = g_strsplit (ref, "/", -1);
partial_ref = strchr (ref, '/') + 1;
if (arch != NULL && strcmp (arch, parts[1]) != 0)
continue;
deploy_data = flatpak_dir_get_deploy_data (dir, ref, cancellable, error);
if (deploy_data == NULL)
return FALSE;
repo = flatpak_deploy_data_get_origin (deploy_data);
if (opt_show_details)
{
if (strcmp (active, latest) == 0)
const char *active = flatpak_deploy_data_get_commit (deploy_data);
const char *alt_id = flatpak_deploy_data_get_alt_id (deploy_data);
g_autofree char *latest = NULL;
g_autofree char *size_s = NULL;
guint64 size = 0;
g_autofree const char **subpaths = NULL;
latest = flatpak_dir_read_latest (dir, repo, ref, NULL, NULL, NULL);
if (latest)
{
g_free (latest);
latest = g_strdup ("-");
if (strcmp (active, latest) == 0)
{
g_free (latest);
latest = g_strdup ("-");
}
else
{
latest[MIN (strlen (latest), 12)] = 0;
}
}
else
{
latest = g_strdup ("?");
}
flatpak_table_printer_add_column (printer, partial_ref);
flatpak_table_printer_add_column (printer, repo);
flatpak_table_printer_add_column_len (printer, active, 12);
flatpak_table_printer_add_column_len (printer, latest, 12);
size = flatpak_deploy_data_get_installed_size (deploy_data);
size_s = g_format_size (size);
flatpak_table_printer_add_column (printer, size_s);
flatpak_table_printer_add_column (printer, ""); /* Options */
if (refs_array->len > 1)
{
g_autofree char *source = NULL;
if (flatpak_dir_is_user (dir))
{
source = g_strdup ("user");
}
else
{
const char *system_source = flatpak_dir_get_id (dir);
source = g_strdup_printf ("system (%s)", system_source ? system_source : "default");
}
flatpak_table_printer_append_with_comma (printer, source);
}
if (alt_id)
{
g_autofree char *alt_id_str = g_strdup_printf ("alt-id=%.12s", alt_id);
flatpak_table_printer_append_with_comma (printer, alt_id_str);
}
if (strcmp (parts[0], "app") == 0)
{
g_autofree char *current;
current = flatpak_dir_current_ref (dir, parts[1], cancellable);
if (current && strcmp (ref, current) == 0)
flatpak_table_printer_append_with_comma (printer, "current");
}
else
{
if (print_apps)
flatpak_table_printer_append_with_comma (printer, "runtime");
}
subpaths = flatpak_deploy_data_get_subpaths (deploy_data);
if (subpaths[0] == NULL)
{
flatpak_table_printer_add_column (printer, "");
}
else
{
int i;
flatpak_table_printer_add_column (printer, ""); /* subpaths */
for (i = 0; subpaths[i] != NULL; i++)
flatpak_table_printer_append_with_comma (printer, subpaths[i]);
}
}
else
{
latest = g_strdup ("?");
}
flatpak_table_printer_add_column (printer, partial_ref);
flatpak_table_printer_add_column (printer, repo);
flatpak_table_printer_add_column_len (printer, active, 12);
flatpak_table_printer_add_column_len (printer, latest, 12);
size = flatpak_deploy_data_get_installed_size (deploy_data);
size_s = g_format_size (size);
flatpak_table_printer_add_column (printer, size_s);
flatpak_table_printer_add_column (printer, ""); /* Options */
if (print_user && print_system)
flatpak_table_printer_append_with_comma (printer, is_user ? "user" : "system");
if (alt_id)
{
g_autofree char *alt_id_str = g_strdup_printf ("alt-id=%.12s", alt_id);
flatpak_table_printer_append_with_comma (printer, alt_id_str);
}
if (strcmp (parts[0], "app") == 0)
{
g_autofree char *current;
current = flatpak_dir_current_ref (dir, parts[1], cancellable);
if (current && strcmp (ref, current) == 0)
flatpak_table_printer_append_with_comma (printer, "current");
}
else
{
if (app)
flatpak_table_printer_append_with_comma (printer, "runtime");
}
subpaths = flatpak_deploy_data_get_subpaths (deploy_data);
if (subpaths[0] == NULL)
{
flatpak_table_printer_add_column (printer, "");
}
else
{
int i;
flatpak_table_printer_add_column (printer, ""); /* subpaths */
for (i = 0; subpaths[i] != NULL; i++)
flatpak_table_printer_append_with_comma (printer, subpaths[i]);
if (last == NULL || strcmp (last, parts[1]) != 0)
{
flatpak_table_printer_add_column (printer, parts[1]);
g_clear_pointer (&last, g_free);
last = g_strdup (parts[1]);
}
}
flatpak_table_printer_finish_row (printer);
}
else
{
if (last == NULL || strcmp (last, parts[1]) != 0)
{
flatpak_table_printer_add_column (printer, parts[1]);
g_clear_pointer (&last, g_free);
last = g_strdup (parts[1]);
}
}
flatpak_table_printer_finish_row (printer);
}
flatpak_table_printer_print (printer);
@@ -248,6 +264,90 @@ print_installed_refs (gboolean app, gboolean runtime, gboolean print_system, gbo
return TRUE;
}
static gboolean
print_installed_refs (gboolean app, gboolean runtime, gboolean print_system, gboolean print_user, const char *installation, const char *arch, GCancellable *cancellable, GError **error)
{
g_autoptr(GPtrArray) refs_array = NULL;
gboolean print_all = !print_user && !print_system && (installation == NULL);
refs_array = g_ptr_array_new_with_free_func ((GDestroyNotify) refs_data_free);
if (print_user || print_all)
{
g_autoptr(FlatpakDir) user_dir = NULL;
g_auto(GStrv) user_app = NULL;
g_auto(GStrv) user_runtime = NULL;
user_dir =flatpak_dir_get_user ();
if (!find_refs_for_dir (user_dir, app ? &user_app : NULL, runtime ? &user_runtime : NULL, cancellable, error))
return FALSE;
g_ptr_array_add (refs_array, refs_data_new (user_dir, user_app, user_runtime));
}
if (print_all)
{
g_autoptr(GPtrArray) system_dirs = NULL;
int i;
system_dirs = flatpak_dir_get_system_list (cancellable, error);
if (system_dirs == NULL)
return FALSE;
for (i = 0; i < system_dirs->len; i++)
{
FlatpakDir *dir = g_ptr_array_index (system_dirs, i);
g_auto(GStrv) apps = NULL;
g_auto(GStrv) runtimes = NULL;
if (!find_refs_for_dir (dir, app ? &apps : NULL, runtime ? &runtimes : NULL, cancellable, error))
return FALSE;
g_ptr_array_add (refs_array, refs_data_new (dir, apps, runtimes));
}
}
else
{
if (print_system)
{
g_autoptr(FlatpakDir) system_dir = NULL;
g_auto(GStrv) system_app = NULL;
g_auto(GStrv) system_runtime = NULL;
system_dir = flatpak_dir_get_system_default ();
if (!find_refs_for_dir (system_dir, app ? &system_app : NULL, runtime ? &system_runtime : NULL, cancellable, error))
return FALSE;
g_ptr_array_add (refs_array, refs_data_new (system_dir, system_app, system_runtime));
}
if (installation != NULL)
{
g_autoptr(FlatpakDir) system_dir = NULL;
g_auto(GStrv) installation_apps = NULL;
g_auto(GStrv) installation_runtimes = NULL;
system_dir = flatpak_dir_get_system_by_id (installation, cancellable, error);
if (system_dir == NULL)
return FALSE;
if (system_dir == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"Can't find installation %s", opt_installation);
return FALSE;
}
if (!find_refs_for_dir (system_dir, app ? &installation_apps : NULL, runtime ? &installation_runtimes : NULL, cancellable, error))
return FALSE;
g_ptr_array_add (refs_array, refs_data_new (system_dir, installation_apps, installation_runtimes));
}
}
if (!print_table_for_refs (app, refs_array, arch, cancellable, error))
return FALSE;
return TRUE;
}
gboolean
flatpak_builtin_list (int argc, char **argv, GCancellable *cancellable, GError **error)
{
@@ -266,8 +366,9 @@ flatpak_builtin_list (int argc, char **argv, GCancellable *cancellable, GError *
opt_app = TRUE;
if (!print_installed_refs (opt_app, opt_runtime,
opt_system || (!opt_user && !opt_system),
opt_user || (!opt_user && !opt_system),
opt_system,
opt_user,
opt_installation,
opt_arch,
cancellable, error))
return FALSE;