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 */
23 #include <tracker-sparql.h>
24 #include <libtracker-miner/tracker-miner.h>
26 #include <atalk/util.h>
27 #include <atalk/errchk.h>
28 #include <atalk/logger.h>
29 #include <atalk/unix.h>
31 #include "spotlight.h"
32 #include "spotlight_rawquery_parser.h"
34 #define MAX_SL_RESULTS 20
36 static TrackerSparqlConnection *connection;
37 static TrackerMinerManager *manager;
39 static char *tracker_to_unix_path(const char *uri)
45 EC_NULL_LOG( f = g_file_new_for_uri(uri) );
46 EC_NULL_LOG( path = g_file_get_path(f) );
56 static int sl_mod_init(void *p)
62 LOG(log_info, logtype_sl, "Initializing Spotlight module");
65 setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
66 setenv("TRACKER_SPARQL_BACKEND", "bus", 1);
69 setenv("TRACKER_VERBOSITY", "3", 1);
70 dup2(type_configs[logtype_sl].fd, 1);
71 dup2(type_configs[logtype_sl].fd, 2);
75 connection = tracker_sparql_connection_get(NULL, &error);
76 manager = tracker_miner_manager_new_full(FALSE, &error);
80 LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
81 error ? error->message : "unknown error");
82 g_clear_error(&error);
87 LOG(log_error, logtype_sl, "Couldn't connect to Tracker miner");
88 g_clear_error(&error);
97 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
99 slq_t *slq = user_data;
100 TrackerSparqlCursor *cursor;
101 GError *error = NULL;
103 LOG(log_debug, logtype_sl, "tracker_cb");
105 cursor = tracker_sparql_connection_query_finish(connection, res, &error);
108 LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
109 error ? error->message : "unknown error");
110 g_clear_error(&error);
114 slq->slq_tracker_cursor = cursor;
117 static int sl_mod_start_search(void *p)
122 GError *error = NULL;
124 LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
126 EC_ZERO_LOGSTR( map_spotlight_to_sparql_query(slq, &sparql_query),
127 "Mapping Spotlight query failed: \"%s\"", slq->slq_qstring );
128 LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
131 /* Start the async query */
132 tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
136 slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
140 LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
141 error ? error->message : "unknown error");
142 g_clear_error(&error);
145 slq->slq_state = SLQ_STATE_RUNNING;
151 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
158 if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
159 dalloc_add_copy(fm_array, &nil, sl_nil_t);
163 LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
165 meta = talloc_zero(fm_array, sl_array_t);
167 for (i = 0; i < metacount; i++) {
168 if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
170 if ((p = strrchr(path, '/'))) {
171 name = dalloc_strdup(meta, p + 1);
172 dalloc_add(meta, name, "char *");
175 dalloc_add_copy(meta, &nil, sl_nil_t);
179 dalloc_add(fm_array, meta, sl_array_t);
185 static int sl_mod_fetch_result(void *p)
189 GError *error = NULL;
196 sl_array_t *fm_array;
199 gboolean qres, firstmatch = true;
201 if (!slq->slq_tracker_cursor) {
202 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
207 cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
208 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
209 cnids->ca_unkn1 = 0xadd;
210 cnids->ca_context = slq->slq_ctx2;
212 /* Prepare FileMeta */
213 fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
214 fm_array = talloc_zero(fm, sl_array_t);
215 dalloc_add(fm, fm_array, sl_array_t);
217 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
219 while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) {
221 qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error);
228 /* For some reason the list of results always starts with a nil entry */
229 dalloc_add_copy(fm_array, &nil, sl_nil_t);
234 uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
237 EC_NULL_LOG( path = tracker_to_unix_path(uri) );
239 if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
240 LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", path);
243 LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), path);
246 dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
247 add_filemeta(slq->slq_reqinfo, fm_array, id, path);
255 LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
256 error ? error->message : "unknown error");
257 g_clear_error (&error);
261 if (i < MAX_SL_RESULTS)
262 slq->slq_state = SLQ_STATE_DONE;
264 uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
265 dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
266 dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
267 dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
271 if (slq->slq_tracker_cursor) {
272 g_object_unref(slq->slq_tracker_cursor);
273 slq->slq_tracker_cursor = NULL;
279 /* Free ressources allocated in this module */
280 static int sl_mod_close_query(void *p)
285 if (slq->slq_tracker_cursor) {
286 g_object_unref(slq->slq_tracker_cursor);
287 slq->slq_tracker_cursor = NULL;
294 static int sl_mod_error(void *p)
302 if (slq->slq_tracker_cursor) {
303 g_object_unref(slq->slq_tracker_cursor);
304 slq->slq_tracker_cursor = NULL;
311 static int sl_mod_index_file(const void *p)
313 #ifdef HAVE_TRACKER_MINER
320 GError *error = NULL;
323 file = g_file_new_for_commandline_arg(f);
326 tracker_miner_manager_index_file(manager, file, &error);
330 LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): indexing failed", f);
332 LOG(log_debug, logtype_sl, "sl_mod_index_file(\"%s\"): indexing file was successful", f);
336 g_object_unref(file);
343 struct sl_module_export sl_mod = {