]> arthur.barton.de Git - netatalk.git/blob - etc/spotlight/slmod_rdf.c
Try using Tracker 0.15 from OpenCSW
[netatalk.git] / etc / spotlight / slmod_rdf.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.h>
24
25 #include <atalk/util.h>
26 #include <atalk/errchk.h>
27 #include <atalk/logger.h>
28 #include <atalk/unix.h>
29 #include <atalk/spotlight.h>
30
31 #include "slmod_rdf_parser.h"
32
33 #define MAX_SL_RESULTS 20
34
35 TrackerClient *client;
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_info, logtype_sl, "Initializing Tracker 0.6 RDF Spotlight module");
44
45     g_type_init();
46     setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
47
48     become_root();
49     client = tracker_connect(FALSE);
50     unbecome_root();
51
52     if (!client) {
53         LOG(log_error, logtype_sl, "Failed connecting to Tracker");
54         EC_FAIL;
55     }
56
57 EC_CLEANUP:
58     EC_EXIT;
59 }
60
61
62 static int sl_mod_start_search(void *p)
63 {
64     EC_INIT;
65     slq_t *slq = p; 
66     GError *error = NULL;
67
68     EC_ZERO_LOG( map_spotlight_to_rdf_query(slq) );
69
70     LOG(log_debug, logtype_sl, "sl_mod_start_search: Tracker service: %s, FTS: %s, RDF query:\n%s",
71         tracker_type_to_service_name(slq->slq_service),
72         slq->slq_fts,
73         slq->slq_trackerquery ? slq->slq_trackerquery : "false");
74
75     if (slq->slq_trackerquery)
76         slq->slq_state = SLQ_STATE_RUNNING;
77     else
78         slq->slq_state = SLQ_STATE_DONE;
79
80 EC_CLEANUP:
81     EC_EXIT;
82 }
83
84 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
85 {
86     EC_INIT;
87     sl_array_t *meta;
88     sl_nil_t nil = 0;
89     int i, metacount;
90
91     if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
92         dalloc_add_copy(fm_array, &nil, sl_nil_t);
93         goto EC_CLEANUP;
94     }
95
96     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
97
98     meta = talloc_zero(fm_array, sl_array_t);
99
100     for (i = 0; i < metacount; i++) {
101         if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
102             char *p, *name;
103             if ((p = strrchr(path, '/'))) {
104                 name = dalloc_strdup(meta, p + 1);
105                 dalloc_add(meta, name, "char *");
106             }
107         } else {
108             dalloc_add_copy(meta, &nil, sl_nil_t);
109         }
110     }
111
112     dalloc_add(fm_array, meta, sl_array_t);
113
114 EC_CLEANUP:
115     EC_EXIT;
116 }
117
118 static int cnid_cmp_fn(const void *p1, const void *p2)
119 {
120     const uint64_t *cnid1 = p1, *cnid2 = p2;
121     if (*cnid1 == *cnid2)
122         return 0;
123     if (*cnid1 < *cnid2)
124         return -1;
125     else
126         return 1;            
127 }
128
129 static int sl_mod_fetch_result(void *p)
130 {
131     EC_INIT;
132     slq_t *slq = p;
133     GError *error = NULL;
134     int i = 0;
135     cnid_t did, id;
136     sl_cnids_t *cnids;
137     sl_filemeta_t *fm;
138     sl_array_t *fm_array;
139     sl_nil_t nil;
140     uint64_t uint64;
141     gboolean qres, firstmatch = true;
142     GPtrArray *array = NULL;
143
144     /* Prepare CNIDs */
145     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
146     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
147     cnids->ca_unkn1 = 0xadd;
148     cnids->ca_context = slq->slq_ctx2;
149
150     /* Prepare FileMeta */
151     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
152     fm_array = talloc_zero(fm, sl_array_t);
153     dalloc_add(fm, fm_array, sl_array_t);
154
155     LOG(log_debug, logtype_sl, "sl_mod_fetch_result");
156
157     if (slq->slq_state == SLQ_STATE_RUNNING) {
158         /* Run the query */
159         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: calling tracker");
160         become_root();
161         array = tracker_search_query(client,
162                                      time(NULL),
163                                      slq->slq_service,
164                                      NULL, /* Fields */
165                                      slq->slq_fts, /* FTS search test */
166                                      NULL,         /* Keywords */
167                                      slq->slq_trackerquery,
168                                      slq->slq_offset,
169                                      MAX_SL_RESULTS,
170                                      FALSE, /* Sort by service */
171                                      NULL,
172                                      FALSE,
173                                      &error);
174         unbecome_root();
175
176         if (error) {
177             slq->slq_state = SLQ_STATE_DONE;
178             LOG(log_error, logtype_sl, "Couldn't query Tracker: '%s'", error->message);
179             g_clear_error(&error);
180             EC_FAIL;
181         }
182
183         if (!array) {
184             slq->slq_state = SLQ_STATE_DONE;
185             LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
186             EC_EXIT_STATUS(0);
187         }
188
189         while (i < array->len) {
190             char **resmeta = g_ptr_array_index(array, i);
191             char *respath = resmeta[0];
192             LOG(log_debug, logtype_sl, "sl_mod_fetch_result: result %d: %s", slq->slq_offset, respath);
193
194             if (firstmatch) {
195                 /* For some reason the list of results always starts with a nil entry */
196                 dalloc_add_copy(fm_array, &nil, sl_nil_t);
197                 firstmatch = false;
198             }
199
200             struct stat st;
201             if (stat(respath, &st) != 0) {
202                 if (errno == ENOENT)
203                     tracker_files_delete(client, respath, NULL);
204                 goto loop_continue;
205             }
206
207             if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, respath, &did)) == CNID_INVALID) {
208                 LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", respath);
209                 goto loop_continue;
210             }
211             LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), respath);
212
213             uint64 = ntohl(id);
214             if (slq->slq_cnids) {
215                 if (!bsearch(&uint64, slq->slq_cnids, slq->slq_cnids_num, sizeof(uint64_t), cnid_cmp_fn))
216                     goto loop_continue;
217             }
218
219             dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
220             add_filemeta(slq->slq_reqinfo, fm_array, id, respath);
221
222         loop_continue:
223             i++;
224             slq->slq_offset++;
225         }
226
227         g_ptr_array_free(array, TRUE);
228         array = NULL;
229
230         if (i < MAX_SL_RESULTS)
231             slq->slq_state = SLQ_STATE_DONE;
232     }
233
234     uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
235     dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
236     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
237     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
238
239 EC_CLEANUP:
240     if (array)
241         g_ptr_array_free(array, TRUE);
242     EC_EXIT;
243 }
244
245 /* Free ressources allocated in this module */
246 static int sl_mod_close_query(void *p)
247 {
248     EC_INIT;
249     slq_t *slq = p;
250
251 EC_CLEANUP:
252     EC_EXIT;
253 }
254
255 static int sl_mod_error(void *p)
256 {
257     EC_INIT;
258     slq_t *slq = p;
259
260     if (!slq)
261         goto EC_CLEANUP;
262
263 EC_CLEANUP:
264     EC_EXIT;
265 }
266
267 static int sl_mod_index_file(const void *p)
268 {
269     return 0;
270 }
271
272 struct sl_module_export sl_mod = {
273     SL_MODULE_VERSION,
274     sl_mod_init,
275     sl_mod_start_search,
276     sl_mod_fetch_result,
277     sl_mod_close_query,
278     sl_mod_error,
279     sl_mod_index_file
280 };