From 071887b8a84c7111477d9e1fcc8be9ea2b0c47e5 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Tue, 5 Aug 2014 13:14:21 +0200 Subject: [PATCH] Spotlight: use async Tracker SPARQL API Signed-off-by: Ralph Boehme --- NEWS | 1 + etc/Makefile.am | 2 +- etc/afpd/Makefile.am | 15 +- etc/afpd/afp_dsi.c | 7 +- etc/afpd/auth.c | 2 + etc/afpd/file.c | 1 - etc/afpd/spotlight.c | 1211 ++++++++++++----- etc/afpd/spotlight_marshalling.c | 7 +- etc/netatalk/netatalk.c | 7 +- etc/spotlight/Makefile.am | 27 +- etc/spotlight/slmod_sparql.c | 36 +- .../{slmod_sparql_map.c => sparql_map.c} | 2 +- .../{slmod_sparql_map.h => sparql_map.h} | 0 ...{slmod_sparql_parser.c => sparql_parser.c} | 85 +- ...{slmod_sparql_parser.h => sparql_parser.h} | 14 +- ...{slmod_sparql_parser.y => sparql_parser.y} | 19 +- etc/spotlight/spotlight_rawquery_lexer.c | 71 +- etc/spotlight/spotlight_rawquery_lexer.l | 4 +- include/atalk/globals.h | 2 +- include/atalk/spotlight.h | 120 +- include/atalk/util.h | 4 + libatalk/util/netatalk_conf.c | 6 - macros/netatalk.m4 | 4 +- 23 files changed, 1076 insertions(+), 571 deletions(-) rename etc/spotlight/{slmod_sparql_map.c => sparql_map.c} (99%) rename etc/spotlight/{slmod_sparql_map.h => sparql_map.h} (100%) rename etc/spotlight/{slmod_sparql_parser.c => sparql_parser.c} (97%) rename etc/spotlight/{slmod_sparql_parser.h => sparql_parser.h} (92%) rename etc/spotlight/{slmod_sparql_parser.y => sparql_parser.y} (95%) diff --git a/NEWS b/NEWS index aeae4c16..f332611f 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ Changes in 3.1.4 "fce ignore names" and "fce notify script" * UPD: afpd: check for modified included config file, FR #95. * UPD: libatalk: logger: remove flood protection and allocate messages +* UPD: Spotlight: use async Tracker SPARQL API Changes in 3.1.3 ================ diff --git a/etc/Makefile.am b/etc/Makefile.am index 6877a761..959b864d 100644 --- a/etc/Makefile.am +++ b/etc/Makefile.am @@ -1,3 +1,3 @@ # Makefile.am for etc/ -SUBDIRS = afpd cnid_dbd netatalk spotlight uams +SUBDIRS = spotlight afpd cnid_dbd netatalk uams diff --git a/etc/afpd/Makefile.am b/etc/afpd/Makefile.am index 77ed9d64..724cb52f 100644 --- a/etc/afpd/Makefile.am +++ b/etc/afpd/Makefile.am @@ -7,7 +7,6 @@ CLEANFILES = DISTCLEANFILES = sbin_PROGRAMS = afpd -noinst_PROGRAMS = hash spot afpd_SOURCES = \ afp_avahi.c \ @@ -39,7 +38,6 @@ afpd_SOURCES = \ nfsquota.c \ ofork.c \ quota.c \ - spotlight.c \ spotlight_marshalling.c \ status.c \ switch.c \ @@ -64,6 +62,12 @@ afpd_CFLAGS = \ -D_PATH_CONFDIR=\"$(pkgconfdir)/\" \ -D_PATH_STATEDIR='"$(localstatedir)/netatalk/"' +if HAVE_TRACKER +afpd_SOURCES += spotlight.c +afpd_LDADD += $(top_builddir)/etc/spotlight/libspotlight.la +afpd_CFLAGS += @TRACKER_CFLAGS@ +endif + if HAVE_ACLS afpd_SOURCES += acls.c endif @@ -98,10 +102,3 @@ noinst_HEADERS = auth.h afp_config.h desktop.h directory.h fce_api_internal.h fi filedir.h fork.h icon.h mangle.h misc.h status.h switch.h \ uam_auth.h uid.h unix.h volume.h hash.h acls.h acl_mappings.h extattrs.h \ dircache.h afp_zeroconf.h afp_avahi.h afp_mdns.h afpstats.h afpstats_obj.h - -hash_SOURCES = hash.c -hash_CFLAGS = -DKAZLIB_TEST_MAIN -I$(top_srcdir)/include - -spot_SOURCES = spotlight.c spotlight_marshalling.c -spot_CFLAGS = -DSPOT_TEST_MAIN -spot_LDADD = $(top_builddir)/libatalk/libatalk.la diff --git a/etc/afpd/afp_dsi.c b/etc/afpd/afp_dsi.c index 124db47a..98404ce2 100644 --- a/etc/afpd/afp_dsi.c +++ b/etc/afpd/afp_dsi.c @@ -493,9 +493,12 @@ void afp_over_dsi(AFPObj *obj) int flag = 1; setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag)); +#ifdef HAVE_TRACKER /* Initialize Spotlight */ - if ((obj->options.flags & OPTION_SPOTLIGHT) && (obj->options.slmod_path)) - sl_mod_load(obj); + if (obj->options.flags & OPTION_SPOTLIGHT) { + spotlight_init(obj); + } +#endif ipc_child_state(obj, DSI_RUNNING); diff --git a/etc/afpd/auth.c b/etc/afpd/auth.c index 7a7c41e5..dc4dd8bc 100644 --- a/etc/afpd/auth.c +++ b/etc/afpd/auth.c @@ -187,7 +187,9 @@ static int set_auth_switch(const AFPObj *obj, int expired) case 31: uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL); uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL); +#ifdef HAVE_TRACKER uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_spotlight_rpc, NULL); +#endif uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL); case 30: diff --git a/etc/afpd/file.c b/etc/afpd/file.c index 881640da..6d5cb4fc 100644 --- a/etc/afpd/file.c +++ b/etc/afpd/file.c @@ -765,7 +765,6 @@ createfile_iderr: ad_flush(&ad); ad_close(&ad, ADFLAGS_DF|ADFLAGS_HF ); fce_register(obj, FCE_FILE_CREATE, fullpathname(upath), NULL); - sl_index_file(path); curdir->d_offcnt++; setvoltime(obj, vol ); diff --git a/etc/afpd/spotlight.c b/etc/afpd/spotlight.c index 56546244..621ef383 100644 --- a/etc/afpd/spotlight.c +++ b/etc/afpd/spotlight.c @@ -1,5 +1,5 @@ /* - Copyright (c) 2012 Frank Lahm + Copyright (c) 2012-2014 Ralph Boehme This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -40,152 +40,659 @@ #include #include "directory.h" +#include "etc/spotlight/sparql_parser.h" -static TALLOC_CTX *sl_ctx; -static void *sl_module; -static struct sl_module_export *sl_module_export; - -/* Helper functions and stuff */ -static const char *neststrings[] = { - "", - "\t", - "\t\t", - "\t\t\t", - "\t\t\t\t", - "\t\t\t\t\t", - "\t\t\t\t\t\t", -}; - -static int dd_dump(DALLOC_CTX *dd, int nestinglevel) +#define MAX_SL_RESULTS 20 + +static char *tracker_to_unix_path(TALLOC_CTX *mem_ctx, const char *uri); +static int cnid_comp_fn(const void *p1, const void *p2); +static bool create_result_handle(slq_t *slq); +static bool add_filemeta(sl_array_t *reqinfo, + sl_array_t *fm_array, + const char *path, + const struct stat *sp); + +/************************************************ + * Misc utility functions + ************************************************/ + +static char *tab_level(TALLOC_CTX *mem_ctx, int level) { - const char *type; + int i; + char *string = talloc_array(mem_ctx, char, level + 1); + + for (i = 0; i < level; i++) { + string[i] = '\t'; + } - LOG(log_debug, logtype_sl, "%s%s(#%d): {", - neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array)); + string[i] = '\0'; + return string; +} - for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) { +static char *dd_dump(DALLOC_CTX *dd, int nestinglevel) +{ + const char *type; + int n; + uint64_t i; + sl_bool_t bl; + sl_time_t t; + struct tm *tm; + char datestring[256]; + sl_cnids_t cnids; + char *logstring, *nested_logstring; + char *tab_string1, *tab_string2; + + tab_string1 = tab_level(dd, nestinglevel); + tab_string2 = tab_level(dd, nestinglevel + 1); + if (tab_string1 == NULL || tab_string2 == NULL) { + return NULL; + } - type = talloc_get_name(dd->dd_talloc_array[n]); + logstring = talloc_asprintf(dd, + "%s%s(#%lu): {\n", + tab_string1, + talloc_get_name(dd), + talloc_array_length(dd->dd_talloc_array)); - if (STRCMP(type, ==, "DALLOC_CTX") - || STRCMP(type, ==, "sl_array_t") - || STRCMP(type, ==, "sl_filemeta_t") - || STRCMP(type, ==, "sl_dict_t")) { - dd_dump(dd->dd_talloc_array[n], nestinglevel + 1); - } else if (STRCMP(type, ==, "uint64_t")) { - uint64_t i; + for (n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) { + type = talloc_get_name(dd->dd_talloc_array[n]); + if (strequal(type, "DALLOC_CTX") + || strequal(type, "sl_array_t") + || strequal(type, "sl_filemeta_t") + || strequal(type, "sl_dict_t")) { + nested_logstring = dd_dump(dd->dd_talloc_array[n], + nestinglevel + 1); + if (!nested_logstring) { + return NULL; + } + logstring = talloc_strdup_append(logstring, + nested_logstring); + if (!logstring) { + return NULL; + } + } else if (strequal(type, "uint64_t")) { memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t)); - LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i); - } else if (STRCMP(type, ==, "char *")) { - LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], (char *)dd->dd_talloc_array[n]); - } else if (STRCMP(type, ==, "sl_bool_t")) { - sl_bool_t bl; + logstring = talloc_asprintf_append( + logstring, + "%suint64_t: 0x%04" PRIx64 "\n", + tab_string2, i); + if (!logstring) { + return NULL; + } + } else if (strequal(type, "char *")) { + logstring = talloc_asprintf_append( + logstring, + "%sstring: %s\n", + tab_string2, + (char *)dd->dd_talloc_array[n]); + if (!logstring) { + return NULL; + } + } else if (strequal(type, "smb_ucs2_t *")) { + logstring = talloc_asprintf_append( + logstring, + "%sUTF16-string: %s\n", + tab_string2, + (char *)dd->dd_talloc_array[n]); + if (!logstring) { + return NULL; + } + } else if (strequal(type, "sl_bool_t")) { memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t)); - LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false"); - } else if (STRCMP(type, ==, "sl_nil_t")) { - LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]); - } else if (STRCMP(type, ==, "sl_time_t")) { - sl_time_t t; - struct tm *tm; - char datestring[256]; + logstring = talloc_asprintf_append( + logstring, + "%sbool: %s\n", + tab_string2, + bl ? "true" : "false"); + if (!logstring) { + return NULL; + } + } else if (strequal(type, "sl_nil_t")) { + logstring = talloc_asprintf_append( + logstring, + "%snil\n", + tab_string2); + if (!logstring) { + return NULL; + } + } else if (strequal(type, "sl_time_t")) { memcpy(&t, dd->dd_talloc_array[n], sizeof(sl_time_t)); tm = localtime(&t.tv_sec); - strftime(datestring, sizeof(datestring), "%Y-%m-%d %H:%M:%S", tm); - LOG(log_debug, logtype_sl, "%ssl_time_t: %s.%06d", neststrings[nestinglevel + 1], datestring, t.tv_usec); - } else if (STRCMP(type, ==, "sl_cnids_t")) { - sl_cnids_t cnids; + strftime(datestring, + sizeof(datestring), + "%Y-%m-%d %H:%M:%S", tm); + logstring = talloc_asprintf_append( + logstring, + "%ssl_time_t: %s.%06lu\n", + tab_string2, + datestring, + (unsigned long)t.tv_usec); + if (!logstring) { + return NULL; + } + } else if (strequal(type, "sl_cnids_t")) { memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t)); - LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32, - neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context); - if (cnids.ca_cnids) - dd_dump(cnids.ca_cnids, nestinglevel + 2); + logstring = talloc_asprintf_append( + logstring, + "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32 "\n", + tab_string2, + cnids.ca_unkn1, + cnids.ca_context); + if (!logstring) { + return NULL; + } + if (cnids.ca_cnids) { + nested_logstring = dd_dump( + cnids.ca_cnids, + nestinglevel + 2); + if (!nested_logstring) { + return NULL; + } + logstring = talloc_strdup_append(logstring, + nested_logstring); + if (!logstring) { + return NULL; + } + } + } else { + logstring = talloc_asprintf_append( + logstring, + "%stype: %s\n", + tab_string2, + type); + if (!logstring) { + return NULL; + } + } + } + logstring = talloc_asprintf_append(logstring, + "%s}\n", + tab_string1); + if (!logstring) { + return NULL; + } + return logstring; +} + +static int cnid_comp_fn(const void *p1, const void *p2) +{ + const uint64_t *cnid1 = p1, *cnid2 = p2; + if (*cnid1 == *cnid2) { + return 0; + } + if (*cnid1 < *cnid2) { + return -1; + } + return 1; +} + +static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p) +{ + EC_INIT; + uint64_t *cnids = NULL; + + EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) ); + + for (int i = 0; i < talloc_array_length(p); i++) { + memcpy(&cnids[i], p->dd_talloc_array[i], sizeof(uint64_t)); + } + qsort(cnids, talloc_array_length(p), sizeof(uint64_t), cnid_comp_fn); + + slq->slq_cnids = cnids; + slq->slq_cnids_num = talloc_array_length(p); + +EC_CLEANUP: + if (ret != 0) { + if (cnids) + talloc_free(cnids); + } + EC_EXIT; +} + +static char *tracker_to_unix_path(TALLOC_CTX *mem_ctx, const char *uri) +{ + GFile *f; + char *path; + char *talloc_path = NULL; + + f = g_file_new_for_uri(uri); + if (!f) { + return NULL; + } + + path = g_file_get_path(f); + g_object_unref(f); + + if (!path) { + return NULL; + } + + talloc_path = talloc_strdup(mem_ctx, path); + g_free(path); + + return talloc_path; +} + +/** + * Add requested metadata for a query result element + * + * This could be rewritten to something more sophisticated like + * querying metadata from Tracker. + * + * If path or sp is NULL, simply add nil values for all attributes. + **/ +static bool add_filemeta(sl_array_t *reqinfo, + sl_array_t *fm_array, + const char *path, + const struct stat *sp) +{ + sl_array_t *meta; + sl_nil_t nil; + int i, metacount; + uint64_t uint64var; + sl_time_t sl_time; + char *p, *name; + + metacount = talloc_array_length(reqinfo->dd_talloc_array); + if (metacount == 0 || path == NULL || sp == NULL) { + dalloc_add_copy(fm_array, &nil, sl_nil_t); + return true; + } + + meta = talloc_zero(fm_array, sl_array_t); + + for (i = 0; i < metacount; i++) { + if (strequal(reqinfo->dd_talloc_array[i], "kMDItemDisplayName") + || strequal(reqinfo->dd_talloc_array[i], "kMDItemFSName")) { + if ((p = strrchr(path, '/'))) { + name = dalloc_strdup(meta, p + 1); + dalloc_add(meta, name, "char *"); + } + } else if (strequal(reqinfo->dd_talloc_array[i], + "kMDItemPath")) { + name = dalloc_strdup(meta, path); + dalloc_add(meta, name, "char *"); + } else if (strequal(reqinfo->dd_talloc_array[i], + "kMDItemFSSize")) { + uint64var = sp->st_size; + dalloc_add_copy(meta, &uint64var, uint64_t); + } else if (strequal(reqinfo->dd_talloc_array[i], + "kMDItemFSOwnerUserID")) { + uint64var = sp->st_uid; + dalloc_add_copy(meta, &uint64var, uint64_t); + } else if (strequal(reqinfo->dd_talloc_array[i], + "kMDItemFSOwnerGroupID")) { + uint64var = sp->st_gid; + dalloc_add_copy(meta, &uint64var, uint64_t); + } else if (strequal(reqinfo->dd_talloc_array[i], + "kMDItemFSContentChangeDate")) { + sl_time.tv_sec = sp->st_mtime; + dalloc_add_copy(meta, &sl_time, sl_time_t); } else { - LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type); + dalloc_add_copy(meta, &nil, sl_nil_t); } } - LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]); + + dalloc_add(fm_array, meta, sl_array_t); + return true; +} + +/** + * Allocate result handle used in the async Tracker cursor result + * handler for storing results + **/ +static bool create_result_handle(slq_t *slq) +{ + sl_nil_t nil = 0; + struct sl_rslts *query_results; + + if (slq->query_results) { + LOG(log_error, logtype_sl,"unexpected existing result handle"); + return false; + } + + query_results = talloc_zero(slq, struct sl_rslts); + + /* CNIDs */ + query_results->cnids = talloc_zero(query_results, sl_cnids_t); + if (query_results->cnids == NULL) { + return false; + } + query_results->cnids->ca_cnids = talloc_zero(query_results->cnids, + DALLOC_CTX); + if (query_results->cnids->ca_cnids == NULL) { + return false; + } + + query_results->cnids->ca_unkn1 = 0xadd; + query_results->cnids->ca_context = slq->slq_ctx2; + + /* FileMeta */ + query_results->fm_array = talloc_zero(query_results, sl_array_t); + if (query_results->fm_array == NULL) { + return false; + } + + /* For some reason the list of results always starts with a nil entry */ + dalloc_add_copy(query_results->fm_array, &nil, sl_nil_t); + + slq->query_results = query_results; + return true; +} + +static bool add_results(sl_array_t *array, slq_t *slq) +{ + sl_filemeta_t *fm; + uint64_t status = 0; + + /* FileMeta */ + fm = talloc_zero(array, sl_filemeta_t); + if (!fm) { + return false; + } + + dalloc_add_copy(array, &status, uint64_t); + dalloc_add(array, slq->query_results->cnids, sl_cnids_t); + if (slq->query_results->num_results > 0) { + dalloc_add(fm, slq->query_results->fm_array, sl_array_t); + } + dalloc_add(array, fm, sl_filemeta_t); + + /* This ensure the results get clean up after been sent to the client */ + talloc_steal(array, slq->query_results); + slq->query_results = NULL; + + if (!create_result_handle(slq)) { + LOG(log_error, logtype_sl, "couldn't add result handle"); + slq->slq_state = SLQ_STATE_ERROR; + return false; + } + + return true; } -#ifndef SPOT_TEST_MAIN -/************************************************************************************************** +/****************************************************************************** * Spotlight queries - **************************************************************************************************/ + ******************************************************************************/ static ATALK_LIST_HEAD(sl_queries); +static ATALK_LIST_HEAD(sl_cancelled_queries); -/*! +/** * Add a query to the list of active queries - */ -static int slq_add(slq_t *slq) + **/ +static void slq_add(slq_t *slq) { list_add(&(slq->slq_list), &sl_queries); - return 0; } -static int slq_remove(slq_t *slq) +/** + * Add a query to the list of active queries + **/ +static void slq_cancelled_add(slq_t *slq) +{ + list_add(&(slq->slq_list), &sl_cancelled_queries); +} + +/** + * Remove a query from the active list + **/ +static void slq_remove(slq_t *slq) { - EC_INIT; struct list_head *p; slq_t *q = NULL; list_for_each(p, &sl_queries) { q = list_entry(p, slq_t, slq_list); - if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) { + if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) { list_del(p); break; } - q = NULL; - } - - if (q == NULL) { - /* The SL query 'slq' was not found in the list, this is not supposed to happen! */ - LOG(log_warning, logtype_sl, "slq_remove: slq not in active query list"); } -EC_CLEANUP: - EC_EXIT; + return; } static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2) { - EC_INIT; slq_t *q = NULL; struct list_head *p; list_for_each(p, &sl_queries) { q = list_entry(p, slq_t, slq_list); - - LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64 - "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, - ctx1, ctx2, q->slq_ctx1, q->slq_ctx2); - - if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) { + if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) { break; } q = NULL; } -EC_CLEANUP: - if (ret != 0) - q = NULL; return q; } -/* Error handling for queries */ -static void slq_error(slq_t *slq) +/** + * Remove a query from the active queue and free it + **/ +static void slq_destroy(slq_t *slq) { - if (!slq) + if (slq == NULL) { return; - sl_module_export->sl_mod_error(slq); + } slq_remove(slq); talloc_free(slq); } -/************************************************************************************************** +/** + * Cancel a query + **/ +static void slq_cancel(slq_t *slq) +{ + g_cancellable_cancel(slq->cancellable); + slq->slq_state = SLQ_STATE_CANCEL_PENDING; + slq_remove(slq); + slq_cancelled_add(slq); +} + +/** + * talloc destructor cb + **/ +static int slq_free_cb(slq_t *slq) +{ + if (slq->tracker_cursor) { + g_object_unref(slq->tracker_cursor); + } + if (slq->cancellable) { + g_object_unref(slq->cancellable); + } + return 0; +} + +/** + * Free all cancelled queries + **/ +static void slq_cancelled_cleanup(void) +{ + struct list_head *p; + slq_t *q = NULL; + + list_for_each(p, &sl_cancelled_queries) { + q = list_entry(p, slq_t, slq_list); + list_del(p); + if (q->slq_state != SLQ_STATE_CANCELLED) { + LOG(log_error, logtype_sl, "unexpected state: %d", q->slq_state); + } + talloc_free(q); + } + + return; +} + +/************************************************ + * Tracker async callbacks + ************************************************/ + +static void tracker_con_cb(GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + struct sl_ctx *sl_ctx = user_data; + GError *error = NULL; + + sl_ctx->tracker_con = tracker_sparql_connection_get_finish(res, + &error); + if (error) { + LOG(log_error, logtype_sl, "Could not connect to Tracker: %s", + error->message); + sl_ctx->tracker_con = NULL; + g_error_free(error); + return; + } + + LOG(log_info, logtype_sl, "connected to Tracker"); +} + +static void tracker_cursor_cb(GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + GError *error = NULL; + slq_t *slq = user_data; + gboolean more_results; + const gchar *uri; + char *path; + int result; + struct stat sb; + uint64_t uint64var; + bool ok; + cnid_t did, id; + + if (g_cancellable_is_cancelled(slq->cancellable)) { + slq->slq_state = SLQ_STATE_CANCELLED; + return; + } + + more_results = tracker_sparql_cursor_next_finish(slq->tracker_cursor, + res, + &error); + + if (error) { + LOG(log_error, logtype_sl, "Tracker cursor: %s", error->message); + g_error_free(error); + slq->slq_state = SLQ_STATE_ERROR; + return; + } + + if (!more_results) { + slq->slq_state = SLQ_STATE_DONE; + return; + } + + uri = tracker_sparql_cursor_get_string(slq->tracker_cursor, 0, NULL); + if (uri == NULL) { + /* + * Not sure how this could happen if + * tracker_sparql_cursor_next_finish() returns true, but I've + * seen it. + */ + LOG(log_debug, logtype_sl, "no URI for result"); + goto exit; + } + + path = tracker_to_unix_path(slq->query_results, uri); + if (path == NULL) { + LOG(log_error, logtype_sl, "error converting Tracker URI: %s", uri); + slq->slq_state = SLQ_STATE_ERROR; + return; + } + + result = access(path, R_OK); + if (result != 0) { + goto exit; + } + + id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did); + if (id == CNID_INVALID) { + LOG(log_error, logtype_sl, "cnid_for_path error: %s", path); + goto exit; + } + uint64var = ntohl(id); + + if (slq->slq_cnids) { + ok = bsearch(&uint64var, slq->slq_cnids, slq->slq_cnids_num, + sizeof(uint64_t), cnid_comp_fn); + if (!ok) { + goto exit; + } + } + + dalloc_add_copy(slq->query_results->cnids->ca_cnids, + &uint64var, uint64_t); + ok = add_filemeta(slq->slq_reqinfo, slq->query_results->fm_array, + path, &sb); + if (!ok) { + LOG(log_error, logtype_sl, "add_filemeta error"); + slq->slq_state = SLQ_STATE_ERROR; + return; + } + + slq->query_results->num_results++; + +exit: + if (slq->query_results->num_results < MAX_SL_RESULTS) { + tracker_sparql_cursor_next_async(slq->tracker_cursor, + slq->cancellable, + tracker_cursor_cb, + slq); + } +} + +static void tracker_query_cb(GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + bool ok; + GError *error = NULL; + slq_t *slq = user_data; + + if (g_cancellable_is_cancelled(slq->cancellable)) { + slq->slq_state = SLQ_STATE_CANCELLED; + return; + } + + slq->tracker_cursor = tracker_sparql_connection_query_finish( + TRACKER_SPARQL_CONNECTION(object), + res, + &error); + + if (error) { + slq->slq_state = SLQ_STATE_ERROR; + LOG(log_error, logtype_sl, "Tracker query error: %s", error->message); + g_error_free(error); + return; + } + + slq->slq_state = SLQ_STATE_RESULTS; + + ok = create_result_handle(slq); + if (!ok) { + LOG(log_error, logtype_sl, "create_result_handle error"); + slq->slq_state = SLQ_STATE_ERROR; + return; + } + + tracker_sparql_cursor_next_async(slq->tracker_cursor, + slq->slq_obj->sl_ctx->cancellable, + tracker_cursor_cb, + slq); +} + +/******************************************************************************* * Spotlight RPC functions - **************************************************************************************************/ + ******************************************************************************/ -static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) +static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, + const DALLOC_CTX *query, + DALLOC_CTX *reply, + const struct vol *v) { EC_INIT; @@ -194,9 +701,9 @@ static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX sl_array_t *array; sl_uuid_t uuid; - if (!v->v_uuid) - EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname); - + if (!v->v_uuid) { + EC_FAIL_LOG("missing UUID for volume: %s", v->v_localname); + } dict = talloc_zero(reply, sl_dict_t); /* key/val 1 */ @@ -236,39 +743,10 @@ EC_CLEANUP: EC_EXIT; } -static int cnid_comp_fn(const void *p1, const void *p2) -{ - const uint64_t *cnid1 = p1, *cnid2 = p2; - if (*cnid1 == *cnid2) - return 0; - if (*cnid1 < *cnid2) - return -1; - else - return 1; -} - -static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p) -{ - EC_INIT; - uint64_t *cnids = NULL; - - EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) ); - for (int i = 0; i < talloc_array_length(p); i++) - memcpy(&cnids[i], p->dd_talloc_array[i], sizeof(uint64_t)); - qsort(cnids, talloc_array_length(p), sizeof(uint64_t), cnid_comp_fn); - - slq->slq_cnids = cnids; - slq->slq_cnids_num = talloc_array_length(p); - -EC_CLEANUP: - if (ret != 0) { - if (cnids) - talloc_free(cnids); - } - EC_EXIT; -} - -static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v) +static int sl_rpc_openQuery(AFPObj *obj, + const DALLOC_CTX *query, + DALLOC_CTX *reply, + struct vol *v) { EC_INIT; char *sl_query; @@ -276,96 +754,187 @@ static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *re DALLOC_CTX *reqinfo; sl_array_t *array; sl_cnids_t *cnids; - slq_t *slq = NULL; + slq_t *slq; + char slq_host[MAXPATHLEN + 1]; + uint16_t convflags = v->v_mtou_flags; + uint64_t result; + gchar *sparql_query; + GError *error = NULL; + + array = talloc_zero(reply, sl_array_t); + + if (obj->sl_ctx->tracker_con == NULL) { + EC_FAIL; + } /* Allocate and initialize query object */ - slq = talloc_zero(sl_ctx, slq_t); + slq = talloc_zero(obj->sl_ctx, slq_t); slq->slq_state = SLQ_STATE_NEW; slq->slq_obj = obj; slq->slq_vol = v; slq->slq_allow_expr = obj->options.flags & OPTION_SPOTLIGHT_EXPR ? true : false; slq->slq_result_limit = obj->options.sparql_limit; + slq->cancellable = g_cancellable_new(); + talloc_set_destructor(slq, slq_free_cb); - LOG(log_info, logtype_sl, "sl_rpc_openQuery: expr: %s, limit: %" PRIu64, + LOG(log_debug, logtype_sl, "Spotlight: expr: %s, limit: %" PRIu64, slq->slq_allow_expr ? "yes" : "no", slq->slq_result_limit); /* convert spotlight query charset to host charset */ - EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") ); - char slq_host[MAXPATHLEN + 1]; - uint16_t convflags = v->v_mtou_flags; - size_t slq_maclen; - if (convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset, sl_query, strlen(sl_query), slq_host, MAXPATHLEN, &convflags) == -1) { - LOG(log_error, logtype_afpd, "sl_rpc_openQuery(\"%s\"): charset conversion failed", sl_query); + sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, + "DALLOC_CTX", 1, + "kMDQueryString"); + if (sl_query == NULL) { + EC_FAIL; + } + ret = convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset, + sl_query, strlen(sl_query), slq_host, MAXPATHLEN, + &convflags); + if (ret == -1) { + LOG(log_error, logtype_sl, "charset conversion failed"); EC_FAIL; } slq->slq_qstring = talloc_strdup(slq, slq_host); - LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", slq->slq_qstring); + LOG(log_debug, logtype_sl, "Spotlight query: \"%s\"", slq->slq_qstring); slq->slq_time = time(NULL); - EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); + uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1); + if (uint64 == NULL) { + EC_FAIL; + } slq->slq_ctx1 = *uint64; - EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) ); + + uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2); + if (uint64 == NULL) { + EC_FAIL; + } slq->slq_ctx2 = *uint64; - EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") ); + + reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, + "kMDAttributeArray"); + if (reqinfo == NULL) { + EC_FAIL; + } slq->slq_reqinfo = talloc_steal(slq, reqinfo); - if ((cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryItemArray"))) { + + cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, + "kMDQueryItemArray"); + if (cnids) { EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) ); } - - LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:"); - dd_dump(slq->slq_reqinfo, 0); - (void)slq_add(slq); - - /* Run the query */ - EC_ZERO( sl_module_export->sl_mod_start_search(slq) ); + ret = map_spotlight_to_sparql_query(slq, &sparql_query); + if (ret != 0) { + LOG(log_debug, logtype_sl, "mapping retured non-zero"); + EC_FAIL; + } + LOG(log_debug, logtype_sl, "SPARQL query: \"%s\"", sparql_query); + + tracker_sparql_connection_query_async(obj->sl_ctx->tracker_con, + sparql_query, + slq->cancellable, + tracker_query_cb, + slq); + if (error) { + LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'", + error->message); + g_clear_error(&error); + EC_FAIL; + } - array = talloc_zero(reply, sl_array_t); - uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX; - dalloc_add_copy(array, &sl_res, uint64_t); - dalloc_add(reply, array, sl_array_t); + slq->slq_state = SLQ_STATE_RUNNING; + slq_add(slq); EC_CLEANUP: if (ret != 0) { - slq_error(slq); + slq_destroy(slq); + result = UINT64_MAX; + ret = 0; + } else { + result = 0; } + + dalloc_add_copy(array, &result, 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) +static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, + const DALLOC_CTX *query, + DALLOC_CTX *reply, + const struct vol *v) { EC_INIT; slq_t *slq = NULL; - uint64_t *uint64, ctx1, ctx2; + uint64_t *uint64, ctx1, ctx2, status; + sl_array_t *array; + bool ok; + + array = talloc_zero(reply, sl_array_t); + if (array == NULL) { + return false; + } /* Context */ - EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); + uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1); + if (uint64 == NULL) { + EC_FAIL; + } ctx1 = *uint64; - EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) ); + uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2); + if (uint64 == NULL) { + EC_FAIL; + } ctx2 = *uint64; /* Get query for context */ - EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) ); - if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) { - EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active"); + slq = slq_for_ctx(ctx1, ctx2); + if (slq == NULL) { + EC_FAIL; } - /* Create and pass reply handle */ - EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) ); + switch (slq->slq_state) { + case SLQ_STATE_RUNNING: + case SLQ_STATE_RESULTS: + case SLQ_STATE_DONE: + ok = add_results(array, slq); + if (!ok) { + LOG(log_error, logtype_sl, "error adding results"); + EC_FAIL; + } + if (slq->slq_state == SLQ_STATE_RESULTS) { + tracker_sparql_cursor_next_async( + slq->tracker_cursor, + slq->cancellable, + tracker_cursor_cb, + slq); + } + break; - /* Fetch Tracker results*/ - EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) ); + case SLQ_STATE_ERROR: + LOG(log_error, logtype_sl, "query in error state"); + EC_FAIL; - dalloc_add(reply, slq->slq_reply, sl_array_t); + default: + LOG(log_error, logtype_sl, "unexpected query state %d", slq->slq_state); + EC_FAIL; + } + + dalloc_add(reply, array, sl_array_t); + EC_EXIT; EC_CLEANUP: - if (ret != 0) { - slq_error(slq); - } + slq_destroy(slq); + status = UINT64_MAX; + dalloc_add_copy(array, &status, uint64_t); + dalloc_add(reply, array, sl_array_t); EC_EXIT; } -static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol) +static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, + const DALLOC_CTX *query, + DALLOC_CTX *reply, + const struct vol *vol) { EC_INIT; uint64_t uint64; @@ -375,11 +944,11 @@ static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, const DALLOC_CTX cnid_t id; char *path; struct dir *dir; - + EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) ); memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t)); id = (cnid_t)uint64; - LOG(log_debug, logtype_sl, "sl_rpc_storeAttributesForOIDArray: CNID: %" PRIu32, id); + LOG(log_debug, logtype_sl, "CNID: %" PRIu32, id); if (htonl(id) == DIRDID_ROOT) { path = vol->v_path; @@ -422,11 +991,10 @@ static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC EC_INIT; uint64_t uint64; sl_cnids_t *cnids; - sl_time_t *sl_time; cnid_t id; char *path; struct dir *dir; - + EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) ); memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t)); id = (cnid_t)uint64; @@ -488,155 +1056,206 @@ EC_CLEANUP: static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol) { EC_INIT; - slq_t *slq = NULL; uint64_t uint64; - sl_cnids_t *cnids; - sl_time_t *sl_time; - cnid_t id; + sl_cnids_t *cnids, *replycnids; + cnid_t id, did; struct dir *dir; - sl_array_t *reqinfo; + sl_array_t *array, *reqinfo, *fm_array; + char buffer[12 + MAXPATHLEN + 1]; + char *name, *path; + sl_filemeta_t *fm; + sl_nil_t nil; + uint64_t sl_res; + struct stat sb; + array = talloc_zero(reply, sl_array_t); + replycnids = talloc_zero(reply, sl_cnids_t); + replycnids->ca_cnids = talloc_zero(replycnids, DALLOC_CTX); + fm = talloc_zero(array, sl_filemeta_t); + fm_array = talloc_zero(fm, sl_array_t); + if (array == NULL || replycnids == NULL || replycnids->ca_cnids == NULL + || fm == NULL || fm_array == NULL) { + EC_FAIL; + } + + reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1); + if (reqinfo == NULL) { + EC_FAIL; + } + cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2); + if (cnids == NULL) { + EC_FAIL; + } - /* Allocate and initialize query object */ - slq = talloc_zero(reply, slq_t); - slq->slq_state = SLQ_STATE_ATTRS; - slq->slq_obj = obj; - slq->slq_vol = vol; - EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) ); - EC_NULL( reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1) ); - slq->slq_reqinfo = talloc_steal(slq, reqinfo); - - EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) ); memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t)); id = (cnid_t)uint64; if (htonl(id) == DIRDID_ROOT) { - slq->slq_path = talloc_strdup(slq, vol->v_path); + path = talloc_strdup(reply, vol->v_path); } else if (id < CNID_START) { EC_FAIL; } else { - cnid_t did; - char buffer[12 + MAXPATHLEN + 1]; - char *name; did = htonl(id); EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) ); EC_NULL( dir = dirlookup(vol, did) ); - EC_NULL( slq->slq_path = talloc_asprintf(slq, "%s/%s", bdata(dir->d_fullpath), name) ); + EC_NULL( path = talloc_asprintf(reply, "%s/%s", bdata(dir->d_fullpath), name) ); } - /* Return result value 0 */ - uint64_t sl_res = 0; - dalloc_add_copy(slq->slq_reply, &sl_res, uint64_t); + EC_ZERO( stat(path, &sb) ); + + sl_res = 0; + dalloc_add_copy(array, &sl_res, uint64_t); - /* Return CNID array */ - sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t); - replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX); replycnids->ca_unkn1 = 0xfec; replycnids->ca_context = cnids->ca_context; uint64 = (uint64_t)id; dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t); - dalloc_add(slq->slq_reply, replycnids, sl_cnids_t); - - /* Fetch attributes from module */ - EC_ZERO_LOG( sl_module_export->sl_mod_fetch_attrs(slq) ); + dalloc_add(array, replycnids, sl_cnids_t); + dalloc_add(fm, fm_array, fm_array_t); + dalloc_add_copy(fm_array, &nil, sl_nil_t); + add_filemeta(reqinfo, fm_array, path, &sb); - dalloc_add(reply, slq->slq_reply, sl_array_t); + /* Now add result */ + dalloc_add(array, fm, sl_filemeta_t); + dalloc_add(reply, array, sl_array_t); + EC_EXIT; EC_CLEANUP: + sl_res = UINT64_MAX; + dalloc_add_copy(array, &sl_res, uint64_t); + dalloc_add(array, fm, sl_filemeta_t); + dalloc_add(reply, array, sl_array_t); EC_EXIT; } -static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) +static int sl_rpc_closeQueryForContext(const AFPObj *obj, + const DALLOC_CTX *query, + DALLOC_CTX *reply, + const struct vol *v) { EC_INIT; slq_t *slq = NULL; uint64_t *uint64, ctx1, ctx2; sl_array_t *array; - + uint64_t sl_result; + + array = talloc_zero(reply, sl_array_t); + /* Context */ - EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); + uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1); + if (uint64 == NULL) { + EC_FAIL; + } ctx1 = *uint64; - EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) ); + uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2); + if (uint64 == NULL) { + EC_FAIL; + } ctx2 = *uint64; /* Get query for context and free it */ - EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) ); - if (slq->slq_state != SLQ_STATE_DONE) - LOG(log_warning, logtype_sl, "Closing active query"); - sl_module_export->sl_mod_end_search(slq); - slq_remove(slq); - talloc_free(slq); - slq = NULL; + slq = slq_for_ctx(ctx1, ctx2); + if (slq == NULL) { + EC_FAIL; + } - array = talloc_zero(reply, sl_array_t); - uint64_t sl_res = 0; - dalloc_add_copy(array, &sl_res, uint64_t); - dalloc_add(reply, array, sl_array_t); + switch (slq->slq_state) { + case SLQ_STATE_DONE: + case SLQ_STATE_ERROR: + slq_destroy(slq); + break; + + case SLQ_STATE_RUNNING: + case SLQ_STATE_RESULTS: + slq_cancel(slq); + break; + + default: + LOG(log_error, logtype_sl, "Unexpected state %d", slq->slq_state); + EC_FAIL; + } + + sl_result = 0; EC_CLEANUP: if (ret != 0) { - slq_error(slq); + sl_result = UINT64_MAX; } + dalloc_add_copy(array, &sl_result, uint64_t); + dalloc_add(reply, array, sl_array_t); EC_EXIT; } -/************************************************************************************************** - * Spotlight module functions - **************************************************************************************************/ +/****************************************************************************** + * Spotlight functions + ******************************************************************************/ -int sl_mod_load(AFPObj *obj) +int spotlight_init(AFPObj *obj) { - EC_INIT; + const char *attributes; + struct sl_ctx *sl_ctx; - sl_ctx = talloc_new(NULL); + LOG(log_info, logtype_sl, "Initializing Spotlight"); - if ((sl_module = mod_open(obj->options.slmod_path)) == NULL) { - LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", obj->options.slmod_path, mod_error()); - EC_FAIL; - } + sl_ctx = talloc_zero(NULL, struct sl_ctx); + obj->sl_ctx = sl_ctx; - if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) { - LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol sl_mod", obj->options.slmod_path); - EC_FAIL; + attributes = atalk_iniparser_getstring(obj->iniconfig, INISEC_GLOBAL, + "spotlight attributes", NULL); + if (attributes) { + configure_spotlight_attributes(attributes); } - sl_module_export->sl_mod_init(obj); - -EC_CLEANUP: - EC_EXIT; -} + /* + * Tracker uses glibs event dispatching, so we need a mainloop + */ + sl_ctx->mainloop = g_main_loop_new(NULL, false); + sl_ctx->cancellable = g_cancellable_new(); -/** - * Index a file - **/ -void sl_index_file(const char *path) -{ - if (sl_module_export && sl_module_export->sl_mod_index_file) - sl_module_export->sl_mod_index_file(path); + setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=" _PATH_STATEDIR "spotlight.ipc", 1); + setenv("XDG_DATA_HOME", _PATH_STATEDIR, 0); + setenv("XDG_CACHE_HOME", _PATH_STATEDIR, 0); + setenv("TRACKER_USE_LOG_FILES", "1", 0); + + tracker_sparql_connection_get_async(sl_ctx->cancellable, + tracker_con_cb, sl_ctx); + + return 0; } -/************************************************************************************************** +/****************************************************************************** * AFP functions - **************************************************************************************************/ + ******************************************************************************/ -int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen) +int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, + char *rbuf, size_t *rbuflen) { EC_INIT; TALLOC_CTX *tmp_ctx = talloc_new(NULL); uint16_t vid; int cmd; - int endianess = SL_ENC_LITTLE_ENDIAN; struct vol *vol; DALLOC_CTX *query; DALLOC_CTX *reply; char *rpccmd; int len; + bool event; *rbuflen = 0; - if (sl_module == NULL) + if (!(obj->options.flags & OPTION_SPOTLIGHT)) { return AFPERR_NOOP; + } + + /* + * Process finished glib events + */ + event = true; + while (event) { + event = g_main_context_iteration(NULL, false); + } + slq_cancelled_cleanup(); ibuf += 2; ibuflen -= 2; @@ -678,8 +1297,8 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_ EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) ); EC_NEG1_LOG( sl_unpack(query, ibuf + 22) ); - LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:"); - dd_dump(query, 0); + LOG(log_debug, logtype_sl, "Spotlight RPC request:\n%s", + dd_dump(query, 0)); EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) ); @@ -701,8 +1320,8 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_ LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd); } - LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:"); - dd_dump(reply, 0); + LOG(log_debug, logtype_sl, "Spotlight RPC reply dump:\n%s", + dd_dump(reply, 0)); memset(rbuf, 0, 4); *rbuflen += 4; @@ -720,97 +1339,3 @@ EC_CLEANUP: } EC_EXIT; } -#endif - -/************************************************************************************************** - * Testing - **************************************************************************************************/ - -#ifdef SPOT_TEST_MAIN - -int main(int argc, char **argv) -{ - EC_INIT; - TALLOC_CTX *mem_ctx = talloc_new(NULL); - DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX); - uint64_t i; - - set_processname("spot"); - setuplog("default:info,spotlight:debug", "/dev/tty"); - - LOG(log_info, logtype_sl, "Start"); - - i = 1; - dalloc_add_copy(dd, &i, uint64_t); - char *str = dalloc_strdup(dd, "hello world"); - dalloc_add(dd, str, char *); - sl_bool_t b = true; - dalloc_add_copy(dd, &b, sl_bool_t); - - /* add a nested array */ - DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX); - i = 3; - dalloc_add_copy(nested, &i, uint64_t); - dalloc_add(dd, nested, DALLOC_CTX); - - /* 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; - dalloc_add_copy(cnids->ca_cnids, &id, uint64_t); - dalloc_add(dd, cnids, sl_cnids_t); - - /* Now the Spotlight types */ - sl_array_t *sl_array = talloc_zero(dd, sl_array_t); - i = 0x1234; - dalloc_add_copy(sl_array, &i, uint64_t); - - sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t); - i = 0xffff; - dalloc_add_copy(sl_dict, &i, uint64_t); - dalloc_add(sl_array, sl_dict, sl_dict_t); - - dalloc_add(dd, sl_array, sl_array_t); - - dd_dump(dd, 0); - - /* now parse a real spotlight packet */ - if (argc > 1) { - char ibuf[8192]; - char rbuf[8192]; - int fd; - size_t len; - DALLOC_CTX *query; - - EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) ); - - EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) ); - EC_NEG1_LOG( len = read(fd, ibuf, 8192) ); - close(fd); - EC_NEG1_LOG( sl_unpack(query, ibuf + 24) ); - - /* Now dump the whole thing */ - dd_dump(query, 0); - } - -#if 0 - /* packing */ - int qlen; - char buf[MAX_SLQ_DAT]; - EC_NEG1_LOG( qlen = sl_pack(query, buf) ); - - EC_NEG1_LOG( fd = open("test.bin", O_RDWR) ); - lseek(fd, 24, SEEK_SET); - write(fd, buf, qlen); - close(fd); -#endif - -EC_CLEANUP: - if (mem_ctx) { - talloc_free(mem_ctx); - mem_ctx = NULL; - } - EC_EXIT; -} -#endif diff --git a/etc/afpd/spotlight_marshalling.c b/etc/afpd/spotlight_marshalling.c index 69733f49..9329dde2 100644 --- a/etc/afpd/spotlight_marshalling.c +++ b/etc/afpd/spotlight_marshalling.c @@ -216,7 +216,7 @@ EC_CLEANUP: static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx) { EC_INIT; - int off = 0, len; + int len; int cnid_count = talloc_array_length(cnids->ca_cnids->dd_talloc_array); uint64_t id; @@ -472,7 +472,6 @@ static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint { int count, i; uint64_t query_data64; - double fval; union { double d; uint32_t w[2]; @@ -786,8 +785,8 @@ EC_CLEANUP: int sl_unpack(DALLOC_CTX *query, const char *buf) { EC_INIT; - int encoding, i, toc_entries; - uint64_t toc_offset, tquerylen, toc_entry; + int encoding, toc_entries; + uint64_t toc_offset; if (strncmp(buf, "md031234", 8) == 0) encoding = SL_ENC_BIG_ENDIAN; diff --git a/etc/netatalk/netatalk.c b/etc/netatalk/netatalk.c index 1ce7befc..b8987b7e 100644 --- a/etc/netatalk/netatalk.c +++ b/etc/netatalk/netatalk.c @@ -173,7 +173,7 @@ static void sigterm_cb(evutil_socket_t fd, short what, void *arg) event_del(sigquit_ev); event_del(timer_ev); -#ifdef HAVE_TRACKER_SPARQL +#ifdef HAVE_TRACKER system("tracker-control -t"); #endif kill_childs(SIGTERM, &afpd_pid, &cnid_metad_pid, &dbus_pid, NULL); @@ -183,7 +183,7 @@ static void sigterm_cb(evutil_socket_t fd, short what, void *arg) static void sigquit_cb(evutil_socket_t fd, short what, void *arg) { LOG(log_note, logtype_afpd, "Exiting on SIGQUIT"); -#ifdef HAVE_TRACKER_SPARQL +#ifdef HAVE_TRACKER system("tracker-control -t"); #endif kill_childs(SIGQUIT, &afpd_pid, &cnid_metad_pid, &dbus_pid, NULL); @@ -420,7 +420,7 @@ int main(int argc, char **argv) if (atalk_iniparser_getboolean(obj.iniconfig, INISEC_GLOBAL, "start dbus", 1)) { dbus_path = atalk_iniparser_getstring(obj.iniconfig, INISEC_GLOBAL, "dbus daemon", DBUS_DAEMON_PATH); - LOG(log_debug, logtype_default, "DBUS: '%s'", dbus_path); + LOG(log_note, logtype_default, "Starting dbus: %s", dbus_path); if ((dbus_pid = run_process(dbus_path, "--config-file=" _PATH_CONFDIR "dbus-session.conf", NULL)) == NETATALK_SRV_ERROR) { LOG(log_error, logtype_default, "Error starting '%s'", dbus_path); netatalk_exit(EXITERR_CONF); @@ -433,6 +433,7 @@ int main(int argc, char **argv) set_sl_volumes(); if (atalk_iniparser_getboolean(obj.iniconfig, INISEC_GLOBAL, "start tracker", 1)) { + LOG(log_note, logtype_default, "Starting Tracker"); system(TRACKER_PREFIX "/bin/tracker-control -s"); } } diff --git a/etc/spotlight/Makefile.am b/etc/spotlight/Makefile.am index 60e0c25b..95f1ba48 100644 --- a/etc/spotlight/Makefile.am +++ b/etc/spotlight/Makefile.am @@ -1,36 +1,33 @@ # Makefile.am for etc/spotlight/ pkgconfdir = @PKGCONFDIR@ -moduledir = @UAMS_PATH@ -module_LTLIBRARIES = +noinst_LTLIBRARIES = noinst_PROGRAMS = -noinst_HEADERS = slmod_sparql_map.h +noinst_HEADERS = sparql_map.h BUILT_SOURCES = AM_YFLAGS = -d -if HAVE_TRACKER_SPARQL -BUILT_SOURCES += slmod_sparql_parser.h +if HAVE_TRACKER +BUILT_SOURCES += sparql_parser.h noinst_PROGRAMS += srp -module_LTLIBRARIES += slmod_sparql.la +noinst_LTLIBRARIES += libspotlight.la -slmod_sparql_la_SOURCES = \ - slmod_sparql.c \ - slmod_sparql_map.c \ - slmod_sparql_parser.y \ +libspotlight_la_SOURCES = \ + sparql_map.c \ + sparql_parser.y \ spotlight_rawquery_lexer.l -slmod_sparql_la_CFLAGS = \ +libspotlight_la_CFLAGS = \ -DDBUS_API_SUBJECT_TO_CHANGE \ @TRACKER_CFLAGS@ \ - @TRACKER_MINER_CFLAGS@ \ -D_PATH_STATEDIR='"$(localstatedir)/netatalk"' -slmod_sparql_la_LDFLAGS = -module -avoid-version @TRACKER_LIBS@ @TRACKER_MINER_LIBS@ +libspotlight_la_LDFLAGS = @TRACKER_LIBS@ @TRACKER_MINER_LIBS@ srp_SOURCES = \ - slmod_sparql_map.c \ - slmod_sparql_parser.y \ + sparql_map.c \ + sparql_parser.y \ spotlight_rawquery_lexer.l srp_CFLAGS = -DMAIN -I$(top_srcdir)/include @TRACKER_CFLAGS@ diff --git a/etc/spotlight/slmod_sparql.c b/etc/spotlight/slmod_sparql.c index 21e8d382..e58d7df6 100644 --- a/etc/spotlight/slmod_sparql.c +++ b/etc/spotlight/slmod_sparql.c @@ -1,5 +1,5 @@ /* - Copyright (c) 2012 Frank Lahm + Copyright (C) Ralph Boehme 2012-2014 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -34,9 +34,6 @@ #define MAX_SL_RESULTS 20 static TrackerSparqlConnection *connection; -#if 0 -static TrackerMinerManager *manager; -#endif static char *tracker_to_unix_path(const char *uri) { @@ -398,37 +395,6 @@ static int sl_mod_index_file(const void *p) * impose limits on the maximum number of watched directories. */ return 0; - -#if 0 -#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 /* HAVE_TRACKER_MINER */ -#endif /* 0 */ } struct sl_module_export sl_mod = { diff --git a/etc/spotlight/slmod_sparql_map.c b/etc/spotlight/sparql_map.c similarity index 99% rename from etc/spotlight/slmod_sparql_map.c rename to etc/spotlight/sparql_map.c index 7ac4963c..3a0e4691 100644 --- a/etc/spotlight/slmod_sparql_map.c +++ b/etc/spotlight/sparql_map.c @@ -22,7 +22,7 @@ #include -#include "slmod_sparql_map.h" +#include "sparql_map.h" #define NOTSUPPORTED NULL #define SPECIAL NULL diff --git a/etc/spotlight/slmod_sparql_map.h b/etc/spotlight/sparql_map.h similarity index 100% rename from etc/spotlight/slmod_sparql_map.h rename to etc/spotlight/sparql_map.h diff --git a/etc/spotlight/slmod_sparql_parser.c b/etc/spotlight/sparql_parser.c similarity index 97% rename from etc/spotlight/slmod_sparql_parser.c rename to etc/spotlight/sparql_parser.c index 3d32af62..77fa9e50 100644 --- a/etc/spotlight/slmod_sparql_parser.c +++ b/etc/spotlight/sparql_parser.c @@ -63,7 +63,7 @@ /* Copy the first part of user declarations. */ /* Line 371 of yacc.c */ -#line 1 "slmod_sparql_parser.y" +#line 1 "sparql_parser.y" #include @@ -79,7 +79,7 @@ #include #include - #include "slmod_sparql_map.h" + #include "sparql_map.h" struct yy_buffer_state; typedef struct yy_buffer_state *YY_BUFFER_STATE; @@ -103,7 +103,7 @@ static char *result_limit; /* Line 371 of yacc.c */ -#line 107 "slmod_sparql_parser.c" +#line 107 "sparql_parser.c" # ifndef YY_NULL # if defined __cplusplus && 201103L <= __cplusplus @@ -123,8 +123,8 @@ /* In a future release of Bison, this section will be replaced by #include "y.tab.h". */ -#ifndef YY_YY_SLMOD_SPARQL_PARSER_H_INCLUDED -# define YY_YY_SLMOD_SPARQL_PARSER_H_INCLUDED +#ifndef YY_YY_SPARQL_PARSER_H_INCLUDED +# define YY_YY_SPARQL_PARSER_H_INCLUDED /* Enabling traces. */ #ifndef YYDEBUG # define YYDEBUG 0 @@ -177,7 +177,7 @@ extern int yydebug; typedef union YYSTYPE { /* Line 387 of yacc.c */ -#line 46 "slmod_sparql_parser.y" +#line 46 "sparql_parser.y" int ival; const char *sval; @@ -186,7 +186,7 @@ typedef union YYSTYPE /* Line 387 of yacc.c */ -#line 190 "slmod_sparql_parser.c" +#line 190 "sparql_parser.c" } YYSTYPE; # define YYSTYPE_IS_TRIVIAL 1 # define yystype YYSTYPE /* obsolescent; will be withdrawn */ @@ -210,7 +210,7 @@ int yyparse (); #endif /* ! YYPARSE_PARAM */ /* "%code provides" blocks. */ /* Line 387 of yacc.c */ -#line 40 "slmod_sparql_parser.y" +#line 40 "sparql_parser.y" #define SPRAW_TIME_OFFSET 978307200 extern int map_spotlight_to_sparql_query(slq_t *slq, gchar **sparql_result); @@ -218,14 +218,14 @@ int yyparse (); /* Line 387 of yacc.c */ -#line 222 "slmod_sparql_parser.c" +#line 222 "sparql_parser.c" -#endif /* !YY_YY_SLMOD_SPARQL_PARSER_H_INCLUDED */ +#endif /* !YY_YY_SPARQL_PARSER_H_INCLUDED */ /* Copy the second part of user declarations. */ /* Line 390 of yacc.c */ -#line 229 "slmod_sparql_parser.c" +#line 229 "sparql_parser.c" #ifdef short # undef short @@ -524,9 +524,9 @@ static const yytype_int8 yyrhs[] = /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint8 yyrline[] = { - 0, 68, 68, 70, 74, 88, 94, 102, 103, 104, - 105, 112, 125, 126, 127, 128, 129, 130, 131, 132, - 136, 140, 141 + 0, 68, 68, 70, 74, 88, 99, 107, 108, 109, + 110, 117, 130, 131, 132, 133, 134, 135, 136, 137, + 141, 145, 146 }; #endif @@ -1447,7 +1447,7 @@ yyreduce: { case 4: /* Line 1792 of yacc.c */ -#line 74 "slmod_sparql_parser.y" +#line 74 "sparql_parser.y" { if (ssp_slq->slq_result_limit) result_limit = talloc_asprintf(ssp_slq, "LIMIT %ld", ssp_slq->slq_result_limit); @@ -1463,18 +1463,23 @@ yyreduce: case 5: /* Line 1792 of yacc.c */ -#line 88 "slmod_sparql_parser.y" +#line 88 "sparql_parser.y" { - if ((yyvsp[(1) - (1)].bval) == false) - YYACCEPT; - else - YYABORT; + /* + * We can't properly handle these in expressions, fortunately this + * is probably only ever used by OS X as sole element in an + * expression ie "False" (when Finder window selected our share + * but no search string entered yet). Packet traces showed that OS + * X Spotlight server then returns a failure (ie -1) which is what + * we do here too by calling YYABORT. + */ + YYABORT; } break; case 6: /* Line 1792 of yacc.c */ -#line 94 "slmod_sparql_parser.y" +#line 99 "sparql_parser.y" { if ((yyvsp[(1) - (3)].sval) == NULL || (yyvsp[(3) - (3)].sval) == NULL) YYABORT; @@ -1487,25 +1492,25 @@ yyreduce: case 7: /* Line 1792 of yacc.c */ -#line 102 "slmod_sparql_parser.y" +#line 107 "sparql_parser.y" {(yyval.sval) = (yyvsp[(1) - (1)].sval); if ((yyval.sval) == NULL) YYABORT;} break; case 8: /* Line 1792 of yacc.c */ -#line 103 "slmod_sparql_parser.y" +#line 108 "sparql_parser.y" {(yyval.sval) = (yyvsp[(1) - (1)].sval);} break; case 9: /* Line 1792 of yacc.c */ -#line 104 "slmod_sparql_parser.y" +#line 109 "sparql_parser.y" {(yyval.sval) = talloc_asprintf(ssp_slq, "%s", (yyvsp[(2) - (3)].sval));} break; case 10: /* Line 1792 of yacc.c */ -#line 105 "slmod_sparql_parser.y" +#line 110 "sparql_parser.y" { if (!ssp_slq->slq_allow_expr) { yyerror("Spotlight queries with logic expressions are disabled"); @@ -1517,7 +1522,7 @@ yyreduce: case 11: /* Line 1792 of yacc.c */ -#line 112 "slmod_sparql_parser.y" +#line 117 "sparql_parser.y" { if (!ssp_slq->slq_allow_expr) { yyerror("Spotlight queries with logic expressions are disabled"); @@ -1532,73 +1537,73 @@ yyreduce: case 12: /* Line 1792 of yacc.c */ -#line 125 "slmod_sparql_parser.y" +#line 130 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (5)].sval), '=', (yyvsp[(4) - (5)].sval));} break; case 13: /* Line 1792 of yacc.c */ -#line 126 "slmod_sparql_parser.y" +#line 131 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (5)].sval), '!', (yyvsp[(4) - (5)].sval));} break; case 14: /* Line 1792 of yacc.c */ -#line 127 "slmod_sparql_parser.y" +#line 132 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (5)].sval), '<', (yyvsp[(4) - (5)].sval));} break; case 15: /* Line 1792 of yacc.c */ -#line 128 "slmod_sparql_parser.y" +#line 133 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (5)].sval), '>', (yyvsp[(4) - (5)].sval));} break; case 16: /* Line 1792 of yacc.c */ -#line 129 "slmod_sparql_parser.y" +#line 134 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '=', (yyvsp[(4) - (6)].sval));} break; case 17: /* Line 1792 of yacc.c */ -#line 130 "slmod_sparql_parser.y" +#line 135 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '!', (yyvsp[(4) - (6)].sval));} break; case 18: /* Line 1792 of yacc.c */ -#line 131 "slmod_sparql_parser.y" +#line 136 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '<', (yyvsp[(4) - (6)].sval));} break; case 19: /* Line 1792 of yacc.c */ -#line 132 "slmod_sparql_parser.y" +#line 137 "sparql_parser.y" {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '>', (yyvsp[(4) - (6)].sval));} break; case 20: /* Line 1792 of yacc.c */ -#line 136 "slmod_sparql_parser.y" +#line 141 "sparql_parser.y" {(yyval.sval) = map_daterange((yyvsp[(3) - (8)].sval), (yyvsp[(5) - (8)].tval), (yyvsp[(7) - (8)].tval));} break; case 21: /* Line 1792 of yacc.c */ -#line 140 "slmod_sparql_parser.y" +#line 145 "sparql_parser.y" {(yyval.tval) = isodate2unix((yyvsp[(3) - (4)].sval));} break; case 22: /* Line 1792 of yacc.c */ -#line 141 "slmod_sparql_parser.y" +#line 146 "sparql_parser.y" {(yyval.tval) = atoi((yyvsp[(1) - (1)].sval)) + SPRAW_TIME_OFFSET;} break; /* Line 1792 of yacc.c */ -#line 1602 "slmod_sparql_parser.c" +#line 1607 "sparql_parser.c" default: break; } /* User semantic actions sometimes alter yychar, and that requires @@ -1830,7 +1835,7 @@ yyreturn: /* Line 2055 of yacc.c */ -#line 144 "slmod_sparql_parser.y" +#line 149 "sparql_parser.y" static time_t isodate2unix(const char *s) @@ -1970,6 +1975,8 @@ static const char *map_expr(const char *attr, char op, const char *val) } EC_CLEANUP: + if (ret != 0) + result = NULL; if (q) bdestroy(q); if (search) diff --git a/etc/spotlight/slmod_sparql_parser.h b/etc/spotlight/sparql_parser.h similarity index 92% rename from etc/spotlight/slmod_sparql_parser.h rename to etc/spotlight/sparql_parser.h index 9eb22b37..6c5267dc 100644 --- a/etc/spotlight/slmod_sparql_parser.h +++ b/etc/spotlight/sparql_parser.h @@ -30,8 +30,8 @@ This special exception was added by the Free Software Foundation in version 2.2 of Bison. */ -#ifndef YY_YY_SLMOD_SPARQL_PARSER_H_INCLUDED -# define YY_YY_SLMOD_SPARQL_PARSER_H_INCLUDED +#ifndef YY_YY_SPARQL_PARSER_H_INCLUDED +# define YY_YY_SPARQL_PARSER_H_INCLUDED /* Enabling traces. */ #ifndef YYDEBUG # define YYDEBUG 0 @@ -84,7 +84,7 @@ extern int yydebug; typedef union YYSTYPE { /* Line 2058 of yacc.c */ -#line 46 "slmod_sparql_parser.y" +#line 46 "sparql_parser.y" int ival; const char *sval; @@ -93,7 +93,7 @@ typedef union YYSTYPE /* Line 2058 of yacc.c */ -#line 97 "slmod_sparql_parser.h" +#line 97 "sparql_parser.h" } YYSTYPE; # define YYSTYPE_IS_TRIVIAL 1 # define yystype YYSTYPE /* obsolescent; will be withdrawn */ @@ -117,7 +117,7 @@ int yyparse (); #endif /* ! YYPARSE_PARAM */ /* "%code provides" blocks. */ /* Line 2058 of yacc.c */ -#line 40 "slmod_sparql_parser.y" +#line 40 "sparql_parser.y" #define SPRAW_TIME_OFFSET 978307200 extern int map_spotlight_to_sparql_query(slq_t *slq, gchar **sparql_result); @@ -125,6 +125,6 @@ int yyparse (); /* Line 2058 of yacc.c */ -#line 129 "slmod_sparql_parser.h" +#line 129 "sparql_parser.h" -#endif /* !YY_YY_SLMOD_SPARQL_PARSER_H_INCLUDED */ +#endif /* !YY_YY_SPARQL_PARSER_H_INCLUDED */ diff --git a/etc/spotlight/slmod_sparql_parser.y b/etc/spotlight/sparql_parser.y similarity index 95% rename from etc/spotlight/slmod_sparql_parser.y rename to etc/spotlight/sparql_parser.y index 76d8b5a7..ede21872 100644 --- a/etc/spotlight/slmod_sparql_parser.y +++ b/etc/spotlight/sparql_parser.y @@ -13,7 +13,7 @@ #include #include - #include "slmod_sparql_map.h" + #include "sparql_map.h" struct yy_buffer_state; typedef struct yy_buffer_state *YY_BUFFER_STATE; @@ -85,11 +85,16 @@ expr { ; expr: -BOOL { - if ($1 == false) - YYACCEPT; - else - YYABORT; +BOOL { + /* + * We can't properly handle these in expressions, fortunately this + * is probably only ever used by OS X as sole element in an + * expression ie "False" (when Finder window selected our share + * but no search string entered yet). Packet traces showed that OS + * X Spotlight server then returns a failure (ie -1) which is what + * we do here too by calling YYABORT. + */ + YYABORT; } | match OR match { if ($1 == NULL || $3 == NULL) @@ -280,6 +285,8 @@ static const char *map_expr(const char *attr, char op, const char *val) } EC_CLEANUP: + if (ret != 0) + result = NULL; if (q) bdestroy(q); if (search) diff --git a/etc/spotlight/spotlight_rawquery_lexer.c b/etc/spotlight/spotlight_rawquery_lexer.c index 38b7a73f..b13b721b 100644 --- a/etc/spotlight/spotlight_rawquery_lexer.c +++ b/etc/spotlight/spotlight_rawquery_lexer.c @@ -14,7 +14,7 @@ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 -#define YY_FLEX_SUBMINOR_VERSION 35 +#define YY_FLEX_SUBMINOR_VERSION 37 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif @@ -59,7 +59,6 @@ typedef int flex_int32_t; typedef unsigned char flex_uint8_t; typedef unsigned short int flex_uint16_t; typedef unsigned int flex_uint32_t; -#endif /* ! C99 */ /* Limits of integral types. */ #ifndef INT8_MIN @@ -90,6 +89,8 @@ typedef unsigned int flex_uint32_t; #define UINT32_MAX (4294967295U) #endif +#endif /* ! C99 */ + #endif /* ! FLEXINT_H */ #ifdef __cplusplus @@ -158,7 +159,12 @@ typedef unsigned int flex_uint32_t; typedef struct yy_buffer_state *YY_BUFFER_STATE; #endif -extern int yyleng; +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern yy_size_t yyleng; extern FILE *yyin, *yyout; @@ -184,11 +190,6 @@ extern FILE *yyin, *yyout; #define unput(c) yyunput( c, (yytext_ptr) ) -#ifndef YY_TYPEDEF_YY_SIZE_T -#define YY_TYPEDEF_YY_SIZE_T -typedef size_t yy_size_t; -#endif - #ifndef YY_STRUCT_YY_BUFFER_STATE #define YY_STRUCT_YY_BUFFER_STATE struct yy_buffer_state @@ -206,7 +207,7 @@ struct yy_buffer_state /* Number of characters read into yy_ch_buf, not including EOB * characters. */ - int yy_n_chars; + yy_size_t yy_n_chars; /* Whether we "own" the buffer - i.e., we know we created it, * and can realloc() it to grow it, and should free() it to @@ -276,8 +277,8 @@ static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ /* yy_hold_char holds the character lost when yytext is formed. */ static char yy_hold_char; -static int yy_n_chars; /* number of characters read into yy_ch_buf */ -int yyleng; +static yy_size_t yy_n_chars; /* number of characters read into yy_ch_buf */ +yy_size_t yyleng; /* Points to current character in buffer. */ static char *yy_c_buf_p = (char *) 0; @@ -305,7 +306,7 @@ static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ); YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); -YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len ); +YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ); void *yyalloc (yy_size_t ); void *yyrealloc (void *,yy_size_t ); @@ -501,11 +502,11 @@ char *yytext; #include #include #include -#ifdef HAVE_TRACKER_SPARQL -#include "slmod_sparql_parser.h" +#ifdef HAVE_TRACKER +#include "sparql_parser.h" #define SLQ_VAR ssp_slq #endif -#line 509 "spotlight_rawquery_lexer.c" +#line 510 "spotlight_rawquery_lexer.c" #define INITIAL 0 @@ -544,7 +545,7 @@ FILE *yyget_out (void ); void yyset_out (FILE * out_str ); -int yyget_leng (void ); +yy_size_t yyget_leng (void ); char *yyget_text (void ); @@ -594,7 +595,7 @@ static int input (void ); /* This used to be an fputs(), but since the string might contain NUL's, * we now use fwrite(). */ -#define ECHO fwrite( yytext, yyleng, 1, yyout ) +#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0) #endif /* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, @@ -605,7 +606,7 @@ static int input (void ); if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ { \ int c = '*'; \ - int n; \ + size_t n; \ for ( n = 0; n < max_size && \ (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ buf[n] = (char) c; \ @@ -689,7 +690,7 @@ YY_DECL #line 27 "spotlight_rawquery_lexer.l" -#line 693 "spotlight_rawquery_lexer.c" +#line 694 "spotlight_rawquery_lexer.c" if ( !(yy_init) ) { @@ -858,7 +859,7 @@ YY_RULE_SETUP #line 44 "spotlight_rawquery_lexer.l" ECHO; YY_BREAK -#line 862 "spotlight_rawquery_lexer.c" +#line 863 "spotlight_rawquery_lexer.c" case YY_STATE_EOF(INITIAL): yyterminate(); @@ -1044,21 +1045,21 @@ static int yy_get_next_buffer (void) else { - int num_to_read = + yy_size_t num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; while ( num_to_read <= 0 ) { /* Not enough room in the buffer - grow it. */ /* just a shorter name for the current buffer */ - YY_BUFFER_STATE b = YY_CURRENT_BUFFER; + YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; int yy_c_buf_p_offset = (int) ((yy_c_buf_p) - b->yy_ch_buf); if ( b->yy_is_our_buffer ) { - int new_size = b->yy_buf_size * 2; + yy_size_t new_size = b->yy_buf_size * 2; if ( new_size <= 0 ) b->yy_buf_size += b->yy_buf_size / 8; @@ -1089,7 +1090,7 @@ static int yy_get_next_buffer (void) /* Read in more data. */ YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), - (yy_n_chars), (size_t) num_to_read ); + (yy_n_chars), num_to_read ); YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); } @@ -1184,7 +1185,7 @@ static int yy_get_next_buffer (void) yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; yy_is_jam = (yy_current_state == 56); - return yy_is_jam ? 0 : yy_current_state; + return yy_is_jam ? 0 : yy_current_state; } static void yyunput (int c, register char * yy_bp ) @@ -1199,7 +1200,7 @@ static int yy_get_next_buffer (void) if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) { /* need to shift things up to make room */ /* +2 for EOB chars. */ - register int number_to_move = (yy_n_chars) + 2; + register yy_size_t number_to_move = (yy_n_chars) + 2; register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; register char *source = @@ -1248,7 +1249,7 @@ static int yy_get_next_buffer (void) else { /* need more input */ - int offset = (yy_c_buf_p) - (yytext_ptr); + yy_size_t offset = (yy_c_buf_p) - (yytext_ptr); ++(yy_c_buf_p); switch ( yy_get_next_buffer( ) ) @@ -1408,10 +1409,6 @@ static void yy_load_buffer_state (void) yyfree((void *) b ); } -#ifndef __cplusplus -extern int isatty (int ); -#endif /* __cplusplus */ - /* Initializes or reinitializes a buffer. * This function is sometimes called more than once on the same buffer, * such as during a yyrestart() or at EOF. @@ -1524,7 +1521,7 @@ void yypop_buffer_state (void) */ static void yyensure_buffer_stack (void) { - int num_to_alloc; + yy_size_t num_to_alloc; if (!(yy_buffer_stack)) { @@ -1616,17 +1613,17 @@ YY_BUFFER_STATE yy_scan_string (yyconst char * yystr ) /** Setup the input buffer state to scan the given bytes. The next call to yylex() will * scan from a @e copy of @a bytes. - * @param bytes the byte buffer to scan - * @param len the number of bytes in the buffer pointed to by @a bytes. + * @param yybytes the byte buffer to scan + * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. * * @return the newly allocated buffer state object. */ -YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, int _yybytes_len ) +YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len ) { YY_BUFFER_STATE b; char *buf; yy_size_t n; - int i; + yy_size_t i; /* Get memory for full buffer, including space for trailing EOB's. */ n = _yybytes_len + 2; @@ -1708,7 +1705,7 @@ FILE *yyget_out (void) /** Get the length of the current token. * */ -int yyget_leng (void) +yy_size_t yyget_leng (void) { return yyleng; } diff --git a/etc/spotlight/spotlight_rawquery_lexer.l b/etc/spotlight/spotlight_rawquery_lexer.l index aa1e25f9..db7f40cd 100644 --- a/etc/spotlight/spotlight_rawquery_lexer.l +++ b/etc/spotlight/spotlight_rawquery_lexer.l @@ -9,8 +9,8 @@ #include #include #include -#ifdef HAVE_TRACKER_SPARQL -#include "slmod_sparql_parser.h" +#ifdef HAVE_TRACKER +#include "sparql_parser.h" #define SLQ_VAR ssp_slq #endif %} diff --git a/include/atalk/globals.h b/include/atalk/globals.h index dfb356a8..fb2848f3 100644 --- a/include/atalk/globals.h +++ b/include/atalk/globals.h @@ -124,7 +124,6 @@ struct afp_options { char *mimicmodel; char *adminauthuser; char *ignored_attr; - char *slmod_path; int splice_size; char *cnid_mysql_host; char *cnid_mysql_user; @@ -161,6 +160,7 @@ typedef struct AFPObj { int fce_version; char *fce_ign_names; char *fce_notify_script; + struct sl_ctx *sl_ctx; } AFPObj; /* typedef for AFP functions handlers */ diff --git a/include/atalk/spotlight.h b/include/atalk/spotlight.h index bafa52f5..69121380 100644 --- a/include/atalk/spotlight.h +++ b/include/atalk/spotlight.h @@ -26,29 +26,15 @@ #include #include -/************************************************************************************************** - * Spotlight module stuff - **************************************************************************************************/ - -#define SL_MODULE_VERSION 1 - -struct sl_module_export { - int sl_mod_version; - int (*sl_mod_init) (void *); - int (*sl_mod_start_search)(void *); - int (*sl_mod_fetch_result)(void *); - int (*sl_mod_end_search) (void *); - int (*sl_mod_fetch_attrs) (void *); - int (*sl_mod_error) (void *); - int (*sl_mod_index_file) (const void *); -}; - -extern int sl_mod_load(AFPObj *obj); -extern void sl_index_file(const char *path); +#ifdef HAVE_TRACKER +#include +#include +#include +#endif -/************************************************************************************************** +/****************************************************************************** * Spotlight RPC and marshalling stuff - **************************************************************************************************/ + ******************************************************************************/ /* FPSpotlightRPC subcommand codes */ #define SPOTLIGHT_CMD_OPEN 1 @@ -61,59 +47,81 @@ extern void sl_index_file(const char *path); #define SL_ENC_BIG_ENDIAN 2 #define SL_ENC_UTF_16 4 -typedef DALLOC_CTX sl_array_t; /* an array of elements */ -typedef DALLOC_CTX sl_dict_t; /* an array of key/value elements */ -typedef DALLOC_CTX sl_filemeta_t; /* contains one sl_array_t */ -typedef int sl_nil_t; /* a nil element */ -typedef bool sl_bool_t; /* a boolean, we avoid bool_t as it's a define for something else */ -typedef struct timeval sl_time_t; /* a boolean, we avoid bool_t as it's a define for something else */ +typedef DALLOC_CTX sl_array_t; /* an array of elements */ +typedef DALLOC_CTX sl_dict_t; /* an array of key/value elements */ +typedef DALLOC_CTX sl_filemeta_t; /* contains one sl_array_t */ +typedef int sl_nil_t; /* a nil element */ +typedef bool sl_bool_t; /* a boolean, we avoid bool_t */ +typedef struct timeval sl_time_t; 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 CNIDs */ +} sl_cnids_t; /* an array of CNIDs */ -/************************************************************************************************** +/****************************************************************************** * Some helper stuff dealing with queries - **************************************************************************************************/ + ******************************************************************************/ -/* Internal query state */ +/* 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_ATTRS = 5 /* Fetch metadata for an object */ + SLQ_STATE_NEW, /* Query received from client */ + SLQ_STATE_RUNNING, /* Query dispatched to Tracker */ + SLQ_STATE_RESULTS, /* Async Tracker query read */ + SLQ_STATE_DONE, /* Got all results from Tracker */ + SLQ_STATE_CANCEL_PENDING, /* a cancel op for the query is pending */ + SLQ_STATE_CANCELLED, /* the query has been cancelled */ + SLQ_STATE_ERROR /* an error happended somewhere */ } slq_state_t; +/* Handle for query results */ +struct sl_rslts { + int num_results; + sl_cnids_t *cnids; + sl_array_t *fm_array; +}; + /* Internal query data structure */ typedef struct _slq_t { - struct list_head slq_list; /* queries are stored in a list */ - slq_state_t slq_state; /* State */ - AFPObj *slq_obj; /* global AFPObj handle */ - const struct vol *slq_vol; /* volume handle */ - DALLOC_CTX *slq_reply; /* reply handle */ - time_t slq_time; /* timestamp where we received this query */ - uint64_t slq_ctx1; /* client context 1 */ - uint64_t slq_ctx2; /* client context 2 */ - sl_array_t *slq_reqinfo; /* array with requested metadata */ - const char *slq_qstring; /* the Spotlight query string */ - uint64_t *slq_cnids; /* Pointer to array with CNIDs to which a query applies */ - size_t slq_cnids_num; /* Size of slq_cnids array */ - const char *slq_path; /* Path to file or dir, used in fetchAttributes */ - void *slq_tracker_cursor; /* Tracker SPARQL query result cursor */ - bool slq_allow_expr; /* Whether to allow logic expressions */ - uint64_t slq_result_limit; /* Whether to LIMIT SPARQL results, default of 0 means no limit */ + struct list_head slq_list; /* queries are stored in a list */ + slq_state_t slq_state; /* State */ + AFPObj *slq_obj; /* global AFPObj handle */ + const struct vol *slq_vol; /* volume handle */ + time_t slq_time; /* timestamp received query */ + uint64_t slq_ctx1; /* client context 1 */ + uint64_t slq_ctx2; /* client context 2 */ + sl_array_t *slq_reqinfo; /* array with requested metadata */ + const char *slq_qstring; /* the Spotlight query string */ + uint64_t *slq_cnids; /* Pointer to array with CNIDs */ + size_t slq_cnids_num; /* Size of slq_cnids array */ + void *tracker_cursor; /* Tracker SPARQL cursor */ + bool slq_allow_expr; /* Whether to allow expressions */ + uint64_t slq_result_limit; /* Whether to LIMIT SPARQL results */ + struct sl_rslts *query_results; /* query results */ +#ifdef HAVE_TRACKER + GCancellable *cancellable; +#endif } slq_t; -/************************************************************************************************** +struct sl_ctx { +#ifdef HAVE_TRACKER + TrackerSparqlConnection *tracker_con; + GCancellable *cancellable; + GMainLoop *mainloop; +#endif + slq_t *query_list; /* list of active queries */ +}; + +/****************************************************************************** * Function declarations - **************************************************************************************************/ + ******************************************************************************/ -extern int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen); +extern int spotlight_init(AFPObj *obj); +extern int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen _U_, + char *rbuf, size_t *rbuflen); extern int sl_pack(DALLOC_CTX *query, char *buf); extern int sl_unpack(DALLOC_CTX *query, const char *buf); extern void configure_spotlight_attributes(const char *attributes); diff --git a/include/atalk/util.h b/include/atalk/util.h index 6d92e15d..94aab1af 100644 --- a/include/atalk/util.h +++ b/include/atalk/util.h @@ -131,6 +131,10 @@ extern void *mod_symbol (void *, const char *); #define mod_close(a) dlclose(a) #endif /* ! HAVE_DLFCN_H */ +#ifndef strequal +#define strequal(a,b) (strcmp((a),(b)) == 0) +#endif + /****************************************************************** * locking.c ******************************************************************/ diff --git a/libatalk/util/netatalk_conf.c b/libatalk/util/netatalk_conf.c index b5652608..16ade10c 100644 --- a/libatalk/util/netatalk_conf.c +++ b/libatalk/util/netatalk_conf.c @@ -1861,9 +1861,6 @@ int afp_config_parse(AFPObj *AFPObj, char *processname) options->configfile = AFPObj->cmdlineconfigfile ? strdup(AFPObj->cmdlineconfigfile) : strdup(_PATH_CONFDIR "afp.conf"); options->sigconffile = strdup(_PATH_STATEDIR "afp_signature.conf"); options->uuidconf = strdup(_PATH_STATEDIR "afp_voluuid.conf"); -#ifdef HAVE_TRACKER_SPARQL - options->slmod_path = strdup(_PATH_AFPDUAMPATH "slmod_sparql.so"); -#endif options->flags = OPTION_UUID | AFPObj->cmdlineflags; if ((config = atalk_iniparser_load(AFPObj->options.configfile)) == NULL) @@ -2153,9 +2150,6 @@ void afp_config_free(AFPObj *obj) CONFIG_ARG_FREE(obj->options.fqdn); if (obj->options.ignored_attr) CONFIG_ARG_FREE(obj->options.ignored_attr); - if (obj->options.slmod_path) - CONFIG_ARG_FREE(obj->options.slmod_path); - if (obj->options.unixcodepage) CONFIG_ARG_FREE(obj->options.unixcodepage); if (obj->options.maccodepage) diff --git a/macros/netatalk.m4 b/macros/netatalk.m4 index 060e4679..ddbd3498 100644 --- a/macros/netatalk.m4 +++ b/macros/netatalk.m4 @@ -169,8 +169,6 @@ AC_DEFUN([AC_NETATALK_SPOTLIGHT], [ fi else AC_DEFINE(HAVE_TRACKER, 1, [Define if Tracker is available]) - AC_DEFINE(HAVE_TRACKER_SPARQL, 1, [Define if Tracker SPARQL is available]) - AC_DEFINE(HAVE_TRACKER_MINER, 1, [Define if Tracker miner library is available]) AC_DEFINE_UNQUOTED(TRACKER_PREFIX, ["$ac_cv_tracker_prefix"], [Path to Tracker]) AC_DEFINE([DBUS_DAEMON_PATH], ["/bin/dbus-daemon"], [Path to dbus-daemon]) fi @@ -183,7 +181,7 @@ AC_DEFUN([AC_NETATALK_SPOTLIGHT], [ AC_SUBST(TRACKER_LIBS) AC_SUBST(TRACKER_MINER_CFLAGS) AC_SUBST(TRACKER_MINER_LIBS) - AM_CONDITIONAL(HAVE_TRACKER_SPARQL, [test x"$ac_cv_have_tracker_sparql" = x"yes"]) + AM_CONDITIONAL(HAVE_TRACKER, [test x"$ac_cv_have_tracker_sparql" = x"yes"]) ]) dnl Whether to disable bundled libevent -- 2.39.2