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