]> arthur.barton.de Git - netatalk.git/blob - etc/spotlight/slmod_sparql.c
Fix check for unsupported attributes
[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 #if 0 
77     /*
78      * Disabled for now, hangs in tracker_miner_manager_new_full() since adding
79      * the dbus afpstats stuff.
80      */
81     manager = tracker_miner_manager_new_full(FALSE, &error);
82 #endif
83     unbecome_root();
84
85     if (!connection) {
86         LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
87             error ? error->message : "unknown error");
88         g_clear_error(&error);
89         EC_FAIL;
90     }
91
92 #if 0
93     if (!manager) {
94         LOG(log_error, logtype_sl, "Couldn't connect to Tracker miner");
95         g_clear_error(&error);
96         EC_FAIL;
97     }
98 #endif
99
100 EC_CLEANUP:
101     EC_EXIT;
102 }
103
104
105 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
106 {
107     slq_t *slq = user_data;
108     TrackerSparqlCursor *cursor;
109     GError *error = NULL;
110
111     LOG(log_debug, logtype_sl, "tracker_cb");
112
113     cursor = tracker_sparql_connection_query_finish(connection, res, &error);
114
115     if (error) {
116         LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
117             error ? error->message : "unknown error");
118         g_clear_error(&error);
119         return;
120     }
121
122     slq->slq_tracker_cursor = cursor;
123 }
124
125 static int sl_mod_start_search(void *p)
126 {
127     EC_INIT;
128     slq_t *slq = p; 
129     gchar *sparql_query;
130     GError *error = NULL;
131
132     LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
133
134     EC_ZERO_LOGSTR( map_spotlight_to_sparql_query(slq, &sparql_query),
135                     "Mapping Spotlight query failed: \"%s\"", slq->slq_qstring );
136     LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
137
138 #if 0
139     /* Start the async query */
140     tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
141 #endif
142
143     become_root();
144     slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
145     unbecome_root();
146
147     if (error) {
148         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
149             error ? error->message : "unknown error");
150         g_clear_error(&error);
151         EC_FAIL;
152     }
153     slq->slq_state = SLQ_STATE_RUNNING;
154
155 EC_CLEANUP:
156     EC_EXIT;
157 }
158
159 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
160 {
161     EC_INIT;
162     sl_array_t *meta;
163     sl_nil_t nil = 0;
164     int i, metacount;
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             char *p, *name;
178             if ((p = strrchr(path, '/'))) {
179                 name = dalloc_strdup(meta, p + 1);
180                 dalloc_add(meta, name, "char *");
181             }
182         } else {
183             dalloc_add_copy(meta, &nil, sl_nil_t);
184         }
185     }
186
187     dalloc_add(fm_array, meta, sl_array_t);
188
189 EC_CLEANUP:
190     EC_EXIT;
191 }
192
193 static int cnid_cmp_fn(const void *p1, const void *p2)
194 {
195     const uint64_t *cnid1 = p1, *cnid2 = p2;
196     if (*cnid1 == *cnid2)
197         return 0;
198     if (*cnid1 < *cnid2)
199         return -1;
200     else
201         return 1;            
202 }
203
204 static int sl_mod_fetch_result(void *p)
205 {
206     EC_INIT;
207     slq_t *slq = p;
208     GError *error = NULL;
209     int i = 0;
210     cnid_t did, id;
211     const gchar *uri;
212     char *path;
213     sl_cnids_t *cnids;
214     sl_filemeta_t *fm;
215     sl_array_t *fm_array;
216     sl_nil_t nil;
217     uint64_t uint64;
218     gboolean qres, firstmatch = true;
219
220     if (!slq->slq_tracker_cursor) {
221         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
222         goto EC_CLEANUP;
223     }
224
225     /* Prepare CNIDs */
226     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
227     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
228     cnids->ca_unkn1 = 0xadd;
229     cnids->ca_context = slq->slq_ctx2;
230
231     /* Prepare FileMeta */
232     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
233     fm_array = talloc_zero(fm, sl_array_t);
234     dalloc_add(fm, fm_array, sl_array_t);
235
236     LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
237
238     while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) {
239         become_root();
240         qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error);
241         unbecome_root();
242
243         if (!qres)
244             break;
245
246         if (firstmatch) {
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             firstmatch = false;
250         }
251
252         become_root();
253         uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
254         unbecome_root();
255
256         EC_NULL_LOG( path = tracker_to_unix_path(uri) );
257
258         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
259             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", path);
260             goto loop_cleanup;
261         }
262         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), path);
263
264         uint64 = ntohl(id);
265         if (slq->slq_cnids) {
266             if (!bsearch(&uint64, slq->slq_cnids, slq->slq_cnids_num, sizeof(uint64_t), cnid_cmp_fn))
267                 goto loop_cleanup;
268         }
269
270         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
271         add_filemeta(slq->slq_reqinfo, fm_array, id, path);
272
273     loop_cleanup:
274         g_free(path);
275         i++;
276    }
277
278     if (error) {
279         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
280             error ? error->message : "unknown error");
281         g_clear_error (&error);
282         EC_FAIL;
283     }
284
285     if (i < MAX_SL_RESULTS)
286         slq->slq_state = SLQ_STATE_DONE;
287
288     uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
289     dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
290     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
291     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
292
293 EC_CLEANUP:
294     if (ret != 0) {
295         if (slq->slq_tracker_cursor) {
296             g_object_unref(slq->slq_tracker_cursor);
297             slq->slq_tracker_cursor = NULL;
298         }
299     }
300     EC_EXIT;
301 }
302
303 /* Free ressources allocated in this module */
304 static int sl_mod_close_query(void *p)
305 {
306     EC_INIT;
307     slq_t *slq = p;
308
309     if (slq->slq_tracker_cursor) {
310         g_object_unref(slq->slq_tracker_cursor);
311         slq->slq_tracker_cursor = NULL;
312     }
313
314 EC_CLEANUP:
315     EC_EXIT;
316 }
317
318 static int sl_mod_error(void *p)
319 {
320     EC_INIT;
321     slq_t *slq = p;
322
323     if (!slq)
324         goto EC_CLEANUP;
325
326     if (slq->slq_tracker_cursor) {
327         g_object_unref(slq->slq_tracker_cursor);
328         slq->slq_tracker_cursor = NULL;
329     }
330
331 EC_CLEANUP:
332     EC_EXIT;
333 }
334
335 static int sl_mod_index_file(const void *p)
336 {
337     return 0;
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 };