]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight_module.c
Fix file indexing
[netatalk.git] / etc / afpd / spotlight_module.c
index c1ab157b72036669fd50990c809b7b855253d180..7e6556f93ad93e89ea059f8747ac582fa516fcf8 100644 (file)
 #endif /* HAVE_CONFIG_H */
 
 #include <string.h>
+#include <locale.h>
 
+#include <gio/gio.h>
 #include <tracker-sparql.h>
+#include <libtracker-miner/tracker-miner.h>
 
 #include <atalk/util.h>
 #include <atalk/errchk.h>
 #include <atalk/logger.h>
+#include <atalk/unix.h>
 
 #include "spotlight.h"
 
+#define MAX_SL_RESULTS 20
+
 static TrackerSparqlConnection *connection;
+static TrackerMinerManager *manager;
 
-const char *tracker_to_unix_path(const char *path)
+char *tracker_to_unix_path(const char *uri)
 {
-    /* just skip 'file://' */
-    return path + 7;
+    EC_INIT;
+    GFile *f = NULL;
+    char *path = NULL;
+
+    EC_NULL_LOG( f = g_file_new_for_uri(uri) );
+    EC_NULL_LOG( path = g_file_get_path(f) );
+
+EC_CLEANUP:
+    if (f)
+        g_object_unref(f);
+    if (ret != 0)
+        return NULL;
+    return path;
 }
 
 static int sl_mod_init(void *p)
@@ -40,11 +58,23 @@ static int sl_mod_init(void *p)
     GError *error = NULL;
     const char *msg = p;
 
-    LOG(log_note, logtype_sl, "sl_mod_init: %s", msg);
-    setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
+    LOG(log_info, logtype_sl, "sl_mod_init: %s", msg);
 
     g_type_init();
+    setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
+    setenv("TRACKER_SPARQL_BACKEND", "bus", 1);
+
+#ifdef DEBUG
+    setenv("TRACKER_VERBOSITY", "3", 1);
+    dup2(type_configs[logtype_sl].fd, 1);
+    dup2(type_configs[logtype_sl].fd, 2);
+#endif
+
+    become_root();
     connection = tracker_sparql_connection_get(NULL, &error);
+    manager = tracker_miner_manager_new_full(FALSE, &error);
+    unbecome_root();
+
     if (!connection) {
         LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
             error ? error->message : "unknown error");
@@ -52,6 +82,12 @@ static int sl_mod_init(void *p)
         EC_FAIL;
     }
 
+    if (!manager) {
+        LOG(log_error, logtype_sl, "Couldn't connect to Tracker miner");
+        g_clear_error(&error);
+        EC_FAIL;
+    }
+
 EC_CLEANUP:
     EC_EXIT;
 }
@@ -64,22 +100,38 @@ static const gchar *map_spotlight_to_sparql_query(slq_t *slq)
 {
     EC_INIT;
     const gchar *sparql_query;
-    const char *sparql_query_format = "SELECT nie:url(?f) WHERE { ?f nie:url ?name FILTER regex(?name, \"%s\")}";
+    const char *sparql_query_format;
     const char *slquery = slq->slq_qstring;
     char *word, *p;
 
     LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", slquery);
 
-    EC_NULL_LOG( word = strstr(slquery, "*==") );
-    word += 4; /* skip *== and the left enclosing quote */
-    EC_NULL( word = dalloc_strdup(slq, word) );
-    /* Search asterisk */
-    EC_NULL_LOG( p = strchr(word, '*') );
-    *p = 0;
-
-    sparql_query = talloc_asprintf(slq, sparql_query_format, word);
-
-    LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
+    if ((word = strstr(slquery, "*==")) || (word = strstr(slquery, "kMDItemTextContent=="))) {
+        /* Full text search */
+        sparql_query_format = "SELECT nie:url(?uri) WHERE {?uri nie:url ?url . FILTER(fn:starts-with(?url, 'file://%s')) . ?uri fts:match '%s'}";
+        EC_NULL_LOG( word = strchr(word, '"') );
+        word++;
+        EC_NULL( word = dalloc_strdup(slq, word) );
+        EC_NULL_LOG( p = strchr(word, '"') );
+        *p = 0;
+        sparql_query = talloc_asprintf(slq, sparql_query_format, slq->slq_vol->v_path, word);
+        LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
+    } else if ((word = strstr(slquery, "kMDItemDisplayName=="))) {
+        /* Filename search */
+        sparql_query_format = "SELECT ?url WHERE { ?x nie:url ?url ; nfo:fileName ?name FILTER(fn:starts-with(?url, 'file://%s/') && regex(?name, '%s')) }";
+        EC_NULL_LOG( word = strchr(word, '"') );
+        word++;
+        EC_NULL( word = dalloc_strdup(slq, word) );
+        EC_NULL_LOG( p = strchr(word, '"') );
+        if (*(p-1) == '*')
+            *(p-1) = 0;
+        else
+            *p = 0;
+        sparql_query = talloc_asprintf(slq, sparql_query_format, slq->slq_vol->v_path, word);
+        LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
+    } else {
+        EC_FAIL_LOG("Can't parse SL query: '%s'", slquery);
+    }
 
 EC_CLEANUP:
     if (ret != 0)
@@ -124,13 +176,17 @@ static int sl_mod_start_search(void *p)
     tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
 #endif
 
+    become_root();
     slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
+    unbecome_root();
+
     if (error) {
         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
             error ? error->message : "unknown error");
         g_clear_error(&error);
         EC_FAIL;
     }
