]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_module.c
90225721b4213a2813abdfba2f4106d22f6c16c6
[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 #include "spotlight_rawquery_parser.h"
33
34 #define MAX_SL_RESULTS 20
35
36 static TrackerSparqlConnection *connection;
37 static TrackerMinerManager *manager;
38
39 static char *tracker_to_unix_path(const char *uri)
40 {
41     EC_INIT;
42     GFile *f = NULL;
43     char *path = NULL;
44
45     EC_NULL_LOG( f = g_file_new_for_uri(uri) );
46     EC_NULL_LOG( path = g_file_get_path(f) );
47
48 EC_CLEANUP:
49     if (f)
50         g_object_unref(f);
51     if (ret != 0)
52         return NULL;
53     return path;
54 }
55
56 static int sl_mod_init(void *p)
57 {
58     EC_INIT;
59     GError *error = NULL;
60     const char *msg = p;
61
62     LOG(log_info, logtype_sl, "sl_mod_init: %s", msg);
63
64     g_type_init();
65     setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
66     setenv("TRACKER_SPARQL_BACKEND", "bus", 1);
67
68 #ifdef DEBUG
69     setenv("TRACKER_VERBOSITY", "3", 1);
70     dup2(type_configs[logtype_sl].fd, 1);
71     dup2(type_configs[logtype_sl].fd, 2);
72 #endif
73
74     become_root();
75     connection = tracker_sparql_connection_get(NULL, &error);
76     manager = tracker_miner_manager_new_full(FALSE, &error);
77     unbecome_root();
78
79     if (!connection) {
80         LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
81             error ? error->message : "unknown error");
82         g_clear_error(&error);
83         EC_FAIL;
84     }
85
86     if (!manager) {
87         LOG(log_error, logtype_sl, "Couldn't connect to Tracker miner");
88         g_clear_error(&error);
89         EC_FAIL;
90     }
91
92 EC_CLEANUP:
93     EC_EXIT;
94 }
95
96
97 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
98 {
99     slq_t *slq = user_data;
100     TrackerSparqlCursor *cursor;
101     GError *error = NULL;
102
103     LOG(log_debug, logtype_sl, "tracker_cb");
104
105     cursor = tracker_sparql_connection_query_finish(connection, res, &error);
106
107     if (error) {
108         LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
109             error ? error->message : "unknown error");
110         g_clear_error(&error);
111         return;
112     }
113
114     slq->slq_tracker_cursor = cursor;
115 }
116
117 static int sl_mod_start_search(void *p)
118 {
119     EC_INIT;
120     slq_t *slq = p; 
121     const gchar *sparql_query;
122     GError *error = NULL;
123
124     LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
125
126     EC_NULL_LOG( sparql_query = map_spotlight_to_sparql_query(slq) );
127     LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
128
129 #if 0
130     /* Start the async query */
131     tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
132 #endif
133
134     become_root();
135     slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
136     unbecome_root();
137
138     if (error) {
139         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
140             error ? error->message : "unknown error");
141         g_clear_error(&error);
142         EC_FAIL;
143     }
144     slq->slq_state = SLQ_STATE_RUNNING;
145
146 EC_CLEANUP:
147     EC_EXIT;
148 }
149
150 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
151 {
152     EC_INIT;
153     sl_array_t *meta;
154     sl_nil_t nil = 0;
155     int i, metacount;
156
157     if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
158         dalloc_add_copy(fm_array, &nil, sl_nil_t);
159         goto EC_CLEANUP;
160     }
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     sl_nil_t nil;
197     uint64_t uint64;
198     gboolean qres, firstmatch = true;
199
200     if (!slq->slq_tracker_cursor) {
201         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
202         goto EC_CLEANUP;
203     }
204
205     /* Prepare CNIDs */
206     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
207     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
208     cnids->ca_unkn1 = 0xadd;
209     cnids->ca_context = slq->slq_ctx2;
210
211     /* Prepare FileMeta */
212     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
213     fm_array = talloc_zero(fm, sl_array_t);
214     dalloc_add(fm, fm_array, sl_array_t);
215
216     LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
217
218     while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) {
219         become_root();
220         qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error);
221         unbecome_root();
222
223         if (!qres)
224             break;
225
226         if (firstmatch) {
227             /* For some reason the list of results always starts with a nil entry */
228             dalloc_add_copy(fm_array, &nil, sl_nil_t);
229             firstmatch = false;
230         }
231
232         become_root();
233         uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
234         unbecome_root();
235
236         EC_NULL_LOG( path = tracker_to_unix_path(uri) );
237
238         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
239             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", path);
240             goto loop_cleanup;
241         }
242         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), path);
243
244         uint64 = ntohl(id);
245         dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
246         add_filemeta(slq->slq_reqinfo, fm_array, id, path);
247
248     loop_cleanup:
249         g_free(path);
250         i++;
251    }
252
253     if (error) {
254         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
255             error ? error->message : "unknown error");
256         g_clear_error (&error);
257         EC_FAIL;
258     }
259
260     if (i < MAX_SL_RESULTS)
261         slq->slq_state = SLQ_STATE_DONE;
262
263     uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
264     dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
265     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
266     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
267
268 EC_CLEANUP:
269     if (ret != 0) {
270         if (slq->slq_tracker_cursor) {
271             g_object_unref(slq->slq_tracker_cursor);
272             slq->slq_tracker_cursor = NULL;
273         }
274     }
275     EC_EXIT;
276 }
277
278 /* Free ressources allocated in this module */
279 static int sl_mod_close_query(void *p)
280 {
281     EC_INIT;
282     slq_t *slq = p;
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_error(void *p)
294 {
295     EC_INIT;
296     slq_t *slq = p;
297
298     if (!slq)
299         goto EC_CLEANUP;
300
301     if (slq->slq_tracker_cursor) {
302         g_object_unref(slq->slq_tracker_cursor);
303         slq->slq_tracker_cursor = NULL;
304     }
305
306 EC_CLEANUP:
307     EC_EXIT;
308 }
309
310 static int sl_mod_index_file(const void *p)
311 {
312 #ifdef HAVE_TRACKER_MINER
313     EC_INIT;
314     const char *f = p;
315
316     if (!f)
317         goto EC_CLEANUP;
318
319     GError *error = NULL;
320     GFile *file = NULL;
321
322     file = g_file_new_for_commandline_arg(f);
323
324     become_root();
325     tracker_miner_manager_index_file(manager, file, &error);
326     unbecome_root();
327
328     if (error)
329         LOG(log_error, logtype_sl, "sl_mod_index_file(\"%s\"): indexing failed", f);
330     else
331         LOG(log_debug, logtype_sl, "sl_mod_index_file(\"%s\"): indexing file was successful", f);
332
333 EC_CLEANUP:
334     if (file)
335         g_object_unref(file);
336     EC_EXIT;
337 #else
338     return 0;
339 #endif
340 }
341
342 struct sl_module_export sl_mod = {
343     SL_MODULE_VERSION,
344     sl_mod_init,
345     sl_mod_start_search,
346     sl_mod_fetch_result,
347     sl_mod_close_query,
348     sl_mod_error,
349     sl_mod_index_file
350 };