]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight_module.c
Set Tracker environment
[netatalk.git] / etc / afpd / spotlight_module.c
index 5ea1e95d1ec773f86c6fddcc6df6690f30db4bb4..dc0369226270cd2e66e50d3241c7c1ca90be9606 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"
+#include "spotlight_rawquery_parser.h"
+
+#define MAX_SL_RESULTS 20
 
 static TrackerSparqlConnection *connection;
+static TrackerMinerManager *manager;
 
-const char *tracker_to_unix_path(const char *path)
+static 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 +59,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, "Initializing Spotlight module");
 
     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,39 +83,16 @@ 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;
 }
 
-/*!
- * Return talloced query from query string
- * *=="query*"
- */
-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(?uri) WHERE {?uri fts:match '%s' . ?uri nie:url ?url FILTER(fn:starts-with(?url, 'file://%s')) }";
-    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[1] = 0;
-    sparql_query = talloc_asprintf(slq, sparql_query_format, word, slq->slq_vol->v_path);
-
-    LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
-
-EC_CLEANUP:
-    if (ret != 0)
-        sparql_query = NULL;
-    return sparql_query;
-}
 
 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
@@ -110,12 +118,13 @@ static int sl_mod_start_search(void *p)
 {
     EC_INIT;
     slq_t *slq = p; 
-    const gchar *sparql_query;
+    gchar *sparql_query;
     GError *error = NULL;
 
     LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
 
-    EC_NULL_LOG( sparql_query = map_spotlight_to_sparql_query(slq) );
+    EC_ZERO_LOGSTR( map_spotlight_to_sparql_query(slq, &sparql_query),
+                    "Mapping Spotlight query failed: \"%s\"", slq->slq_qstring );
     LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
 
 #if 0
@@ -123,7 +132,10 @@ 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");
@@ -143,8 +155,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 +189,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,31 +214,64 @@ 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;
+            LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", path);
+            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 (slq->slq_tracker_cursor) {
-        g_object_unref(slq->slq_tracker_cursor);
-        slq->slq_tracker_cursor = NULL;
+    if (ret != 0) {
+        if (slq->slq_tracker_cursor) {
+            g_object_unref(slq->slq_tracker_cursor);
+            slq->slq_tracker_cursor = NULL;
+        }
     }
     EC_EXIT;
 }
@@ -258,11 +308,44 @@ 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 = {
     SL_MODULE_VERSION,
     sl_mod_init,
     sl_mod_start_search,
     sl_mod_fetch_result,
     sl_mod_close_query,
-    sl_mod_error
+    sl_mod_error,
+    sl_mod_index_file
 };