#include <errno.h>
#include <stdbool.h>
#include <inttypes.h>
+#include <time.h>
#include <atalk/errchk.h>
#include <atalk/util.h>
#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[] = {
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
**************************************************************************************************/
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;
}
{
EC_INIT;
+ sl_ctx = talloc_new(NULL);
sl_queries = queue_init();
if ((sl_module = mod_open(path)) == NULL) {
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);
LOG(log_info, logtype_sl, "Start");
-#if 0
i = 2;
dalloc_add(dd, &i, uint64_t);
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];
/* Now dump the whole thing */
dd_dump(query, 0);
}
+#endif
#if 0
/* packing */
#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
};