]> arthur.barton.de Git - netatalk.git/blob - etc/spotlight/slmod_sparql.c
Merge remote-tracking branch 'origin/branch-netatalk-3-0' into develop
[netatalk.git] / etc / spotlight / slmod_sparql.c
1 /*
2   Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
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 static TrackerMinerManager *manager;
38
39 static char *tracker_to_unix_path(const char *uri)
40 {
41     EC_INIT;
42     GFile *f = NULL;
43     char *path = NULL;
44
45     EC_NULL_LOG( f = g_file_new_for_uri(uri) );
46     EC_NULL_LOG( path = g_file_get_path(f) );
47
48 EC_CLEANUP:
49     if (f)
50         g_object_unref(f);
51     if (ret != 0)
52         return NULL;
53     return path;
54 }
55
56 static int sl_mod_init(void *p)
57 {
58     EC_INIT;
59     GError *error = NULL;
60     const char *msg = p;
61
62     LOG(log_info, logtype_sl, "Initializing Spotlight module");
63
64     g_type_init();
65     setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/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     manager = tracker_miner_manager_new_full(FALSE, &error);
77     unbecome_root();
78
79     if (!connection) {
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);
83         EC_FAIL;
84     }
85
86     if (!manager) {
87         LOG(log_error, logtype_sl, "Couldn't connect to Tracker miner");
88         g_clear_error(&error);
89         EC_FAIL;
90     }
91
92 EC_CLEANUP:
93     EC_EXIT;
94 }
95
96
97 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
98 {
99     slq_t *slq = user_data;
100     TrackerSparqlCursor *cursor;
101     GError *error = NULL;
102
103     LOG(log_debug, logtype_sl, "tracker_cb");
104
105     cursor = tracker_sparql_connection_query_finish(connection, res, &error);
106
107     if (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);
111         return;
112     }
113
114     slq->slq_tracker_cursor = cursor;
115 }
116
117 static int sl_mod_start_search(void *p)
118 {
119     EC_INIT;
120     slq_t *slq = p; 
121     gchar *sparql_query;
122     GError *error = NULL;
123
124     LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
125
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);
129
130 #if 0
131     /* Start the async query */
132     tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
133 #endif
134
135     become_root();
136     slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
137     unbecome_root();
138
139     if (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);
143         EC_FAIL;
144     }
145     slq->slq_state = SLQ_STATE_RUNNING;
146
147 EC_CLEANUP:
148     EC_EXIT;
149 }
150
151 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
152 {
153     EC_INIT;
154     sl_array_t *meta;
155     sl_nil_t nil = 0;
156     int i, metacount;
157
158     if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
159         dalloc_add_copy(fm_array, &nil, sl_nil_t);
160         goto EC_CLEANUP;
161     }
162
163     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
164
165     meta = talloc_zero(fm_array, sl_array_t);
166
167     for (i = 0; i < metacount; i++) {
168         if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
169             char *p, *name;
170             if ((p = strrchr(path, '/'))) {
171                 name = dalloc_strdup(meta, p + 1);
172                 dalloc_add(meta, name, "char *");
173             }
174         } else {
175             dalloc_add_copy(meta, &nil, sl_nil_t);
176         }
177     }
178
179     dalloc_add(fm_array, meta, sl_array_t);
180
181 EC_CLEANUP:
182     EC_EXIT;
183 }
184
185 static int cnid_cmp_fn(const void *p1, const void *p2)
186 {
187     const uint64_t *cnid1 = p1, *cnid2 = p2;
188     if (*cnid1 == *cnid2)
189         return 0;
190     if (*cnid1 < *cnid2)
191         return -1;
192     else
193         return 1;            
194 }
195
196 static int sl_mod_fetch_result(void *p)
197 {
198     EC_INIT;
199     slq_t *slq = p;
200     GError *error = NULL;
201     int i = 0;
202     cnid_t did, id;
203     const gchar *uri;
204     char *path;
205     sl_cnids_t *cnids;
206     sl_filemeta_t *fm;
207     sl_array_t *fm_array;
208     sl_nil_t nil;
209     uint64_t uint64;
210     gboolean qres, firstmatch = true;
211
212     if (!slq->slq_tracker_cursor) {
213         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
214         goto EC_CLEANUP;
215     }
216
217     /* Prepare CNIDs */
218     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
219     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
220     cnids->ca_unkn1 = 0xadd;
221     cnids->ca_context = slq->slq_ctx2;
222
223     /* Prepare FileMeta */
224     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
225     fm_array = talloc_zero(fm, sl_array_t);
226     dalloc_add(fm, fm_array, sl_array_t);
227
228     LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
229
230     while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) {
231         become_root();
232         qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error);
233         unbecome_root();
234
235         if (!qres)
236             break;
237
238         if (firstmatch) {
239             /* For some reason the list of results always starts with a nil entry */
240             dalloc_add_copy(fm_array, &nil, sl_nil_t);
241             firstmatch = false;
242         }
243
244         become_root();
245         uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
246         unbecome_root();
247
248         EC_NULL_LOG( path = tracker_to_unix_path(uri) );
249
250         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
251             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", path);
252             goto loop_cleanup;
253         }
254         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), path);
255
256         uint64 = ntohl(id);
257         if (slq->slq_cnids) {
258             if (!bsearch(&uint64, slq->slq_cnids, slq->slq_cnids_num, sizeof(uint64_t), cnid_cmp_fn))
259                 goto loop_cleanup;
260         }
261
262         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
263         add_filemeta(slq->slq_reqinfo, fm_array, id, path);
264
265     loop_cleanup:
266         g_free(path);
267         i++;
268    }
269
270     if (error) {
271         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
272             error ? error->message : "unknown error");
273         g_clear_error (&error);
274         EC_FAIL;
275     }
276
277     if (i < MAX_SL_RESULTS)
278         slq->slq_state = SLQ_STATE_DONE;
279
280     uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
281     dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
282     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
283     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
284
285 EC_CLEANUP:
286     if (ret != 0) {
287         if (slq->slq_tracker_cursor) {
288             g_object_unref(slq->slq_tracker_cursor);
289             slq->slq_tracker_cursor = NULL;
290         }
291     }
292     EC_EXIT;
293 }
294
295 /* Free ressources allocated in this module */
296 static int sl_mod_close_query(void *p)
297 {
298     EC_INIT;
299     slq_t *slq = p;
300
301     if (slq->slq_tracker_cursor) {
302         g_object_unref(slq->slq_tracker_cursor);
303         slq->slq_tracker_cursor = NULL;
304     }
305
306 EC_CLEANUP:
307     EC_EXIT;
308 }
309
310 static int sl_mod_error(void *p)
311 {
312     EC_INIT;
313     slq_t *slq = p;
314
315     if (!slq)
316         goto EC_CLEANUP;
317
318     if (slq->slq_tracker_cursor) {
319         g_object_unref(slq->slq_tracker_cursor);
320         slq->slq_tracker_cursor = NULL;
321     }
322
323 EC_CLEANUP:
324     EC_EXIT;
325 }
326
327 static int sl_mod_index_file(const void *p)
328 {
329     /*
330      * This seems to cause index problems on volumes that are watched and indexed
331      * by Tracker, so we disable this extra manual indexing for now.
332      * It's primary pupose was ensuring files created via AFP are indexed on large
333      * volumes where the filesystem event notification engine (eg FAM or FEN) may
334      * impose limits on the maximum number of watched directories.
335      */
336     return 0;
337
338 #ifdef HAVE_TRACKER_MINER
339     EC_INIT;
340     const char *f = p;
341
342     if (!f)
343         goto EC_CLEANUP;
344
345     GError *error = NULL;
346     GFile *file = NULL;
347
348     file = g_file_new_for_commandline_arg(f);
349
350     become_root();
351     tracker_miner_manager_index_file(manager, file, &error);
352     unbecome_root();
353
354     if (error)
355         LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): indexing failed", f);
356     else
357         LOG(log_debug, logtype_sl, "sl_mod_index_file(\"%s\"): indexing file was successful", f);
358
359 EC_CLEANUP:
360     if (file)
361         g_object_unref(file);
362     EC_EXIT;
363 #else
364     return 0;
365 #endif
366 }
367
368 struct sl_module_export sl_mod = {
369     SL_MODULE_VERSION,
370     sl_mod_init,
371     sl_mod_start_search,
372     sl_mod_fetch_result,
373     sl_mod_close_query,
374     sl_mod_error,
375     sl_mod_index_file
376 };