]> arthur.barton.de Git - netatalk.git/commitdiff
Dispatch queries to Tracker and get results
authorFrank Lahm <franklahm@googlemail.com>
Thu, 20 Sep 2012 09:06:00 +0000 (11:06 +0200)
committerFrank Lahm <franklahm@googlemail.com>
Thu, 20 Sep 2012 09:06:00 +0000 (11:06 +0200)
etc/afpd/spotlight.c
etc/afpd/spotlight.h
etc/afpd/spotlight_marshalling.c
etc/afpd/spotlight_module.c

index 205a20e18b4c4e5ea9ae1ebb8b52105bfffd90a7..2742d002029884ad9584c18bbd1e84746ed51dd8 100644 (file)
@@ -23,6 +23,7 @@
 #include <errno.h>
 #include <stdbool.h>
 #include <inttypes.h>
+#include <time.h>
 
 #include <atalk/errchk.h>
 #include <atalk/util.h>
@@ -36,9 +37,9 @@
 
 #include "spotlight.h"
 
+static TALLOC_CTX *sl_ctx;
 static void *sl_module;
 static struct sl_module_export *sl_module_export;
-static q_t *sl_queries;
 
 /* Helper functions and stuff */
 static const char *neststrings[] = {
@@ -90,6 +91,51 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
     LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
 }
 
+/**************************************************************************************************
+ * Spotlight queries
+ **************************************************************************************************/
+
+static q_t *sl_queries;
+static slq_t *slq_active;
+
+/*!
+ * Add a query to the list of active queries
+ */
+static int slq_add(slq_t *slq)
+{
+    EC_INIT;
+
+    LOG(log_debug, logtype_sl, "slq_add(q: \"%s\"ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64 ")",
+        slq->slq_qstring, slq->slq_ctx1, slq->slq_ctx2);
+
+    if (slq_active)
+        talloc_free(slq_active);
+    slq_active = slq;
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
+static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
+{
+    EC_INIT;
+    slq_t *q;
+
+    LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
+        "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
+        ctx1, ctx2, slq_active->slq_ctx1, slq_active->slq_ctx2);
+
+    if ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2))
+        q = slq_active;
+    else
+        q = NULL;
+    
+EC_CLEANUP:
+    if (ret != 0)
+        q = NULL;
+    return q;
+}
+
 /**************************************************************************************************
  * Spotlight RPC functions
  **************************************************************************************************/
@@ -148,15 +194,67 @@ EC_CLEANUP:
 static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
 {
     EC_INIT;
-
     char **sl_query;
-    EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
+    uint64_t *uint64;
+    DALLOC_CTX *dalloc_ctx;
+    sl_array_t *array;
 
+    slq_t *slq = talloc_zero(sl_ctx, slq_t);
+
+    /* Allocate and initialize query object */
+    EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
     LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", *sl_query);
+    slq->slq_qstring = talloc_steal(slq, *sl_query);
+    slq->slq_state = SLQ_STATE_NEW;
+    slq->slq_time = time(NULL);
+    EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
+    slq->slq_ctx1 = *uint64;
+    EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
+    slq->slq_ctx2 = *uint64;
+    EC_NULL_LOG (dalloc_ctx = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
+    slq->slq_reqinfo = talloc_steal(slq, dalloc_ctx);
+    LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
+    dd_dump(slq->slq_reqinfo, 0);
+
+    (void)slq_add(slq);
+
+    /* Run the query */
+    sl_module_export->sl_mod_start_search(slq);
 
-    enqueue(sl_queries, query);
+EC_CLEANUP:
+    array = talloc_zero(reply, sl_array_t);
+    uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
+    dalloc_add(array, &sl_res, uint64_t);
+    dalloc_add(reply, array, sl_array_t);
+
+    EC_EXIT;
+}
+
+static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
+{
+    EC_INIT;
+    slq_t *slq;
+    uint64_t *uint64, ctx1, ctx2;
+    sl_array_t *array;
+    
+    /* Context */
+    EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
+    ctx1 = *uint64;
+    EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
+    ctx2 = *uint64;
+
+    /* Get query for context */
+    EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
+
+    /* Fetch Tracker results*/
+    sl_module_export->sl_mod_fetch_result(slq);
 
 EC_CLEANUP:
+    array = talloc_zero(reply, sl_array_t);
+    uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
+    dalloc_add(array, &sl_res, uint64_t);
+    dalloc_add(reply, array, sl_array_t);
+
     EC_EXIT;
 }
 
@@ -168,6 +266,7 @@ int sl_mod_load(const char *path)
 {
     EC_INIT;
 
+    sl_ctx = talloc_new(NULL);
     sl_queries = queue_init();
 
     if ((sl_module = mod_open(path)) == NULL) {
@@ -251,12 +350,14 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_
         if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
         } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
+            uint64_t *ctx1, *ctx2;
+            EC_NULL_LOG (ctx1 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
+            EC_NULL_LOG (ctx2 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
+            LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *ctx1, *ctx2);
+
             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
         } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
-            uint64_t *p;
-            if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
-                LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
-            }
+            EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
         }
 
         dd_dump(reply, 0);
