Skip to content

Commit

Permalink
main: Factor out sysroot loading
Browse files Browse the repository at this point in the history
It can be useful to parse the options and initialize the sysroot without
actually loading it until later. Factor out the sysroot loading to a new
`ostree_admin_sysroot_load` and add a new
`OSTREE_ADMIN_BUILTIN_FLAG_NO_LOAD` flag to accommodate this.
  • Loading branch information
dbnicholson committed Aug 30, 2022
1 parent 93a6d7b commit e30a3b6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
83 changes: 49 additions & 34 deletions src/ostree/ot-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,41 +579,11 @@ on_sysroot_journal_msg (OstreeSysroot *sysroot,
}

gboolean
ostree_admin_option_context_parse (GOptionContext *context,
const GOptionEntry *main_entries,
int *argc,
char ***argv,
OstreeAdminBuiltinFlags flags,
OstreeCommandInvocation *invocation,
OstreeSysroot **out_sysroot,
GCancellable *cancellable,
GError **error)
ostree_admin_sysroot_load (OstreeSysroot *sysroot,
OstreeAdminBuiltinFlags flags,
GCancellable *cancellable,
GError **error)
{
/* Entries are listed in --help output in the order added. We add the
* main entries ourselves so that we can add the --sysroot entry first. */

g_option_context_add_main_entries (context, global_admin_entries, NULL);

if (!ostree_option_context_parse (context, main_entries, argc, argv,
invocation, NULL, cancellable, error))
return FALSE;

if (!opt_print_current_dir && (flags & OSTREE_ADMIN_BUILTIN_FLAG_NO_SYSROOT))
{
g_assert_null (out_sysroot);
/* Early return if no sysroot is requested */
return TRUE;
}

g_autoptr(GFile) sysroot_path = NULL;
if (opt_sysroot != NULL)
sysroot_path = g_file_new_for_path (opt_sysroot);

g_autoptr(OstreeSysroot) sysroot = ostree_sysroot_new (sysroot_path);
if (!ostree_sysroot_initialize (sysroot, error))
return FALSE;
g_signal_connect (sysroot, "journal-msg", G_CALLBACK (on_sysroot_journal_msg), NULL);

if ((flags & OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED) == 0)
{
/* If we're requested to lock the sysroot, first check if we're operating
Expand Down Expand Up @@ -657,6 +627,51 @@ ostree_admin_option_context_parse (GOptionContext *context,
}
}

return TRUE;
}

gboolean
ostree_admin_option_context_parse (GOptionContext *context,
const GOptionEntry *main_entries,
int *argc,
char ***argv,
OstreeAdminBuiltinFlags flags,
OstreeCommandInvocation *invocation,
OstreeSysroot **out_sysroot,
GCancellable *cancellable,
GError **error)
{
/* Entries are listed in --help output in the order added. We add the
* main entries ourselves so that we can add the --sysroot entry first. */

g_option_context_add_main_entries (context, global_admin_entries, NULL);

if (!ostree_option_context_parse (context, main_entries, argc, argv,
invocation, NULL, cancellable, error))
return FALSE;

if (!opt_print_current_dir && (flags & OSTREE_ADMIN_BUILTIN_FLAG_NO_SYSROOT))
{
g_assert_null (out_sysroot);
/* Early return if no sysroot is requested */
return TRUE;
}

g_autoptr(GFile) sysroot_path = NULL;
if (opt_sysroot != NULL)
sysroot_path = g_file_new_for_path (opt_sysroot);

g_autoptr(OstreeSysroot) sysroot = ostree_sysroot_new (sysroot_path);
if (!ostree_sysroot_initialize (sysroot, error))
return FALSE;
g_signal_connect (sysroot, "journal-msg", G_CALLBACK (on_sysroot_journal_msg), NULL);

if (opt_print_current_dir || (flags & OSTREE_ADMIN_BUILTIN_FLAG_NO_LOAD) == 0)
{
if (!ostree_admin_sysroot_load (sysroot, flags, cancellable, error))
return FALSE;
}

if (opt_print_current_dir)
{
g_autoptr(GPtrArray) deployments = NULL;
Expand Down
6 changes: 6 additions & 0 deletions src/ostree/ot-main.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef enum {
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER = (1 << 0),
OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED = (1 << 1),
OSTREE_ADMIN_BUILTIN_FLAG_NO_SYSROOT = (1 << 2),
OSTREE_ADMIN_BUILTIN_FLAG_NO_LOAD = (1 << 3),
} OstreeAdminBuiltinFlags;


Expand Down Expand Up @@ -91,6 +92,11 @@ gboolean ostree_admin_option_context_parse (GOptionContext *context,
OstreeSysroot **out_sysroot,
GCancellable *cancellable, GError **error);

gboolean ostree_admin_sysroot_load (OstreeSysroot *sysroot,
OstreeAdminBuiltinFlags flags,
GCancellable *cancellable,
GError **error);

gboolean ostree_ensure_repo_writable (OstreeRepo *repo, GError **error);

void ostree_print_gpg_verify_result (OstreeGpgVerifyResult *result);
Expand Down

0 comments on commit e30a3b6

Please sign in to comment.