main: Factor out sysroot loading
authorDan Nicholson <dbn@endlessos.org>
Tue, 30 Aug 2022 14:38:36 +0000 (08:38 -0600)
committerDan Nicholson <dbn@endlessos.org>
Tue, 30 Aug 2022 14:46:26 +0000 (08:46 -0600)
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.

src/ostree/ot-main.c
src/ostree/ot-main.h

index 7a4405a50d0f4c5589955ac220ba38b13ae08d91..770962a43aef92bafe2cda84891c2a9406dfdce7 100644 (file)
@@ -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
@@ -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;
index b369deb8743728b0211461582594824ecd867dce..e296501aea4e2cda5468eee44fe27e9ab4f8dac2 100644 (file)
@@ -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;
 
 
@@ -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);