Skip to content

Commit

Permalink
ostree: Add a find-remotes built-in command
Browse files Browse the repository at this point in the history
This is a wrapper around the new ostree_repo_find_remotes() method; it
tries to find available remotes which can serve updates for the
user-provided refs.

Signed-off-by: Philip Withnall <[email protected]>
  • Loading branch information
pwithnall committed Apr 25, 2017
1 parent 60fb936 commit df46a16
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile-ostree.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ostree_SOURCES = src/ostree/main.c \
src/ostree/ot-builtin-commit.c \
src/ostree/ot-builtin-diff.c \
src/ostree/ot-builtin-export.c \
src/ostree/ot-builtin-find-remotes.c \
src/ostree/ot-builtin-fsck.c \
src/ostree/ot-builtin-gpg-sign.c \
src/ostree/ot-builtin-init.c \
Expand Down
1 change: 1 addition & 0 deletions src/ostree/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static OstreeCommand commands[] = {
{ "config", ostree_builtin_config },
{ "diff", ostree_builtin_diff },
{ "export", ostree_builtin_export },
{ "find-remotes", ostree_builtin_find_remotes },
{ "fsck", ostree_builtin_fsck },
{ "gpg-sign", ostree_builtin_gpg_sign },
{ "init", ostree_builtin_init },
Expand Down
162 changes: 162 additions & 0 deletions src/ostree/ot-builtin-find-remotes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright © 2017 Endless Mobile, Inc.
*
* This library 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 License, 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.
*
* Authors:
* - Philip Withnall <[email protected]>
*/

#include "config.h"

#include "ot-main.h"
#include "ot-builtins.h"
#include "ostree.h"
#include "otutil.h"

static gchar *opt_cache_dir = NULL;
static gboolean opt_disable_fsync = FALSE;

static GOptionEntry options[] =
{
{ "cache-dir", 0, 0, G_OPTION_ARG_FILENAME, &opt_cache_dir, "Use custom cache dir", NULL },
{ "disable-fsync", 0, 0, G_OPTION_ARG_NONE, &opt_disable_fsync, "Do not invoke fsync()", NULL },
{ NULL }
};

/* TODO: Move this into ostree-repo.h? */
typedef OstreeRepoFinderResult* RepoFinderResultArray;

static void
repo_finder_result_array_free (RepoFinderResultArray *array)
{
gsize i;

for (i = 0; array[i] != NULL; i++)
ostree_repo_finder_result_free (array[i]);

g_free (array);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC(RepoFinderResultArray, repo_finder_result_array_free)

static gchar *
uint64_secs_to_iso8601 (guint64 secs)
{
GTimeVal tv = { secs, 0 };
return g_time_val_to_iso8601 (&tv);
}

/* TODO: Move to ostree-remote.c? */
static gchar *
remote_get_uri (OstreeRemote *remote)
{
g_autoptr(GError) error = NULL;
g_autofree gchar *uri = NULL;

uri = g_key_file_get_string (remote->options, remote->group, "url", &error);
g_assert_no_error (error);

return g_steal_pointer (&uri);
}

gboolean
ostree_builtin_find_remotes (int argc,
char **argv,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GOptionContext) context = NULL;
glnx_unref_object OstreeRepo *repo = NULL;
g_autoptr(GPtrArray) refs = NULL;
glnx_unref_object OstreeAsyncProgress *progress = NULL;
gsize i;
g_autoptr(RepoFinderResultArray) results = NULL;
g_auto(GLnxConsoleRef) console = { 0, };

context = g_option_context_new ("REF [REF...] - Find remotes to serve the given refs");

/* Parse options. */
if (!ostree_option_context_parse (context, options, &argc, &argv, OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error))
return FALSE;

if (!ostree_ensure_repo_writable (repo, error))
return FALSE;

if (argc < 2)
{
ot_util_usage_error (context, "At least one REF must be specified", error);
return FALSE;
}

if (opt_disable_fsync)
ostree_repo_set_disable_fsync (repo, TRUE);

if (opt_cache_dir &&
!ostree_repo_set_cache_dir (repo, AT_FDCWD, opt_cache_dir, cancellable, error))
return FALSE;

/* Read in the refs to search for remotes for. */
refs = g_ptr_array_new_with_free_func (NULL);

for (i = 1; i < argc; i++)
g_ptr_array_add (refs, argv[i]);

g_ptr_array_add (refs, NULL);

/* Run the operation. */
glnx_console_lock (&console);

if (console.is_tty)
progress = ostree_async_progress_new_and_connect (ostree_repo_pull_default_console_progress_changed, &console);

results = ostree_repo_find_remotes (repo,
(const gchar * const *) refs->pdata,
NULL /* no options */,
NULL /* default finders */, /* TODO: allow this to be customised */
NULL /* default context */,
progress, cancellable, error);

if (results == NULL)
return FALSE;

if (progress)
ostree_async_progress_finish (progress);

/* Print results. */
for (i = 0; results[i] != NULL; i++)
{
g_autofree gchar *uri = NULL;
g_autofree gchar *refs_string = NULL;
g_autofree gchar *last_modified_string = NULL;

uri = remote_get_uri (results[i]->remote);
refs_string = g_strjoinv ("\n - ", results[i]->refs);
last_modified_string = uint64_secs_to_iso8601 (results[i]->summary_last_modified);

g_print ("Result %" G_GSIZE_FORMAT ": %s\n"
" - Priority: %d\n"
" - Summary last modified: %s\n"
" - Refs:%s\n",
i, uri, results[i]->priority, last_modified_string, refs_string);
}

if (results[0] == NULL)
g_print ("No results.\n");

return TRUE;
}
1 change: 1 addition & 0 deletions src/ostree/ot-builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ BUILTINPROTO(checksum);
BUILTINPROTO(commit);
BUILTINPROTO(diff);
BUILTINPROTO(export);
BUILTINPROTO(find_remotes);
BUILTINPROTO(gpg_sign);
BUILTINPROTO(init);
BUILTINPROTO(log);
Expand Down

0 comments on commit df46a16

Please sign in to comment.