+    slq->slq_state = SLQ_STATE_RUNNING;
 
 EC_CLEANUP:
     EC_EXIT;
@@ -143,8 +199,10 @@ static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, co
     sl_nil_t nil = 0;
     int i, metacount;
 
-    if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0)
-        EC_FAIL;
+    if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
+        dalloc_add_copy(fm_array, &nil, sl_nil_t);
+        goto EC_CLEANUP;
+    }
 
     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
 
@@ -175,11 +233,14 @@ static int sl_mod_fetch_result(void *p)
     GError *error = NULL;
     int i = 0;
     cnid_t did, id;
-    const char *path;
+    const gchar *uri;
+    char *path;
     sl_cnids_t *cnids;
     sl_filemeta_t *fm;
     sl_array_t *fm_array;
+    sl_nil_t nil;
     uint64_t uint64;
+    gboolean qres, firstmatch = true;
 
     if (!slq->slq_tracker_cursor) {
         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
@@ -197,33 +258,130 @@ static int sl_mod_fetch_result(void *p)
     fm_array = talloc_zero(fm, sl_array_t);
     dalloc_add(fm, fm_array, sl_array_t);
 
-    /* For some reason the list of results always starts with a nil entry */
-    sl_nil_t nil;
-    dalloc_add_copy(fm_array, &nil, sl_nil_t);
+    LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
+
+    while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) {
+        become_root();
+        qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error);
+        unbecome_root();
+
+        if (!qres)
+            break;
+
+        if (firstmatch) {
+            /* For some reason the list of results always starts with a nil entry */
+            dalloc_add_copy(fm_array, &nil, sl_nil_t);
+            firstmatch = false;
+        }
+
+        become_root();
+        uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
+        unbecome_root();
+
+        EC_NULL_LOG( path = tracker_to_unix_path(uri) );
 
-    while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
-        EC_NULL_LOG( path = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL) );
-        path = tracker_to_unix_path(path);
-        LOG(log_debug, logtype_sl, "sl_mod_fetch_result: path(volpath: %s): \"%s\"", slq->slq_vol->v_path, path);
         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
-            continue;
+            goto loop_cleanup;
         }
-        LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
+        LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), path);
+
         uint64 = ntohl(id);
         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
         add_filemeta(slq->slq_reqinfo, fm_array, id, path);
-        //dalloc_add_copy(fm_array, &nil, sl_nil_t);
+
+    loop_cleanup:
+        g_free(path);
+        i++;
+   }
+
+    if (error) {
+        LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
+            error ? error->message : "unknown error");
+        g_clear_error (&error);
+        EC_FAIL;
     }
+
+    if (i < MAX_SL_RESULTS)
+        slq->slq_state = SLQ_STATE_DONE;
+
+    uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
+    dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
 
 EC_CLEANUP:
+    if (ret != 0) {
+        if (slq->slq_tracker_cursor) {
+            g_object_unref(slq->slq_tracker_cursor);
+            slq->slq_tracker_cursor = NULL;
+        }
+    }
+    EC_EXIT;
+}
+
+/* Free ressources allocated in this module */
+static int sl_mod_close_query(void *p)
+{
+    EC_INIT;
+    slq_t *slq = p;
+
+    if (slq->slq_tracker_cursor) {
+        g_object_unref(slq->slq_tracker_cursor);
+        slq->slq_tracker_cursor = NULL;
+    }
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
+static int sl_mod_error(void *p)
+{
+    EC_INIT;
+    slq_t *slq = p;
+
+    if (!slq)
+        goto EC_CLEANUP;
+
     if (slq->slq_tracker_cursor) {
         g_object_unref(slq->slq_tracker_cursor);
         slq->slq_tracker_cursor = NULL;
     }
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
+static int sl_mod_index_file(const void *p)
+{
+#ifdef HAVE_TRACKER_MINER
+    EC_INIT;
+    const char *f = p;
+
+    if (!f)
+        goto EC_CLEANUP;
+
+    GError *error = NULL;
+    GFile *file = NULL;
+
+    file = g_file_new_for_commandline_arg(f);
+
+    become_root();
+    tracker_miner_manager_index_file(manager, file, &error);
+    unbecome_root();
+
+    if (error)
+        LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): indexing failed", f);
+    else
+        LOG(log_debug, logtype_sl, "sl_mod_index_file(\"%s\"): indexing file was successful", f);
+
+EC_CLEANUP:
+    if (file)
+        g_object_unref(file);
     EC_EXIT;
+#else
+    return 0;
+#endif
 }
 
 struct sl_module_export sl_mod = {
@@ -231,5 +389,7 @@ struct sl_module_export sl_mod = {
     sl_mod_init,
     sl_mod_start_search,
     sl_mod_fetch_result,
-    NULL
+    sl_mod_close_query,
+    sl_mod_error,
+    sl_mod_index_file
 };