]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
a3fa587068b3ed6f699844e050c1793bd336c411
[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
287     /* convert spotlight query charset to host charset */
288     EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
289     char slq_host[MAXPATHLEN + 1];
290     uint16_t convflags = v->v_mtou_flags;
291     size_t slq_maclen;
292     if (convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset, sl_query, strlen(sl_query), slq_host, MAXPATHLEN, &convflags) == -1) {
293         LOG(log_error, logtype_afpd, "sl_rpc_openQuery(\"%s\"): charset conversion failed", sl_query);
294         EC_FAIL;
295     }
296     slq->slq_qstring = talloc_strdup(slq, slq_host);
297     LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", slq->slq_qstring);
298
299     slq->slq_time = time(NULL);
300     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
301     slq->slq_ctx1 = *uint64;
302     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
303     slq->slq_ctx2 = *uint64;
304     EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
305     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
306     if ((cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryItemArray"))) {
307         EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
308     }
309         
310     LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
311     dd_dump(slq->slq_reqinfo, 0);
312
313     (void)slq_add(slq);
314     
315     /* Run the query */
316     EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
317
318     array = talloc_zero(reply, sl_array_t);
319     uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
320     dalloc_add_copy(array, &sl_res, uint64_t);
321     dalloc_add(reply, array, sl_array_t);
322
323 EC_CLEANUP:
324     if (ret != 0) {
325         slq_error(slq);
326     }
327     EC_EXIT;
328 }
329
330 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
331 {
332     EC_INIT;
333     slq_t *slq = NULL;
334     uint64_t *uint64, ctx1, ctx2;
335
336     /* Context */
337     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
338     ctx1 = *uint64;
339     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
340     ctx2 = *uint64;
341
342     /* Get query for context */
343     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
344     if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) {
345         EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active");
346     }
347
348     /* Create and pass reply handle */
349     EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
350
351     /* Fetch Tracker results*/
352     EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) );
353
354     dalloc_add(reply, slq->slq_reply, sl_array_t);
355
356 EC_CLEANUP:
357     if (ret != 0) {
358         slq_error(slq);
359     }
360     EC_EXIT;
361 }
362
363 static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
364 {
365     EC_INIT;
366     uint64_t uint64;
367     sl_array_t *array;
368     sl_cnids_t *cnids;
369     sl_time_t *sl_time;
370     cnid_t id;
371     char *path;
372     struct dir *dir;
373     
374     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
375     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
376     id = (cnid_t)uint64;
377     LOG(log_debug, logtype_sl, "sl_rpc_storeAttributesForOIDArray: CNID: %" PRIu32, id);
378
379     if (htonl(id) == DIRDID_ROOT) {
380         path = vol->v_path;
381     } else if (id < CNID_START) {
382         EC_FAIL;
383     } else {
384         cnid_t did;
385         char buffer[12 + MAXPATHLEN + 1];
386
387         did = htonl(id);
388         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
389         EC_NULL_LOG( dir = dirlookup(vol, did) );
390         EC_NEG1_LOG( movecwd(vol, dir) );
391     }
392
393     /*
394      * We're possibly supposed to update attributes in two places: the
395      * database and the filesystem.  Due to the lack of documentation
396      * and not yet implemented database updates, we cherry pick attributes
397      * that seems to be candidates for updating filesystem metadata.
398      */
399
400     if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemFSContentChangeDate"))) {
401         struct utimbuf utimes;
402         utimes.actime = utimes.modtime = sl_time->tv_sec;
403         utime(path, &utimes);
404     }
405
406     array = talloc_zero(reply, sl_array_t);
407     uint64_t sl_res = 0;
408     dalloc_add_copy(array, &sl_res, uint64_t);
409     dalloc_add(reply, array, sl_array_t);
410
411 EC_CLEANUP:
412     EC_EXIT;
413 }
414
415 static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
416 {
417     EC_INIT;
418     uint64_t uint64;
419     sl_cnids_t *cnids;
420     sl_time_t *sl_time;
421     cnid_t id;
422     char *path;
423     struct dir *dir;
424     
425     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) );
426     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
427     id = (cnid_t)uint64;
428     LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
429
430     if (htonl(id) == DIRDID_ROOT) {
431         path = vol->v_path;
432     } else if (id < CNID_START) {
433         EC_FAIL;
434     } else {
435         cnid_t did;
436         char buffer[12 + MAXPATHLEN + 1];
437
438         did = htonl(id);
439         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
440         EC_NULL_LOG( dir = dirlookup(vol, did) );
441         EC_NEG1_LOG( movecwd(vol, dir) );
442     }
443
444     /* Result array */
445     sl_array_t *array = talloc_zero(reply, sl_array_t);
446     dalloc_add(reply, array, sl_array_t);
447
448     /* Return result value 0 */
449     uint64_t sl_res = 0;
450     dalloc_add_copy(array, &sl_res, uint64_t);
451
452     /* Return CNID array */
453     sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
454     replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
455     replycnids->ca_unkn1 = 0xfec;
456     replycnids->ca_context = cnids->ca_context;
457     uint64 = (uint64_t)id;
458     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
459     dalloc_add(array, replycnids, sl_cnids_t);
460
461     /* Return filemeta array */
462
463     /*
464      * FIXME: this should return the real attributes from all known metadata sources
465      * (Tracker and filesystem)
466      */
467     sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
468     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
469     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
470     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
471     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
472     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
473     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
474
475     sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
476     dalloc_add(fmeta, mdattrs, sl_array_t);
477     dalloc_add(array, fmeta, sl_filemeta_t);
478
479 EC_CLEANUP:
480     EC_EXIT;
481 }
482
483 static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
484 {
485     EC_INIT;
486     slq_t *slq = NULL;
487     uint64_t uint64;
488     sl_cnids_t *cnids;
489     sl_time_t *sl_time;
490     cnid_t id;
491     struct dir *dir;
492     sl_array_t *reqinfo;
493
494
495
496     /* Allocate and initialize query object */
497     slq = talloc_zero(reply, slq_t);
498     slq->slq_state = SLQ_STATE_ATTRS;
499     slq->slq_obj = obj;
500     slq->slq_vol = vol;
501     EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
502     EC_NULL( reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1) );
503     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
504     
505     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
506     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
507     id = (cnid_t)uint64;
508
509     if (htonl(id) == DIRDID_ROOT) {
510         slq->slq_path = talloc_strdup(slq, vol->v_path);
511     } else if (id < CNID_START) {
512         EC_FAIL;
513     } else {
514         cnid_t did;
515         char buffer[12 + MAXPATHLEN + 1];
516         char *name;
517         did = htonl(id);
518         EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
519         EC_NULL( dir = dirlookup(vol, did) );
520         EC_NULL( slq->slq_path = talloc_asprintf(slq, "%s/%s", bdata(dir->d_fullpath), name) );
521     }
522
523     /* Return result value 0 */
524     uint64_t sl_res = 0;
525     dalloc_add_copy(slq->slq_reply, &sl_res, uint64_t);
526
527     /* Return CNID array */
528     sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
529     replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
530     replycnids->ca_unkn1 = 0xfec;
531     replycnids->ca_context = cnids->ca_context;
532     uint64 = (uint64_t)id;
533     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
534     dalloc_add(slq->slq_reply, replycnids, sl_cnids_t);
535
536     /* Fetch attributes from module */
537     EC_ZERO_LOG( sl_module_export->sl_mod_fetch_attrs(slq) );
538
539     dalloc_add(reply, slq->slq_reply, sl_array_t);
540
541 EC_CLEANUP:
542     EC_EXIT;
543 }
544
545 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
546 {
547     EC_INIT;
548     slq_t *slq = NULL;
549     uint64_t *uint64, ctx1, ctx2;
550     sl_array_t *array;
551     
552     /* Context */
553     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
554     ctx1 = *uint64;
555     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
556     ctx2 = *uint64;
557
558     /* Get query for context and free it */
559     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
560     if (slq->slq_state != SLQ_STATE_DONE)
561         LOG(log_warning, logtype_sl, "Closing active query");
562     sl_module_export->sl_mod_end_search(slq);
563     slq_remove(slq);
564     talloc_free(slq);
565     slq = NULL;
566
567     array = talloc_zero(reply, sl_array_t);
568     uint64_t sl_res = 0;
569     dalloc_add_copy(array, &sl_res, uint64_t);
570     dalloc_add(reply, array, sl_array_t);
571
572 EC_CLEANUP:
573     if (ret != 0) {
574         slq_error(slq);
575     }
576     EC_EXIT;
577 }
578
579 /**************************************************************************************************
580  * Spotlight module functions
581  **************************************************************************************************/
582
583 int sl_mod_load(const char *path)
584 {
585     EC_INIT;
586
587     sl_ctx = talloc_new(NULL);
588
589     if ((sl_module = mod_open(path)) == NULL) {
590         LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", path, mod_error());
591         EC_FAIL;
592     }
593
594     if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
595         LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
596         EC_FAIL;
597     }
598
599     sl_module_export->sl_mod_init("test");
600    
601 EC_CLEANUP:
602     EC_EXIT;
603 }
604
605 /**
606  * Index a file
607  **/
608 void sl_index_file(const char *path)
609 {
610     if (sl_module_export && sl_module_export->sl_mod_index_file)
611         sl_module_export->sl_mod_index_file(path);
612 }
613
614 /**************************************************************************************************
615  * AFP functions
616  **************************************************************************************************/
617
618 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
619 {
620     EC_INIT;
621     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
622     uint16_t vid;
623     int cmd;
624     int endianess = SL_ENC_LITTLE_ENDIAN;
625     struct vol      *vol;
626     DALLOC_CTX *query;
627     DALLOC_CTX *reply;
628     char *rpccmd;
629     int len;
630
631     *rbuflen = 0;
632
633     if (sl_module == NULL)
634         return AFPERR_NOOP;
635
636     ibuf += 2;
637     ibuflen -= 2;
638
639     vid = SVAL(ibuf, 0);
640     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
641
642     if ((vol = getvolbyvid(vid)) == NULL) {
643         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
644         ret = AFPERR_ACCESS;
645         goto EC_CLEANUP;
646     }
647
648     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
649
650     cmd = RIVAL(ibuf, 6);
651     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
652
653     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
654
655     switch (cmd) {
656
657     case SPOTLIGHT_CMD_OPEN:
658     case SPOTLIGHT_CMD_OPEN2:
659         RSIVAL(rbuf, 0, ntohs(vid));
660         RSIVAL(rbuf, 4, 0);
661         len = strlen(vol->v_path) + 1;
662         strncpy(rbuf + 8, vol->v_path, len);
663         *rbuflen += 8 + len;
664         break;
665
666     case SPOTLIGHT_CMD_FLAGS:
667         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
668         *rbuflen += 4;
669         break;
670
671     case SPOTLIGHT_CMD_RPC:
672         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
673         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
674         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
675
676         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
677         dd_dump(query, 0);
678
679         EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
680
681         if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
682             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
683         } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
684             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
685         } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
686             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
687         } else if (STRCMP(rpccmd, ==, "storeAttributes:forOIDArray:context:")) {
688             EC_ZERO_LOG( sl_rpc_storeAttributesForOIDArray(obj, query, reply, vol) );
689         } else if (STRCMP(rpccmd, ==, "fetchAttributeNamesForOIDArray:context:")) {
690             EC_ZERO_LOG( sl_rpc_fetchAttributeNamesForOIDArray(obj, query, reply, vol) );
691         } else if (STRCMP(rpccmd, ==, "fetchAttributes:forOIDArray:context:")) {
692             EC_ZERO_LOG( sl_rpc_fetchAttributesForOIDArray(obj, query, reply, vol) );
693         } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
694             EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
695         } else {
696             LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
697         }
698
699         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
700         dd_dump(reply, 0);
701
702         memset(rbuf, 0, 4);
703         *rbuflen += 4;
704
705         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
706         *rbuflen += len;
707         break;
708     }
709
710 EC_CLEANUP:
711     talloc_free(tmp_ctx);
712     if (ret != AFP_OK) {
713         *rbuflen = 0;
714         return AFPERR_MISC;
715     }
716     EC_EXIT;
717 }
718 #endif
719
720 /**************************************************************************************************
721  * Testing
722  **************************************************************************************************/
723
724 #ifdef SPOT_TEST_MAIN
725
726 int main(int argc, char **argv)
727 {
728     EC_INIT;
729     TALLOC_CTX *mem_ctx = talloc_new(NULL);
730     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
731     uint64_t i;
732
733     set_processname("spot");
734     setuplog("default:info,spotlight:debug", "/dev/tty");
735
736     LOG(log_info, logtype_sl, "Start");
737
738     i = 1;
739     dalloc_add_copy(dd, &i, uint64_t);
740     char *str = dalloc_strdup(dd, "hello world");
741     dalloc_add(dd, str, char *);
742     sl_bool_t b = true;
743     dalloc_add_copy(dd, &b, sl_bool_t);
744
745     /* add a nested array */
746     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
747     i = 3;
748     dalloc_add_copy(nested, &i, uint64_t);
749     dalloc_add(dd, nested, DALLOC_CTX);
750
751     /* test an allocated CNID array */
752     uint64_t id = 16;
753     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
754     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
755     cnids->ca_unkn1 = 1;
756     dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
757     dalloc_add(dd, cnids, sl_cnids_t);
758
759     /* Now the Spotlight types */
760     sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
761     i = 0x1234;
762     dalloc_add_copy(sl_array, &i, uint64_t);
763
764     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
765     i = 0xffff;
766     dalloc_add_copy(sl_dict, &i, uint64_t);
767     dalloc_add(sl_array, sl_dict, sl_dict_t);
768
769     dalloc_add(dd, sl_array, sl_array_t);
770
771     dd_dump(dd, 0);
772
773     /* now parse a real spotlight packet */
774     if (argc > 1) {
775         char ibuf[8192];
776         char rbuf[8192];
777         int fd;
778         size_t len;
779         DALLOC_CTX *query;
780
781         EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
782
783         EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
784         EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
785         close(fd);
786         EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
787
788         /* Now dump the whole thing */
789         dd_dump(query, 0);
790     }
791
792 #if 0
793     /* packing  */
794     int qlen;
795     char buf[MAX_SLQ_DAT];
796     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
797
798     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
799     lseek(fd, 24, SEEK_SET);
800     write(fd, buf, qlen);
801     close(fd);
802 #endif
803
804 EC_CLEANUP:
805     if (mem_ctx) {
806         talloc_free(mem_ctx);
807         mem_ctx = NULL;
808     }
809     EC_EXIT;
810 }
811 #endif