[tracker-miners/wip/carlosg/cli-improvements: 35/47] tracker: Add --add/--remove subcommands to "tracker3 index"




commit 6ec7516929f45d45bc06e86d2078f94d03e8099b
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Aug 18 00:18:43 2020 +0200

    tracker: Add --add/--remove subcommands to "tracker3 index"
    
    This replaces the old DBus, one-shot calls to index files. These options
    handle settings changes instead. The --recursive command can be passed
    together with --add, to make search recursive.

 src/tracker/tracker-index.c | 226 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 197 insertions(+), 29 deletions(-)
---
diff --git a/src/tracker/tracker-index.c b/src/tracker/tracker-index.c
index 7c8507579..65a209c2a 100644
--- a/src/tracker/tracker-index.c
+++ b/src/tracker/tracker-index.c
@@ -36,11 +36,28 @@
 
 #include "tracker-color.h"
 #include "tracker-dbus.h"
-#include "tracker-miner-manager.h"
 
+static gboolean opt_add;
+static gboolean opt_remove;
+static gboolean opt_recursive;
 static gchar **filenames;
 
+#define INDEX_OPTIONS_ENABLED()          \
+       (opt_add || opt_remove || opt_recursive)
+
 static GOptionEntry entries[] = {
+       { "add", 'a', 0, G_OPTION_ARG_NONE, &opt_add,
+         /* TRANSLATORS: FILE is a commandline argument, named like that */
+         N_("Adds FILE as an indexed location"),
+         NULL },
+       { "remove", 'd', 0, G_OPTION_ARG_NONE, &opt_remove,
+         /* TRANSLATORS: FILE is a commandline argument, named like that */
+         N_("Removes FILE from indexed locations"),
+         NULL },
+       { "recursive", 'r', 0, G_OPTION_ARG_NONE, &opt_recursive,
+         /* TRANSLATORS: FILE is a commandline argument, named like that */
+         N_("Makes indexing recursive"),
+         NULL },
        { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames,
          N_("FILE"),
          N_("FILE") },
@@ -74,6 +91,20 @@ alias_to_path (const gchar *alias)
        return NULL;
 }
 
+static const gchar *
+path_to_alias (const gchar *path)
+{
+       guint i;
+
+       for (i = 0; i < G_N_ELEMENTS (special_dirs); i++) {
+               if (g_strcmp0 (path,
+                              g_get_user_special_dir (special_dirs[i].user_dir)) == 0)
+                       return special_dirs[i].symbol;
+       }
+
+       return NULL;
+}
+
 static const gchar *
 envvar_to_path (const gchar *envvar)
 {
@@ -86,42 +117,155 @@ envvar_to_path (const gchar *envvar)
        return NULL;
 }
 
