[tracker/tracker-control-with-backup-and-restore] tracker-control: Allow backup/restore to take local file names



commit bcb02f43f392d746454d1dcf7a4d73983fb5f1e8
Author: Martyn Russell <martyn lanedo com>
Date:   Tue May 3 16:57:49 2011 +0100

    tracker-control: Allow backup/restore to take local file names
    
    This will now convert the arguments given into a real URI because a
    URI is expected in tracker-store.

 docs/manpages/tracker-control.1               |    8 +-
 src/tracker-control/tracker-control-general.c |   79 +++++++++++++++++++++---
 2 files changed, 73 insertions(+), 14 deletions(-)
---
diff --git a/docs/manpages/tracker-control.1 b/docs/manpages/tracker-control.1
index 5ce9aee..73b3525 100644
--- a/docs/manpages/tracker-control.1
+++ b/docs/manpages/tracker-control.1
@@ -85,16 +85,16 @@ configuration files.
 Starts all miners. This indirectly starts tracker-store too because it
 is needed for miners to operate properly.
 .TP
-.B \-b, \-\-backup=PATH
+.B \-b, \-\-backup=FILE
 Begins backing up the Tracker databases to the
-.B PATH
+.B FILE
 given.
 .TP
-.B \-o, \-\-restore=PATH
+.B \-o, \-\-restore=FILE
 Begins restoring a previous backup (see
 .B \-\-backup
 ) to the Tracker databases. The
-.B PATH
+.B FILE
 points to the location of the backup.
 
 .SH STATUS OPTIONS
diff --git a/src/tracker-control/tracker-control-general.c b/src/tracker-control/tracker-control-general.c
index a8756e3..e23b36a 100644
--- a/src/tracker-control/tracker-control-general.c
+++ b/src/tracker-control/tracker-control-general.c
@@ -92,12 +92,12 @@ static GOptionEntry entries[] = {
 	{ "start", 's', 0, G_OPTION_ARG_NONE, &start,
 	  N_("Starts miners (which indirectly starts tracker-store too)"),
 	  NULL },
-	{ "backup", 'b', 0, G_OPTION_ARG_STRING, &backup,
-	  N_("Backup databases to the location provided"),
-	  N_("PATH") },
-	{ "restore", 'o', 0, G_OPTION_ARG_STRING, &restore,
-	  N_("Restore databases from the location provided"),
-	  N_("PATH") },
+	{ "backup", 'b', 0, G_OPTION_ARG_FILENAME, &backup,
+	  N_("Backup databases to the file provided"),
+	  N_("FILE") },
+	{ "restore", 'o', 0, G_OPTION_ARG_FILENAME, &restore,
+	  N_("Restore databases from the file provided"),
+	  N_("FILE") },
 	{ NULL }
 };
 
@@ -246,6 +246,43 @@ term_option_arg_func (const gchar  *option_value,
 	return TRUE;
 }
 
+static gboolean
+has_valid_uri_scheme (const gchar *uri)
+{
+	const gchar *s;
+
+	s = uri;
+
+	if (!g_ascii_isalpha (*s)) {
+		return FALSE;
+	}
+
+	do {
+		s++;
+	} while (g_ascii_isalnum (*s) || *s == '+' || *s == '.' || *s == '-');
+
+	return (*s == ':');
+}
+
+static gchar *
+get_uri_from_arg (const gchar *arg)
+{
+	gchar *uri;
+
+	/* support both, URIs and local file paths */
+	if (has_valid_uri_scheme (arg)) {
+		uri = g_strdup (arg);
+	} else {
+		GFile *file;
+
+		file = g_file_new_for_commandline_arg (arg);
+		uri = g_file_get_uri (file);
+		g_object_unref (file);
+	}
+
+	return uri;
+}
+
 void
 tracker_control_general_run_default (void)
 {
@@ -554,9 +591,12 @@ tracker_control_general_run (void)
 		GDBusProxy *proxy;
 		GError *error = NULL;
 		GVariant *v;
+		gchar *uri;
+
+		uri = get_uri_from_arg (backup);
 
 		g_print ("%s\n", _("Backing up database"));
-		g_print ("  %s\n", backup);
+		g_print ("  %s\n", uri);
 
 		connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
@@ -564,6 +604,8 @@ tracker_control_general_run (void)
 			g_critical ("Could not connect to the D-Bus session bus, %s",
 			            error ? error->message : "no error given.");
 			g_clear_error (&error);
+			g_free (uri);
+
 			return EXIT_FAILURE;
 		}
 
@@ -580,12 +622,14 @@ tracker_control_general_run (void)
 			g_critical ("Could not create proxy on the D-Bus session bus, %s",
 			            error ? error->message : "no error given.");
 			g_clear_error (&error);
+			g_free (uri);
+
 			return EXIT_FAILURE;
 		}
 
 		v = g_dbus_proxy_call_sync (proxy,
 		                            "Save",
-		                            g_variant_new ("(s)", backup),
+		                            g_variant_new ("(s)", uri),
 		                            G_DBUS_CALL_FLAGS_NONE,
 		                            -1,
 		                            NULL,
@@ -599,12 +643,16 @@ tracker_control_general_run (void)
 			g_critical ("Could not backup database, %s",
 			            error ? error->message : "no error given.");
 			g_clear_error (&error);
+			g_free (uri);
+
 			return EXIT_FAILURE;
 		}
 
 		if (v) {
 			g_variant_unref (v);
 		}
+
+		g_free (uri);
 	}
 
 	if (restore) {
@@ -612,9 +660,12 @@ tracker_control_general_run (void)
 		GDBusProxy *proxy;
 		GError *error = NULL;
 		GVariant *v;
+		gchar *uri;
+
+		uri = get_uri_from_arg (restore);
 
 		g_print ("%s\n", _("Restoring database from backup"));
-		g_print ("  %s\n", restore);
+		g_print ("  %s\n", uri);
 
 		connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 
@@ -622,6 +673,8 @@ tracker_control_general_run (void)
 			g_critical ("Could not connect to the D-Bus session bus, %s",
 			            error ? error->message : "no error given.");
 			g_clear_error (&error);
+			g_free (uri);
+
 			return EXIT_FAILURE;
 		}
 
@@ -638,12 +691,14 @@ tracker_control_general_run (void)
 			g_critical ("Could not create proxy on the D-Bus session bus, %s",
 			            error ? error->message : "no error given.");
 			g_clear_error (&error);
+			g_free (uri);
+
 			return EXIT_FAILURE;
 		}
 
 		v = g_dbus_proxy_call_sync (proxy,
 		                            "Restore",
-		                            g_variant_new ("(s)", restore),
+		                            g_variant_new ("(s)", uri),
 		                            G_DBUS_CALL_FLAGS_NONE,
 		                            -1,
 		                            NULL,
@@ -657,12 +712,16 @@ tracker_control_general_run (void)
 			g_critical ("Could not restore database, %s",
 			            error ? error->message : "no error given.");
 			g_clear_error (&error);
+			g_free (uri);
+
 			return EXIT_FAILURE;
 		}
 
 		if (v) {
 			g_variant_unref (v);
 		}
+
+		g_free (uri);
 	}
 
 	return EXIT_SUCCESS;



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