2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
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.
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.
19 #endif /* HAVE_CONFIG_H */
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>
42 #include "directory.h"
44 static TALLOC_CTX *sl_ctx;
45 static void *sl_module;
46 static struct sl_module_export *sl_module_export;
48 /* Helper functions and stuff */
49 static const char *neststrings[] = {
59 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
63 LOG(log_debug, logtype_sl, "%s%s(#%d): {",
64 neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
66 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
68 type = talloc_get_name(dd->dd_talloc_array[n]);
70 if (STRCMP(type, ==, "DALLOC_CTX")
71 || STRCMP(type, ==, "sl_array_t")
72 || STRCMP(type, ==, "sl_filemeta_t")
73 || STRCMP(type, ==, "sl_dict_t")) {
74 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
75 } else if (STRCMP(type, ==, "uint64_t")) {
77 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
78 LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
79 } else if (STRCMP(type, ==, "char *")) {
80 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], (char *)dd->dd_talloc_array[n]);
81 } else if (STRCMP(type, ==, "sl_bool_t")) {
83 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
84 LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
85 } else if (STRCMP(type, ==, "sl_nil_t")) {
86 LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]);
87 } else if (STRCMP(type, ==, "sl_time_t")) {
91 memcpy(&t, dd->dd_talloc_array[n], sizeof(sl_time_t));
92 tm = localtime(&t.tv_sec);
93 strftime(datestring, sizeof(datestring), "%Y-%m-%d %H:%M:%S", tm);
94 LOG(log_debug, logtype_sl, "%ssl_time_t: %s.%06d", neststrings[nestinglevel + 1], datestring, t.tv_usec);
95 } else if (STRCMP(type, ==, "sl_cnids_t")) {
97 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
98 LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32,
99 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
101 dd_dump(cnids.ca_cnids, nestinglevel + 2);
103 LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type);
106 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
109 #ifndef SPOT_TEST_MAIN
110 /**************************************************************************************************
112 **************************************************************************************************/
114 static ATALK_LIST_HEAD(sl_queries);
117 * Add a query to the list of active queries
119 static int slq_add(slq_t *slq)
121 list_add(&(slq->slq_list), &sl_queries);
125 static int slq_remove(slq_t *slq)
131 list_for_each(p, &sl_queries) {
132 q = list_entry(p, slq_t, slq_list);
133 if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) {
141 /* The SL query 'slq' was not found in the list, this is not supposed to happen! */
142 LOG(log_warning, logtype_sl, "slq_remove: slq not in active query list");
149 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
155 list_for_each(p, &sl_queries) {
156 q = list_entry(p, slq_t, slq_list);
158 LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
159 "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
160 ctx1, ctx2, q->slq_ctx1, q->slq_ctx2);
162 if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) {
174 /* Error handling for queries */
175 static void slq_error(slq_t *slq)
179 sl_module_export->sl_mod_error(slq);
184 /**************************************************************************************************
185 * Spotlight RPC functions
186 **************************************************************************************************/
188 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
198 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
200 dict = talloc_zero(reply, sl_dict_t);
203 s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
204 dalloc_add(dict, s, char *);
206 array = talloc_zero(dict, sl_array_t);
207 s = dalloc_strdup(array, "kMDQueryScopeComputer");
208 dalloc_add(array, s, char *);
209 dalloc_add(dict, array, sl_array_t);
212 s = dalloc_strdup(dict, "kMDSStorePathScopes");
213 dalloc_add(dict, s, char *);
215 array = talloc_zero(dict, sl_array_t);
216 s = dalloc_strdup(array, v->v_path);
217 dalloc_add(array, s, char *);
218 dalloc_add(dict, array, sl_array_t);
221 s = dalloc_strdup(dict, "kMDSStoreUUID");
222 dalloc_add(dict, s, char *);
224 memcpy(uuid.sl_uuid, v->v_uuid, 16);
225 dalloc_add_copy(dict, &uuid, sl_uuid_t);
228 s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
229 dalloc_add(dict, s, char *);
231 dalloc_add_copy(dict, &b, sl_bool_t);
233 dalloc_add(reply, dict, sl_dict_t);
239 static int cnid_comp_fn(const void *p1, const void *p2)
241 const uint64_t *cnid1 = p1, *cnid2 = p2;
242 if (*cnid1 == *cnid2)
250 static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p)
253 uint64_t *cnids = NULL;
255 EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) );
256 for (int i = 0; i < talloc_array_length(p); i++)
257 memcpy(&cnids[i], p->dd_talloc_array[i], sizeof(uint64_t));
258 qsort(cnids, talloc_array_length(p), sizeof(uint64_t), cnid_comp_fn);
260 slq->slq_cnids = cnids;
261 slq->slq_cnids_num = talloc_array_length(p);
271 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
281 /* Allocate and initialize query object */
282 slq = talloc_zero(sl_ctx, slq_t);
283 slq->slq_state = SLQ_STATE_NEW;
286 slq->slq_allow_expr = obj->options.flags & OPTION_SPOTLIGHT_EXPR ? true : false;
287 slq->slq_result_limit = obj->options.sparql_limit;
289 LOG(log_info, logtype_sl, "sl_rpc_openQuery: expr: %s, limit: %" PRIu64,
290 slq->slq_allow_expr ? "yes" : "no", slq->slq_result_limit);
292 /* convert spotlight query charset to host charset */
293 EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
294 char slq_host[MAXPATHLEN + 1];
295 uint16_t convflags = v->v_mtou_flags;
297 if (convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset, sl_query, strlen(sl_query), slq_host, MAXPATHLEN, &convflags) == -1) {
298 LOG(log_error, logtype_afpd, "sl_rpc_openQuery(\"%s\"): charset conversion failed", sl_query);
301 slq->slq_qstring = talloc_strdup(slq, slq_host);
302 LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", slq->slq_qstring);
304 slq->slq_time = time(NULL);
305 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
306 slq->slq_ctx1 = *uint64;
307 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
308 slq->slq_ctx2 = *uint64;
309 EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
310 slq->slq_reqinfo = talloc_steal(slq, reqinfo);
311 if ((cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryItemArray"))) {
312 EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
315 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
316 dd_dump(slq->slq_reqinfo, 0);
321 EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
323 array = talloc_zero(reply, sl_array_t);
324 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
325 dalloc_add_copy(array, &sl_res, uint64_t);
326 dalloc_add(reply, array, sl_array_t);
335 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
339 uint64_t *uint64, ctx1, ctx2;
342 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
344 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
347 /* Get query for context */
348 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
349 if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) {
350 EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active");
353 /* Create and pass reply handle */
354 EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
356 /* Fetch Tracker results*/
357 EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) );
359 dalloc_add(reply, slq->slq_reply, sl_array_t);
368 static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
379 EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
380 memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
382 LOG(log_debug, logtype_sl, "sl_rpc_storeAttributesForOIDArray: CNID: %" PRIu32, id);
384 if (htonl(id) == DIRDID_ROOT) {
386 } else if (id < CNID_START) {
390 char buffer[12 + MAXPATHLEN + 1];
393 EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
394 EC_NULL_LOG( dir = dirlookup(vol, did) );
395 EC_NEG1_LOG( movecwd(vol, dir) );
399 * We're possibly supposed to update attributes in two places: the
400 * database and the filesystem. Due to the lack of documentation
401 * and not yet implemented database updates, we cherry pick attributes
402 * that seems to be candidates for updating filesystem metadata.
405 if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemFSContentChangeDate"))) {
406 struct utimbuf utimes;
407 utimes.actime = utimes.modtime = sl_time->tv_sec;
408 utime(path, &utimes);
411 array = talloc_zero(reply, sl_array_t);
413 dalloc_add_copy(array, &sl_res, uint64_t);
414 dalloc_add(reply, array, sl_array_t);
420 static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
430 EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) );
431 memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
433 LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
435 if (htonl(id) == DIRDID_ROOT) {
437 } else if (id < CNID_START) {
441 char buffer[12 + MAXPATHLEN + 1];
444 EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
445 EC_NULL_LOG( dir = dirlookup(vol, did) );
446 EC_NEG1_LOG( movecwd(vol, dir) );
450 sl_array_t *array = talloc_zero(reply, sl_array_t);
451 dalloc_add(reply, array, sl_array_t);
453 /* Return result value 0 */
455 dalloc_add_copy(array, &sl_res, uint64_t);
457 /* Return CNID array */
458 sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
459 replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
460 replycnids->ca_unkn1 = 0xfec;
461 replycnids->ca_context = cnids->ca_context;
462 uint64 = (uint64_t)id;
463 dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
464 dalloc_add(array, replycnids, sl_cnids_t);
466 /* Return filemeta array */
469 * FIXME: this should return the real attributes from all known metadata sources
470 * (Tracker and filesystem)
472 sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
473 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
474 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
475 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
476 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
477 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
478 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
480 sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
481 dalloc_add(fmeta, mdattrs, sl_array_t);
482 dalloc_add(array, fmeta, sl_filemeta_t);
488 static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
501 /* Allocate and initialize query object */
502 slq = talloc_zero(reply, slq_t);
503 slq->slq_state = SLQ_STATE_ATTRS;
506 EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
507 EC_NULL( reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1) );
508 slq->slq_reqinfo = talloc_steal(slq, reqinfo);
510 EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
511 memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
514 if (htonl(id) == DIRDID_ROOT) {
515 slq->slq_path = talloc_strdup(slq, vol->v_path);
516 } else if (id < CNID_START) {
520 char buffer[12 + MAXPATHLEN + 1];
523 EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
524 EC_NULL( dir = dirlookup(vol, did) );
525 EC_NULL( slq->slq_path = talloc_asprintf(slq, "%s/%s", bdata(dir->d_fullpath), name) );
528 /* Return result value 0 */
530 dalloc_add_copy(slq->slq_reply, &sl_res, uint64_t);
532 /* Return CNID array */
533 sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
534 replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
535 replycnids->ca_unkn1 = 0xfec;
536 replycnids->ca_context = cnids->ca_context;
537 uint64 = (uint64_t)id;
538 dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
539 dalloc_add(slq->slq_reply, replycnids, sl_cnids_t);
541 /* Fetch attributes from module */
542 EC_ZERO_LOG( sl_module_export->sl_mod_fetch_attrs(slq) );
544 dalloc_add(reply, slq->slq_reply, sl_array_t);
550 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
554 uint64_t *uint64, ctx1, ctx2;
558 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
560 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
563 /* Get query for context and free it */
564 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
565 if (slq->slq_state != SLQ_STATE_DONE)
566 LOG(log_warning, logtype_sl, "Closing active query");
567 sl_module_export->sl_mod_end_search(slq);
572 array = talloc_zero(reply, sl_array_t);
574 dalloc_add_copy(array, &sl_res, uint64_t);
575 dalloc_add(reply, array, sl_array_t);
584 /**************************************************************************************************
585 * Spotlight module functions
586 **************************************************************************************************/
588 int sl_mod_load(AFPObj *obj)
592 sl_ctx = talloc_new(NULL);
594 if ((sl_module = mod_open(obj->options.slmod_path)) == NULL) {
595 LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", obj->options.slmod_path, mod_error());
599 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
600 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol sl_mod", obj->options.slmod_path);
604 sl_module_export->sl_mod_init(obj);
613 void sl_index_file(const char *path)
615 if (sl_module_export && sl_module_export->sl_mod_index_file)
616 sl_module_export->sl_mod_index_file(path);
619 /**************************************************************************************************
621 **************************************************************************************************/
623 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
626 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
629 int endianess = SL_ENC_LITTLE_ENDIAN;
638 if (sl_module == NULL)
645 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
647 if ((vol = getvolbyvid(vid)) == NULL) {
648 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
653 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
655 cmd = RIVAL(ibuf, 6);
656 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
658 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
662 case SPOTLIGHT_CMD_OPEN:
663 case SPOTLIGHT_CMD_OPEN2:
664 RSIVAL(rbuf, 0, ntohs(vid));
666 len = strlen(vol->v_path) + 1;
667 strncpy(rbuf + 8, vol->v_path, len);
671 case SPOTLIGHT_CMD_FLAGS:
672 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
676 case SPOTLIGHT_CMD_RPC:
677 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
678 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
679 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
681 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
684 EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
686 if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
687 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
688 } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
689 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
690 } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
691 EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
692 } else if (STRCMP(rpccmd, ==, "storeAttributes:forOIDArray:context:")) {
693 EC_ZERO_LOG( sl_rpc_storeAttributesForOIDArray(obj, query, reply, vol) );
694 } else if (STRCMP(rpccmd, ==, "fetchAttributeNamesForOIDArray:context:")) {
695 EC_ZERO_LOG( sl_rpc_fetchAttributeNamesForOIDArray(obj, query, reply, vol) );
696 } else if (STRCMP(rpccmd, ==, "fetchAttributes:forOIDArray:context:")) {
697 EC_ZERO_LOG( sl_rpc_fetchAttributesForOIDArray(obj, query, reply, vol) );
698 } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
699 EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
701 LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
704 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
710 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
716 talloc_free(tmp_ctx);
725 /**************************************************************************************************
727 **************************************************************************************************/
729 #ifdef SPOT_TEST_MAIN
731 int main(int argc, char **argv)
734 TALLOC_CTX *mem_ctx = talloc_new(NULL);
735 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
738 set_processname("spot");
739 setuplog("default:info,spotlight:debug", "/dev/tty");
741 LOG(log_info, logtype_sl, "Start");
744 dalloc_add_copy(dd, &i, uint64_t);
745 char *str = dalloc_strdup(dd, "hello world");
746 dalloc_add(dd, str, char *);
748 dalloc_add_copy(dd, &b, sl_bool_t);
750 /* add a nested array */
751 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
753 dalloc_add_copy(nested, &i, uint64_t);
754 dalloc_add(dd, nested, DALLOC_CTX);
756 /* test an allocated CNID array */
758 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
759 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
761 dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
762 dalloc_add(dd, cnids, sl_cnids_t);
764 /* Now the Spotlight types */
765 sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
767 dalloc_add_copy(sl_array, &i, uint64_t);
769 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
771 dalloc_add_copy(sl_dict, &i, uint64_t);
772 dalloc_add(sl_array, sl_dict, sl_dict_t);
774 dalloc_add(dd, sl_array, sl_array_t);
778 /* now parse a real spotlight packet */
786 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
788 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
789 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
791 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
793 /* Now dump the whole thing */
800 char buf[MAX_SLQ_DAT];
801 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
803 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
804 lseek(fd, 24, SEEK_SET);
805 write(fd, buf, qlen);
811 talloc_free(mem_ctx);