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