]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Spotlight: new options for controlling query behaviour
[netatalk.git] / etc / afpd / spotlight.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 #define USE_LIST
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include <string.h>
22 #include <strings.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <stdbool.h>
27 #include <inttypes.h>
28 #include <time.h>
29 #include <utime.h>
30
31 #include <atalk/list.h>
32 #include <atalk/errchk.h>
33 #include <atalk/util.h>
34 #include <atalk/logger.h>
35 #include <atalk/talloc.h>
36 #include <atalk/dalloc.h>
37 #include <atalk/byteorder.h>
38 #include <atalk/netatalk_conf.h>
39 #include <atalk/volume.h>
40 #include <atalk/spotlight.h>
41
42 #include "directory.h"
43
44 static TALLOC_CTX *sl_ctx;
45 static void *sl_module;
46 static struct sl_module_export *sl_module_export;
47
48 /* Helper functions and stuff */
49 static const char *neststrings[] = {
50     "",
51     "\t",
52     "\t\t",
53     "\t\t\t",
54     "\t\t\t\t",
55     "\t\t\t\t\t",
56     "\t\t\t\t\t\t",
57 };
58
59 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
60 {
61     const char *type;
62
63     LOG(log_debug, logtype_sl, "%s%s(#%d): {",
64         neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
65
66     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
67
68         type = talloc_get_name(dd->dd_talloc_array[n]);
69
70         if (STRCMP(type, ==, "DALLOC_CTX")
71                    || STRCMP(type, ==, "sl_array_t")
72                    || STRCMP(type, ==, "sl_filemeta_t")
73                    || STRCMP(type, ==, "sl_dict_t")) {
74             dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
75         } else if (STRCMP(type, ==, "uint64_t")) {
76             uint64_t i;
77             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
78             LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
79         } else if (STRCMP(type, ==, "char *")) {
80             LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], (char *)dd->dd_talloc_array[n]);
81         } else if (STRCMP(type, ==, "sl_bool_t")) {
82             sl_bool_t bl;
83             memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
84             LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
85         } else if (STRCMP(type, ==, "sl_nil_t")) {
86             LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]);
87         } else if (STRCMP(type, ==, "sl_time_t")) {
88             sl_time_t t;
89             struct tm *tm;
90             char datestring[256];
91             memcpy(&t, dd->dd_talloc_array[n], sizeof(sl_time_t));
92             tm = localtime(&t.tv_sec);
93             strftime(datestring, sizeof(datestring), "%Y-%m-%d %H:%M:%S", tm);
94             LOG(log_debug, logtype_sl, "%ssl_time_t: %s.%06d", neststrings[nestinglevel + 1], datestring, t.tv_usec);
95         } else if (STRCMP(type, ==, "sl_cnids_t")) {
96             sl_cnids_t cnids;
97             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
98             LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32,
99                    neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
100             if (cnids.ca_cnids)
101                 dd_dump(cnids.ca_cnids, nestinglevel + 2);
102         } else {
103             LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type);
104         }
105     }
106     LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
107 }
108
109 #ifndef SPOT_TEST_MAIN
110 /**************************************************************************************************
111  * Spotlight queries
112  **************************************************************************************************/
113
114 static ATALK_LIST_HEAD(sl_queries);
115
116 /*!
117  * Add a query to the list of active queries
118  */
119 static int slq_add(slq_t *slq)
120 {
121     list_add(&(slq->slq_list), &sl_queries);
122     return 0;
123 }
124
125 static int slq_remove(slq_t *slq)
126 {
127     EC_INIT;
128     struct list_head *p;
129     slq_t *q = NULL;
130
131     list_for_each(p, &sl_queries) {
132         q = list_entry(p, slq_t, slq_list);
133         if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) {            
134             list_del(p);
135             break;
136         }
137         q = NULL;
138     }
139
140     if (q == NULL) {
141         /* The SL query 'slq' was not found in the list, this is not supposed to happen! */
142         LOG(log_warning, logtype_sl, "slq_remove: slq not in active query list");
143     }
144
145 EC_CLEANUP:
146     EC_EXIT;
147 }
148
149 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
150 {
151     EC_INIT;
152     slq_t *q = NULL;
153     struct list_head *p;
154
155     list_for_each(p, &sl_queries) {
156         q = list_entry(p, slq_t, slq_list);
157
158         LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
159             "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
160             ctx1, ctx2, q->slq_ctx1, q->slq_ctx2);
161
162         if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) {            
163             break;
164         }
165         q = NULL;
166     }
167
168 EC_CLEANUP:
169     if (ret != 0)
170         q = NULL;
171     return q;
172 }
173
174 /* Error handling for queries */
175 static void slq_error(slq_t *slq)
176 {
177     if (!slq)
178         return;
179     sl_module_export->sl_mod_error(slq);
180     slq_remove(slq);
181     talloc_free(slq);
182 }
183
184 /**************************************************************************************************
185  * Spotlight RPC functions
186  **************************************************************************************************/
187
188 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
189 {
190     EC_INIT;
191
192     char *s;
193     sl_dict_t *dict;
194     sl_array_t *array;
195     sl_uuid_t uuid;
196
197     if (!v->v_uuid)
198         EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
199
200     dict = talloc_zero(reply, sl_dict_t);
201
202     /* key/val 1 */
203     s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
204     dalloc_add(dict, s, char *);
205
206     array = talloc_zero(dict, sl_array_t);
207     s = dalloc_strdup(array, "kMDQueryScopeComputer");
208     dalloc_add(array, s, char *);
209     dalloc_add(dict, array, sl_array_t);
210
211     /* key/val 2 */
212     s = dalloc_strdup(dict, "kMDSStorePathScopes");
213     dalloc_add(dict, s, char *);
214
215     array = talloc_zero(dict, sl_array_t);
216     s = dalloc_strdup(array, v->v_path);
217     dalloc_add(array, s, char *);
218     dalloc_add(dict, array, sl_array_t);
219
220     /* key/val 3 */
221     s = dalloc_strdup(dict, "kMDSStoreUUID");
222     dalloc_add(dict, s, char *);
223
224     memcpy(uuid.sl_uuid, v->v_uuid, 16);
225     dalloc_add_copy(dict, &uuid, sl_uuid_t);
226
227     /* key/val 4 */
228     s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
229     dalloc_add(dict, s, char *);
230     sl_bool_t b = true;
231     dalloc_add_copy(dict, &b, sl_bool_t);
232
233     dalloc_add(reply, dict, sl_dict_t);
234
235 EC_CLEANUP:
236     EC_EXIT;
237 }
238
239 static int cnid_comp_fn(const void *p1, const void *p2)
240 {
241     const uint64_t *cnid1 = p1, *cnid2 = p2;
242     if (*cnid1 == *cnid2)
243         return 0;
244     if (*cnid1 < *cnid2)
245         return -1;
246     else
247         return 1;            
248 }
249
250 static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p)
251 {
252     EC_INIT;
253     uint64_t *cnids = NULL;
254
255     EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) );
256     for (int i = 0; i < talloc_array_length(p); i++)
257         memcpy(&cnids[i], p->dd_talloc_array[i], sizeof(uint64_t));
258     qsort(cnids, talloc_array_length(p), sizeof(uint64_t), cnid_comp_fn);
259
260     slq->slq_cnids = cnids;
261     slq->slq_cnids_num = talloc_array_length(p);
262
263 EC_CLEANUP:
264     if (ret != 0) {
265         if (cnids)
266             talloc_free(cnids);
267     }
268     EC_EXIT;
269 }
270
271 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
272 {
273     EC_INIT;
274     char *sl_query;
275     uint64_t *uint64;
276     DALLOC_CTX *reqinfo;
277     sl_array_t *array;
278     sl_cnids_t *cnids;
279     slq_t *slq = NULL;
280
281     /* Allocate and initialize query object */
282     slq = talloc_zero(sl_ctx, slq_t);
283     slq->slq_state = SLQ_STATE_NEW;
284     slq->slq_obj = obj;
285     slq->slq_vol = v;
286     slq->slq_allow_expr = obj->options.flags & OPTION_SPOTLIGHT_EXPR ? true : false;
287     slq->slq_result_limit = obj->options.sparql_limit;
288
289     LOG(log_info, logtype_sl, "sl_rpc_openQuery: expr: %s, limit: %" PRIu64,
290         slq->slq_allow_expr ? "yes" : "no", slq->slq_result_limit);
291
292     /* convert spotlight query charset to host charset */
293     EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
294     char slq_host[MAXPATHLEN + 1];
295     uint16_t convflags = v->v_mtou_flags;
296     size_t slq_maclen;
297     if (convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset, sl_query, strlen(sl_query), slq_host, MAXPATHLEN, &convflags) == -1) {
298         LOG(log_error, logtype_afpd, "sl_rpc_openQuery(\"%s\"): charset conversion failed", sl_query);
299         EC_FAIL;
300     }
301     slq->slq_qstring = talloc_strdup(slq, slq_host);
302     LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", slq->slq_qstring);
303
304     slq->slq_time = time(NULL);
305     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
306     slq->slq_ctx1 = *uint64;
307     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
308     slq->slq_ctx2 = *uint64;
309     EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
310     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
311     if ((cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryItemArray"))) {
312         EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
313     }
314         
315     LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
316     dd_dump(slq->slq_reqinfo, 0);
317
318     (void)slq_add(slq);
319     
320     /* Run the query */
321     EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
322
323     array = talloc_zero(reply, sl_array_t);
324     uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
325     dalloc_add_copy(array, &sl_res, uint64_t);
326     dalloc_add(reply, array, sl_array_t);
327
328 EC_CLEANUP:
329     if (ret != 0) {
330         slq_error(slq);
331     }
332     EC_EXIT;
333 }
334
335 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
336 {
337     EC_INIT;
338     slq_t *slq = NULL;
339     uint64_t *uint64, ctx1, ctx2;
340
341     /* Context */
342     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
343     ctx1 = *uint64;
344     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
345     ctx2 = *uint64;
346
347     /* Get query for context */
348     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
349     if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) {
350         EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active");
351     }
352
353     /* Create and pass reply handle */
354     EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
355
356     /* Fetch Tracker results*/
357     EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) );
358
359     dalloc_add(reply, slq->slq_reply, sl_array_t);
360
361 EC_CLEANUP:
362     if (ret != 0) {
363         slq_error(slq);
364     }
365     EC_EXIT;
366 }
367
368 static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
369 {
370     EC_INIT;
371     uint64_t uint64;
372     sl_array_t *array;
373     sl_cnids_t *cnids;
374     sl_time_t *sl_time;
375     cnid_t id;
376     char *path;
377     struct dir *dir;
378     
379     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
380     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
381     id = (cnid_t)uint64;
382     LOG(log_debug, logtype_sl, "sl_rpc_storeAttributesForOIDArray: CNID: %" PRIu32, id);
383
384     if (htonl(id) == DIRDID_ROOT) {
385         path = vol->v_path;
386     } else if (id < CNID_START) {
387         EC_FAIL;
388     } else {
389         cnid_t did;
390         char buffer[12 + MAXPATHLEN + 1];
391
392         did = htonl(id);
393         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
394         EC_NULL_LOG( dir = dirlookup(vol, did) );
395         EC_NEG1_LOG( movecwd(vol, dir) );
396     }
397
398     /*
399      * We're possibly supposed to update attributes in two places: the
400      * database and the filesystem.  Due to the lack of documentation
401      * and not yet implemented database updates, we cherry pick attributes
402      * that seems to be candidates for updating filesystem metadata.
403      */
404
405     if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemFSContentChangeDate"))) {
406         struct utimbuf utimes;
407         utimes.actime = utimes.modtime = sl_time->tv_sec;
408         utime(path, &utimes);
409     }
410
411     array = talloc_zero(reply, sl_array_t);
412     uint64_t sl_res = 0;
413     dalloc_add_copy(array, &sl_res, uint64_t);
414     dalloc_add(reply, array, sl_array_t);
415
416 EC_CLEANUP:
417     EC_EXIT;
418 }
419
420 static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
421 {
422     EC_INIT;
423     uint64_t uint64;
424     sl_cnids_t *cnids;
425     sl_time_t *sl_time;
426     cnid_t id;
427     char *path;
428     struct dir *dir;
429     
430     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) );
431     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
432     id = (cnid_t)uint64;
433     LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
434
435     if (htonl(id) == DIRDID_ROOT) {
436         path = vol->v_path;
437     } else if (id < CNID_START) {
438         EC_FAIL;
439     } else {
440         cnid_t did;
441         char buffer[12 + MAXPATHLEN + 1];
442
443         did = htonl(id);
444         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
445         EC_NULL_LOG( dir = dirlookup(vol, did) );
446         EC_NEG1_LOG( movecwd(vol, dir) );
447     }
448
449     /* Result array */
450     sl_array_t *array = talloc_zero(reply, sl_array_t);
451     dalloc_add(reply, array, sl_array_t);
452
453     /* Return result value 0 */
454     uint64_t sl_res = 0;
455     dalloc_add_copy(array, &sl_res, uint64_t);
456
457     /* Return CNID array */
458     sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
459     replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
460     replycnids->ca_unkn1 = 0xfec;
461     replycnids->ca_context = cnids->ca_context;
462     uint64 = (uint64_t)id;
463     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
464     dalloc_add(array, replycnids, sl_cnids_t);
465
466     /* Return filemeta array */
467
468     /*
469      * FIXME: this should return the real attributes from all known metadata sources
470      * (Tracker and filesystem)
471      */
472     sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
473     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
474     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
475     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
476     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
477     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
478     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
479
480     sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
481     dalloc_add(fmeta, mdattrs, sl_array_t);
482     dalloc_add(array, fmeta, sl_filemeta_t);
483
484 EC_CLEANUP:
485     EC_EXIT;
486 }
487
488 static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
489 {
490     EC_INIT;
491     slq_t *slq = NULL;
492     uint64_t uint64;
493     sl_cnids_t *cnids;
494     sl_time_t *sl_time;
495     cnid_t id;
496     struct dir *dir;
497     sl_array_t *reqinfo;
498
499
500
501     /* Allocate and initialize query object */
502     slq = talloc_zero(reply, slq_t);
503     slq->slq_state = SLQ_STATE_ATTRS;
504     slq->slq_obj = obj;
505     slq->slq_vol = vol;
506     EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
507     EC_NULL( reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1) );
508     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
509     
510     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
511     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
512     id = (cnid_t)uint64;
513
514     if (htonl(id) == DIRDID_ROOT) {
515         slq->slq_path = talloc_strdup(slq, vol->v_path);
516     } else if (id < CNID_START) {
517         EC_FAIL;
518     } else {
519         cnid_t did;
520         char buffer[12 + MAXPATHLEN + 1];
521         char *name;
522         did = htonl(id);
523         EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
524         EC_NULL( dir = dirlookup(vol, did) );
525         EC_NULL( slq->slq_path = talloc_asprintf(slq, "%s/%s", bdata(dir->d_fullpath), name) );
526     }
527
528     /* Return result value 0 */
529     uint64_t sl_res = 0;
530     dalloc_add_copy(slq->slq_reply, &sl_res, uint64_t);
531
532     /* Return CNID array */
533     sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
534     replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
535     replycnids->ca_unkn1 = 0xfec;
536     replycnids->ca_context = cnids->ca_context;
537     uint64 = (uint64_t)id;
538     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
539     dalloc_add(slq->slq_reply, replycnids, sl_cnids_t);
540
541     /* Fetch attributes from module */
542     EC_ZERO_LOG( sl_module_export->sl_mod_fetch_attrs(slq) );
543
544     dalloc_add(reply, slq->slq_reply, sl_array_t);
545
546 EC_CLEANUP:
547     EC_EXIT;
548 }
549
550 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
551 {
552     EC_INIT;
553     slq_t *slq = NULL;
554     uint64_t *uint64, ctx1, ctx2;
555     sl_array_t *array;
556     
557     /* Context */
558     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
559     ctx1 = *uint64;
560     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
561     ctx2 = *uint64;
562
563     /* Get query for context and free it */
564     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
565     if (slq->slq_state != SLQ_STATE_DONE)
566         LOG(log_warning, logtype_sl, "Closing active query");
567     sl_module_export->sl_mod_end_search(slq);
568     slq_remove(slq);
569     talloc_free(slq);
570     slq = NULL;
571
572     array = talloc_zero(reply, sl_array_t);
573     uint64_t sl_res = 0;
574     dalloc_add_copy(array, &sl_res, uint64_t);
575     dalloc_add(reply, array, sl_array_t);
576
577 EC_CLEANUP:
578     if (ret != 0) {
579         slq_error(slq);
580     }
581     EC_EXIT;
582 }
583
584 /**************************************************************************************************
585  * Spotlight module functions
586  **************************************************************************************************/
587
588 int sl_mod_load(AFPObj *obj)
589 {
590     EC_INIT;
591
592     sl_ctx = talloc_new(NULL);
593
594     if ((sl_module = mod_open(obj->options.slmod_path)) == NULL) {
595         LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", obj->options.slmod_path, mod_error());
596         EC_FAIL;
597     }
598
599     if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
600         LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol sl_mod", obj->options.slmod_path);
601         EC_FAIL;
602     }
603
604     sl_module_export->sl_mod_init(obj);
605    
606 EC_CLEANUP:
607     EC_EXIT;
608 }
609
610 /**
611  * Index a file
612  **/
613 void sl_index_file(const char *path)
614 {
615     if (sl_module_export && sl_module_export->sl_mod_index_file)
616         sl_module_export->sl_mod_index_file(path);
617 }
618
619 /**************************************************************************************************
620  * AFP functions
621  **************************************************************************************************/
622
623 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
624 {
625     EC_INIT;
626     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
627     uint16_t vid;
628     int cmd;
629     int endianess = SL_ENC_LITTLE_ENDIAN;
630     struct vol      *vol;
631     DALLOC_CTX *query;
632     DALLOC_CTX *reply;
633     char *rpccmd;
634     int len;
635
636     *rbuflen = 0;
637
638     if (sl_module == NULL)
639         return AFPERR_NOOP;
640
641     ibuf += 2;
642     ibuflen -= 2;
643
644     vid = SVAL(ibuf, 0);
645     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
646
647     if ((vol = getvolbyvid(vid)) == NULL) {
648         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
649         ret = AFPERR_ACCESS;
650         goto EC_CLEANUP;
651     }
652
653     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
654
655     cmd = RIVAL(ibuf, 6);
656     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
657
658     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
659
660     switch (cmd) {
661
662     case SPOTLIGHT_CMD_OPEN:
663     case SPOTLIGHT_CMD_OPEN2:
664         RSIVAL(rbuf, 0, ntohs(vid));
665         RSIVAL(rbuf, 4, 0);
666         len = strlen(vol->v_path) + 1;
667         strncpy(rbuf + 8, vol->v_path, len);
668         *rbuflen += 8 + len;
669         break;
670
671     case SPOTLIGHT_CMD_FLAGS:
672         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
673         *rbuflen += 4;
674         break;
675
676     case SPOTLIGHT_CMD_RPC:
677         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
678         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
679         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
680
681         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
682         dd_dump(query, 0);
683
684         EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
685
686         if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
687             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
688         } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
689             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
690         } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
691             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
692         } else if (STRCMP(rpccmd, ==, "storeAttributes:forOIDArray:context:")) {
693             EC_ZERO_LOG( sl_rpc_storeAttributesForOIDArray(obj, query, reply, vol) );
694         } else if (STRCMP(rpccmd, ==, "fetchAttributeNamesForOIDArray:context:")) {
695             EC_ZERO_LOG( sl_rpc_fetchAttributeNamesForOIDArray(obj, query, reply, vol) );
696         } else if (STRCMP(rpccmd, ==, "fetchAttributes:forOIDArray:context:")) {
697             EC_ZERO_LOG( sl_rpc_fetchAttributesForOIDArray(obj, query, reply, vol) );
698         } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
699             EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
700         } else {
701             LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
702         }
703
704         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
705         dd_dump(reply, 0);
706
707         memset(rbuf, 0, 4);
708         *rbuflen += 4;
709
710         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
711         *rbuflen += len;
712         break;
713     }
714
715 EC_CLEANUP:
716     talloc_free(tmp_ctx);
717     if (ret != AFP_OK) {
718         *rbuflen = 0;
719         return AFPERR_MISC;
720     }
721     EC_EXIT;
722 }
723 #endif
724
725 /**************************************************************************************************
726  * Testing
727  **************************************************************************************************/
728
729 #ifdef SPOT_TEST_MAIN
730
731 int main(int argc, char **argv)
732 {
733     EC_INIT;
734     TALLOC_CTX *mem_ctx = talloc_new(NULL);
735     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
736     uint64_t i;
737
738     set_processname("spot");
739     setuplog("default:info,spotlight:debug", "/dev/tty");
740
741     LOG(log_info, logtype_sl, "Start");
742
743     i = 1;
744     dalloc_add_copy(dd, &i, uint64_t);
745     char *str = dalloc_strdup(dd, "hello world");
746     dalloc_add(dd, str, char *);
747     sl_bool_t b = true;
748     dalloc_add_copy(dd, &b, sl_bool_t);
749
750     /* add a nested array */
751     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
752     i = 3;
753     dalloc_add_copy(nested, &i, uint64_t);
754     dalloc_add(dd, nested, DALLOC_CTX);
755
756     /* test an allocated CNID array */
757     uint64_t id = 16;
758     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
759     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
760     cnids->ca_unkn1 = 1;
761     dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
762     dalloc_add(dd, cnids, sl_cnids_t);
763
764     /* Now the Spotlight types */
765     sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
766     i = 0x1234;
767     dalloc_add_copy(sl_array, &i, uint64_t);
768
769     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
770     i = 0xffff;
771     dalloc_add_copy(sl_dict, &i, uint64_t);
772     dalloc_add(sl_array, sl_dict, sl_dict_t);
773
774     dalloc_add(dd, sl_array, sl_array_t);
775
776     dd_dump(dd, 0);
777
778     /* now parse a real spotlight packet */
779     if (argc > 1) {
780         char ibuf[8192];
781         char rbuf[8192];
782         int fd;
783         size_t len;
784         DALLOC_CTX *query;
785
786         EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
787
788         EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
789         EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
790         close(fd);
791         EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
792
793         /* Now dump the whole thing */
794         dd_dump(query, 0);
795     }
796
797 #if 0
798     /* packing  */
799     int qlen;
800     char buf[MAX_SLQ_DAT];
801     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
802
803     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
804     lseek(fd, 24, SEEK_SET);
805     write(fd, buf, qlen);
806     close(fd);
807 #endif
808
809 EC_CLEANUP:
810     if (mem_ctx) {
811         talloc_free(mem_ctx);
812         mem_ctx = NULL;
813     }
814     EC_EXIT;
815 }
816 #endif