[tracker/sam/cli-create-database: 2/2] cli: Allow creating a DB with `tracker endpoint`



commit 2ad6f226742de827dca7af6e9ce34e2c44b9539d
Author: Sam Thursfield <sam afuera me uk>
Date:   Tue Feb 25 16:55:06 2020 +0100

    cli: Allow creating a DB with `tracker endpoint`
    
    This is convenient for local testing. Simply omit the --dbus-service
    option and the command will create a database and exit immediately,
    instead of serving the database over D-Bus until CTRL+C.
    
    Closes https://gitlab.gnome.org/GNOME/tracker/issues/177

 src/tracker/tracker-endpoint.c | 157 ++++++++++++++++++++++++-----------------
 1 file changed, 91 insertions(+), 66 deletions(-)
---
diff --git a/src/tracker/tracker-endpoint.c b/src/tracker/tracker-endpoint.c
index 95e1942ae..7d1f0eaff 100644
--- a/src/tracker/tracker-endpoint.c
+++ b/src/tracker/tracker-endpoint.c
@@ -70,6 +70,19 @@ static GOptionEntry entries[] = {
        { NULL }
 };
 
+#define TRACKER_ENDPOINT_ERROR tracker_endpoint_error_quark ()
+
+static GQuark
+tracker_endpoint_error_quark (void)
+{
+       return g_quark_from_static_string ("tracker-endpoint-error-quark");
+}
+
+typedef enum _TrackerEndpointError {
+       TRACKER_ENDPOINT_ERROR_COULD_NOT_OWN_NAME,
+       TRACKER_ENDPOINT_ERROR_NAME_LOST,
+} TrackerEndpointError;
+
 static gboolean
 sanity_check (void)
 {
@@ -78,11 +91,6 @@ sanity_check (void)
                return FALSE;
        }
 
-       if (!dbus_service) {
-               g_printerr ("%s\n", _("No endpoint information was provided"));
-               return FALSE;
-       }
-
        if (!!ontology_path == !!ontology_name) {
                /* TRANSLATORS: those are commandline arguments */
                g_printerr ("%s\n", _("One “ontology” or “ontology-path” option should be provided"));
@@ -118,13 +126,80 @@ name_lost_cb (GDBusConnection *connection,
        g_main_loop_quit (user_data);
 }
 
+static gboolean
+run_endpoint (TrackerSparqlConnection  *connection,
+              GError                  **error)
+{
+       TrackerEndpoint *endpoint = NULL;
+       GDBusConnection *dbus_connection;
+       g_autoptr(GMainLoop) main_loop;
+       GError *inner_error = NULL;
+
+       g_print (_("Creating endpoint at %s…"), dbus_service);
+       g_print ("\n");
+
+       if (system_bus) {
+               dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &inner_error);
+       } else {
+               dbus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &inner_error);
+       }
+
+       if (dbus_connection) {
+               endpoint = TRACKER_ENDPOINT (tracker_endpoint_dbus_new (connection,
+                                                                       dbus_connection,
+                                                                       NULL, NULL, &inner_error));
+       }
+
+       main_loop = g_main_loop_new (NULL, FALSE);
+
+       if (endpoint) {
+               g_bus_own_name_on_connection (dbus_connection,
+                                             dbus_service,
+                                             G_BUS_NAME_OWNER_FLAGS_NONE,
+                                             name_acquired_cb,
+                                             name_lost_cb,
+                                             main_loop, NULL);
+
+               g_main_loop_run (main_loop);
+       }
+
+       if (inner_error) {
+               g_propagate_error (error, inner_error);
+               return FALSE;
+       }
+
+       if (!name_owned) {
+               g_set_error_literal (error, TRACKER_ENDPOINT_ERROR,
+                                    TRACKER_ENDPOINT_ERROR_COULD_NOT_OWN_NAME,
+                                    _("Could not own DBus name"));
+               return FALSE;
+       }
+
+       g_print ("%s\n", _("Listening to SPARQL commands. Press Ctrl-C to stop."));
+
+       g_unix_signal_add (SIGINT, sigterm_cb, main_loop);
+       g_unix_signal_add (SIGTERM, sigterm_cb, main_loop);
+
+       g_main_loop_run (main_loop);
+
+       if (!name_owned) {
+               g_set_error_literal (error, TRACKER_ENDPOINT_ERROR,
+                                    TRACKER_ENDPOINT_ERROR_COULD_NOT_OWN_NAME,
+                                    _("DBus name lost"));
+               return FALSE;
+       }
+
+       /* Carriage return, so we paper over the ^C */
+       g_print ("\r%s\n", _("Closing connection…"));
+       g_clear_object (&endpoint);
+
+       return TRUE;
+}
+
 int
 tracker_endpoint (int argc, const char **argv)
 {
        TrackerSparqlConnection *connection;
-       TrackerEndpoint *endpoint = NULL;
-       GDBusConnection *dbus_connection;
-       GMainLoop *main_loop;
        GOptionContext *context;
        GError *error = NULL;
        GFile *database, *ontology = NULL;
@@ -172,74 +247,24 @@ tracker_endpoint (int argc, const char **argv)
                return EXIT_FAILURE;
        }
 
-       g_print (_("Creating endpoint at %s…"), dbus_service);
-       g_print ("\n");
+       if (dbus_service) {
+               run_endpoint (connection, &error);
 
-       if (system_bus) {
-               dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
+               if (error) {
+                       g_printerr ("%s\n", error->message);
+                       g_error_free (error);
+               }
        } else {
-               dbus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
-       }
-
-       if (dbus_connection) {
-               endpoint = TRACKER_ENDPOINT (tracker_endpoint_dbus_new (connection,
-                                                                       dbus_connection,
-                                                                       NULL, NULL, &error));
-       }
-
-       main_loop = g_main_loop_new (NULL, FALSE);
-
-       if (endpoint) {
-               g_bus_own_name_on_connection (dbus_connection,
-                                             dbus_service,
-                                             G_BUS_NAME_OWNER_FLAGS_NONE,
-                                             name_acquired_cb,
-                                             name_lost_cb,
-                                             main_loop, NULL);
-
-               g_main_loop_run (main_loop);
-       }
-
-       if (error) {
-               g_printerr ("%s\n", error->message);
-               g_error_free (error);
-               g_option_context_free (context);
-               g_main_loop_unref (main_loop);
-               return EXIT_FAILURE;
-       }
-
-       if (!name_owned) {
-               g_printerr ("%s\n", _("Could not own DBus name"));
-               g_option_context_free (context);
-               g_main_loop_unref (main_loop);
-               return EXIT_FAILURE;
-       }
-
-       g_print ("%s\n", _("Listening to SPARQL commands. Press Ctrl-C to stop."));
-
-       g_unix_signal_add (SIGINT, sigterm_cb, main_loop);
-       g_unix_signal_add (SIGTERM, sigterm_cb, main_loop);
-
-       g_main_loop_run (main_loop);
-
-       if (!name_owned) {
-               g_printerr ("%s\n", _("DBus name lost"));
-               g_option_context_free (context);
-               g_main_loop_unref (main_loop);
-               return EXIT_FAILURE;
+               g_print (_("New database created. Use the --dbus-service option to "
+                          "share this database on a message bus."));
        }
 
-       /* Carriage return, so we paper over the ^C */
-       g_print ("\r%s\n", _("Closing connection…"));
-       g_clear_object (&endpoint);
-
        if (connection) {
                tracker_sparql_connection_close (connection);
                g_clear_object (&connection);
        }
 
        g_option_context_free (context);
-       g_main_loop_unref (main_loop);
 
        return EXIT_SUCCESS;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]