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