]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Spotlight: fix async query close
[netatalk.git] / etc / afpd / spotlight.c
1 /*
2   Copyright (c) 2012-2014 Ralph Boehme
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 #include "etc/spotlight/sparql_parser.h"
44
45 #define MAX_SL_RESULTS 20
46
47 struct slq_state_names {
48     slq_state_t state;
49     const char *state_name;
50 };
51
52 static struct slq_state_names slq_state_names[] = {
53     {SLQ_STATE_NEW, "SLQ_STATE_NEW"},
54     {SLQ_STATE_RUNNING, "SLQ_STATE_RUNNING"},
55     {SLQ_STATE_RESULTS, "SLQ_STATE_RESULTS"},
56     {SLQ_STATE_FULL, "SLQ_STATE_FULL"},
57     {SLQ_STATE_DONE, "SLQ_STATE_DONE"},
58     {SLQ_STATE_CANCEL_PENDING, "SLQ_STATE_CANCEL_PENDING"},
59     {SLQ_STATE_CANCELLED, "SLQ_STATE_CANCELLED"},
60     {SLQ_STATE_ERROR, "SLQ_STATE_ERROR"}
61 };
62
63
64 static char *tracker_to_unix_path(TALLOC_CTX *mem_ctx, const char *uri);
65 static int cnid_comp_fn(const void *p1, const void *p2);
66 static bool create_result_handle(slq_t *slq);
67 static bool add_filemeta(sl_array_t *reqinfo,
68                          sl_array_t *fm_array,
69                          const char *path,
70                          const struct stat *sp);
71
72 /************************************************
73  * Misc utility functions
74  ************************************************/
75
76 static char *tab_level(TALLOC_CTX *mem_ctx, int level)
77 {
78     int i;
79     char *string = talloc_array(mem_ctx, char, level + 1);
80
81     for (i = 0; i < level; i++) {
82         string[i] = '\t';
83     }
84
85     string[i] = '\0';
86     return string;
87 }
88
89 static char *dd_dump(DALLOC_CTX *dd, int nestinglevel)
90 {
91     const char *type;
92     int n;
93     uint64_t i;
94     sl_bool_t bl;
95     sl_time_t t;
96     struct tm *tm;
97     char datestring[256];
98     sl_cnids_t cnids;
99     char *logstring, *nested_logstring;
100     char *tab_string1, *tab_string2;
101
102     tab_string1 = tab_level(dd, nestinglevel);
103     tab_string2 = tab_level(dd, nestinglevel + 1);
104     if (tab_string1 == NULL || tab_string2 == NULL) {
105         return NULL;
106     }
107
108     logstring = talloc_asprintf(dd,
109                                 "%s%s(#%lu): {\n",
110                                 tab_string1,
111                                 talloc_get_name(dd),
112                                 talloc_array_length(dd->dd_talloc_array));
113
114     for (n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
115         type = talloc_get_name(dd->dd_talloc_array[n]);
116         if (strequal(type, "DALLOC_CTX")
117             || strequal(type, "sl_array_t")
118             || strequal(type, "sl_filemeta_t")
119             || strequal(type, "sl_dict_t")) {
120             nested_logstring = dd_dump(dd->dd_talloc_array[n],
121                                        nestinglevel + 1);
122             if (!nested_logstring) {
123                 return NULL;
124             }
125             logstring = talloc_strdup_append(logstring,
126                                              nested_logstring);
127             if (!logstring) {
128                 return NULL;
129             }
130         } else if (strequal(type, "uint64_t")) {
131             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
132             logstring = talloc_asprintf_append(
133                 logstring,
134                 "%suint64_t: 0x%04" PRIx64 "\n",
135                 tab_string2, i);
136             if (!logstring) {
137                 return NULL;
138             }
139         } else if (strequal(type, "char *")) {
140             logstring = talloc_asprintf_append(
141                 logstring,
142                 "%sstring: %s\n",
143                 tab_string2,
144                 (char *)dd->dd_talloc_array[n]);
145             if (!logstring) {
146                 return NULL;
147             }
148         } else if (strequal(type, "smb_ucs2_t *")) {
149             logstring = talloc_asprintf_append(
150                 logstring,
151                 "%sUTF16-string: %s\n",
152                 tab_string2,
153                 (char *)dd->dd_talloc_array[n]);
154             if (!logstring) {
155                 return NULL;
156             }
157         } else if (strequal(type, "sl_bool_t")) {
158             memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
159             logstring = talloc_asprintf_append(
160                 logstring,
161                 "%sbool: %s\n",
162                 tab_string2,
163                 bl ? "true" : "false");
164             if (!logstring) {
165                 return NULL;
166             }
167         } else if (strequal(type, "sl_nil_t")) {
168             logstring = talloc_asprintf_append(
169                 logstring,
170                 "%snil\n",
171                 tab_string2);
172             if (!logstring) {
173                 return NULL;
174             }
175         } else if (strequal(type, "sl_time_t")) {
176             memcpy(&t, dd->dd_talloc_array[n], sizeof(sl_time_t));
177             tm = localtime(&t.tv_sec);
178             strftime(datestring,
179                      sizeof(datestring),
180                      "%Y-%m-%d %H:%M:%S", tm);
181             logstring = talloc_asprintf_append(
182                 logstring,
183                 "%ssl_time_t: %s.%06lu\n",
184                 tab_string2,
185                 datestring,
186                 (unsigned long)t.tv_usec);
187             if (!logstring) {
188                 return NULL;
189             }
190         } else if (strequal(type, "sl_cnids_t")) {
191             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
192             logstring = talloc_asprintf_append(
193                 logstring,
194                 "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32 "\n",
195                 tab_string2,
196                 cnids.ca_unkn1,
197                 cnids.ca_context);
198             if (!logstring) {
199                 return NULL;
200             }
201             if (cnids.ca_cnids) {
202                 nested_logstring = dd_dump(
203                     cnids.ca_cnids,
204                     nestinglevel + 2);
205                 if (!nested_logstring) {
206                     return NULL;
207                 }
208                 logstring = talloc_strdup_append(logstring,
209                                                  nested_logstring);
210                 if (!logstring) {
211                     return NULL;
212                 }
213             }
214         } else {
215             logstring = talloc_asprintf_append(
216                 logstring,
217                 "%stype: %s\n",
218                 tab_string2,
219                 type);
220             if (!logstring) {
221                 return NULL;
222             }
223         }
224     }
225     logstring = talloc_asprintf_append(logstring,
226                                        "%s}\n",
227                                        tab_string1);
228     if (!logstring) {
229         return NULL;
230     }
231     return logstring;
232 }
233
234 static int cnid_comp_fn(const void *p1, const void *p2)
235 {
236     const uint64_t *cnid1 = p1, *cnid2 = p2;
237     if (*cnid1 == *cnid2) {
238         return 0;
239     }
240     if (*cnid1 < *cnid2) {
241         return -1;
242     }
243     return 1;
244 }
245
246 static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p)
247 {
248     EC_INIT;
249     uint64_t *cnids = NULL;
250
251     EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) );
252
253     for (int i = 0; i < talloc_array_length(p); i++) {
254         memcpy(&cnids[i], p->dd_talloc_array[i], sizeof(uint64_t));
255     }
256     qsort(cnids, talloc_array_length(p), sizeof(uint64_t), cnid_comp_fn);
257
258     slq->slq_cnids = cnids;
259     slq->slq_cnids_num = talloc_array_length(p);
260
261 EC_CLEANUP:
262     if (ret != 0) {
263         if (cnids)
264             talloc_free(cnids);
265     }
266     EC_EXIT;
267 }
268
269 static char *tracker_to_unix_path(TALLOC_CTX *mem_ctx, const char *uri)
270 {
271     GFile *f;
272     char *path;
273     char *talloc_path = NULL;
274
275     f = g_file_new_for_uri(uri);
276     if (!f) {
277         return NULL;
278     }
279
280     path = g_file_get_path(f);
281     g_object_unref(f);
282
283     if (!path) {
284         return NULL;
285     }
286
287     talloc_path = talloc_strdup(mem_ctx, path);
288     g_free(path);
289
290     return talloc_path;
291 }
292
293 /**
294  * Add requested metadata for a query result element
295  *
296  * This could be rewritten to something more sophisticated like
297  * querying metadata from Tracker.
298  *
299  * If path or sp is NULL, simply add nil values for all attributes.
300  **/
301 static bool add_filemeta(sl_array_t *reqinfo,
302                          sl_array_t *fm_array,
303                          const char *path,
304                          const struct stat *sp)
305 {
306     sl_array_t *meta;
307     sl_nil_t nil;
308     int i, metacount;
309     uint64_t uint64var;
310     sl_time_t sl_time;
311     char *p, *name;
312
313     metacount = talloc_array_length(reqinfo->dd_talloc_array);
314     if (metacount == 0 || path == NULL || sp == NULL) {
315         dalloc_add_copy(fm_array, &nil, sl_nil_t);
316         return true;
317     }
318
319     meta = talloc_zero(fm_array, sl_array_t);
320
321     for (i = 0; i < metacount; i++) {
322         if (strequal(reqinfo->dd_talloc_array[i], "kMDItemDisplayName")
323             || strequal(reqinfo->dd_talloc_array[i], "kMDItemFSName")) {
324             if ((p = strrchr(path, '/'))) {
325                 name = dalloc_strdup(meta, p + 1);
326                 dalloc_add(meta, name, "char *");
327             }
328         } else if (strequal(reqinfo->dd_talloc_array[i],
329                             "kMDItemPath")) {
330             name = dalloc_strdup(meta, path);
331             dalloc_add(meta, name, "char *");
332         } else if (strequal(reqinfo->dd_talloc_array[i],
333                             "kMDItemFSSize")) {
334             uint64var = sp->st_size;
335             dalloc_add_copy(meta, &uint64var, uint64_t);
336         } else if (strequal(reqinfo->dd_talloc_array[i],
337                             "kMDItemFSOwnerUserID")) {
338             uint64var = sp->st_uid;
339             dalloc_add_copy(meta, &uint64var, uint64_t);
340         } else if (strequal(reqinfo->dd_talloc_array[i],
341                             "kMDItemFSOwnerGroupID")) {
342             uint64var = sp->st_gid;
343             dalloc_add_copy(meta, &uint64var, uint64_t);
344         } else if (strequal(reqinfo->dd_talloc_array[i],
345                             "kMDItemFSContentChangeDate")) {
346             sl_time.tv_sec = sp->st_mtime;
347             dalloc_add_copy(meta, &sl_time, sl_time_t);
348         } else {
349             dalloc_add_copy(meta, &nil, sl_nil_t);
350         }
351     }
352
353     dalloc_add(fm_array, meta, sl_array_t);
354     return true;
355 }
356
357 /**
358  * Allocate result handle used in the async Tracker cursor result
359  * handler for storing results
360  **/
361 static bool create_result_handle(slq_t *slq)
362 {
363     sl_nil_t nil = 0;
364     struct sl_rslts *query_results;
365
366     if (slq->query_results) {
367         LOG(log_error, logtype_sl,"unexpected existing result handle");
368         return false;
369     }
370
371     query_results = talloc_zero(slq, struct sl_rslts);
372
373     /* CNIDs */
374     query_results->cnids = talloc_zero(query_results, sl_cnids_t);
375     if (query_results->cnids == NULL) {
376         return false;
377     }
378     query_results->cnids->ca_cnids = talloc_zero(query_results->cnids,
379                                                  DALLOC_CTX);
380     if (query_results->cnids->ca_cnids == NULL) {
381         return false;
382     }
383
384     query_results->cnids->ca_unkn1 = 0xadd;
385     query_results->cnids->ca_context = slq->slq_ctx2;
386
387     /* FileMeta */
388     query_results->fm_array = talloc_zero(query_results, sl_array_t);
389     if (query_results->fm_array == NULL) {
390         return false;
391     }
392
393     /* For some reason the list of results always starts with a nil entry */
394     dalloc_add_copy(query_results->fm_array, &nil, sl_nil_t);
395
396     slq->query_results = query_results;
397     return true;
398 }
399
400 static bool add_results(sl_array_t *array, slq_t *slq)
401 {
402     sl_filemeta_t *fm;
403     uint64_t status = 0;
404
405     /* FileMeta */
406     fm = talloc_zero(array, sl_filemeta_t);
407     if (!fm) {
408         return false;
409     }
410
411     dalloc_add_copy(array, &status, uint64_t);
412     dalloc_add(array, slq->query_results->cnids, sl_cnids_t);
413     if (slq->query_results->num_results > 0) {
414         dalloc_add(fm, slq->query_results->fm_array, sl_array_t);
415     }
416     dalloc_add(array, fm, sl_filemeta_t);
417
418     /* This ensure the results get clean up after been sent to the client */
419     talloc_steal(array, slq->query_results);
420     slq->query_results = NULL;
421
422     if (!create_result_handle(slq)) {
423         LOG(log_error, logtype_sl, "couldn't add result handle");
424         slq->slq_state = SLQ_STATE_ERROR;
425         return false;
426     }
427
428     return true;
429 }
430
431 /******************************************************************************
432  * Spotlight queries
433  ******************************************************************************/
434
435 static ATALK_LIST_HEAD(sl_queries);
436 static ATALK_LIST_HEAD(sl_cancelled_queries);
437
438 /**
439  * Add a query to the list of active queries
440  **/
441 static void slq_add(slq_t *slq)
442 {
443     list_add(&(slq->slq_list), &sl_queries);
444 }
445
446 /**
447  * Add a query to the list of active queries
448  **/
449 static void slq_cancelled_add(slq_t *slq)
450 {
451     list_add(&(slq->slq_list), &sl_cancelled_queries);
452 }
453
454 /**
455  * Remove a query from the active list
456  **/
457 static void slq_remove(slq_t *slq)
458 {
459     struct list_head *p;
460     slq_t *q = NULL;
461
462     list_for_each(p, &sl_queries) {
463         q = list_entry(p, slq_t, slq_list);
464         if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) {
465             list_del(p);
466             break;
467         }
468     }
469
470     return;
471 }
472
473 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
474 {
475     slq_t *q = NULL;
476     struct list_head *p;
477
478     list_for_each(p, &sl_queries) {
479         q = list_entry(p, slq_t, slq_list);
480         if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) {
481             break;
482         }
483         q = NULL;
484     }
485
486     return q;
487 }
488
489 /**
490  * Remove a query from the active queue and free it
491  **/
492 static void slq_destroy(slq_t *slq)
493 {
494     if (slq == NULL) {
495         return;
496     }
497     slq_remove(slq);
498     talloc_free(slq);
499 }
500
501 /**
502  * Cancel a query
503  **/
504 static void slq_cancel(slq_t *slq)
505 {
506     slq->slq_state = SLQ_STATE_CANCEL_PENDING;
507     slq_remove(slq);
508     slq_cancelled_add(slq);
509 }
510
511 /**
512  * talloc destructor cb
513  **/
514 static int slq_free_cb(slq_t *slq)
515 {
516     if (slq->tracker_cursor) {
517         g_object_unref(slq->tracker_cursor);
518     }
519     return 0;
520 }
521
522 /**
523  * Free all cancelled queries
524  **/
525 static void slq_cancelled_cleanup(void)
526 {
527     struct list_head *p;
528     slq_t *q = NULL;
529
530     list_for_each(p, &sl_cancelled_queries) {
531         q = list_entry(p, slq_t, slq_list);
532         if (q->slq_state == SLQ_STATE_CANCELLED) {
533             LOG(log_debug, logtype_sl,
534                 "ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": cancelled",
535                 q->slq_ctx1, q->slq_ctx2);
536             list_del(p);
537             talloc_free(q);
538         } else {
539             LOG(log_debug, logtype_sl,
540                 "ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": %s",
541                 q->slq_ctx1, q->slq_ctx2, slq_state_names[q->slq_state].state_name);
542         }
543     }
544
545     return;
546 }
547
548 static void slq_dump(void)
549 {
550     struct list_head *p;
551     slq_t *q = NULL;
552     int i = 0;
553
554     list_for_each(p, &sl_queries) {
555         q = list_entry(p, slq_t, slq_list);
556         LOG(log_debug, logtype_sl,
557             "query[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ", state: %s",
558             i++, q->slq_ctx1, q->slq_ctx2,
559             slq_state_names[q->slq_state].state_name);
560     }
561
562     return;
563 }
564
565 /************************************************
566  * Tracker async callbacks
567  ************************************************/
568
569 static void tracker_con_cb(GObject      *object,
570                            GAsyncResult *res,
571                            gpointer      user_data)
572 {
573     struct sl_ctx *sl_ctx = user_data;
574     GError *error = NULL;
575
576     sl_ctx->tracker_con = tracker_sparql_connection_get_finish(res,
577                                                                &error);
578     if (error) {
579         LOG(log_error, logtype_sl, "Could not connect to Tracker: %s",
580             error->message);
581         sl_ctx->tracker_con = NULL;
582         g_error_free(error);
583         return;
584     }
585
586     LOG(log_info, logtype_sl, "connected to Tracker");
587 }
588
589 static void tracker_cursor_cb(GObject      *object,
590                               GAsyncResult *res,
591                               gpointer      user_data)
592 {
593     GError *error = NULL;
594     slq_t *slq = user_data;
595     gboolean more_results;
596     const gchar *uri;
597     char *path;
598     int result;
599     struct stat sb;
600     uint64_t uint64var;
601     bool ok;
602     cnid_t did, id;
603
604     LOG(log_debug, logtype_sl,
605         "cursor cb: ctx1: %" PRIx64 ", ctx2: %" PRIx64,
606         slq->slq_ctx1, slq->slq_ctx2);
607
608     more_results = tracker_sparql_cursor_next_finish(slq->tracker_cursor,
609                                                      res,
610                                                      &error);
611
612     if (slq->slq_state == SLQ_STATE_CANCEL_PENDING) {
613         LOG(log_debug, logtype_sl,
614             "cursor cb: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": cancelled",
615             slq->slq_ctx1, slq->slq_ctx2);
616         slq->slq_state = SLQ_STATE_CANCELLED;
617         return;
618     }
619
620     if (error) {
621         LOG(log_error, logtype_sl, "Tracker cursor: %s", error->message);
622         g_error_free(error);
623         slq->slq_state = SLQ_STATE_ERROR;
624         return;
625     }
626
627     if (!more_results) {
628         LOG(log_debug, logtype_sl, "tracker_cursor_cb: done");
629         slq->slq_state = SLQ_STATE_DONE;
630         return;
631     }
632
633     uri = tracker_sparql_cursor_get_string(slq->tracker_cursor, 0, NULL);
634     if (uri == NULL) {
635         /*
636          * Not sure how this could happen if
637          * tracker_sparql_cursor_next_finish() returns true, but I've
638          * seen it.
639          */
640         LOG(log_debug, logtype_sl, "no URI for result");
641         return;
642     }
643
644     LOG(log_debug, logtype_sl, "URI: %s", uri);
645
646     path = tracker_to_unix_path(slq->query_results, uri);
647     if (path == NULL) {
648         LOG(log_error, logtype_sl, "error converting Tracker URI: %s", uri);
649         slq->slq_state = SLQ_STATE_ERROR;
650         return;
651     }
652
653     result = access(path, R_OK);
654     if (result != 0) {
655         goto exit;
656     }
657
658     id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did);
659     if (id == CNID_INVALID) {
660         LOG(log_error, logtype_sl, "cnid_for_path error: %s", path);
661         goto exit;
662     }
663     uint64var = ntohl(id);
664
665     if (slq->slq_cnids) {
666         ok = bsearch(&uint64var, slq->slq_cnids, slq->slq_cnids_num,
667                      sizeof(uint64_t), cnid_comp_fn);
668         if (!ok) {
669             goto exit;
670         }
671     }
672
673     dalloc_add_copy(slq->query_results->cnids->ca_cnids,
674                     &uint64var, uint64_t);
675     ok = add_filemeta(slq->slq_reqinfo, slq->query_results->fm_array,
676                       path, &sb);
677     if (!ok) {
678         LOG(log_error, logtype_sl, "add_filemeta error");
679         slq->slq_state = SLQ_STATE_ERROR;
680         return;
681     }
682
683     slq->query_results->num_results++;
684
685 exit:
686     if (slq->query_results->num_results < MAX_SL_RESULTS) {
687         slq->slq_state = SLQ_STATE_RESULTS;
688         tracker_sparql_cursor_next_async(slq->tracker_cursor,
689                                          slq->slq_obj->sl_ctx->cancellable,
690                                          tracker_cursor_cb,
691                                          slq);
692     } else {
693         slq->slq_state = SLQ_STATE_FULL;
694     }
695 }
696
697 static void tracker_query_cb(GObject      *object,
698                              GAsyncResult *res,
699                              gpointer      user_data)
700 {
701     bool ok;
702     GError *error = NULL;
703     slq_t *slq = user_data;
704
705     LOG(log_debug, logtype_sl,
706         "query cb: ctx1: %" PRIx64 ", ctx2: %" PRIx64,
707         slq->slq_ctx1, slq->slq_ctx2);
708
709     slq->tracker_cursor = tracker_sparql_connection_query_finish(
710         TRACKER_SPARQL_CONNECTION(object),
711         res,
712         &error);
713
714     if (slq->slq_state == SLQ_STATE_CANCEL_PENDING) {
715         slq->slq_state = SLQ_STATE_CANCELLED;
716         return;
717     }
718
719     if (error) {
720         slq->slq_state = SLQ_STATE_ERROR;
721         LOG(log_error, logtype_sl, "Tracker query error: %s", error->message);
722         g_error_free(error);
723         return;
724     }
725
726     slq->slq_state = SLQ_STATE_RESULTS;
727
728     ok = create_result_handle(slq);
729     if (!ok) {
730         LOG(log_error, logtype_sl, "create_result_handle error");
731         slq->slq_state = SLQ_STATE_ERROR;
732         return;
733     }
734
735     tracker_sparql_cursor_next_async(slq->tracker_cursor,
736                                      slq->slq_obj->sl_ctx->cancellable,
737                                      tracker_cursor_cb,
738                                      slq);
739 }
740
741 /*******************************************************************************
742  * Spotlight RPC functions
743  ******************************************************************************/
744
745 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj,
746                                             const DALLOC_CTX *query,
747                                             DALLOC_CTX *reply,
748                                             const struct vol *v)
749 {
750     EC_INIT;
751
752     char *s;
753     sl_dict_t *dict;
754     sl_array_t *array;
755     sl_uuid_t uuid;
756
757     if (!v->v_uuid) {
758         EC_FAIL_LOG("missing UUID for volume: %s", v->v_localname);
759     }
760     dict = talloc_zero(reply, sl_dict_t);
761
762     /* key/val 1 */
763     s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
764     dalloc_add(dict, s, char *);
765
766     array = talloc_zero(dict, sl_array_t);
767     s = dalloc_strdup(array, "kMDQueryScopeComputer");
768     dalloc_add(array, s, char *);
769     dalloc_add(dict, array, sl_array_t);
770
771     /* key/val 2 */
772     s = dalloc_strdup(dict, "kMDSStorePathScopes");
773     dalloc_add(dict, s, char *);
774
775     array = talloc_zero(dict, sl_array_t);
776     s = dalloc_strdup(array, v->v_path);
777     dalloc_add(array, s, char *);
778     dalloc_add(dict, array, sl_array_t);
779
780     /* key/val 3 */
781     s = dalloc_strdup(dict, "kMDSStoreUUID");
782     dalloc_add(dict, s, char *);
783
784     memcpy(uuid.sl_uuid, v->v_uuid, 16);
785     dalloc_add_copy(dict, &uuid, sl_uuid_t);
786
787     /* key/val 4 */
788     s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
789     dalloc_add(dict, s, char *);
790     sl_bool_t b = true;
791     dalloc_add_copy(dict, &b, sl_bool_t);
792
793     dalloc_add(reply, dict, sl_dict_t);
794
795 EC_CLEANUP:
796     EC_EXIT;
797 }
798
799 static int sl_rpc_openQuery(AFPObj *obj,
800                             const DALLOC_CTX *query,
801                             DALLOC_CTX *reply,
802                             struct vol *v)
803 {
804     EC_INIT;
805     char *sl_query;
806     uint64_t *uint64;
807     DALLOC_CTX *reqinfo;
808     sl_array_t *array;
809     sl_cnids_t *cnids;
810     slq_t *slq;
811     char slq_host[MAXPATHLEN + 1];
812     uint16_t convflags = v->v_mtou_flags;
813     uint64_t result;
814     gchar *sparql_query;
815     GError *error = NULL;
816
817     array = talloc_zero(reply, sl_array_t);
818
819     if (obj->sl_ctx->tracker_con == NULL) {
820         LOG(log_error, logtype_sl, "no tracker connection");
821         EC_FAIL;
822     }
823
824     /* Allocate and initialize query object */
825     slq = talloc_zero(obj->sl_ctx, slq_t);
826     slq->slq_state = SLQ_STATE_NEW;
827     slq->slq_obj = obj;
828     slq->slq_vol = v;
829     slq->slq_allow_expr = obj->options.flags & OPTION_SPOTLIGHT_EXPR ? true : false;
830     slq->slq_result_limit = obj->options.sparql_limit;
831     talloc_set_destructor(slq, slq_free_cb);
832
833     LOG(log_debug, logtype_sl, "Spotlight: expr: %s, limit: %" PRIu64,
834         slq->slq_allow_expr ? "yes" : "no", slq->slq_result_limit);
835
836     /* convert spotlight query charset to host charset */
837     sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0,
838                                     "DALLOC_CTX", 1,
839                                     "kMDQueryString");
840     if (sl_query == NULL) {
841         EC_FAIL;
842     }
843     ret = convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset,
844                           sl_query, strlen(sl_query), slq_host, MAXPATHLEN,
845                           &convflags);
846     if (ret == -1) {
847         LOG(log_error, logtype_sl, "charset conversion failed");
848         EC_FAIL;
849     }
850     slq->slq_qstring = talloc_strdup(slq, slq_host);
851     LOG(log_debug, logtype_sl, "Spotlight query: \"%s\"", slq->slq_qstring);
852
853     slq->slq_time = time(NULL);
854     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
855     if (uint64 == NULL) {
856         EC_FAIL;
857     }
858     slq->slq_ctx1 = *uint64;
859
860     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
861     if (uint64 == NULL) {
862         EC_FAIL;
863     }
864     slq->slq_ctx2 = *uint64;
865
866     reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1,
867                                    "kMDAttributeArray");
868     if (reqinfo == NULL) {
869         EC_FAIL;
870     }
871     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
872
873     cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1,
874                                  "kMDQueryItemArray");
875     if (cnids) {
876         EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
877     }
878
879     ret = map_spotlight_to_sparql_query(slq, &sparql_query);
880     if (ret != 0) {
881         LOG(log_debug, logtype_sl, "mapping retured non-zero");
882         EC_FAIL;
883     }
884     LOG(log_debug, logtype_sl, "SPARQL query: \"%s\"", sparql_query);
885
886     tracker_sparql_connection_query_async(obj->sl_ctx->tracker_con,
887                                           sparql_query,
888                                           slq->slq_obj->sl_ctx->cancellable,
889                                           tracker_query_cb,
890                                           slq);
891     if (error) {
892         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
893             error->message);
894         g_clear_error(&error);
895         EC_FAIL;
896     }
897
898     slq->slq_state = SLQ_STATE_RUNNING;
899     slq_add(slq);
900
901 EC_CLEANUP:
902     if (ret != 0) {
903         slq_destroy(slq);
904         result = UINT64_MAX;
905         ret = 0;
906     } else {
907         result = 0;
908     }
909
910     dalloc_add_copy(array, &result, uint64_t);
911     dalloc_add(reply, array, sl_array_t);
912     EC_EXIT;
913 }
914
915 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj,
916                                               const DALLOC_CTX *query,
917                                               DALLOC_CTX *reply,
918                                               const struct vol *v)
919 {
920     EC_INIT;
921     slq_t *slq = NULL;
922     uint64_t *uint64, ctx1, ctx2, status;
923     sl_array_t *array;
924     bool ok;
925
926     array = talloc_zero(reply, sl_array_t);
927     if (array == NULL) {
928         return false;
929     }
930
931     /* Context */
932     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
933     if (uint64 == NULL) {
934         EC_FAIL;
935     }
936     ctx1 = *uint64;
937     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
938     if (uint64 == NULL) {
939         EC_FAIL;
940     }
941     ctx2 = *uint64;
942
943     /* Get query for context */
944     slq = slq_for_ctx(ctx1, ctx2);
945     if (slq == NULL) {
946         EC_FAIL;
947     }
948
949     switch (slq->slq_state) {
950     case SLQ_STATE_RUNNING:
951     case SLQ_STATE_RESULTS:
952     case SLQ_STATE_FULL:
953     case SLQ_STATE_DONE:
954         ok = add_results(array, slq);
955         if (!ok) {
956             LOG(log_error, logtype_sl, "error adding results");
957             EC_FAIL;
958         }
959         if (slq->slq_state == SLQ_STATE_RESULTS
960             || slq->slq_state == SLQ_STATE_FULL) {
961             tracker_sparql_cursor_next_async(
962                 slq->tracker_cursor,
963                 slq->slq_obj->sl_ctx->cancellable,
964                 tracker_cursor_cb,
965                 slq);
966         }
967         break;
968
969     case SLQ_STATE_ERROR:
970         LOG(log_error, logtype_sl, "query in error state");
971         EC_FAIL;
972
973     default:
974         LOG(log_error, logtype_sl, "unexpected query state %d", slq->slq_state);
975         EC_FAIL;
976     }
977
978     dalloc_add(reply, array, sl_array_t);
979     EC_EXIT;
980
981 EC_CLEANUP:
982     slq_destroy(slq);
983     status = UINT64_MAX;
984     dalloc_add_copy(array, &status, uint64_t);
985     dalloc_add(reply, array, sl_array_t);
986     EC_EXIT;
987 }
988
989 static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj,
990                                              const DALLOC_CTX *query,
991                                              DALLOC_CTX *reply,
992                                              const struct vol *vol)
993 {
994     EC_INIT;
995     uint64_t uint64;
996     sl_array_t *array;
997     sl_cnids_t *cnids;
998     sl_time_t *sl_time;
999     cnid_t id;
1000     char *path;
1001     struct dir *dir;
1002
1003     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
1004     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
1005     id = (cnid_t)uint64;
1006     LOG(log_debug, logtype_sl, "CNID: %" PRIu32, id);
1007
1008     if (htonl(id) == DIRDID_ROOT) {
1009         path = vol->v_path;
1010     } else if (id < CNID_START) {
1011         EC_FAIL;
1012     } else {
1013         cnid_t did;
1014         char buffer[12 + MAXPATHLEN + 1];
1015
1016         did = htonl(id);
1017         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
1018         EC_NULL_LOG( dir = dirlookup(vol, did) );
1019         EC_NEG1_LOG( movecwd(vol, dir) );
1020     }
1021
1022     /*
1023      * We're possibly supposed to update attributes in two places: the
1024      * database and the filesystem.  Due to the lack of documentation
1025      * and not yet implemented database updates, we cherry pick attributes
1026      * that seems to be candidates for updating filesystem metadata.
1027      */
1028
1029     if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemFSContentChangeDate"))) {
1030         struct utimbuf utimes;
1031         utimes.actime = utimes.modtime = sl_time->tv_sec;
1032         utime(path, &utimes);
1033     }
1034
1035     array = talloc_zero(reply, sl_array_t);
1036     uint64_t sl_res = 0;
1037     dalloc_add_copy(array, &sl_res, uint64_t);
1038     dalloc_add(reply, array, sl_array_t);
1039
1040 EC_CLEANUP:
1041     EC_EXIT;
1042 }
1043
1044 static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
1045 {
1046     EC_INIT;
1047     uint64_t uint64;
1048     sl_cnids_t *cnids;
1049     cnid_t id;
1050     char *path;
1051     struct dir *dir;
1052
1053     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) );
1054     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
1055     id = (cnid_t)uint64;
1056     LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
1057
1058     if (htonl(id) == DIRDID_ROOT) {
1059         path = vol->v_path;
1060     } else if (id < CNID_START) {
1061         EC_FAIL;
1062     } else {
1063         cnid_t did;
1064         char buffer[12 + MAXPATHLEN + 1];
1065
1066         did = htonl(id);
1067         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
1068         EC_NULL_LOG( dir = dirlookup(vol, did) );
1069         EC_NEG1_LOG( movecwd(vol, dir) );
1070     }
1071
1072     /* Result array */
1073     sl_array_t *array = talloc_zero(reply, sl_array_t);
1074     dalloc_add(reply, array, sl_array_t);
1075
1076     /* Return result value 0 */
1077     uint64_t sl_res = 0;
1078     dalloc_add_copy(array, &sl_res, uint64_t);
1079
1080     /* Return CNID array */
1081     sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
1082     replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
1083     replycnids->ca_unkn1 = 0xfec;
1084     replycnids->ca_context = cnids->ca_context;
1085     uint64 = (uint64_t)id;
1086     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
1087     dalloc_add(array, replycnids, sl_cnids_t);
1088
1089     /* Return filemeta array */
1090
1091     /*
1092      * FIXME: this should return the real attributes from all known metadata sources
1093      * (Tracker and filesystem)
1094      */
1095     sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
1096     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
1097     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
1098     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
1099     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
1100     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
1101     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
1102
1103     sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
1104     dalloc_add(fmeta, mdattrs, sl_array_t);
1105     dalloc_add(array, fmeta, sl_filemeta_t);
1106
1107 EC_CLEANUP:
1108     EC_EXIT;
1109 }
1110
1111 static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
1112 {
1113     EC_INIT;
1114     uint64_t uint64;
1115     sl_cnids_t *cnids, *replycnids;
1116     cnid_t id, did;
1117     struct dir *dir;
1118     sl_array_t *array, *reqinfo, *fm_array;
1119     char buffer[12 + MAXPATHLEN + 1];
1120     char *name, *path;
1121     sl_filemeta_t *fm;
1122     sl_nil_t nil;
1123     uint64_t sl_res;
1124     struct stat sb;
1125
1126     array = talloc_zero(reply, sl_array_t);
1127     replycnids = talloc_zero(reply, sl_cnids_t);
1128     replycnids->ca_cnids = talloc_zero(replycnids, DALLOC_CTX);
1129     fm = talloc_zero(array, sl_filemeta_t);
1130     fm_array = talloc_zero(fm, sl_array_t);
1131
1132     if (array == NULL || replycnids == NULL || replycnids->ca_cnids == NULL
1133         || fm == NULL || fm_array == NULL) {
1134         EC_FAIL;
1135     }
1136
1137     reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1);
1138     if (reqinfo == NULL) {
1139         EC_FAIL;
1140     }
1141     cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2);
1142     if (cnids == NULL) {
1143         EC_FAIL;
1144     }
1145
1146     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
1147     id = (cnid_t)uint64;
1148
1149     if (htonl(id) == DIRDID_ROOT) {
1150         path = talloc_strdup(reply, vol->v_path);
1151     } else if (id < CNID_START) {
1152         EC_FAIL;
1153     } else {
1154         did = htonl(id);
1155         EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
1156         EC_NULL( dir = dirlookup(vol, did) );
1157         EC_NULL( path = talloc_asprintf(reply, "%s/%s", bdata(dir->d_fullpath), name) );
1158     }
1159
1160     EC_ZERO( stat(path, &sb) );
1161
1162     sl_res = 0;
1163     dalloc_add_copy(array, &sl_res, uint64_t);
1164
1165     replycnids->ca_unkn1 = 0xfec;
1166     replycnids->ca_context = cnids->ca_context;
1167     uint64 = (uint64_t)id;
1168     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
1169     dalloc_add(array, replycnids, sl_cnids_t);
1170     dalloc_add(fm, fm_array, fm_array_t);
1171     dalloc_add_copy(fm_array, &nil, sl_nil_t);
1172     add_filemeta(reqinfo, fm_array, path, &sb);
1173
1174     /* Now add result */
1175     dalloc_add(array, fm, sl_filemeta_t);
1176     dalloc_add(reply, array, sl_array_t);
1177     EC_EXIT;
1178
1179 EC_CLEANUP:
1180     sl_res = UINT64_MAX;
1181     dalloc_add_copy(array, &sl_res, uint64_t);
1182     dalloc_add(array, fm, sl_filemeta_t);
1183     dalloc_add(reply, array, sl_array_t);
1184     EC_EXIT;
1185 }
1186
1187 static int sl_rpc_closeQueryForContext(const AFPObj *obj,
1188                                        const DALLOC_CTX *query,
1189                                        DALLOC_CTX *reply,
1190                                        const struct vol *v)
1191 {
1192     EC_INIT;
1193     slq_t *slq = NULL;
1194     uint64_t *uint64, ctx1, ctx2;
1195     sl_array_t *array;
1196     uint64_t sl_result;
1197
1198     array = talloc_zero(reply, sl_array_t);
1199
1200     /* Context */
1201     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
1202     if (uint64 == NULL) {
1203         EC_FAIL;
1204     }
1205     ctx1 = *uint64;
1206     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
1207     if (uint64 == NULL) {
1208         EC_FAIL;
1209     }
1210     ctx2 = *uint64;
1211
1212     /* Get query for context and free it */
1213     slq = slq_for_ctx(ctx1, ctx2);
1214     if (slq == NULL) {
1215         EC_FAIL;
1216     }
1217
1218     switch (slq->slq_state) {
1219     case SLQ_STATE_FULL:
1220     case SLQ_STATE_DONE:
1221     case SLQ_STATE_ERROR:
1222         LOG(log_debug, logtype_sl, "close: destroying query: state %s",
1223             slq_state_names[slq->slq_state].state_name);
1224         slq_destroy(slq);
1225         break;
1226
1227     case SLQ_STATE_RUNNING:
1228     case SLQ_STATE_RESULTS:
1229         LOG(log_debug, logtype_sl, "close: cancel query: state %s",
1230             slq_state_names[slq->slq_state].state_name);
1231         slq_cancel(slq);
1232         break;
1233
1234     default:
1235         LOG(log_error, logtype_sl, "Unexpected state %d", slq->slq_state);
1236         EC_FAIL;
1237     }
1238
1239     sl_result = 0;
1240
1241 EC_CLEANUP:
1242     if (ret != 0) {
1243         sl_result = UINT64_MAX;
1244     }
1245     dalloc_add_copy(array, &sl_result, uint64_t);
1246     dalloc_add(reply, array, sl_array_t);
1247     EC_EXIT;
1248 }
1249
1250 /******************************************************************************
1251  * Spotlight functions
1252  ******************************************************************************/
1253
1254 int spotlight_init(AFPObj *obj)
1255 {
1256     static bool initialized = false;
1257     const char *attributes;
1258     struct sl_ctx *sl_ctx;
1259
1260     if (initialized) {
1261         return 0;
1262     }
1263
1264     LOG(log_info, logtype_sl, "Initializing Spotlight");
1265
1266     sl_ctx = talloc_zero(NULL, struct sl_ctx);
1267     obj->sl_ctx = sl_ctx;
1268
1269     attributes = atalk_iniparser_getstring(obj->iniconfig, INISEC_GLOBAL,
1270                                            "spotlight attributes", NULL);
1271     if (attributes) {
1272         configure_spotlight_attributes(attributes);
1273     }
1274
1275     /*
1276      * Tracker uses glibs event dispatching, so we need a mainloop
1277      */
1278     sl_ctx->mainloop = g_main_loop_new(NULL, false);
1279     sl_ctx->cancellable = g_cancellable_new();
1280
1281     setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=" _PATH_STATEDIR "spotlight.ipc", 1);
1282     setenv("XDG_DATA_HOME", _PATH_STATEDIR, 0);
1283     setenv("XDG_CACHE_HOME", _PATH_STATEDIR, 0);
1284     setenv("TRACKER_USE_LOG_FILES", "1", 0);
1285
1286     tracker_sparql_connection_get_async(sl_ctx->cancellable,
1287                                         tracker_con_cb, sl_ctx);
1288
1289     initialized = true;
1290     return 0;
1291 }
1292
1293 /******************************************************************************
1294  * AFP functions
1295  ******************************************************************************/
1296
1297 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen,
1298                       char *rbuf, size_t *rbuflen)
1299 {
1300     EC_INIT;
1301     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1302     uint16_t vid;
1303     int cmd;
1304     struct vol      *vol;
1305     DALLOC_CTX *query;
1306     DALLOC_CTX *reply;
1307     char *rpccmd;
1308     int len;
1309     bool event;
1310
1311     *rbuflen = 0;
1312
1313     if (!(obj->options.flags & OPTION_SPOTLIGHT)) {
1314         return AFPERR_NOOP;
1315     }
1316
1317     spotlight_init(obj);
1318     slq_dump();
1319
1320     /*
1321      * Process finished glib events
1322      */
1323     event = true;
1324     while (event) {
1325         event = g_main_context_iteration(NULL, false);
1326     }
1327     slq_cancelled_cleanup();
1328
1329     ibuf += 2;
1330     ibuflen -= 2;
1331
1332     vid = SVAL(ibuf, 0);
1333     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
1334
1335     if ((vol = getvolbyvid(vid)) == NULL) {
1336         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
1337         ret = AFPERR_ACCESS;
1338         goto EC_CLEANUP;
1339     }
1340
1341     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
1342
1343     cmd = RIVAL(ibuf, 6);
1344     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
1345
1346     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
1347
1348     switch (cmd) {
1349
1350     case SPOTLIGHT_CMD_OPEN:
1351     case SPOTLIGHT_CMD_OPEN2:
1352         RSIVAL(rbuf, 0, ntohs(vid));
1353         RSIVAL(rbuf, 4, 0);
1354         len = strlen(vol->v_path) + 1;
1355         strncpy(rbuf + 8, vol->v_path, len);
1356         *rbuflen += 8 + len;
1357         break;
1358
1359     case SPOTLIGHT_CMD_FLAGS:
1360         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
1361         *rbuflen += 4;
1362         break;
1363
1364     case SPOTLIGHT_CMD_RPC:
1365         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
1366         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
1367         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
1368
1369         LOG(log_debug, logtype_sl, "Spotlight RPC request:\n%s",
1370             dd_dump(query, 0));
1371
1372         EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
1373
1374         if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
1375             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
1376         } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
1377             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
1378         } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
1379             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
1380         } else if (STRCMP(rpccmd, ==, "storeAttributes:forOIDArray:context:")) {
1381             EC_ZERO_LOG( sl_rpc_storeAttributesForOIDArray(obj, query, reply, vol) );
1382         } else if (STRCMP(rpccmd, ==, "fetchAttributeNamesForOIDArray:context:")) {
1383             EC_ZERO_LOG( sl_rpc_fetchAttributeNamesForOIDArray(obj, query, reply, vol) );
1384         } else if (STRCMP(rpccmd, ==, "fetchAttributes:forOIDArray:context:")) {
1385             EC_ZERO_LOG( sl_rpc_fetchAttributesForOIDArray(obj, query, reply, vol) );
1386         } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
1387             EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
1388         } else {
1389             LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
1390         }
1391
1392         LOG(log_debug, logtype_sl, "Spotlight RPC reply dump:\n%s",
1393             dd_dump(reply, 0));
1394
1395         memset(rbuf, 0, 4);
1396         *rbuflen += 4;
1397
1398         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
1399         *rbuflen += len;
1400         break;
1401     }
1402
1403 EC_CLEANUP:
1404     talloc_free(tmp_ctx);
1405     if (ret != AFP_OK) {
1406         *rbuflen = 0;
1407         return AFPERR_MISC;
1408     }
1409     EC_EXIT;
1410 }