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;
287 /* convert spotlight query charset to host charset */
288 EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
289 char slq_host[MAXPATHLEN + 1];
290 uint16_t convflags = v->v_mtou_flags;
292 if (convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset, sl_query, strlen(sl_query), slq_host, MAXPATHLEN, &convflags) == -1) {
293 LOG(log_error, logtype_afpd, "sl_rpc_openQuery(\"%s\"): charset conversion failed", sl_query);
296 slq->slq_qstring = talloc_strdup(slq, slq_host);
297 LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", slq->slq_qstring);
299 slq->slq_time = time(NULL);
300 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
301 slq->slq_ctx1 = *uint64;
302 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
303 slq->slq_ctx2 = *uint64;
304 EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
305 slq->slq_reqinfo = talloc_steal(slq, reqinfo);
306 if ((cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryItemArray"))) {
307 EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
310 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
311 dd_dump(slq->slq_reqinfo, 0);
316 EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
318 array = talloc_zero(reply, sl_array_t);
319 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
320 dalloc_add_copy(array, &sl_res, uint64_t);
321 dalloc_add(reply, array, sl_array_t);
330 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
334 uint64_t *uint64, ctx1, ctx2;
337 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
339 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
342 /* Get query for context */
343 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
344 if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) {
345 EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active");
348 /* Create and pass reply handle */
349 EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
351 /* Fetch Tracker results*/
352 EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) );
354 dalloc_add(reply, slq->slq_reply, sl_array_t);
363 static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
374 EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
375 memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
377 LOG(log_debug, logtype_sl, "sl_rpc_storeAttributesForOIDArray: CNID: %" PRIu32, id);
379 if (htonl(id) == DIRDID_ROOT) {
381 } else if (id < CNID_START) {
385 char buffer[12 + MAXPATHLEN + 1];
388 EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
389 EC_NULL_LOG( dir = dirlookup(vol, did) );
390 EC_NEG1_LOG( movecwd(vol, dir) );
393 if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemLastUsedDate"))) {
394 struct utimbuf utimes;
395 utimes.actime = utimes.modtime = sl_time->tv_sec;
396 utime(path, &utimes);
399 array = talloc_zero(reply, sl_array_t);
401 dalloc_add_copy(array, &sl_res, uint64_t);
402 dalloc_add(reply, array, sl_array_t);
408 static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
418 EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) );
419 memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
421 LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
423 if (htonl(id) == DIRDID_ROOT) {
425 } else if (id < CNID_START) {
429 char buffer[12 + MAXPATHLEN + 1];
432 EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
433 EC_NULL_LOG( dir = dirlookup(vol, did) );
434 EC_NEG1_LOG( movecwd(vol, dir) );
438 sl_array_t *array = talloc_zero(reply, sl_array_t);
439 dalloc_add(reply, array, sl_array_t);
441 /* Return result value 0 */
443 dalloc_add_copy(array, &sl_res, uint64_t);
445 /* Return CNID array */
446 sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
447 replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
448 replycnids->ca_unkn1 = 0xfec;
449 replycnids->ca_context = cnids->ca_context;
450 uint64 = (uint64_t)id;
451 dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
452 dalloc_add(array, replycnids, sl_cnids_t);
454 /* Return filemeta array */
457 * FIXME: this should return the real attributes from all known metadata sources
458 * (Tracker and filesystem)
460 sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
461 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
462 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
463 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
464 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
465 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
466 dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
468 sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
469 dalloc_add(fmeta, mdattrs, sl_array_t);
470 dalloc_add(array, fmeta, sl_filemeta_t);
476 static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
489 /* Allocate and initialize query object */
490 slq = talloc_zero(reply, slq_t);
491 slq->slq_state = SLQ_STATE_ATTRS;
494 EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
495 EC_NULL( reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1) );
496 slq->slq_reqinfo = talloc_steal(slq, reqinfo);
498 EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
499 memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
502 if (htonl(id) == DIRDID_ROOT) {
503 slq->slq_path = talloc_strdup(slq, vol->v_path);
504 } else if (id < CNID_START) {
508 char buffer[12 + MAXPATHLEN + 1];
511 EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
512 EC_NULL( dir = dirlookup(vol, did) );
513 EC_NULL( slq->slq_path = talloc_asprintf(slq, "%s/%s", bdata(dir->d_fullpath), name) );
516 /* Return result value 0 */
518 dalloc_add_copy(slq->slq_reply, &sl_res, uint64_t);
520 /* Return CNID array */
521 sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
522 replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
523 replycnids->ca_unkn1 = 0xfec;
524 replycnids->ca_context = cnids->ca_context;
525 uint64 = (uint64_t)id;
526 dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
527 dalloc_add(slq->slq_reply, replycnids, sl_cnids_t);
529 /* Fetch attributes from module */
530 EC_ZERO_LOG( sl_module_export->sl_mod_fetch_attrs(slq) );
532 dalloc_add(reply, slq->slq_reply, sl_array_t);
538 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
542 uint64_t *uint64, ctx1, ctx2;
546 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
548 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
551 /* Get query for context and free it */
552 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
553 if (slq->slq_state != SLQ_STATE_DONE)
554 LOG(log_warning, logtype_sl, "Closing active query");
555 sl_module_export->sl_mod_end_search(slq);
560 array = talloc_zero(reply, sl_array_t);
562 dalloc_add_copy(array, &sl_res, uint64_t);
563 dalloc_add(reply, array, sl_array_t);
572 /**************************************************************************************************
573 * Spotlight module functions
574 **************************************************************************************************/
576 int sl_mod_load(const char *path)
580 sl_ctx = talloc_new(NULL);
582 if ((sl_module = mod_open(path)) == NULL) {
583 LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", path, mod_error());
587 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
588 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
592 sl_module_export->sl_mod_init("test");
601 void sl_index_file(const char *path)
603 if (sl_module_export && sl_module_export->sl_mod_index_file)
604 sl_module_export->sl_mod_index_file(path);
607 /**************************************************************************************************
609 **************************************************************************************************/
611 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
614 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
617 int endianess = SL_ENC_LITTLE_ENDIAN;
626 if (sl_module == NULL)
633 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
635 if ((vol = getvolbyvid(vid)) == NULL) {
636 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
641 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
643 cmd = RIVAL(ibuf, 6);
644 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
646 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
650 case SPOTLIGHT_CMD_OPEN:
651 case SPOTLIGHT_CMD_OPEN2:
652 RSIVAL(rbuf, 0, ntohs(vid));
654 len = strlen(vol->v_path) + 1;
655 strncpy(rbuf + 8, vol->v_path, len);
659 case SPOTLIGHT_CMD_FLAGS:
660 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
664 case SPOTLIGHT_CMD_RPC:
665 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
666 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
667 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
669 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
672 EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
674 if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
675 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
676 } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
677 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
678 } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
679 EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
680 } else if (STRCMP(rpccmd, ==, "storeAttributes:forOIDArray:context:")) {
681 EC_ZERO_LOG( sl_rpc_storeAttributesForOIDArray(obj, query, reply, vol) );
682 } else if (STRCMP(rpccmd, ==, "fetchAttributeNamesForOIDArray:context:")) {
683 EC_ZERO_LOG( sl_rpc_fetchAttributeNamesForOIDArray(obj, query, reply, vol) );
684 } else if (STRCMP(rpccmd, ==, "fetchAttributes:forOIDArray:context:")) {
685 EC_ZERO_LOG( sl_rpc_fetchAttributesForOIDArray(obj, query, reply, vol) );
686 } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
687 EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
689 LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
692 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
698 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
704 talloc_free(tmp_ctx);
713 /**************************************************************************************************
715 **************************************************************************************************/
717 #ifdef SPOT_TEST_MAIN
719 int main(int argc, char **argv)
722 TALLOC_CTX *mem_ctx = talloc_new(NULL);
723 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
726 set_processname("spot");
727 setuplog("default:info,spotlight:debug", "/dev/tty");
729 LOG(log_info, logtype_sl, "Start");
732 dalloc_add_copy(dd, &i, uint64_t);
733 char *str = dalloc_strdup(dd, "hello world");
734 dalloc_add(dd, str, char *);
736 dalloc_add_copy(dd, &b, sl_bool_t);
738 /* add a nested array */
739 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
741 dalloc_add_copy(nested, &i, uint64_t);
742 dalloc_add(dd, nested, DALLOC_CTX);
744 /* test an allocated CNID array */
746 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
747 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
749 dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
750 dalloc_add(dd, cnids, sl_cnids_t);
752 /* Now the Spotlight types */
753 sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
755 dalloc_add_copy(sl_array, &i, uint64_t);
757 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
759 dalloc_add_copy(sl_dict, &i, uint64_t);
760 dalloc_add(sl_array, sl_dict, sl_dict_t);
762 dalloc_add(dd, sl_array, sl_array_t);
766 /* now parse a real spotlight packet */
774 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
776 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
777 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
779 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
781 /* Now dump the whole thing */
788 char buf[MAX_SLQ_DAT];
789 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
791 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
792 lseek(fd, 24, SEEK_SET);
793 write(fd, buf, qlen);
799 talloc_free(mem_ctx);