2015-01-16 04:25:03 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libgsystem.h"
|
2015-01-21 20:23:14 +00:00
|
|
|
#include <libsoup/soup.h>
|
2015-01-16 04:25:03 +00:00
|
|
|
|
|
|
|
#include "xdg-app-builtins.h"
|
|
|
|
#include "xdg-app-utils.h"
|
|
|
|
|
|
|
|
static gboolean opt_show_details;
|
|
|
|
static gboolean opt_only_runtimes;
|
|
|
|
static gboolean opt_only_apps;
|
2015-01-20 22:06:53 +00:00
|
|
|
static gboolean opt_only_updates;
|
2015-01-16 04:25:03 +00:00
|
|
|
|
|
|
|
static GOptionEntry options[] = {
|
|
|
|
{ "show-details", 0, 0, G_OPTION_ARG_NONE, &opt_show_details, "Show arches and branches", NULL },
|
2015-01-16 19:11:22 +00:00
|
|
|
{ "runtimes", 0, 0, G_OPTION_ARG_NONE, &opt_only_runtimes, "Show only runtimes", NULL },
|
|
|
|
{ "apps", 0, 0, G_OPTION_ARG_NONE, &opt_only_apps, "Show only apps", NULL },
|
2015-01-20 22:06:53 +00:00
|
|
|
{ "updates", 0, 0, G_OPTION_ARG_NONE, &opt_only_updates, "Show only those where updates are available", NULL },
|
2015-01-16 04:25:03 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2015-01-21 19:37:29 +00:00
|
|
|
static gboolean
|
2015-01-21 20:23:14 +00:00
|
|
|
load_contents (const char *uri, GBytes **contents, GCancellable *cancellable, GError **error)
|
2015-01-21 19:37:29 +00:00
|
|
|
{
|
2015-01-21 20:23:14 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
gs_free char *scheme = NULL;
|
|
|
|
|
|
|
|
scheme = g_uri_parse_scheme (uri);
|
|
|
|
if (strcmp (scheme, "file") == 0)
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
gsize length;
|
|
|
|
gs_unref_object GFile *file = NULL;
|
|
|
|
|
|
|
|
g_debug ("Loading summary %s using GIO", uri);
|
|
|
|
file = g_file_new_for_uri (uri);
|
|
|
|
if (!g_file_load_contents (file, cancellable, &buffer, &length, NULL, NULL))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
*contents = g_bytes_new_take (buffer, length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gs_unref_object SoupSession *session = NULL;
|
|
|
|
gs_unref_object SoupMessage *msg = NULL;
|
2015-01-21 19:37:29 +00:00
|
|
|
|
2015-01-21 20:23:14 +00:00
|
|
|
g_debug ("Loading summary %s using libsoup", uri);
|
|
|
|
session = soup_session_new ();
|
|
|
|
msg = soup_message_new ("GET", uri);
|
|
|
|
soup_session_send_message (session, msg);
|
|
|
|
|
|
|
|
if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
*contents = g_bytes_new (msg->response_body->data, msg->response_body->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
g_debug ("Received %ld bytes", g_bytes_get_size (*contents));
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
2015-01-21 19:37:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 04:25:03 +00:00
|
|
|
gboolean
|
|
|
|
xdg_app_builtin_repo_contents (int argc, char **argv, GCancellable *cancellable, GError **error)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
GOptionContext *context;
|
|
|
|
gs_unref_object XdgAppDir *dir = NULL;
|
|
|
|
gs_unref_object OstreeRepo *repo = NULL;
|
|
|
|
gs_unref_hashtable GHashTable *refs = NULL;
|
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key;
|
2015-01-20 22:06:53 +00:00
|
|
|
gpointer value;
|
2015-01-16 19:28:54 +00:00
|
|
|
gs_unref_ptrarray GPtrArray *names = NULL;
|
|
|
|
int i;
|
2015-01-16 04:42:48 +00:00
|
|
|
const char *repository;
|
2015-01-21 03:42:55 +00:00
|
|
|
gs_free char *url = NULL;
|
2015-01-21 19:37:29 +00:00
|
|
|
gs_free char *summary_url = NULL;
|
2015-01-21 20:23:14 +00:00
|
|
|
gs_unref_bytes GBytes *bytes = NULL;
|
2015-01-16 04:25:03 +00:00
|
|
|
|
2015-01-16 04:42:48 +00:00
|
|
|
context = g_option_context_new (" REPOSITORY - Show available runtimes and applications");
|
2015-01-16 04:25:03 +00:00
|
|
|
|
|
|
|
if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error))
|
|
|
|
goto out;
|
|
|
|
|
2015-01-16 04:42:48 +00:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
usage_error (context, "REPOSITORY must be specified", error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
repository = argv[1];
|
|
|
|
|
2015-01-16 04:25:03 +00:00
|
|
|
repo = xdg_app_dir_get_repo (dir);
|
2015-01-21 00:51:58 +00:00
|
|
|
if (!ostree_repo_remote_get_url (repo, repository, &url, error))
|
2015-01-16 04:25:03 +00:00
|
|
|
goto out;
|
|
|
|
|
2015-01-21 20:23:14 +00:00
|
|
|
summary_url = g_build_filename (url, "summary", NULL);
|
|
|
|
if (load_contents (summary_url, &bytes, cancellable, NULL))
|
2015-01-21 00:51:58 +00:00
|
|
|
{
|
|
|
|
gs_unref_variant GVariant *summary;
|
|
|
|
gs_unref_variant GVariant *ref_list;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
|
|
|
|
|
2015-01-21 20:23:14 +00:00
|
|
|
summary = g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT, bytes, FALSE);
|
2015-01-21 00:51:58 +00:00
|
|
|
ref_list = g_variant_get_child_value (summary, 0);
|
|
|
|
n = g_variant_n_children (ref_list);
|
2015-01-21 20:23:14 +00:00
|
|
|
g_debug ("Summary contains %d refs", n);
|
2015-01-21 00:51:58 +00:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
gs_unref_variant GVariant *ref = NULL;
|
|
|
|
gs_unref_variant GVariant *csum_v = NULL;
|
|
|
|
char *refname;
|
|
|
|
char *checksum;
|
|
|
|
|
|
|
|
ref = g_variant_get_child_value (ref_list, i);
|
|
|
|
g_variant_get (ref, "(&s(t@aya{sv}))", &refname, NULL, &csum_v, NULL);
|
|
|
|
|
|
|
|
if (!ostree_validate_rev (refname, error))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
checksum = ostree_checksum_from_bytes_v (csum_v);
|
2015-02-02 10:02:41 +00:00
|
|
|
g_debug ("%s summary: %s -> %s\n", repository, refname, checksum);
|
2015-01-21 00:51:58 +00:00
|
|
|
g_hash_table_insert (refs, g_strdup (refname), checksum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_printerr ("Failed to load summary file for remote %s, listing local refs\n", repository);
|
|
|
|
if (!ostree_repo_list_refs (repo, NULL, &refs, cancellable, error))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:28:54 +00:00
|
|
|
names = g_ptr_array_new_with_free_func (g_free);
|
2015-01-16 04:25:03 +00:00
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, refs);
|
2015-01-20 22:06:53 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, &key, &value))
|
2015-01-16 04:25:03 +00:00
|
|
|
{
|
|
|
|
const char *refspec = key;
|
2015-01-20 22:06:53 +00:00
|
|
|
const char *checksum = value;
|
2015-01-16 04:25:03 +00:00
|
|
|
gs_free char *remote = NULL;
|
|
|
|
gs_free char *ref = NULL;
|
|
|
|
char *name = NULL;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!ostree_parse_refspec (refspec, &remote, &ref, error))
|
|
|
|
goto out;
|
|
|
|
|
2015-01-21 00:51:58 +00:00
|
|
|
if (remote != NULL && !g_str_equal (remote, repository))
|
2015-01-16 04:42:48 +00:00
|
|
|
continue;
|
|
|
|
|
2015-01-20 22:06:53 +00:00
|
|
|
if (opt_only_updates)
|
|
|
|
{
|
|
|
|
gs_free char *deployed = NULL;
|
|
|
|
|
|
|
|
deployed = xdg_app_dir_read_active (dir, ref, cancellable);
|
2015-01-21 00:51:58 +00:00
|
|
|
if (deployed == NULL)
|
|
|
|
continue;
|
2015-01-20 22:06:53 +00:00
|
|
|
|
|
|
|
if (g_strcmp0 (deployed, checksum) == 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-16 04:25:03 +00:00
|
|
|
if (g_str_has_prefix (ref, "runtime/") && !opt_only_apps)
|
|
|
|
{
|
|
|
|
if (!opt_show_details)
|
|
|
|
{
|
2015-01-22 11:01:13 +00:00
|
|
|
name = g_strdup (ref + strlen ("runtime/"));
|
2015-01-16 04:25:03 +00:00
|
|
|
p = strchr (name, '/');
|
|
|
|
if (p)
|
|
|
|
*p = 0;
|
|
|
|
}
|
2015-01-22 11:01:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
name = g_strdup (ref);
|
|
|
|
}
|
2015-01-16 04:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (g_str_has_prefix (ref, "app/") && !opt_only_runtimes)
|
|
|
|
{
|
|
|
|
if (!opt_show_details)
|
|
|
|
{
|
2015-01-22 11:01:13 +00:00
|
|
|
name = g_strdup (ref + strlen ("app/"));
|
2015-01-16 04:25:03 +00:00
|
|
|
p = strchr (name, '/');
|
|
|
|
if (p)
|
|
|
|
*p = 0;
|
|
|
|
}
|
2015-01-22 11:01:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
name = g_strdup (ref);
|
|
|
|
}
|
2015-01-16 04:25:03 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 19:28:54 +00:00
|
|
|
if (name)
|
2015-01-16 04:25:03 +00:00
|
|
|
{
|
2015-01-16 19:28:54 +00:00
|
|
|
for (i = 0; i < names->len; i++)
|
|
|
|
{
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
cmp = strcmp (name, g_ptr_array_index (names, i));
|
|
|
|
if (cmp > 0)
|
|
|
|
continue;
|
|
|
|
else if (cmp < 0)
|
|
|
|
g_ptr_array_insert (names, i, name);
|
|
|
|
else
|
|
|
|
g_free (name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == names->len)
|
|
|
|
g_ptr_array_insert (names, i, name);
|
2015-01-16 04:25:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:28:54 +00:00
|
|
|
for (i = 0; i < names->len; i++)
|
|
|
|
g_print ("%s\n", (char *)g_ptr_array_index (names, i));
|
|
|
|
|
2015-01-16 04:25:03 +00:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (context)
|
|
|
|
g_option_context_free (context);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|