@@ -299,7 +400,6 @@ int main(int argc, char **argv)
 
     LOG(log_info, logtype_sl, "Start");
 
-#if 0
     i = 2;
     dalloc_add(dd, &i, uint64_t);
 
@@ -323,35 +423,39 @@ int main(int argc, char **argv)
     dalloc_add(nested, &i, uint64_t);
     dalloc_add(dd, nested, DALLOC_CTX);
 
+#if 0
     /* test an allocated CNID array */
     uint64_t id = 16;
     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
-
     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
-
     cnids->ca_unkn1 = 1;
-    cnids->ca_unkn2 = 2;
-
     dalloc_add(cnids->ca_cnids, &id, uint64_t);
     dalloc_add(dd, cnids, sl_cnids_t);
-
 #endif
 
-#if 0
     /* Now the Spotlight types */
     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
     i = 0x1234;
     dalloc_add(sl_arrary, &i, uint64_t);
+    i = 0x5678;
+    dalloc_add(sl_arrary, &i, uint64_t);
 
+#if 0
     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
-    i = 0x5678;
+    i = 0xffff;
     dalloc_add(sl_dict, &i, uint64_t);
     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
+#endif
 
     dalloc_add(dd, sl_arrary, sl_array_t);
     dd_dump(dd, 0);
-#endif
 
+    uint64_t *int1, *int2;
+    EC_NULL_LOG (int1 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 0) );
+    EC_NULL_LOG (int2 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 1) );
+    LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *int1, *int2);
+
+#if 0
     /* now parse a real spotlight packet */
     if (argc > 1) {
         char ibuf[8192];
@@ -370,6 +474,7 @@ int main(int argc, char **argv)
         /* Now dump the whole thing */
         dd_dump(query, 0);
     }
+#endif
 
 #if 0
     /* packing  */
index 197f90246fab651bf2cc846a6909b0bdbdc629fd..adcac14d411e269dd0f7ce6b5bdd4ed9750fe107 100644 (file)
@@ -65,24 +65,34 @@ typedef bool           sl_bool_t;     /* a boolean, we avoid bool_t as it's a de
 typedef struct timeval sl_time_t;     /* a boolean, we avoid bool_t as it's a define for something else */
 typedef struct {
     char sl_uuid[16];
-}                      sl_uuid_t;     /* a UUID                                                         */
+}  sl_uuid_t;                         /* a UUID                                                         */
 typedef struct {
     uint16_t   ca_unkn1;
     uint32_t   ca_context;
     DALLOC_CTX *ca_cnids;
-}                      sl_cnids_t;    /* an array of CNID                                               */
+}  sl_cnids_t;                        /* an array of CNIDs                                              */
 
 /**************************************************************************************************
  * Some helper stuff dealing with queries
  **************************************************************************************************/
 
