]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_module.c
1d19a32218581c556b69534c5def045f851a58ce
[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 = talloc_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
135 EC_CLEANUP:
136     EC_EXIT;
137 }
138
139 static int add_filemeta(DALLOC_CTX *reqinfo, const int metacount, sl_array_t *fm_array, cnid_t id, const char *path)
140 {
141     EC_INIT;
142     sl_array_t *meta;
143     sl_nil_t nil = 0;
144     int i = metacount;
145
146     LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
147
148     meta = talloc_zero(fm_array, sl_array_t);
149
150     while (i--) {
151         dalloc_add(meta, &nil, sl_nil_t);
152     }
153
154     dalloc_add(fm_array, meta, sl_array_t);
155
156 EC_CLEANUP:
157     EC_EXIT;
158 }
159
160 static int sl_mod_fetch_result(void *p)
161 {
162     EC_INIT;
163     slq_t *slq = p;
164     GError *error = NULL;
165     int i = 0;
166     cnid_t did, id;
167     const char *path;
168     sl_cnids_t *cnids;
169     sl_filemeta_t *fm;
170     sl_array_t *fm_array;
171     uint64_t uint64;
172
173     if (!slq->slq_tracker_cursor) {
174         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
175         goto EC_CLEANUP;
176     }
177
178     /* Prepare CNIDs */
179     cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
180     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
181     cnids->ca_unkn1 = 0xadd;
182     cnids->ca_context = slq->slq_ctx2;
183
184     /* Prepare FileMeta */
185     fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
186     fm_array = talloc_zero(fm, sl_array_t);
187     dalloc_add(fm, fm_array, sl_array_t);
188
189     while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
190         EC_NULL_LOG( path = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL) );
191         path = tracker_to_unix_path(path);
192         LOG(log_debug, logtype_sl, "sl_mod_fetch_result: path(volpath: %s): \"%s\"", slq->slq_vol->v_path, path);
193         if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
194             LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
195             continue;
196         }
197         LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
198         uint64 = ntohl(id);
199         dalloc_add(cnids->ca_cnids, &uint64, uint64_t);
200         add_filemeta(slq->slq_reqinfo, slq->slq_metacount, fm_array, id, path);
201     }
202     dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
203     dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
204
205 EC_CLEANUP:
206     if (slq->slq_tracker_cursor)
207         g_object_unref(slq->slq_tracker_cursor);
208     EC_EXIT;
209 }
210
211 struct sl_module_export sl_mod = {
212     SL_MODULE_VERSION,
213     sl_mod_init,
214     sl_mod_start_search,
215     sl_mod_fetch_result,
216     NULL
217 };