-static gint
-index_or_reindex_file (void)
+static GStrv
+strv_add (GStrv        strv,
+          const gchar *elem)
 {
-       TrackerMinerManager *manager;
-       GError *error = NULL;
-       gchar **p;
-
-       /* Auto-start the miners here if we need to */
-       manager = tracker_miner_manager_new_full (TRUE, &error);
-       if (!manager) {
-               g_printerr (_("Could not (re)index file, manager could not be created, %s"),
-                           error ? error->message : _("No error given"));
-               g_printerr ("\n");
-               g_clear_error (&error);
-               return EXIT_FAILURE;
+       GArray *array;
+       gchar *str;
+
+       array = g_array_new (TRUE, TRUE, sizeof (char *));
+       g_array_append_vals (array, strv, g_strv_length (strv));
+       g_free (strv);
+
+       str = g_strdup (elem);
+       g_array_append_val (array, str);
+
+       return (GStrv) g_array_free (array, FALSE);
+}
+
+static GStrv
+strv_remove (GStrv        strv,
+             const gchar *elem)
+{
+       GArray *array;
+       guint i;
+
+       array = g_array_new (TRUE, TRUE, sizeof (char *));
+       g_array_append_vals (array, strv, g_strv_length (strv));
+
+       for (i = 0; i < array->len; i++) {
+               gchar *str = g_array_index (array, gchar *, i);
+
+               if (g_strcmp0 (str, elem) != 0)
+                       continue;
+
+               g_array_remove_index (array, i);
+               g_free (str);
        }
 
-       for (p = filenames; *p; p++) {
+       g_free (strv);
+
+       return (GStrv) g_array_free (array, FALSE);
+}
+
+static int
+index_add (void)
+{
+       gboolean handled = FALSE;
+       const gchar *setting_path;
+       GSettings *settings;
+       GStrv dirs;
+       guint i;
+
+       settings = g_settings_new ("org.freedesktop.Tracker3.Miner.Files");
+
+       if (opt_recursive)
+               setting_path = "index-recursive-directories";
+       else
+               setting_path = "index-single-directories";
+
+       for (i = 0; filenames[i]; i++) {
                GFile *file;
+               gchar *path;
+               const gchar *alias;
+
+               dirs = g_settings_get_strv (settings, setting_path);
 
-               file = g_file_new_for_commandline_arg (*p);
-               tracker_miner_manager_index_location (manager, file, NULL, TRACKER_INDEX_LOCATION_FLAGS_NONE, 
NULL, &error);
+               file = g_file_new_for_commandline_arg (filenames[i]);
+               path = g_file_get_path (file);
+               alias = path_to_alias (path);
+
+               if (g_strv_contains ((const gchar * const *) dirs, path) ||
+                   (alias && g_strv_contains ((const gchar * const *) dirs, alias))) {
+                       g_strfreev (dirs);
+                       handled = TRUE;
+                       continue;
+               }
 
-               if (error) {
-                       g_printerr ("%s: %s\n",
-                                   _("Could not (re)index file"),
-                                   error->message);
-                       g_error_free (error);
-                       return EXIT_FAILURE;
+               if (!g_file_test (path, G_FILE_TEST_IS_DIR)) {
+                       g_printerr (_("ā€œ%sā€ is not a directory"),
+                                   path);
+                       g_printerr ("\n");
+                       g_strfreev (dirs);
+                       continue;
                }
 
-               g_print ("%s\n", _("(Re)indexing file was successful"));
+               handled = TRUE;
+               dirs = strv_add (dirs, path);
+               g_settings_set_strv (settings, setting_path,
+                                    (const gchar * const *) dirs);
+
+               g_object_unref (file);
+               g_strfreev (dirs);
+               g_free (path);
+       }
+
+       g_settings_sync ();
+       g_object_unref (settings);
+
+       return handled ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+static void
+index_remove_setting (GSettings   *settings,
+                      const gchar *setting_path,
+                      const gchar *path)
+{
+       GStrv dirs;
+       const gchar *alias;
+
+       dirs = g_settings_get_strv (settings, setting_path);
+       alias = path_to_alias (path);
+
+       if (g_strv_contains ((const gchar * const *) dirs, path))
+               dirs = strv_remove (dirs, path);
+       if (alias && g_strv_contains ((const gchar * const *) dirs, alias))
+               dirs = strv_remove (dirs, path_to_alias (path));
+
+       g_settings_set_strv (settings, setting_path,
+                            (const gchar * const *) dirs);
+       g_strfreev (dirs);
+}
+
+static int
+index_remove (void)
+{
+       GSettings *settings;
+       guint i;
+
+       settings = g_settings_new ("org.freedesktop.Tracker3.Miner.Files");
+
+       for (i = 0; filenames[i]; i++) {
+               GFile *file;
+               gchar *path;
+
+               file = g_file_new_for_commandline_arg (filenames[i]);
+               path = g_file_get_path (file);
+
+               index_remove_setting (settings,
+                                     "index-recursive-directories",
+                                     path);
+               index_remove_setting (settings,
+                                     "index-single-directories",
+                                     path);
+
                g_object_unref (file);
+               g_free (path);
        }
 
-       g_object_unref (manager);
+       g_settings_sync ();
+       g_object_unref (settings);
 
        return EXIT_SUCCESS;
 }
@@ -129,7 +273,31 @@ index_or_reindex_file (void)
 static int
 index_run (void)
 {
-       return index_or_reindex_file ();
+       if (!opt_add && !opt_remove) {
+               /* TRANSLATORS: These are commandline options */
+               g_printerr ("%s\n", _("Either --add or --remove must be provided"));
+               return EXIT_FAILURE;
+       } else if (opt_add && opt_remove) {
+               /* TRANSLATORS: These are commandline options */
+               g_printerr ("%s\n", _("--add and --remove are mutually exclusive"));
+               return EXIT_FAILURE;
+       }
+
+       if (opt_add) {
+               return index_add ();
+       }
+
+       if (opt_recursive) {
+               /* TRANSLATORS: These are commandline options */
+               g_printerr ("%s\n", _("--recursive requires --add"));
+               return EXIT_FAILURE;
+       }
+
+       if (opt_remove) {
+               return index_remove ();
+       }
+
+       return EXIT_FAILURE;
 }
 
 static void
@@ -222,11 +390,11 @@ main (int argc, const char **argv)
 
        g_option_context_free (context);
 
-       if (!filenames) {
+       if (!filenames && !INDEX_OPTIONS_ENABLED ()) {
                return list_index_roots ();
        }
 
-       if (g_strv_length (filenames) < 1) {
+       if (!filenames || g_strv_length (filenames) < 1) {
                failed = _("Please specify one or more locations to index.");
                g_printerr ("%s\n", failed);
                return EXIT_FAILURE;


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