]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
994438d900fc5d5e097eaf70f8cd7d0e099d7402
[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[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64,
606         slq->query_results->num_results, 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         LOG(log_debug, logtype_sl,
688             "cursor cb[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": requesting more results",
689             slq->query_results->num_results - 1, slq->slq_ctx1, slq->slq_ctx2);
690
691         slq->slq_state = SLQ_STATE_RESULTS;
692
693         tracker_sparql_cursor_next_async(slq->tracker_cursor,
694                                          slq->slq_obj->sl_ctx->cancellable,
695                                          tracker_cursor_cb,
696                                          slq);
697     } else {
698         LOG(log_debug, logtype_sl,
699             "cursor cb[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": full",
700             slq->query_results->num_results - 1, slq->slq_ctx1, slq->slq_ctx2);
701
702         slq->slq_state = SLQ_STATE_FULL;
703     }
704 }
705
706 static void tracker_query_cb(GObject      *object,
707                              GAsyncResult *res,
708                              gpointer      user_data)
709 {
710     bool ok;
711     GError *error = NULL;
712     slq_t *slq = user_data;
713
714     LOG(log_debug, logtype_sl,
715         "query cb: ctx1: %" PRIx64 ", ctx2: %" PRIx64,
716         slq->slq_ctx1, slq->slq_ctx2);
717
718     slq->tracker_cursor = tracker_sparql_connection_query_finish(
719         TRACKER_SPARQL_CONNECTION(object),
720         res,
721         &error);
722
723     if (slq->slq_state == SLQ_STATE_CANCEL_PENDING) {
724         slq->slq_state = SLQ_STATE_CANCELLED;
725         return;
726     }
727
728     if (error) {
729         slq->slq_state = SLQ_STATE_ERROR;
730         LOG(log_error, logtype_sl, "Tracker query error: %s", error->message);
731         g_error_free(error);
732         return;
733     }
734
735     slq->slq_state = SLQ_STATE_RESULTS;
736
737     ok = create_result_handle(slq);
738     if (!ok) {
739         LOG(log_error, logtype_sl, "create_result_handle error");
740         slq->slq_state = SLQ_STATE_ERROR;
741         return;
742     }
743
744     tracker_sparql_cursor_next_async(slq->tracker_cursor,
745                                      slq->slq_obj->sl_ctx->cancellable,
746                                      tracker_cursor_cb,
747                                      slq);
748 }
749
750 /*******************************************************************************
751  * Spotlight RPC functions
752  ******************************************************************************/
753
754 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj,
755                                             const DALLOC_CTX *query,
756                                             DALLOC_CTX *reply,
757                                             const struct vol *v)
758 {
759     EC_INIT;
760
761     char *s;
762     sl_dict_t *dict;
763     sl_array_t *array;
764     sl_uuid_t uuid;
765
766     if (!v->v_uuid) {
767         EC_FAIL_LOG("missing UUID for volume: %s", v->v_localname);
768     }
769     dict = talloc_zero(reply, sl_dict_t);
770
771     /* key/val 1 */
772     s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
773     dalloc_add(dict, s, char *);
774
775     array = talloc_zero(dict, sl_array_t);
776     s = dalloc_strdup(array, "kMDQueryScopeComputer");
777     dalloc_add(array, s, char *);
778     dalloc_add(dict, array, sl_array_t);
779
780     /* key/val 2 */
781     s = dalloc_strdup(dict, "kMDSStorePathScopes");
782     dalloc_add(dict, s, char *);
783
784     array = talloc_zero(dict, sl_array_t);
785     s = dalloc_strdup(array, v->v_path);
786     dalloc_add(array, s, char *);
787     dalloc_add(dict, array, sl_array_t);
788
789     /* key/val 3 */
790     s = dalloc_strdup(dict, "kMDSStoreUUID");
791     dalloc_add(dict, s, char *);
792
793     memcpy(uuid.sl_uuid, v->v_uuid, 16);
794     dalloc_add_copy(dict, &uuid, sl_uuid_t);
795
796     /* key/val 4 */
797     s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
798     dalloc_add(dict, s, char *);
799     sl_bool_t b = true;
800     dalloc_add_copy(dict, &b, sl_bool_t);
801
802     dalloc_add(reply, dict, sl_dict_t);
803
804 EC_CLEANUP:
805     EC_EXIT;
806 }
807
808 static int sl_rpc_openQuery(AFPObj *obj,
809                             const DALLOC_CTX *query,
810                             DALLOC_CTX *reply,
811                             struct vol *v)
812 {
813     EC_INIT;
814     char *sl_query;
815     uint64_t *uint64;
816     DALLOC_CTX *reqinfo;
817     sl_array_t *array;
818     sl_cnids_t *cnids;
819     slq_t *slq;
820     char slq_host[MAXPATHLEN + 1];
821     uint16_t convflags = v->v_mtou_flags;
822     uint64_t result;
823     gchar *sparql_query;
824     GError *error = NULL;
825
826     array = talloc_zero(reply, sl_array_t);
827
828     if (obj->sl_ctx->tracker_con == NULL) {
829         LOG(log_error, logtype_sl, "no tracker connection");
830         EC_FAIL;
831     }
832
833     /* Allocate and initialize query object */
834     slq = talloc_zero(obj->sl_ctx, slq_t);
835     slq->slq_state = SLQ_STATE_NEW;
836     slq->slq_obj = obj;
837     slq->slq_vol = v;
838     slq->slq_allow_expr = obj->options.flags & OPTION_SPOTLIGHT_EXPR ? true : false;
839     slq->slq_result_limit = obj->options.sparql_limit;
840     talloc_set_destructor(slq, slq_free_cb);
841
842     LOG(log_debug, logtype_sl, "Spotlight: expr: %s, limit: %" PRIu64,
843         slq->slq_allow_expr ? "yes" : "no", slq->slq_result_limit);
844
845     /* convert spotlight query charset to host charset */
846     sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0,
847                                     "DALLOC_CTX", 1,
848                                     "kMDQueryString");
849     if (sl_query == NULL) {
850         EC_FAIL;
851     }
852     ret = convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset,
853                           sl_query, strlen(sl_query), slq_host, MAXPATHLEN,
854                           &convflags);
855     if (ret == -1) {
856         LOG(log_error, logtype_sl, "charset conversion failed");
857         EC_FAIL;
858     }
859     slq->slq_qstring = talloc_strdup(slq, slq_host);
860     LOG(log_debug, logtype_sl, "Spotlight query: \"%s\"", slq->slq_qstring);
861
862     slq->slq_time = time(NULL);
863     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
864     if (uint64 == NULL) {
865         EC_FAIL;
866     }
867     slq->slq_ctx1 = *uint64;
868
869     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
870     if (uint64 == NULL) {
871         EC_FAIL;
872     }
873     slq->slq_ctx2 = *uint64;
874
875     reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1,
876                                    "kMDAttributeArray");
877     if (reqinfo == NULL) {
878         EC_FAIL;
879     }
880     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
881
882     cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1,
883                                  "kMDQueryItemArray");
884     if (cnids) {
885         EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
886     }
887
888     ret = map_spotlight_to_sparql_query(slq, &sparql_query);
889     if (ret != 0) {
890         LOG(log_debug, logtype_sl, "mapping retured non-zero");
891         EC_FAIL;
892     }
893     LOG(log_debug, logtype_sl, "SPARQL query: \"%s\"", sparql_query);
894
895     tracker_sparql_connection_query_async(obj->sl_ctx->tracker_con,
896                                           sparql_query,
897                                           slq->slq_obj->sl_ctx->cancellable,
898                                           tracker_query_cb,
899                                           slq);
900     if (error) {
901         LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
902             error->message);
903         g_clear_error(&error);
904         EC_FAIL;
905     }
906
907     slq->slq_state = SLQ_STATE_RUNNING;
908     slq_add(slq);
909
910 EC_CLEANUP:
911     if (ret != 0) {
912         slq_destroy(slq);
913         result = UINT64_MAX;
914         ret = 0;
915     } else {
916         result = 0;
917     }
918
919     dalloc_add_copy(array, &result, uint64_t);
920     dalloc_add(reply, array, sl_array_t);
921     EC_EXIT;
922 }
923
924 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj,
925                                               const DALLOC_CTX *query,
926                                               DALLOC_CTX *reply,
927                                               const struct vol *v)
928 {
929     EC_INIT;
930     slq_t *slq = NULL;
931     uint64_t *uint64, ctx1, ctx2, status;
932     sl_array_t *array;
933     bool ok;
934
935     array = talloc_zero(reply, sl_array_t);
936     if (array == NULL) {
937         return false;
938     }
939
940     /* Context */
941     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
942     if (uint64 == NULL) {
943         EC_FAIL;
944     }
945     ctx1 = *uint64;
946     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
947     if (uint64 == NULL) {
948         EC_FAIL;
949     }
950     ctx2 = *uint64;
951
952     /* Get query for context */
953     slq = slq_for_ctx(ctx1, ctx2);
954     if (slq == NULL) {
955         EC_FAIL;
956     }
957
958     switch (slq->slq_state) {
959     case SLQ_STATE_RUNNING:
960     case SLQ_STATE_RESULTS:
961     case SLQ_STATE_FULL:
962     case SLQ_STATE_DONE:
963         ok = add_results(array, slq);
964         if (!ok) {
965             LOG(log_error, logtype_sl, "error adding results");
966             EC_FAIL;
967         }
968         if (slq->slq_state == SLQ_STATE_RESULTS
969             || slq->slq_state == SLQ_STATE_FULL) {
970             slq->slq_state = SLQ_STATE_RESULTS;
971
972             tracker_sparql_cursor_next_async(
973                 slq->tracker_cursor,
974                 slq->slq_obj->sl_ctx->cancellable,
975                 tracker_cursor_cb,
976                 slq);
977         }
978         break;
979
980     case SLQ_STATE_ERROR:
981         LOG(log_error, logtype_sl, "query in error state");
982         EC_FAIL;
983
984     default:
985         LOG(log_error, logtype_sl, "unexpected query state %d", slq->slq_state);
986         EC_FAIL;
987     }
988
989     dalloc_add(reply, array, sl_array_t);
990     EC_EXIT;
991
992 EC_CLEANUP:
993     slq_destroy(slq);
994     status = UINT64_MAX;
995     dalloc_add_copy(array, &status, uint64_t);
996     dalloc_add(reply, array, sl_array_t);
997     EC_EXIT;
998 }
999
1000 static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj,
1001                                              const DALLOC_CTX *query,
1002                                              DALLOC_CTX *reply,
1003                                              const struct vol *vol)
1004 {
1005     EC_INIT;
1006     uint64_t uint64;
1007     sl_array_t *array;
1008     sl_cnids_t *cnids;
1009     sl_time_t *sl_time;
1010     cnid_t id;
1011     char *path;
1012     struct dir *dir;
1013
1014     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
1015     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
1016     id = (cnid_t)uint64;
1017     LOG(log_debug, logtype_sl, "CNID: %" PRIu32, id);
1018
1019     if (htonl(id) == DIRDID_ROOT) {
1020         path = vol->v_path;
1021     } else if (id < CNID_START) {
1022         EC_FAIL;
1023     } else {
1024         cnid_t did;
1025         char buffer[12 + MAXPATHLEN + 1];
1026
1027         did = htonl(id);
1028         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
1029         EC_NULL_LOG( dir = dirlookup(vol, did) );
1030         EC_NEG1_LOG( movecwd(vol, dir) );
1031     }
1032
1033     /*
1034      * We're possibly supposed to update attributes in two places: the
1035      * database and the filesystem.  Due to the lack of documentation
1036      * and not yet implemented database updates, we cherry pick attributes
1037      * that seems to be candidates for updating filesystem metadata.
1038      */
1039
1040     if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemFSContentChangeDate"))) {
1041         struct utimbuf utimes;
1042         utimes.actime = utimes.modtime = sl_time->tv_sec;
1043         utime(path, &utimes);
1044     }
1045
1046     array = talloc_zero(reply, sl_array_t);
1047     uint64_t sl_res = 0;
1048     dalloc_add_copy(array, &sl_res, uint64_t);
1049     dalloc_add(reply, array, sl_array_t);
1050
1051 EC_CLEANUP:
1052     EC_EXIT;
1053 }
1054
1055 static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
1056 {
1057     EC_INIT;
1058     uint64_t uint64;
1059     sl_cnids_t *cnids;
1060     cnid_t id;
1061     char *path;
1062     struct dir *dir;
1063
1064     EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) );
1065     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
1066     id = (cnid_t)uint64;
1067     LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
1068
1069     if (htonl(id) == DIRDID_ROOT) {
1070         path = vol->v_path;
1071     } else if (id < CNID_START) {
1072         EC_FAIL;
1073     } else {
1074         cnid_t did;
1075         char buffer[12 + MAXPATHLEN + 1];
1076
1077         did = htonl(id);
1078         EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
1079         EC_NULL_LOG( dir = dirlookup(vol, did) );
1080         EC_NEG1_LOG( movecwd(vol, dir) );
1081     }
1082
1083     /* Result array */
1084     sl_array_t *array = talloc_zero(reply, sl_array_t);
1085     dalloc_add(reply, array, sl_array_t);
1086
1087     /* Return result value 0 */
1088     uint64_t sl_res = 0;
1089     dalloc_add_copy(array, &sl_res, uint64_t);
1090
1091     /* Return CNID array */
1092     sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
1093     replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
1094     replycnids->ca_unkn1 = 0xfec;
1095     replycnids->ca_context = cnids->ca_context;
1096     uint64 = (uint64_t)id;
1097     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
1098     dalloc_add(array, replycnids, sl_cnids_t);
1099
1100     /* Return filemeta array */
1101
1102     /*
1103      * FIXME: this should return the real attributes from all known metadata sources
1104      * (Tracker and filesystem)
1105      */
1106     sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
1107     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
1108     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
1109     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
1110     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
1111     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
1112     dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
1113
1114     sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
1115     dalloc_add(fmeta, mdattrs, sl_array_t);
1116     dalloc_add(array, fmeta, sl_filemeta_t);
1117
1118 EC_CLEANUP:
1119     EC_EXIT;
1120 }
1121
1122 static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
1123 {
1124     EC_INIT;
1125     uint64_t uint64;
1126     sl_cnids_t *cnids, *replycnids;
1127     cnid_t id, did;
1128     struct dir *dir;
1129     sl_array_t *array, *reqinfo, *fm_array;
1130     char buffer[12 + MAXPATHLEN + 1];
1131     char *name, *path;
1132     sl_filemeta_t *fm;
1133     sl_nil_t nil;
1134     uint64_t sl_res;
1135     struct stat sb;
1136
1137     array = talloc_zero(reply, sl_array_t);
1138     replycnids = talloc_zero(reply, sl_cnids_t);
1139     replycnids->ca_cnids = talloc_zero(replycnids, DALLOC_CTX);
1140     fm = talloc_zero(array, sl_filemeta_t);
1141     fm_array = talloc_zero(fm, sl_array_t);
1142
1143     if (array == NULL || replycnids == NULL || replycnids->ca_cnids == NULL
1144         || fm == NULL || fm_array == NULL) {
1145         EC_FAIL;
1146     }
1147
1148     reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1);
1149     if (reqinfo == NULL) {
1150         EC_FAIL;
1151     }
1152     cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2);
1153     if (cnids == NULL) {
1154         EC_FAIL;
1155     }
1156
1157     memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
1158     id = (cnid_t)uint64;
1159
1160     if (htonl(id) == DIRDID_ROOT) {
1161         path = talloc_strdup(reply, vol->v_path);
1162     } else if (id < CNID_START) {
1163         EC_FAIL;
1164     } else {
1165         did = htonl(id);
1166         EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
1167         EC_NULL( dir = dirlookup(vol, did) );
1168         EC_NULL( path = talloc_asprintf(reply, "%s/%s", bdata(dir->d_fullpath), name) );
1169     }
1170
1171     EC_ZERO( stat(path, &sb) );
1172
1173     sl_res = 0;
1174     dalloc_add_copy(array, &sl_res, uint64_t);
1175
1176     replycnids->ca_unkn1 = 0xfec;
1177     replycnids->ca_context = cnids->ca_context;
1178     uint64 = (uint64_t)id;
1179     dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
1180     dalloc_add(array, replycnids, sl_cnids_t);
1181     dalloc_add(fm, fm_array, fm_array_t);
1182     dalloc_add_copy(fm_array, &nil, sl_nil_t);
1183     add_filemeta(reqinfo, fm_array, path, &sb);
1184
1185     /* Now add result */
1186     dalloc_add(array, fm, sl_filemeta_t);
1187     dalloc_add(reply, array, sl_array_t);
1188     EC_EXIT;
1189
1190 EC_CLEANUP:
1191     sl_res = UINT64_MAX;
1192     dalloc_add_copy(array, &sl_res, uint64_t);
1193     dalloc_add(array, fm, sl_filemeta_t);
1194     dalloc_add(reply, array, sl_array_t);
1195     EC_EXIT;
1196 }
1197
1198 static int sl_rpc_closeQueryForContext(const AFPObj *obj,
1199                                        const DALLOC_CTX *query,
1200                                        DALLOC_CTX *reply,
1201                                        const struct vol *v)
1202 {
1203     EC_INIT;
1204     slq_t *slq = NULL;
1205     uint64_t *uint64, ctx1, ctx2;
1206     sl_array_t *array;
1207     uint64_t sl_result;
1208
1209     array = talloc_zero(reply, sl_array_t);
1210
1211     /* Context */
1212     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
1213     if (uint64 == NULL) {
1214         EC_FAIL;
1215     }
1216     ctx1 = *uint64;
1217     uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
1218     if (uint64 == NULL) {
1219         EC_FAIL;
1220     }
1221     ctx2 = *uint64;
1222
1223     /* Get query for context and free it */
1224     slq = slq_for_ctx(ctx1, ctx2);
1225     if (slq == NULL) {
1226         EC_FAIL;
1227     }
1228
1229     switch (slq->slq_state) {
1230     case SLQ_STATE_FULL:
1231     case SLQ_STATE_DONE:
1232     case SLQ_STATE_ERROR:
1233         LOG(log_debug, logtype_sl, "close: destroying query: state %s",
1234             slq_state_names[slq->slq_state].state_name);
1235         slq_destroy(slq);
1236         break;
1237
1238     case SLQ_STATE_RUNNING:
1239     case SLQ_STATE_RESULTS:
1240         LOG(log_debug, logtype_sl, "close: cancel query: state %s",
1241             slq_state_names[slq->slq_state].state_name);
1242         slq_cancel(slq);
1243         break;
1244
1245     default:
1246         LOG(log_error, logtype_sl, "Unexpected state %d", slq->slq_state);
1247         EC_FAIL;
1248     }
1249
1250     sl_result = 0;
1251
1252 EC_CLEANUP:
1253     if (ret != 0) {
1254         sl_result = UINT64_MAX;
1255     }
1256     dalloc_add_copy(array, &sl_result, uint64_t);
1257     dalloc_add(reply, array, sl_array_t);
1258     EC_EXIT;
1259 }
1260
1261 /******************************************************************************
1262  * Spotlight functions
1263  ******************************************************************************/
1264
1265 int spotlight_init(AFPObj *obj)
1266 {
1267     static bool initialized = false;
1268     const char *attributes;
1269     struct sl_ctx *sl_ctx;
1270
1271     if (initialized) {
1272         return 0;
1273     }
1274
1275     LOG(log_info, logtype_sl, "Initializing Spotlight");
1276
1277     sl_ctx = talloc_zero(NULL, struct sl_ctx);
1278     obj->sl_ctx = sl_ctx;
1279
1280     attributes = atalk_iniparser_getstring(obj->iniconfig, INISEC_GLOBAL,
1281                                            "spotlight attributes", NULL);
1282     if (attributes) {
1283         configure_spotlight_attributes(attributes);
1284     }
1285
1286     /*
1287      * Tracker uses glibs event dispatching, so we need a mainloop
1288      */
1289     sl_ctx->mainloop = g_main_loop_new(NULL, false);
1290     sl_ctx->cancellable = g_cancellable_new();
1291
1292     setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=" _PATH_STATEDIR "spotlight.ipc", 1);
1293     setenv("XDG_DATA_HOME", _PATH_STATEDIR, 0);
1294     setenv("XDG_CACHE_HOME", _PATH_STATEDIR, 0);
1295     setenv("TRACKER_USE_LOG_FILES", "1", 0);
1296
1297     tracker_sparql_connection_get_async(sl_ctx->cancellable,
1298                                         tracker_con_cb, sl_ctx);
1299
1300     initialized = true;
1301     return 0;
1302 }
1303
1304 /******************************************************************************
1305  * AFP functions
1306  ******************************************************************************/
1307
1308 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen,
1309                       char *rbuf, size_t *rbuflen)
1310 {
1311     EC_INIT;
1312     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1313     uint16_t vid;
1314     int cmd;
1315     struct vol      *vol;
1316     DALLOC_CTX *query;
1317     DALLOC_CTX *reply;
1318     char *rpccmd;
1319     int len;
1320     bool event;
1321
1322     *rbuflen = 0;
1323
1324     if (!(obj->options.flags & OPTION_SPOTLIGHT)) {
1325         return AFPERR_NOOP;
1326     }
1327
1328     spotlight_init(obj);
1329     slq_dump();
1330
1331     /*
1332      * Process finished glib events
1333      */
1334     event = true;
1335     while (event) {
1336         event = g_main_context_iteration(NULL, false);
1337     }
1338     slq_cancelled_cleanup();
1339
1340     ibuf += 2;
1341     ibuflen -= 2;
1342
1343     vid = SVAL(ibuf, 0);
1344     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
1345
1346     if ((vol = getvolbyvid(vid)) == NULL) {
1347         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
1348         ret = AFPERR_ACCESS;
1349         goto EC_CLEANUP;
1350     }
1351
1352     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
1353
1354     cmd = RIVAL(ibuf, 6);
1355     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
1356
1357     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
1358
1359     switch (cmd) {
1360
1361     case SPOTLIGHT_CMD_OPEN:
1362     case SPOTLIGHT_CMD_OPEN2:
1363         RSIVAL(rbuf, 0, ntohs(vid));
1364         RSIVAL(rbuf, 4, 0);
1365         len = strlen(vol->v_path) + 1;
1366         strncpy(rbuf + 8, vol->v_path, len);
1367         *rbuflen += 8 + len;
1368         break;
1369
1370     case SPOTLIGHT_CMD_FLAGS:
1371         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
1372         *rbuflen += 4;
1373         break;
1374
1375     case SPOTLIGHT_CMD_RPC:
1376         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
1377         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
1378         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
1379
1380         LOG(log_debug, logtype_sl, "Spotlight RPC request:\n%s",
1381             dd_dump(query, 0));
1382
1383         EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
1384
1385         if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
1386             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
1387         } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
1388             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
1389         } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
1390             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
1391         } else if (STRCMP(rpccmd, ==, "storeAttributes:forOIDArray:context:")) {
1392             EC_ZERO_LOG( sl_rpc_storeAttributesForOIDArray(obj, query, reply, vol) );
1393         } else if (STRCMP(rpccmd, ==, "fetchAttributeNamesForOIDArray:context:")) {
1394             EC_ZERO_LOG( sl_rpc_fetchAttributeNamesForOIDArray(obj, query, reply, vol) );
1395         } else if (STRCMP(rpccmd, ==, "fetchAttributes:forOIDArray:context:")) {
1396             EC_ZERO_LOG( sl_rpc_fetchAttributesForOIDArray(obj, query, reply, vol) );
1397         } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
1398             EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
1399         } else {
1400             LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
1401         }
1402
1403         LOG(log_debug, logtype_sl, "Spotlight RPC reply dump:\n%s",
1404             dd_dump(reply, 0));
1405
1406         memset(rbuf, 0, 4);
1407         *rbuflen += 4;
1408
1409         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
1410         *rbuflen += len;
1411         break;
1412     }
1413
1414 EC_CLEANUP:
1415     talloc_free(tmp_ctx);
1416     if (ret != AFP_OK) {
1417         *rbuflen = 0;
1418         return AFPERR_MISC;
1419     }
1420     EC_EXIT;
1421 }