]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_module.c
52e4e8c49299287f138a8e511df7e387ea99b93d
[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         dalloc_add_copy(fm_array, &nil, sl_nil_t);
196         goto EC_CLEANUP;
197     }
198
199     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
200
201     meta = talloc_zero(fm_array, sl_array_t);
202
203     for (i = 0; i < metacount; i++) {
204         if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
205             char *p, *name;
206             if ((p = strrchr(path, '/'))) {
207                 name = dalloc_strdup(meta, p + 1);
208                 dalloc_add(meta, name, "char *");
209             }
210         } else {
211             dalloc_add_copy(meta, &nil, sl_nil_t);
212         }
213     }
214
215     dalloc_add(fm_array, meta, sl_array_t);
216
217 EC_CLEANUP:
218     EC_EXIT;
219 }
220
221 static int sl_mod_fetch_result(void *p)
222 {
223     EC_INIT;
224     slq_t *slq = p;
225     GError *error = NULL;
226     int i = 0;
227     cnid_t did, id;
228     const gchar *uri;
229     char *path;
230     sl_cnids_t *cnids;
231     sl_filemeta_t *fm;
232     sl_array_t *fm_array;
233     sl_nil_t nil;
234     uint64_t uint64;
235     gboolean qres, firstmatch = true;
236
237     if (!slq->slq_tracker_cursor) {
238         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
239         goto EC_CLEANUP;
240     }
241
242     /* Prepare CNIDs */
243     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
244     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
245     cnids->ca_unkn1 = 0xadd;
246     cnids->ca_context = slq->slq_ctx2;
247
248     /* Prepare FileMeta */
249     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
250     fm_array = talloc_zero(fm, sl_array_t);
251     dalloc_add(fm, fm_array, sl_array_t);
252
253     LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
254
255     while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) {
256         become_root();
257         qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error);
258         unbecome_root();
259
260         if (!qres)
261             break;
262
263         if (firstmatch) {
264             /* For some reason the list of results always starts with a nil entry */
265             dalloc_add_copy(fm_array, &nil, sl_nil_t);
266             firstmatch = false;
267         }
268
269         become_root();
270         uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
271         unbecome_root();
272
273         EC_NULL_LOG( path = tracker_to_unix_path(uri) );
274
275         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
276             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
277             goto loop_cleanup;
278         }
279         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), path);
280
281         uint64 = ntohl(id);
282         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
283         add_filemeta(slq->slq_reqinfo, fm_array, id, path);
284
285     loop_cleanup:
286         g_free(path);
287         i++;
288    }
289
290     if (error) {
291         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
292             error ? error->message : "unknown error");
293         g_clear_error (&error);
294         EC_FAIL;
295     }
296
297     if (i < MAX_SL_RESULTS)
298         slq->slq_state = SLQ_STATE_DONE;
299
300     uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
301     dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
302     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
303     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
304
305 EC_CLEANUP:
306     if (ret != 0) {
307         if (slq->slq_tracker_cursor) {
308             g_object_unref(slq->slq_tracker_cursor);
309             slq->slq_tracker_cursor = NULL;
310         }
311     }
312     EC_EXIT;
313 }
314
315 /* Free ressources allocated in this module */
316 static int sl_mod_close_query(void *p)
317 {
318     EC_INIT;
319     slq_t *slq = p;
320
321     if (slq->slq_tracker_cursor) {
322         g_object_unref(slq->slq_tracker_cursor);
323         slq->slq_tracker_cursor = NULL;
324     }
325
326 EC_CLEANUP:
327     EC_EXIT;
328 }
329
330 static int sl_mod_error(void *p)
331 {
332     EC_INIT;
333     slq_t *slq = p;
334
335     if (!slq)
336         goto EC_CLEANUP;
337
338     if (slq->slq_tracker_cursor) {
339         g_object_unref(slq->slq_tracker_cursor);
340         slq->slq_tracker_cursor = NULL;
341     }
342
343 EC_CLEANUP:
344     EC_EXIT;
345 }
346
347 static int sl_mod_index_file(const void *p)
348 {
349 #ifdef HAVE_TRACKER_MINER
350     /* hangs in tracker_miner_manager_new_full() for whatever reason... */
351     return 0;
352
353     EC_INIT;
354     const char *f = p;
355
356     if (!f)
357         goto EC_CLEANUP;
358
359     TrackerMinerManager *manager;
360     GError *error = NULL;
361     GFile *file;
362
363     if ((manager = tracker_miner_manager_new_full(FALSE, &error)) == NULL) {
364         LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): couldn't connect to Tracker miner", f);
365     } else {
366         file = g_file_new_for_commandline_arg(f);
367         tracker_miner_manager_index_file(manager, file, &error);
368         if (error)
369             LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): indexing failed", f);
370         else
371             LOG(log_debug, logtype_sl, "sl_mod_index_file(\"%s\"): indexing file was successful", f);
372         g_object_unref(manager);
373         g_object_unref(file);
374     }
375
376 EC_CLEANUP:
377     EC_EXIT;
378 #else
379     return 0;
380 #endif
381 }
382
383 struct sl_module_export sl_mod = {
384     SL_MODULE_VERSION,
385     sl_mod_init,
386     sl_mod_start_search,
387     sl_mod_fetch_result,
388     sl_mod_close_query,
389     sl_mod_error,
390     sl_mod_index_file
391 };