2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
25 #include <atalk/util.h>
26 #include <atalk/errchk.h>
27 #include <atalk/logger.h>
28 #include <atalk/unix.h>
29 #include <atalk/spotlight.h>
31 #include "slmod_rdf_parser.h"
33 #define MAX_SL_RESULTS 20
35 TrackerClient *client;
37 static int sl_mod_init(void *p)
43 LOG(log_info, logtype_sl, "Initializing Tracker 0.6 RDF Spotlight module");
46 setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
48 client = tracker_connect(FALSE);
51 LOG(log_error, logtype_sl, "Failed connecting to Tracker");
60 static int sl_mod_start_search(void *p)
66 EC_ZERO_LOG( map_spotlight_to_rdf_query(slq) );
68 LOG(log_debug, logtype_sl, "sl_mod_start_search: Tracker service: %s, FTS: %s, RDF query:\n%s",
69 tracker_type_to_service_name(slq->slq_service),
71 slq->slq_trackerquery ? slq->slq_trackerquery : "false");
73 if (slq->slq_trackerquery)
74 slq->slq_state = SLQ_STATE_RUNNING;
76 slq->slq_state = SLQ_STATE_DONE;
82 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
89 if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
90 dalloc_add_copy(fm_array, &nil, sl_nil_t);
94 LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
96 meta = talloc_zero(fm_array, sl_array_t);
98 for (i = 0; i < metacount; i++) {
99 if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
101 if ((p = strrchr(path, '/'))) {
102 name = dalloc_strdup(meta, p + 1);
103 dalloc_add(meta, name, "char *");
106 dalloc_add_copy(meta, &nil, sl_nil_t);
110 dalloc_add(fm_array, meta, sl_array_t);
116 static int cnid_cmp_fn(const void *p1, const void *p2)
118 const uint64_t *cnid1 = p1, *cnid2 = p2;
119 if (*cnid1 == *cnid2)
127 static int sl_mod_fetch_result(void *p)
131 GError *error = NULL;
136 sl_array_t *fm_array;
139 gboolean qres, firstmatch = true;
140 GPtrArray *array = NULL;
143 cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
144 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
145 cnids->ca_unkn1 = 0xadd;
146 cnids->ca_context = slq->slq_ctx2;
148 /* Prepare FileMeta */
149 fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
150 fm_array = talloc_zero(fm, sl_array_t);
151 dalloc_add(fm, fm_array, sl_array_t);
153 LOG(log_debug, logtype_sl, "sl_mod_fetch_result");
155 if (slq->slq_state == SLQ_STATE_RUNNING) {
157 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: calling tracker");
158 array = tracker_search_query(client,
162 slq->slq_fts, /* FTS search test */
164 slq->slq_trackerquery,
167 FALSE, /* Sort by service */
173 slq->slq_state = SLQ_STATE_DONE;
174 LOG(log_error, logtype_sl, "Couldn't query Tracker: '%s'", error->message);
175 g_clear_error(&error);
180 slq->slq_state = SLQ_STATE_DONE;
181 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
185 while (i < array->len) {
186 char **resmeta = g_ptr_array_index(array, i);
187 char *respath = resmeta[0];
188 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: result %d: %s", slq->slq_offset, respath);
191 /* For some reason the list of results always starts with a nil entry */
192 dalloc_add_copy(fm_array, &nil, sl_nil_t);
196 if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, respath, &did)) == CNID_INVALID) {
197 LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", respath);
200 LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), respath);
203 if (slq->slq_cnids) {
204 if (!bsearch(&uint64, slq->slq_cnids, slq->slq_cnids_num, sizeof(uint64_t), cnid_cmp_fn))
208 dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
209 add_filemeta(slq->slq_reqinfo, fm_array, id, respath);
216 g_ptr_array_free(array, TRUE);
219 if (i < MAX_SL_RESULTS)
220 slq->slq_state = SLQ_STATE_DONE;
223 uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
224 dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
225 dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
226 dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
230 g_ptr_array_free(array, TRUE);
234 /* Free ressources allocated in this module */
235 static int sl_mod_close_query(void *p)
244 static int sl_mod_error(void *p)
256 static int sl_mod_index_file(const void *p)
261 struct sl_module_export sl_mod = {