+static int sl_rpc_openQuery(AFPObj *obj,
+ const DALLOC_CTX *query,
+ DALLOC_CTX *reply,
+ struct vol *v)
+{
+ EC_INIT;
+ char *sl_query;
+ uint64_t *uint64;
+ DALLOC_CTX *reqinfo;
+ sl_array_t *array;
+ sl_cnids_t *cnids;
+ 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(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_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 */
+ 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, "Spotlight query: \"%s\"", slq->slq_qstring);
+
+ slq->slq_time = time(NULL);
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ slq->slq_ctx1 = *uint64;
+
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ slq->slq_ctx2 = *uint64;
+
+ 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);
+
+ cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1,
+ "kMDQueryItemArray");
+ if (cnids) {
+ EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
+ }
+
+ 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;
+ }
+
+ slq->slq_state = SLQ_STATE_RUNNING;
+ slq_add(slq);
+
+EC_CLEANUP:
+ if (ret != 0) {
+ 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)
+{
+ EC_INIT;
+ slq_t *slq = NULL;
+ 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 */
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ ctx1 = *uint64;
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ ctx2 = *uint64;
+
+ /* Get query for context */
+ slq = slq_for_ctx(ctx1, ctx2);
+ if (slq == NULL) {
+ EC_FAIL;
+ }
+
+ 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;
+
+ case SLQ_STATE_ERROR:
+ LOG(log_error, logtype_sl, "query in error state");
+ EC_FAIL;
+
+ 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:
+ 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)
+{
+ EC_INIT;
+ uint64_t uint64;
+ sl_array_t *array;
+ 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", 2) );
+ memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
+ id = (cnid_t)uint64;
+ LOG(log_debug, logtype_sl, "CNID: %" PRIu32, id);
+
+ if (htonl(id) == DIRDID_ROOT) {
+ path = vol->v_path;
+ } else if (id < CNID_START) {
+ EC_FAIL;
+ } else {
+ cnid_t did;
+ char buffer[12 + MAXPATHLEN + 1];
+
+ did = htonl(id);
+ EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
+ EC_NULL_LOG( dir = dirlookup(vol, did) );
+ EC_NEG1_LOG( movecwd(vol, dir) );
+ }
+
+ /*
+ * We're possibly supposed to update attributes in two places: the
+ * database and the filesystem. Due to the lack of documentation
+ * and not yet implemented database updates, we cherry pick attributes
+ * that seems to be candidates for updating filesystem metadata.
+ */
+
+ if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemFSContentChangeDate"))) {
+ struct utimbuf utimes;
+ utimes.actime = utimes.modtime = sl_time->tv_sec;
+ utime(path, &utimes);
+ }
+
+ 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);
+
+EC_CLEANUP:
+ EC_EXIT;
+}
+
+static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
+{
+ EC_INIT;
+ uint64_t uint64;
+ sl_cnids_t *cnids;
+ 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;
+ LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
+
+ if (htonl(id) == DIRDID_ROOT) {
+ path = vol->v_path;
+ } else if (id < CNID_START) {
+ EC_FAIL;
+ } else {
+ cnid_t did;
+ char buffer[12 + MAXPATHLEN + 1];
+
+ did = htonl(id);
+ EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
+ EC_NULL_LOG( dir = dirlookup(vol, did) );
+ EC_NEG1_LOG( movecwd(vol, dir) );
+ }
+
+ /* Result array */
+ sl_array_t *array = talloc_zero(reply, sl_array_t);
+ dalloc_add(reply, array, sl_array_t);
+
+ /* Return result value 0 */
+ uint64_t 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(array, replycnids, sl_cnids_t);
+
+ /* Return filemeta array */
+
+ /*
+ * FIXME: this should return the real attributes from all known metadata sources
+ * (Tracker and filesystem)
+ */
+ sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
+
+ sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
+ dalloc_add(fmeta, mdattrs, sl_array_t);
+ dalloc_add(array, fmeta, sl_filemeta_t);
+
+EC_CLEANUP:
+ EC_EXIT;
+}
+
+static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
+{
+ EC_INIT;
+ uint64_t uint64;
+ sl_cnids_t *cnids, *replycnids;
+ cnid_t id, did;
+ struct dir *dir;
+ 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;
+ }
+
+ memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
+ id = (cnid_t)uint64;
+
+ if (htonl(id) == DIRDID_ROOT) {
+ path = talloc_strdup(reply, vol->v_path);
+ } else if (id < CNID_START) {
+ EC_FAIL;
+ } else {
+ did = htonl(id);
+ EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
+ EC_NULL( dir = dirlookup(vol, did) );
+ EC_NULL( path = talloc_asprintf(reply, "%s/%s", bdata(dir->d_fullpath), name) );
+ }
+
+ EC_ZERO( stat(path, &sb) );
+
+ sl_res = 0;
+ dalloc_add_copy(array, &sl_res, uint64_t);
+
+ 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(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);
+
+ /* 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)
+{
+ 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 */
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ ctx1 = *uint64;
+ 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 */
+ slq = slq_for_ctx(ctx1, ctx2);
+ if (slq == NULL) {
+ EC_FAIL;
+ }
+
+ 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) {
+ sl_result = UINT64_MAX;
+ }
+ dalloc_add_copy(array, &sl_result, uint64_t);
+ dalloc_add(reply, array, sl_array_t);
+ EC_EXIT;
+}
+
+/******************************************************************************
+ * Spotlight functions
+ ******************************************************************************/
+
+int spotlight_init(AFPObj *obj)
+{
+ const char *attributes;
+ struct sl_ctx *sl_ctx;
+
+ LOG(log_info, logtype_sl, "Initializing Spotlight");
+
+ sl_ctx = talloc_zero(NULL, struct sl_ctx);
+ obj->sl_ctx = sl_ctx;
+
+ attributes = atalk_iniparser_getstring(obj->iniconfig, INISEC_GLOBAL,
+ "spotlight attributes", NULL);
+ if (attributes) {
+ configure_spotlight_attributes(attributes);
+ }
+
+ /*
+ * 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();
+
+ 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;
+}
+
+/******************************************************************************