]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_module.c
Support for FTS and filename search
[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;
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     if ((word = strstr(slquery, "*==")) || (word = strstr(slquery, "kMDItemTextContent=="))) {
89         /* Full text search */
90         sparql_query_format = "SELECT nie:url(?uri) WHERE {?uri nie:url ?url . FILTER(fn:starts-with(?url, 'file://%s')) . ?uri fts:match '%s'}";
91         EC_NULL_LOG( word = strchr(word, '"') );
92         word++;
93         EC_NULL( word = dalloc_strdup(slq, word) );
94         EC_NULL_LOG( p = strchr(word, '"') );
95         *p = 0;
96         sparql_query = talloc_asprintf(slq, sparql_query_format, slq->slq_vol->v_path, word);
97         LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
98     } else if ((word = strstr(slquery, "kMDItemDisplayName=="))) {
99         /* Filename search */
100         sparql_query_format = "SELECT nie:url(?f) WHERE { ?f nie:url ?url . ?f nfo:fileName ?name . FILTER(fn:starts-with(?url, 'file://%s/') && regex(?name, '%s')) }";
101         EC_NULL_LOG( word = strchr(word, '"') );
102         word++;
103         EC_NULL( word = dalloc_strdup(slq, word) );
104         EC_NULL_LOG( p = strchr(word, '"') );
105         if (*(p-1) == '*')
106             *(p-1) = 0;
107         else
108             *p = 0;
109         sparql_query = talloc_asprintf(slq, sparql_query_format, slq->slq_vol->v_path, word);
110         LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
111     } else {
112         EC_FAIL_LOG("Can't parse SL query: '%s'", slquery);
113     }
114
115 EC_CLEANUP:
116     if (ret != 0)
117         sparql_query = NULL;
118     return sparql_query;
119 }
120
121 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
122 {
123     slq_t *slq = user_data;
124     TrackerSparqlCursor *cursor;
125     GError *error = NULL;
126
127     LOG(log_debug, logtype_sl, "tracker_cb");
128
129     cursor = tracker_sparql_connection_query_finish(connection, res, &error);
130
131     if (error) {
132         LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
133             error ? error->message : "unknown error");
134         g_clear_error(&error);
135         return;
136     }
137
138     slq->slq_tracker_cursor = cursor;
139 }
140
141 static int sl_mod_start_search(void *p)
142 {
143     EC_INIT;
144     slq_t *slq = p; 
145     const gchar *sparql_query;
146     GError *error = NULL;
147
148     LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
149
150     EC_NULL_LOG( sparql_query = map_spotlight_to_sparql_query(slq) );
151     LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
152
153 #if 0
154     /* Start the async query */
155     tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
156 #endif
157
158     slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
159     if (error) {
160         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
161             error ? error->message : "unknown error");
162         g_clear_error(&error);
163         EC_FAIL;
164     }
165     slq->slq_state = SLQ_STATE_RUNNING;
166
167 EC_CLEANUP:
168     EC_EXIT;
169 }
170
171 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
172 {
173     EC_INIT;
174     sl_array_t *meta;
175     sl_nil_t nil = 0;
176     int i, metacount;
177
178     if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0)
179         EC_FAIL;
180
181     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
182
183     meta = talloc_zero(fm_array, sl_array_t);
184
185     for (i = 0; i < metacount; i++) {
186         if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
187             char *p, *name;
188             if ((p = strrchr(path, '/'))) {
189                 name = dalloc_strdup(meta, p + 1);
190                 dalloc_add(meta, name, "char *");
191             }
192         } else {
193             dalloc_add_copy(meta, &nil, sl_nil_t);
194         }
195     }
196
197     dalloc_add(fm_array, meta, sl_array_t);
198
199 EC_CLEANUP:
200     EC_EXIT;
201 }
202
203 static int sl_mod_fetch_result(void *p)
204 {
205     EC_INIT;
206     slq_t *slq = p;
207     GError *error = NULL;
208     int i = 0;
209     cnid_t did, id;
210     const gchar *uri;
211     char *path;
212     sl_cnids_t *cnids;
213     sl_filemeta_t *fm;
214     sl_array_t *fm_array;
215     uint64_t uint64;
216     gboolean more;
217
218     if (!slq->slq_tracker_cursor) {
219         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
220         goto EC_CLEANUP;
221     }
222
223     /* Prepare CNIDs */
224     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
225     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
226     cnids->ca_unkn1 = 0xadd;
227     cnids->ca_context = slq->slq_ctx2;
228
229     /* Prepare FileMeta */
230     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
231     fm_array = talloc_zero(fm, sl_array_t);
232     dalloc_add(fm, fm_array, sl_array_t);
233
234     /* For some reason the list of results always starts with a nil entry */
235     sl_nil_t nil;
236     dalloc_add_copy(fm_array, &nil, sl_nil_t);
237
238     LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
239
240     while (i <= MAX_SL_RESULTS && tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
241         uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
242         EC_NULL_LOG( path = tracker_to_unix_path(uri) );
243
244         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
245             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
246             goto loop_cleanup;
247         }
248         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
249
250         uint64 = ntohl(id);
251         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
252         add_filemeta(slq->slq_reqinfo, fm_array, id, path);
253
254     loop_cleanup:
255         g_free(path);
256     }
257
258     if (error) {
259         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
260             error ? error->message : "unknown error");
261         g_clear_error (&error);
262         EC_FAIL;
263     }
264
265     if (i < MAX_SL_RESULTS)
266         slq->slq_state = SLQ_STATE_DONE;
267
268     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
269     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
270
271 EC_CLEANUP:
272     if (slq->slq_tracker_cursor) {
273         if ((ret != 0) || (slq->slq_state == SLQ_STATE_DONE)) {
274             g_object_unref(slq->slq_tracker_cursor);
275             slq->slq_tracker_cursor = NULL;
276         }
277     }
278     EC_EXIT;
279 }
280
281 /* Free ressources allocated in this module */
282 static int sl_mod_close_query(void *p)
283 {
284     EC_INIT;
285     slq_t *slq = p;
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_error(void *p)
297 {
298     EC_INIT;
299     slq_t *slq = p;
300
301     if (!slq)
302         goto EC_CLEANUP;
303
304     if (slq->slq_tracker_cursor) {
305         g_object_unref(slq->slq_tracker_cursor);
306         slq->slq_tracker_cursor = NULL;
307     }
308
309 EC_CLEANUP:
310     EC_EXIT;
311 }
312
313 static int sl_mod_index_file(const void *p)
314 {
315 #ifdef HAVE_TRACKER_MINER
316     /* hangs in tracker_miner_manager_new_full() for whatever reason... */
317     return 0;
318
319     EC_INIT;
320     const char *f = p;
321
322     if (!f)
323         goto EC_CLEANUP;
324
325     TrackerMinerManager *manager;
326     GError *error = NULL;
327     GFile *file;
328
329     if ((manager = tracker_miner_manager_new_full(FALSE, &error)) == NULL) {
330         LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): couldn't connect to Tracker miner", f);
331     } else {
332         file = g_file_new_for_commandline_arg(f);
333         tracker_miner_manager_index_file(manager, file, &error);
334         if (error)
335             LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): indexing failed", f);
336         else
337             LOG(log_debug, logtype_sl, "sl_mod_index_file(\"%s\"): indexing file was successful", f);
338         g_object_unref(manager);
339         g_object_unref(file);
340     }
341
342 EC_CLEANUP:
343     EC_EXIT;
344 #else
345     return 0;
346 #endif
347 }
348
349 struct sl_module_export sl_mod = {
350     SL_MODULE_VERSION,
351     sl_mod_init,
352     sl_mod_start_search,
353     sl_mod_fetch_result,
354     sl_mod_close_query,
355     sl_mod_error,
356     sl_mod_index_file
357 };