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