]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_module.c
Bound checking when marshalling SL query results
[netatalk.git] / etc / afpd / spotlight_module.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
21 #include <gio/gio.h>
22 #include <tracker-sparql.h>
23 #include <libtracker-miner/tracker-miner.h>
24
25 #include <atalk/util.h>
26 #include <atalk/errchk.h>
27 #include <atalk/logger.h>
28
29 #include "spotlight.h"
30
31 #define MAX_SL_RESULTS 20
32
33 static TrackerSparqlConnection *connection;
34
35 char *tracker_to_unix_path(const char *uri)
36 {
37     EC_INIT;
38     GFile *f = NULL;
39     char *path = NULL;
40
41     EC_NULL_LOG( f = g_file_new_for_uri(uri) );
42     EC_NULL_LOG( path = g_file_get_path(f) );
43
44 EC_CLEANUP:
45     if (f)
46         g_object_unref(f);
47     if (ret != 0)
48         return NULL;
49     return path;
50 }
51
52 static int sl_mod_init(void *p)
53 {
54     EC_INIT;
55     GError *error = NULL;
56     const char *msg = p;
57
58     LOG(log_note, logtype_sl, "sl_mod_init: %s", msg);
59     setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
60
61     g_type_init();
62     connection = tracker_sparql_connection_get(NULL, &error);
63     if (!connection) {
64         LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
65             error ? error->message : "unknown error");
66         g_clear_error(&error);
67         EC_FAIL;
68     }
69
70 EC_CLEANUP:
71     EC_EXIT;
72 }
73
74 /*!
75  * Return talloced query from query string
76  * *=="query*"
77  */
78 static const gchar *map_spotlight_to_sparql_query(slq_t *slq)
79 {
80     EC_INIT;
81     const gchar *sparql_query;
82     const char *sparql_query_format = "SELECT nie:url(?uri) WHERE {?uri fts:match '%s' . ?uri nie:url ?url FILTER(fn:starts-with(?url, 'file://%s')) }";
83     const char *slquery = slq->slq_qstring;
84     char *word, *p;
85
86     LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", slquery);
87
88     EC_NULL_LOG( word = strstr(slquery, "*==") );
89     word += 4; /* skip *== and the left enclosing quote */
90     EC_NULL( word = dalloc_strdup(slq, word) );
91     /* Search asterisk */
92     EC_NULL_LOG( p = strchr(word, '*') );
93     p[1] = 0;
94     sparql_query = talloc_asprintf(slq, sparql_query_format, word, slq->slq_vol->v_path);
95
96     LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
97
98 EC_CLEANUP:
99     if (ret != 0)
100         sparql_query = NULL;
101     return sparql_query;
102 }
103
104 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
105 {
106     slq_t *slq = user_data;
107     TrackerSparqlCursor *cursor;
108     GError *error = NULL;
109
110     LOG(log_debug, logtype_sl, "tracker_cb");
111
112     cursor = tracker_sparql_connection_query_finish(connection, res, &error);
113
114     if (error) {
115         LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
116             error ? error->message : "unknown error");
117         g_clear_error(&error);
118         return;
119     }
120
121     slq->slq_tracker_cursor = cursor;
122 }
123
124 static int sl_mod_start_search(void *p)
125 {
126     EC_INIT;
127     slq_t *slq = p; 
128     const gchar *sparql_query;
129     GError *error = NULL;
130
131     LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
132
133     EC_NULL_LOG( sparql_query = map_spotlight_to_sparql_query(slq) );
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     slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
142     if (error) {
143         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
144             error ? error->message : "unknown error");
145         g_clear_error(&error);
146         EC_FAIL;
147     }
148     slq->slq_state = SLQ_STATE_RUNNING;
149
150 EC_CLEANUP:
151     EC_EXIT;
152 }
153
154 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
155 {
156     EC_INIT;
157     sl_array_t *meta;
158     sl_nil_t nil = 0;
159     int i, metacount;
160
161     if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0)
162         EC_FAIL;
163
164     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
165
166     meta = talloc_zero(fm_array, sl_array_t);
167
168     for (i = 0; i < metacount; i++) {
169         if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
170             char *p, *name;
171             if ((p = strrchr(path, '/'))) {
172                 name = dalloc_strdup(meta, p + 1);
173                 dalloc_add(meta, name, "char *");
174             }
175         } else {
176             dalloc_add_copy(meta, &nil, sl_nil_t);
177         }
178     }
179
180     dalloc_add(fm_array, meta, sl_array_t);
181
182 EC_CLEANUP:
183     EC_EXIT;
184 }
185
186 static int sl_mod_fetch_result(void *p)
187 {
188     EC_INIT;
189     slq_t *slq = p;
190     GError *error = NULL;
191     int i = 0;
192     cnid_t did, id;
193     const gchar *uri;
194     char *path;
195     sl_cnids_t *cnids;
196     sl_filemeta_t *fm;
197     sl_array_t *fm_array;
198     uint64_t uint64;
199     gboolean more;
200
201     if (!slq->slq_tracker_cursor) {
202         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
203         goto EC_CLEANUP;
204     }
205
206     /* Prepare CNIDs */
207     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
208     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
209     cnids->ca_unkn1 = 0xadd;
210     cnids->ca_context = slq->slq_ctx2;
211
212     /* Prepare FileMeta */
213     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
214     fm_array = talloc_zero(fm, sl_array_t);
215     dalloc_add(fm, fm_array, sl_array_t);
216
217     /* For some reason the list of results always starts with a nil entry */
218     sl_nil_t nil;
219     dalloc_add_copy(fm_array, &nil, sl_nil_t);
220
221     LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
222
223     while (i <= MAX_SL_RESULTS && tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
224         uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
225         EC_NULL_LOG( path = tracker_to_unix_path(uri) );
226
227         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
228             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
229             goto loop_cleanup;
230         }
231         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
232
233         uint64 = ntohl(id);
234         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
235         add_filemeta(slq->slq_reqinfo, fm_array, id, path);
236
237     loop_cleanup:
238         g_free(path);
239     }
240
241     if (error) {
242         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
243             error ? error->message : "unknown error");
244         g_clear_error (&error);
245         EC_FAIL;
246     }
247
248     if (i < MAX_SL_RESULTS)
249         slq->slq_state = SLQ_STATE_DONE;
250
251     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
252     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
253
254 EC_CLEANUP:
255     if (slq->slq_tracker_cursor) {
256         if ((ret != 0) || (slq->slq_state == SLQ_STATE_DONE)) {
257             g_object_unref(slq->slq_tracker_cursor);
258             slq->slq_tracker_cursor = NULL;
259         }
260     }
261     EC_EXIT;
262 }
263
264 /* Free ressources allocated in this module */
265 static int sl_mod_close_query(void *p)
266 {
267     EC_INIT;
268     slq_t *slq = p;
269
270     if (slq->slq_tracker_cursor) {
271         g_object_unref(slq->slq_tracker_cursor);
272         slq->slq_tracker_cursor = NULL;
273     }
274
275 EC_CLEANUP:
276     EC_EXIT;
277 }
278
279 static int sl_mod_error(void *p)
280 {
281     EC_INIT;
282     slq_t *slq = p;
283
284     if (!slq)
285         goto EC_CLEANUP;
286
287     if (slq->slq_tracker_cursor) {
288         g_object_unref(slq->slq_tracker_cursor);
289         slq->slq_tracker_cursor = NULL;
290     }
291
292 EC_CLEANUP:
293     EC_EXIT;
294 }
295
296 static int sl_mod_index_file(const void *p)
297 {
298 #ifdef HAVE_TRACKER_MINER
299     /* hangs in tracker_miner_manager_new_full() for whatever reason... */
300     return 0;
301
302     EC_INIT;
303     const char *f = p;
304
305     if (!f)
306         goto EC_CLEANUP;
307
308     TrackerMinerManager *manager;
309     GError *error = NULL;
310     GFile *file;
311
312     if ((manager = tracker_miner_manager_new_full(FALSE, &error)) == NULL) {
313         LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): couldn't connect to Tracker miner", f);
314     } else {
315         file = g_file_new_for_commandline_arg(f);
316         tracker_miner_manager_index_file(manager, file, &error);
317         if (error)
318             LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): indexing failed", f);
319         else
320             LOG(log_debug, logtype_sl, "sl_mod_index_file(\"%s\"): indexing file was successful", f);
321         g_object_unref(manager);
322         g_object_unref(file);
323     }
324
325 EC_CLEANUP:
326     EC_EXIT;
327 #else
328     return 0;
329 #endif
330 }
331
332 struct sl_module_export sl_mod = {
333     SL_MODULE_VERSION,
334     sl_mod_init,
335     sl_mod_start_search,
336     sl_mod_fetch_result,
337     sl_mod_close_query,
338     sl_mod_error,
339     sl_mod_index_file
340 };