]> arthur.barton.de Git - netatalk.git/blob - etc/spotlight/slmod_sparql.c
Spotlight: use async Tracker SPARQL API
[netatalk.git] / etc / spotlight / slmod_sparql.c
1 /*
2   Copyright (C) Ralph Boehme 2012-2014
3
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.
8
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.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <locale.h>
21
22 #include <gio/gio.h>
23 #include <tracker-sparql.h>
24 #include <libtracker-miner/tracker-miner.h>
25
26 #include <atalk/util.h>
27 #include <atalk/errchk.h>
28 #include <atalk/logger.h>
29 #include <atalk/unix.h>
30 #include <atalk/spotlight.h>
31
32 #include "slmod_sparql_parser.h"
33
34 #define MAX_SL_RESULTS 20
35
36 static TrackerSparqlConnection *connection;
37
38 static char *tracker_to_unix_path(const char *uri)
39 {
40     EC_INIT;
41     GFile *f = NULL;
42     char *path = NULL;
43
44     EC_NULL_LOG( f = g_file_new_for_uri(uri) );
45     EC_NULL_LOG( path = g_file_get_path(f) );
46
47 EC_CLEANUP:
48     if (f)
49         g_object_unref(f);
50     if (ret != 0)
51         return NULL;
52     return path;
53 }
54
55 static int sl_mod_init(void *p)
56 {
57     EC_INIT;
58     GError *error = NULL;
59     AFPObj *obj = (AFPObj *)p;
60     const char *attributes;
61
62     LOG(log_info, logtype_sl, "Initializing Spotlight module");
63
64     g_type_init();
65     setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=" _PATH_STATEDIR "/spotlight.ipc", 1);
66     setenv("TRACKER_SPARQL_BACKEND", "bus", 1);
67
68 #ifdef DEBUG
69     setenv("TRACKER_VERBOSITY", "3", 1);
70     dup2(type_configs[logtype_sl].fd, 1);
71     dup2(type_configs[logtype_sl].fd, 2);
72 #endif
73
74     become_root();
75     connection = tracker_sparql_connection_get(NULL, &error);
76 #if 0 /* this may hang, so disable it as we don't use the miner anyway  */
77     manager = tracker_miner_manager_new_full(FALSE, &error);
78 #endif
79     unbecome_root();
80
81     if (!connection) {
82         LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
83             error ? error->message : "unknown error");
84         g_clear_error(&error);
85         EC_FAIL;
86     }
87
88 #if 0
89     if (!manager) {
90         LOG(log_error, logtype_sl, "Couldn't connect to Tracker miner");
91         g_clear_error(&error);
92         EC_FAIL;
93     }
94 #endif
95
96     attributes = atalk_iniparser_getstring(obj->iniconfig, INISEC_GLOBAL, "spotlight attributes", NULL);
97     if (attributes) {
98         configure_spotlight_attributes(attributes);
99     }
100
101 EC_CLEANUP:
102     EC_EXIT;
103 }
104
105
106 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
107 {
108     slq_t *slq = user_data;
109     TrackerSparqlCursor *cursor;
110     GError *error = NULL;
111
112     LOG(log_debug, logtype_sl, "tracker_cb");
113
114     cursor = tracker_sparql_connection_query_finish(connection, res, &error);
115
116     if (error) {
117         LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
118             error ? error->message : "unknown error");
119         g_clear_error(&error);
120         return;
121     }
122
123     slq->slq_tracker_cursor = cursor;
124 }
125
126 static int sl_mod_start_search(void *p)
127 {
128     EC_INIT;
129     slq_t *slq = p; 
130     gchar *sparql_query;
131     GError *error = NULL;
132
133     LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
134
135     EC_ZERO_LOGSTR( map_spotlight_to_sparql_query(slq, &sparql_query),
136                     "Mapping Spotlight query failed: \"%s\"", slq->slq_qstring );
137     LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
138
139 #if 0
140     /* Start the async query */
141     tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
142 #endif
143
144     become_root();
145     slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
146     unbecome_root();
147
148     if (error) {
149         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
150             error ? error->message : "unknown error");
151         g_clear_error(&error);
152         EC_FAIL;
153     }
154     slq->slq_state = SLQ_STATE_RUNNING;
155
156 EC_CLEANUP:
157     EC_EXIT;
158 }
159
160 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path, const struct stat *sp)
161 {
162     EC_INIT;
163     sl_array_t *meta;
164     sl_nil_t nil = 0;
165     int i, metacount;
166     uint64_t uint64;
167     sl_time_t sl_time;
168
169     if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
170         dalloc_add_copy(fm_array, &nil, sl_nil_t);
171         goto EC_CLEANUP;
172     }
173
174     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
175
176     meta = talloc_zero(fm_array, sl_array_t);
177
178     for (i = 0; i < metacount; i++) {
179         if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName") ||
180             STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSName")) {
181             char *p, *name;
182             if ((p = strrchr(path, '/'))) {
183                 name = dalloc_strdup(meta, p + 1);
184                 dalloc_add(meta, name, "char *");
185             }
186         } else if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSSize")) {
187             uint64 = sp->st_size;
188             dalloc_add_copy(meta, &uint64, uint64_t);
189         } else if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSOwnerUserID")) {
190             uint64 = sp->st_uid;
191             dalloc_add_copy(meta, &uint64, uint64_t);
192         } else if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSOwnerGroupID")) {
193             uint64 = sp->st_gid;
194             dalloc_add_copy(meta, &uint64, uint64_t);
195         } else if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSContentChangeDate")) {
196             sl_time.tv_sec = sp->st_mtime;
197             dalloc_add_copy(meta, &sl_time, sl_time_t);
198         } else {
199             dalloc_add_copy(meta, &nil, sl_nil_t);
200         }
201     }
202
203     dalloc_add(fm_array, meta, sl_array_t);
204
205 EC_CLEANUP:
206     EC_EXIT;
207 }
208
209 static int cnid_cmp_fn(const void *p1, const void *p2)
210 {
211     const uint64_t *cnid1 = p1, *cnid2 = p2;
212     if (*cnid1 == *cnid2)
213         return 0;
214     if (*cnid1 < *cnid2)
215         return -1;
216     else
217         return 1;            
218 }
219
220 static int sl_mod_fetch_result(void *p)
221 {
222     EC_INIT;
223     slq_t *slq = p;
224     GError *error = NULL;
225     int i = 0;
226     cnid_t did, id;
227     const gchar *uri;
228     char *path;
229     sl_cnids_t *cnids;
230     sl_filemeta_t *fm;
231     sl_array_t *fm_array;
232     sl_nil_t nil;
233     uint64_t uint64;
234     gboolean qres, firstmatch = true;
235
236     if (!slq->slq_tracker_cursor) {
237         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
238         goto EC_CLEANUP;
239     }
240
241     /* Prepare CNIDs */
242     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
243     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
244     cnids->ca_unkn1 = 0xadd;
245     cnids->ca_context = slq->slq_ctx2;
246
247     /* Prepare FileMeta */
248     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
249     fm_array = talloc_zero(fm, sl_array_t);
250     /* For some reason the list of results always starts with a nil entry */
251     dalloc_add_copy(fm_array, &nil, sl_nil_t);
252     dalloc_add(fm, fm_array, sl_array_t);
253
254     LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
255
256     while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) {
257         become_root();
258         qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error);
259         unbecome_root();
260
261         if (!qres)
262             break;
263
264 #if 0
265         if (firstmatch) {
266             /* For some reason the list of results always starts with a nil entry */
267             dalloc_add_copy(fm_array, &nil, sl_nil_t);
268             firstmatch = false;
269         }
270 #endif
271
272         become_root();
273         uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
274         unbecome_root();
275
276         EC_NULL_LOG( path = tracker_to_unix_path(uri) );
277
278         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
279             LOG(log_debug, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", path);
280             goto loop_cleanup;
281         }
282         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), path);
283
284         uint64 = ntohl(id);
285         if (slq->slq_cnids) {
286             if (!bsearch(&uint64, slq->slq_cnids, slq->slq_cnids_num, sizeof(uint64_t), cnid_cmp_fn))
287                 goto loop_cleanup;
288         }
289
290         struct stat sb;
291         if (stat(path, &sb) != 0)
292             goto loop_cleanup;
293
294         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
295         add_filemeta(slq->slq_reqinfo, fm_array, id, path, &sb);
296
297     loop_cleanup:
298         g_free(path);
299         i++;
300    }
301
302     if (error) {
303         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
304             error ? error->message : "unknown error");
305         g_clear_error (&error);
306         EC_FAIL;
307     }
308
309     if (i < MAX_SL_RESULTS)
310         slq->slq_state = SLQ_STATE_DONE;
311
312     uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
313     dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
314     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
315     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
316
317 EC_CLEANUP:
318     if (ret != 0) {
319         if (slq->slq_tracker_cursor) {
320             g_object_unref(slq->slq_tracker_cursor);
321             slq->slq_tracker_cursor = NULL;
322         }
323     }
324     EC_EXIT;
325 }
326
327 /* Free ressources allocated in this module */
328 static int sl_mod_close_query(void *p)
329 {
330     EC_INIT;
331     slq_t *slq = p;
332
333     if (slq->slq_tracker_cursor) {
334         g_object_unref(slq->slq_tracker_cursor);
335         slq->slq_tracker_cursor = NULL;
336     }
337
338 EC_CLEANUP:
339     EC_EXIT;
340 }
341
342 static int sl_mod_fetch_attrs(void *p)
343 {
344     EC_INIT;
345     slq_t *slq = p;
346     sl_filemeta_t *fm;
347     sl_array_t *fm_array;
348     sl_nil_t nil;
349
350     LOG(log_debug, logtype_sl, "sl_mod_fetch_attrs(\"%s\")", slq->slq_path);
351
352     struct stat sb;
353     EC_ZERO( stat(slq->slq_path, &sb) );
354
355     /* Prepare FileMeta */
356     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
357     fm_array = talloc_zero(fm, sl_array_t);
358     dalloc_add(fm, fm_array, fm_array_t);
359     /* For some reason the list of results always starts with a nil entry */
360     dalloc_add_copy(fm_array, &nil, sl_nil_t);
361
362     add_filemeta(slq->slq_reqinfo, fm_array, CNID_INVALID, slq->slq_path, &sb);
363
364     /* Now add result */
365     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
366
367 EC_CLEANUP:
368     EC_EXIT;
369 }
370
371 static int sl_mod_error(void *p)
372 {
373     EC_INIT;
374     slq_t *slq = p;
375
376     if (!slq)
377         goto EC_CLEANUP;
378
379     if (slq->slq_tracker_cursor) {
380         g_object_unref(slq->slq_tracker_cursor);
381         slq->slq_tracker_cursor = NULL;
382     }
383
384 EC_CLEANUP:
385     EC_EXIT;
386 }
387
388 static int sl_mod_index_file(const void *p)
389 {
390     /*
391      * This seems to cause index problems on volumes that are watched and indexed
392      * by Tracker, so we disable this extra manual indexing for now.
393      * It's primary pupose was ensuring files created via AFP are indexed on large
394      * volumes where the filesystem event notification engine (eg FAM or FEN) may
395      * impose limits on the maximum number of watched directories.
396      */
397     return 0;
398 }
399
400 struct sl_module_export sl_mod = {
401     SL_MODULE_VERSION,
402     sl_mod_init,
403     sl_mod_start_search,
404     sl_mod_fetch_result,
405     sl_mod_close_query,
406     sl_mod_fetch_attrs,
407     sl_mod_error,
408     sl_mod_index_file
409 };