+/* Internal query state */
+typedef enum {
+    SLQ_STATE_NEW      = 1,           /* Query received from client                                     */
+    SLQ_STATE_RUNNING  = 2,           /* Query dispatched to Tracker                                    */
+    SLQ_STATE_DONE     = 3,           /* Tracker finished                                               */
+    SLQ_STATE_END      = 4            /* Query results returned to client                               */
+} slq_state_t;
+
+/* Internal query data structure */
 typedef struct {
-    time_t slq_time;            /* timestamp where we received this query */
-    uint64_t slq_ctx1;          /* client context 1 */
-    uint64_t slq_ctx2;          /* client context 2 */
-    DALLOC_CTX *slq_query;      /* the complete query as unmarshalled in openQuery */
-    const char *sql_qstring;    /* the Spotlight query string */
-    DALLOC_CTX *slq_reqinfo;    /* array with requested metadata */
+    slq_state_t    slq_state;         /* State                                                          */
+    time_t         slq_time;          /* timestamp where we received this query                         */
+    uint64_t       slq_ctx1;          /* client context 1                                               */
+    uint64_t       slq_ctx2;          /* client context 2                                               */
+    const char     *slq_qstring;      /* the Spotlight query string                                     */
+    DALLOC_CTX     *slq_reqinfo;      /* array with requested metadata                                  */
+    void           *slq_tracker_cursor; /* Tracker query result cursor                                  */
 } slq_t;
 
 /**************************************************************************************************
index d01d59d9b7af5eb62b0928106c447eda215667bd..f2ca4027a32c5ef1a8a22305d6f5c12471827027 100644 (file)
@@ -178,7 +178,7 @@ static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf,
         offset += 8;
 
         for (int i = 0; i < cnid_count; i++) {
-            SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
+             SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
             offset += 8;
         }
     }
index 13aa8301d83ce51e36f3db58b9fb87e06e630651..d6ccb0740eba3887b9cc457763f380ef67ddf6df 100644 (file)
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
+#include <string.h>
+
+#include <tracker-sparql.h>
+
 #include <atalk/util.h>
 #include <atalk/errchk.h>
 #include <atalk/logger.h>
 
 #include "spotlight.h"
 
-int sl_mod_init(void *p)
+static TrackerSparqlConnection *connection;
+
+static int sl_mod_init(void *p)
 {
     EC_INIT;
-
+    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);
+
+    g_type_init();
+    connection = tracker_sparql_connection_get(NULL, &error);
+    if (!connection) {
+        LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
+            error ? error->message : "unknown error");
+        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(?f) WHERE { ?f nie:url ?name FILTER regex(?name, \"%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 = talloc_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);
+
+EC_CLEANUP:
+    if (ret != 0)
+        sparql_query = NULL;
+    return sparql_query;
+}
+
+static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+    slq_t *slq = user_data;
+    TrackerSparqlCursor *cursor;
+    GError *error = NULL;
+
+    LOG(log_debug, logtype_sl, "tracker_cb");
+
+    cursor = tracker_sparql_connection_query_finish(connection, res, &error);
+
+    if (error) {
+        LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
+            error ? error->message : "unknown error");
+        g_clear_error(&error);
+        return;
+    }
+
+    slq->slq_tracker_cursor = cursor;
+}
+
+static int sl_mod_start_search(void *p)
+{
+    EC_INIT;
+    slq_t *slq = p; 
+    const 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) );
+    LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
+
+#if 0
+    /* Start the async query */
+    tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
+#endif
+
+    slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
+    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;
+    }
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
+static int sl_mod_fetch_result(void *p)
+{
+    EC_INIT;
+    slq_t *slq = p;
+    GError *error = NULL;
+
+    if (slq->slq_tracker_cursor) {
+        int i = 0;
+        while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
+            LOG(log_debug, logtype_sl, "Result [%d]: %s",
+                i++, tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL));
+        }
+    } else {
+        LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
+    }
 
 EC_CLEANUP:
+    if (slq->slq_tracker_cursor)
+        g_object_unref(slq->slq_tracker_cursor);
     EC_EXIT;
 }
 
 struct sl_module_export sl_mod = {
     SL_MODULE_VERSION,
     sl_mod_init,
-    NULL,
-    NULL,
+    sl_mod_start_search,
+    sl_mod_fetch_result,
     NULL
 };