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