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