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