/*
- Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
+ Copyright (c) 2012-2014 Ralph Boehme
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
GNU General Public License for more details.
*/
+#define USE_LIST
+
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <errno.h>
#include <stdbool.h>
#include <inttypes.h>
+#include <time.h>
+#include <utime.h>
+#include <atalk/list.h>
#include <atalk/errchk.h>
#include <atalk/util.h>
#include <atalk/logger.h>
#include <atalk/byteorder.h>
#include <atalk/netatalk_conf.h>
#include <atalk/volume.h>
+#include <atalk/spotlight.h>
+
+#include "directory.h"
+#include "etc/spotlight/sparql_parser.h"
+
+#include <glib.h>
+
+#define MAX_SL_RESULTS 20
+
+struct slq_state_names {
+ slq_state_t state;
+ const char *state_name;
+};
-#include "spotlight.h"
-
-/**************************************************************************************************
- * RPC data marshalling and unmarshalling
- **************************************************************************************************/
-
-/* FPSpotlightRPC subcommand codes */
-#define SPOTLIGHT_CMD_VOLPATH 1
-#define SPOTLIGHT_CMD_FLAGS 2
-#define SPOTLIGHT_CMD_RPC 3
-
-/* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
-#define SPOTLIGHT_TIME_DELTA INT64_C(280878921600U)
-
-#define SQ_TYPE_NULL 0x0000
-#define SQ_TYPE_COMPLEX 0x0200
-#define SQ_TYPE_INT64 0x8400
-#define SQ_TYPE_BOOL 0x0100
-#define SQ_TYPE_FLOAT 0x8500
-#define SQ_TYPE_DATA 0x0700
-#define SQ_TYPE_CNIDS 0x8700
-#define SQ_TYPE_UUID 0x0e00
-#define SQ_TYPE_DATE 0x8600
-#define SQ_TYPE_TOC 0x8800
-
-#define SQ_CPX_TYPE_ARRAY 0x0a00
-#define SQ_CPX_TYPE_STRING 0x0c00
-#define SQ_CPX_TYPE_UTF16_STRING 0x1c00
-#define SQ_CPX_TYPE_DICT 0x0d00
-#define SQ_CPX_TYPE_CNIDS 0x1a00
-#define SQ_CPX_TYPE_FILEMETA 0x1b00
-
-#define SUBQ_SAFETY_LIM 20
-
-/* Can be ored and used as flags */
-#define SL_ENC_LITTLE_ENDIAN 1
-#define SL_ENC_BIG_ENDIAN 2
-#define SL_ENC_UTF_16 4
-
-/* Forward declarations */
-static int dissect_spotlight(DALLOC_CTX *query, const char *buf);
-static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx);
-
-/* Helper functions and stuff */
-static const char *neststrings[] = {
- "",
- "\t",
- "\t\t",
- "\t\t\t",
- "\t\t\t\t",
- "\t\t\t\t\t",
- "\t\t\t\t\t\t",
+static struct slq_state_names slq_state_names[] = {
+ {SLQ_STATE_NEW, "SLQ_STATE_NEW"},
+ {SLQ_STATE_RUNNING, "SLQ_STATE_RUNNING"},
+ {SLQ_STATE_RESULTS, "SLQ_STATE_RESULTS"},
+ {SLQ_STATE_FULL, "SLQ_STATE_FULL"},
+ {SLQ_STATE_DONE, "SLQ_STATE_DONE"},
+ {SLQ_STATE_CANCEL_PENDING, "SLQ_STATE_CANCEL_PENDING"},
+ {SLQ_STATE_CANCELLED, "SLQ_STATE_CANCELLED"},
+ {SLQ_STATE_ERROR, "SLQ_STATE_ERROR"}
};
-static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
+
+static char *tracker_to_unix_path(TALLOC_CTX *mem_ctx, const char *uri);
+static int cnid_comp_fn(const void *p1, const void *p2);
+static bool create_result_handle(slq_t *slq);
+static bool add_filemeta(sl_array_t *reqinfo,
+ sl_array_t *fm_array,
+ const char *path,
+ const struct stat *sp);
+
+/************************************************
+ * Misc utility functions
+ ************************************************/
+
+static char *tab_level(TALLOC_CTX *mem_ctx, int level)
+{
+ int i;
+ char *string = talloc_array(mem_ctx, char, level + 1);
+
+ for (i = 0; i < level; i++) {
+ string[i] = '\t';
+ }
+
+ string[i] = '\0';
+ return string;
+}
+
+static char *dd_dump(DALLOC_CTX *dd, int nestinglevel)
{
const char *type;
+ int n;
+ uint64_t i;
+ sl_bool_t bl;
+ sl_time_t t;
+ struct tm *tm;
+ char datestring[256];
+ sl_cnids_t cnids;
+ char *logstring, *nested_logstring;
+ char *tab_string1, *tab_string2;
- LOG(log_debug, logtype_sl, "%s%s(#%d): {",
- neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
+ tab_string1 = tab_level(dd, nestinglevel);
+ tab_string2 = tab_level(dd, nestinglevel + 1);
+ if (tab_string1 == NULL || tab_string2 == NULL) {
+ return NULL;
+ }
- for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
+ logstring = talloc_asprintf(dd,
+ "%s%s(#%lu): {\n",
+ tab_string1,
+ talloc_get_name(dd),
+ talloc_array_length(dd->dd_talloc_array));
+ for (n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
type = talloc_get_name(dd->dd_talloc_array[n]);
-
- if (STRCMP(type, ==, "DALLOC_CTX")
- || STRCMP(type, ==, "sl_array_t")
- || STRCMP(type, ==, "sl_dict_t")) {
- dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
- } else if (STRCMP(type, ==, "uint64_t")) {
- uint64_t i;
+ if (strequal(type, "DALLOC_CTX")
+ || strequal(type, "sl_array_t")
+ || strequal(type, "sl_filemeta_t")
+ || strequal(type, "sl_dict_t")) {
+ nested_logstring = dd_dump(dd->dd_talloc_array[n],
+ nestinglevel + 1);
+ if (!nested_logstring) {
+ return NULL;
+ }
+ logstring = talloc_strdup_append(logstring,
+ nested_logstring);
+ if (!logstring) {
+ return NULL;
+ }
+ } else if (strequal(type, "uint64_t")) {
memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
- LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
- } else if (STRCMP(type, ==, "int64_t")) {
- int64_t i;
- memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
- LOG(log_debug, logtype_sl, "%sint64_t: %" PRId64, neststrings[nestinglevel + 1], i);
- } else if (STRCMP(type, ==, "uint32_t")) {
- uint32_t i;
- memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
- LOG(log_debug, logtype_sl, "%s%s: %" PRIu32, neststrings[nestinglevel + 1], type, i);
- } else if (STRCMP(type, ==, "char *")) {
- char *s;
- memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
- LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
- } else if (STRCMP(type, ==, "sl_bool_t")) {
- sl_bool_t bl;
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%suint64_t: 0x%04" PRIx64 "\n",
+ tab_string2, i);
+ if (!logstring) {
+ return NULL;
+ }
+ } else if (strequal(type, "char *")) {
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%sstring: %s\n",
+ tab_string2,
+ (char *)dd->dd_talloc_array[n]);
+ if (!logstring) {
+ return NULL;
+ }
+ } else if (strequal(type, "smb_ucs2_t *")) {
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%sUTF16-string: %s\n",
+ tab_string2,
+ (char *)dd->dd_talloc_array[n]);
+ if (!logstring) {
+ return NULL;
+ }
+ } else if (strequal(type, "sl_bool_t")) {
memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
- LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
- } else if (STRCMP(type, ==, "sl_cnids_t")) {
- sl_cnids_t cnids;
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%sbool: %s\n",
+ tab_string2,
+ bl ? "true" : "false");
+ if (!logstring) {
+ return NULL;
+ }
+ } else if (strequal(type, "sl_nil_t")) {
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%snil\n",
+ tab_string2);
+ if (!logstring) {
+ return NULL;
+ }
+ } else if (strequal(type, "sl_time_t")) {
+ memcpy(&t, dd->dd_talloc_array[n], sizeof(sl_time_t));
+ tm = localtime(&t.tv_sec);
+ strftime(datestring,
+ sizeof(datestring),
+ "%Y-%m-%d %H:%M:%S", tm);
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%ssl_time_t: %s.%06lu\n",
+ tab_string2,
+ datestring,
+ (unsigned long)t.tv_usec);
+ if (!logstring) {
+ return NULL;
+ }
+ } else if (strequal(type, "sl_cnids_t")) {
memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
- LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
- neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
- if (cnids.ca_cnids)
- dd_dump(cnids.ca_cnids, nestinglevel + 1);
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32 "\n",
+ tab_string2,
+ cnids.ca_unkn1,
+ cnids.ca_context);
+ if (!logstring) {
+ return NULL;
+ }
+ if (cnids.ca_cnids) {
+ nested_logstring = dd_dump(
+ cnids.ca_cnids,
+ nestinglevel + 2);
+ if (!nested_logstring) {
+ return NULL;
+ }
+ logstring = talloc_strdup_append(logstring,
+ nested_logstring);
+ if (!logstring) {
+ return NULL;
+ }
+ }
+ } else {
+ logstring = talloc_asprintf_append(
+ logstring,
+ "%stype: %s\n",
+ tab_string2,
+ type);
+ if (!logstring) {
+ return NULL;
+ }
}
}
- LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
+ logstring = talloc_asprintf_append(logstring,
+ "%s}\n",
+ tab_string1);
+ if (!logstring) {
+ return NULL;
+ }
+ return logstring;
}
-/*
-* Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
-* If there is no byte order mark, -1 is returned.
-*/
-static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
- uint utf16_encoding;
-
- /* check for byte order mark */
- utf16_encoding = SL_ENC_BIG_ENDIAN;
- if (query_length >= 2) {
- uint16_t byte_order_mark;
- if (encoding == SL_ENC_LITTLE_ENDIAN)
- byte_order_mark = SVAL(buf, offset);
- else
- byte_order_mark = RSVAL(buf, offset);
-
- if (byte_order_mark == 0xFFFE) {
- utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
- }
- else if (byte_order_mark == 0xFEFF) {
- utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
- }
+static int cnid_comp_fn(const void *p1, const void *p2)
+{
+ const uint64_t *cnid1 = p1, *cnid2 = p2;
+ if (*cnid1 == *cnid2) {
+ return 0;
}
-
- return utf16_encoding;
+ if (*cnid1 < *cnid2) {
+ return -1;
+ }
+ return 1;
}
-/**************************************************************************************************
- * marshalling functions
- **************************************************************************************************/
+static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p)
+{
+ EC_INIT;
+ uint64_t *cnids = NULL;
-#define SL_OFFSET_DELTA 16
+ EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) );
-static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
-{
- uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
- return tag;
+ for (int i = 0; i < talloc_array_length(p); i++) {
+ memcpy(&cnids[i], p->dd_talloc_array[i], sizeof(uint64_t));
+ }
+ qsort(cnids, talloc_array_length(p), sizeof(uint64_t), cnid_comp_fn);
+
+ slq->slq_cnids = cnids;
+ slq->slq_cnids_num = talloc_array_length(p);
+
+EC_CLEANUP:
+ if (ret != 0) {
+ if (cnids)
+ talloc_free(cnids);
+ }
+ EC_EXIT;
}
-static int sl_pack_float(double d, char *buf, int offset)
+static char *tracker_to_unix_path(TALLOC_CTX *mem_ctx, const char *uri)
{
- union {
- double d;
- uint64_t w;
- } ieee_fp_union;
+ GFile *f;
+ char *path;
+ char *talloc_path = NULL;
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
- SLVAL(buf, offset + 8, ieee_fp_union.w);
+ f = g_file_new_for_uri(uri);
+ if (!f) {
+ return NULL;
+ }
- return offset + 2 * sizeof(uint64_t);
-}
+ path = g_file_get_path(f);
+ g_object_unref(f);
-static int sl_pack_uint64(uint64_t u, char *buf, int offset)
-{
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
- SLVAL(buf, offset + 8, u);
+ if (!path) {
+ return NULL;
+ }
+
+ talloc_path = talloc_strdup(mem_ctx, path);
+ g_free(path);
- return offset + 2 * sizeof(uint64_t);
+ return talloc_path;
}
-static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
+/**
+ * Add requested metadata for a query result element
+ *
+ * This could be rewritten to something more sophisticated like
+ * querying metadata from Tracker.
+ *
+ * If path or sp is NULL, simply add nil values for all attributes.
+ **/
+static bool add_filemeta(sl_array_t *reqinfo,
+ sl_array_t *fm_array,
+ const char *path,
+ const struct stat *sp)
{
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
+ sl_array_t *meta;
+ sl_nil_t nil;
+ int i, metacount;
+ uint64_t uint64var;
+ sl_time_t sl_time;
+ char *p, *name;
+
+ metacount = talloc_array_length(reqinfo->dd_talloc_array);
+ if (metacount == 0 || path == NULL || sp == NULL) {
+ dalloc_add_copy(fm_array, &nil, sl_nil_t);
+ return true;
+ }
- return offset + sizeof(uint64_t);
+ meta = talloc_zero(fm_array, sl_array_t);
+
+ for (i = 0; i < metacount; i++) {
+ if (strequal(reqinfo->dd_talloc_array[i], "kMDItemDisplayName")
+ || strequal(reqinfo->dd_talloc_array[i], "kMDItemFSName")) {
+ if ((p = strrchr(path, '/'))) {
+ name = dalloc_strdup(meta, p + 1);
+ dalloc_add(meta, name, "char *");
+ }
+ } else if (strequal(reqinfo->dd_talloc_array[i],
+ "kMDItemPath")) {
+ name = dalloc_strdup(meta, path);
+ dalloc_add(meta, name, "char *");
+ } else if (strequal(reqinfo->dd_talloc_array[i],
+ "kMDItemFSSize")) {
+ uint64var = sp->st_size;
+ dalloc_add_copy(meta, &uint64var, uint64_t);
+ } else if (strequal(reqinfo->dd_talloc_array[i],
+ "kMDItemFSOwnerUserID")) {
+ uint64var = sp->st_uid;
+ dalloc_add_copy(meta, &uint64var, uint64_t);
+ } else if (strequal(reqinfo->dd_talloc_array[i],
+ "kMDItemFSOwnerGroupID")) {
+ uint64var = sp->st_gid;
+ dalloc_add_copy(meta, &uint64var, uint64_t);
+ } else if (strequal(reqinfo->dd_talloc_array[i],
+ "kMDItemFSContentChangeDate")) {
+ sl_time.tv_sec = sp->st_mtime;
+ dalloc_add_copy(meta, &sl_time, sl_time_t);
+ } else {
+ dalloc_add_copy(meta, &nil, sl_nil_t);
+ }
+ }
+
+ dalloc_add(fm_array, meta, sl_array_t);
+ return true;
}
-static int sl_pack_nil(char *buf, int offset)
+/**
+ * Allocate result handle used in the async Tracker cursor result
+ * handler for storing results
+ **/
+static bool create_result_handle(slq_t *slq)
{
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
+ sl_nil_t nil = 0;
+ struct sl_rslts *query_results;
- return offset + sizeof(uint64_t);
+ if (slq->query_results) {
+ LOG(log_error, logtype_sl,"unexpected existing result handle");
+ return false;
+ }
+
+ query_results = talloc_zero(slq, struct sl_rslts);
+
+ /* CNIDs */
+ query_results->cnids = talloc_zero(query_results, sl_cnids_t);
+ if (query_results->cnids == NULL) {
+ return false;
+ }
+ query_results->cnids->ca_cnids = talloc_zero(query_results->cnids,
+ DALLOC_CTX);
+ if (query_results->cnids->ca_cnids == NULL) {
+ return false;
+ }
+
+ query_results->cnids->ca_unkn1 = 0xadd;
+ query_results->cnids->ca_context = slq->slq_ctx2;
+
+ /* FileMeta */
+ query_results->fm_array = talloc_zero(query_results, sl_array_t);
+ if (query_results->fm_array == NULL) {
+ return false;
+ }
+
+ /* For some reason the list of results always starts with a nil entry */
+ dalloc_add_copy(query_results->fm_array, &nil, sl_nil_t);
+
+ slq->query_results = query_results;
+ return true;
}
-static int sl_pack_date(sl_time_t t, char *buf, int offset)
+static bool add_results(sl_array_t *array, slq_t *slq)
{
- uint64_t data = 0;
+ sl_filemeta_t *fm;
+ uint64_t status;
+
+ /* FileMeta */
+ fm = talloc_zero(array, sl_filemeta_t);
+ if (!fm) {
+ return false;
+ }
+
+ switch (slq->slq_state) {
+ case SLQ_STATE_RUNNING:
+ /*
+ * Wtf, why 35? Taken from an AFP capture.
+ */
+ status = 35;
+ break;
+
+ default:
+ status = 0;
+ break;
+ }
+
+ dalloc_add_copy(array, &status, uint64_t);
+ dalloc_add(array, slq->query_results->cnids, sl_cnids_t);
+ if (slq->query_results->num_results > 0) {
+ dalloc_add(fm, slq->query_results->fm_array, sl_array_t);
+ }
+ dalloc_add(array, fm, sl_filemeta_t);
- data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
+ /* This ensure the results get clean up after been sent to the client */
+ talloc_steal(array, slq->query_results);
+ slq->query_results = NULL;
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
- SLVAL(buf, offset + 8, data);
+ if (!create_result_handle(slq)) {
+ LOG(log_error, logtype_sl, "couldn't add result handle");
+ slq->slq_state = SLQ_STATE_ERROR;
+ return false;
+ }
- return offset + 2 * sizeof(uint64_t);
+ return true;
}
-static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
+/******************************************************************************
+ * Spotlight queries
+ ******************************************************************************/
+
+static ATALK_LIST_HEAD(sl_queries);
+static ATALK_LIST_HEAD(sl_cancelled_queries);
+
+/**
+ * Add a query to the list of active queries
+ **/
+static void slq_add(slq_t *slq)
{
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
- memcpy(buf + offset + 8, uuid, 16);
+ list_add(&(slq->slq_list), &sl_queries);
+}
- return offset + sizeof(uint64_t) + 16;
+/**
+ * Add a query to the list of active queries
+ **/
+static void slq_cancelled_add(slq_t *slq)
+{
+ list_add(&(slq->slq_list), &sl_cancelled_queries);
}
-static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
+/**
+ * Remove a query from the active list
+ **/
+static void slq_remove(slq_t *slq)
{
- int len = 0, off = 0;
- int cnid_count = talloc_array_length(cnids->ca_cnids);
+ struct list_head *p;
+ slq_t *q = NULL;
- SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, cnid_count));
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
- *toc_idx += 1;
- offset += 8;
+ list_for_each(p, &sl_queries) {
+ q = list_entry(p, slq_t, slq_list);
+ if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) {
+ list_del(p);
+ break;
+ }
+ }
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
- offset += 8;
+ return;
+}
- if (cnid_count > 0) {
- SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
- offset += 8;
+static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
+{
+ slq_t *q = NULL;
+ struct list_head *p;
- for (int i = 0; i < cnid_count; i++) {
- SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
- offset += 8;
+ list_for_each(p, &sl_queries) {
+ q = list_entry(p, slq_t, slq_list);
+ if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) {
+ break;
}
+ q = NULL;
}
-
- return offset;
+
+ return q;
}
-static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
+/**
+ * Remove a query from the active queue and free it
+ **/
+static void slq_destroy(slq_t *slq)
{
- int count = talloc_array_length(array->dd_talloc_array);
- int octets = (offset + SL_OFFSET_DELTA) / 8;
+ if (slq == NULL) {
+ return;
+ }
+ slq_remove(slq);
+ talloc_free(slq);
+}
- LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
+/**
+ * Cancel a query
+ **/
+static void slq_cancel(slq_t *slq)
+{
+ slq->slq_state = SLQ_STATE_CANCEL_PENDING;
+ slq_remove(slq);
+ slq_cancelled_add(slq);
+}
- SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
- *toc_idx += 1;
- offset += 8;
+/**
+ * talloc destructor cb
+ **/
+static int slq_free_cb(slq_t *slq)
+{
+ if (slq->tracker_cursor) {
+ g_object_unref(slq->tracker_cursor);
+ }
+ return 0;
+}
- offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
+/**
+ * Free all cancelled queries
+ **/
+static void slq_cancelled_cleanup(void)
+{
+ struct list_head *p;
+ slq_t *q = NULL;
+
+ list_for_each(p, &sl_cancelled_queries) {
+ q = list_entry(p, slq_t, slq_list);
+ if (q->slq_state == SLQ_STATE_CANCELLED) {
+ LOG(log_debug, logtype_sl,
+ "ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": cancelled",
+ q->slq_ctx1, q->slq_ctx2);
+ list_del(p);
+ talloc_free(q);
+ } else {
+ LOG(log_debug, logtype_sl,
+ "ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": %s",
+ q->slq_ctx1, q->slq_ctx2, slq_state_names[q->slq_state].state_name);
+ }
+ }
- return offset;
+ return;
}
-static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
+static void slq_dump(void)
{
- SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_DICT, (offset + SL_OFFSET_DELTA) / 8, talloc_array_length(dict->dd_talloc_array)));
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
- *toc_idx += 1;
- offset += 8;
+ struct list_head *p;
+ slq_t *q = NULL;
+ int i = 0;
+
+ list_for_each(p, &sl_queries) {
+ q = list_entry(p, slq_t, slq_list);
+ LOG(log_debug, logtype_sl,
+ "query[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ", state: %s",
+ i++, q->slq_ctx1, q->slq_ctx2,
+ slq_state_names[q->slq_state].state_name);
+ }
- offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
+ return;
+}
- return offset;
+/************************************************
+ * Tracker async callbacks
+ ************************************************/
+
+static void tracker_con_cb(GObject *object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ struct sl_ctx *sl_ctx = user_data;
+ GError *error = NULL;
+
+ sl_ctx->tracker_con = tracker_sparql_connection_get_finish(res,
+ &error);
+ if (error) {
+ LOG(log_error, logtype_sl, "Could not connect to Tracker: %s",
+ error->message);
+ sl_ctx->tracker_con = NULL;
+ g_error_free(error);
+ return;
+ }
+
+ LOG(log_info, logtype_sl, "connected to Tracker");
}
-static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
+static void tracker_cursor_cb(GObject *object,
+ GAsyncResult *res,
+ gpointer user_data)
{
- int len, octets, used_in_last_octet;
- char *s = *string;
- len = strlen(s);
- octets = (len / 8) + (len & 7 ? 1 : 0);
- used_in_last_octet = 8 - (octets * 8 - len);
+ GError *error = NULL;
+ slq_t *slq = user_data;
+ gboolean more_results;
+ const gchar *uri;
+ char *path;
+ int result;
+ struct stat sb;
+ uint64_t uint64var;
+ bool ok;
+ cnid_t did, id;
+
+ LOG(log_debug, logtype_sl,
+ "cursor cb[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64,
+ slq->query_results->num_results, slq->slq_ctx1, slq->slq_ctx2);
+
+ more_results = tracker_sparql_cursor_next_finish(slq->tracker_cursor,
+ res,
+ &error);
+
+ if (slq->slq_state == SLQ_STATE_CANCEL_PENDING) {
+ LOG(log_debug, logtype_sl,
+ "cursor cb: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": cancelled",
+ slq->slq_ctx1, slq->slq_ctx2);
+ slq->slq_state = SLQ_STATE_CANCELLED;
+ return;
+ }
- LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
- s, len, octets, used_in_last_octet);
+ if (error) {
+ LOG(log_error, logtype_sl, "Tracker cursor: %s", error->message);
+ g_error_free(error);
+ slq->slq_state = SLQ_STATE_ERROR;
+ return;
+ }
- SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
- *toc_idx += 1;
- offset += 8;
+ if (!more_results) {
+ LOG(log_debug, logtype_sl, "tracker_cursor_cb: done");
+ slq->slq_state = SLQ_STATE_DONE;
+ return;
+ }
- SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
- offset += 8;
+ uri = tracker_sparql_cursor_get_string(slq->tracker_cursor, 0, NULL);
+ if (uri == NULL) {
+ /*
+ * Not sure how this could happen if
+ * tracker_sparql_cursor_next_finish() returns true, but I've
+ * seen it.
+ */
+ LOG(log_debug, logtype_sl, "no URI for result");
+ return;
+ }
- memset(buf + offset, 0, octets * 8);
- strncpy(buf + offset, s, len);
- offset += octets * 8;
+ LOG(log_debug, logtype_sl, "URI: %s", uri);
- return offset;
-}
+ path = tracker_to_unix_path(slq->query_results, uri);
+ if (path == NULL) {
+ LOG(log_error, logtype_sl, "error converting Tracker URI: %s", uri);
+ slq->slq_state = SLQ_STATE_ERROR;
+ return;
+ }
-static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
-{
- const char *type;
+ result = access(path, R_OK);
+ if (result != 0) {
+ goto exit;
+ }
- for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
-
- type = talloc_get_name(query->dd_talloc_array[n]);
-
- if (STRCMP(type, ==, "sl_array_t")) {
- offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
- } else if (STRCMP(type, ==, "sl_dict_t")) {
- offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
- } else if (STRCMP(type, ==, "uint64_t")) {
- uint64_t i;
- memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
- offset = sl_pack_uint64(i, buf, offset);
- } else if (STRCMP(type, ==, "char *")) {
- offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
- } else if (STRCMP(type, ==, "sl_bool_t")) {
- sl_bool_t bl;
- memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
- offset = sl_pack_bool(bl, buf, offset);
- } else if (STRCMP(type, ==, "double")) {
- double d;
- memcpy(&d, query->dd_talloc_array[n], sizeof(double));
- offset = sl_pack_float(d, buf, offset);
- } else if (STRCMP(type, ==, "sl_nil_t")) {
- offset = sl_pack_nil(buf, offset);
- } else if (STRCMP(type, ==, "sl_time_t")) {
- sl_time_t t;
- memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
- offset = sl_pack_date(t, buf, offset);
- } else if (STRCMP(type, ==, "sl_uuid_t")) {
- offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
- } else if (STRCMP(type, ==, "sl_cnids_t")) {
- offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
+ id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did);
+ if (id == CNID_INVALID) {
+ LOG(log_error, logtype_sl, "cnid_for_path error: %s", path);
+ goto exit;
+ }
+ uint64var = ntohl(id);
+
+ if (slq->slq_cnids) {
+ ok = bsearch(&uint64var, slq->slq_cnids, slq->slq_cnids_num,
+ sizeof(uint64_t), cnid_comp_fn);
+ if (!ok) {
+ goto exit;
}
}
- return offset;
-}
+ dalloc_add_copy(slq->query_results->cnids->ca_cnids,
+ &uint64var, uint64_t);
+ ok = add_filemeta(slq->slq_reqinfo, slq->query_results->fm_array,
+ path, &sb);
+ if (!ok) {
+ LOG(log_error, logtype_sl, "add_filemeta error");
+ slq->slq_state = SLQ_STATE_ERROR;
+ return;
+ }
+
+ slq->query_results->num_results++;
+
+exit:
+ if (slq->query_results->num_results < MAX_SL_RESULTS) {
+ LOG(log_debug, logtype_sl,
+ "cursor cb[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": requesting more results",
+ slq->query_results->num_results - 1, slq->slq_ctx1, slq->slq_ctx2);
-#define MAX_SLQ_DAT 65000
-#define MAX_SLQ_TOC 2048
+ slq->slq_state = SLQ_STATE_RESULTS;
-static int sl_pack(DALLOC_CTX *query, char *buf)
+ tracker_sparql_cursor_next_async(slq->tracker_cursor,
+ slq->slq_obj->sl_ctx->cancellable,
+ tracker_cursor_cb,
+ slq);
+ } else {
+ LOG(log_debug, logtype_sl,
+ "cursor cb[%d]: ctx1: %" PRIx64 ", ctx2: %" PRIx64 ": full",
+ slq->query_results->num_results - 1, slq->slq_ctx1, slq->slq_ctx2);
+
+ slq->slq_state = SLQ_STATE_FULL;
+ }
+}
+
+static void tracker_query_cb(GObject *object,
+ GAsyncResult *res,
+ gpointer user_data)
{
- EC_INIT;
- char toc_buf[MAX_SLQ_TOC];
- int toc_index = 0;
- int len = 0;
+ GError *error = NULL;
+ slq_t *slq = user_data;
- memcpy(buf, "432130dm", 8);
- EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
- SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
- SIVAL(buf, 12, len / 8 + 1);
+ LOG(log_debug, logtype_sl,
+ "query cb: ctx1: %" PRIx64 ", ctx2: %" PRIx64,
+ slq->slq_ctx1, slq->slq_ctx2);
- SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
- memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
+ slq->tracker_cursor = tracker_sparql_connection_query_finish(
+ TRACKER_SPARQL_CONNECTION(object),
+ res,
+ &error);
- len += 16 + (toc_index + 1 ) * 8;
+ if (slq->slq_state == SLQ_STATE_CANCEL_PENDING) {
+ slq->slq_state = SLQ_STATE_CANCELLED;
+ return;
+ }
-EC_CLEANUP:
- if (ret != 0)
- len = -1;
- return len;
-}
+ if (error) {
+ slq->slq_state = SLQ_STATE_ERROR;
+ LOG(log_error, logtype_sl, "Tracker query error: %s", error->message);
+ g_error_free(error);
+ return;
+ }
-/**************************************************************************************************
- * unmarshalling functions
- **************************************************************************************************/
+ slq->slq_state = SLQ_STATE_RESULTS;
-static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
-{
- if (encoding == SL_ENC_LITTLE_ENDIAN)
- return LVAL(buf, offset);
- else
- return RLVAL(buf, offset);
+ tracker_sparql_cursor_next_async(slq->tracker_cursor,
+ slq->slq_obj->sl_ctx->cancellable,
+ tracker_cursor_cb,
+ slq);
}
-static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
+/*******************************************************************************
+ * Spotlight RPC functions
+ ******************************************************************************/
+
+static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj,
+ const DALLOC_CTX *query,
+ DALLOC_CTX *reply,
+ const struct vol *v)
{
- int count, i;
- uint64_t query_data64;
+ EC_INIT;
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- count = query_data64 >> 32;
- offset += 8;
+ char *s;
+ sl_dict_t *dict;
+ sl_array_t *array;
+ sl_uuid_t uuid;
- i = 0;
- while (i++ < count) {
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- dalloc_add(query, &query_data64, uint64_t);
- offset += 8;
+ if (!v->v_uuid) {
+ EC_FAIL_LOG("missing UUID for volume: %s", v->v_localname);
}
+ dict = talloc_zero(reply, sl_dict_t);
+
+ /* key/val 1 */
+ s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
+ dalloc_add(dict, s, char *);
- return count;
+ array = talloc_zero(dict, sl_array_t);
+ s = dalloc_strdup(array, "kMDQueryScopeComputer");
+ dalloc_add(array, s, char *);
+ dalloc_add(dict, array, sl_array_t);
+
+ /* key/val 2 */
+ s = dalloc_strdup(dict, "kMDSStorePathScopes");
+ dalloc_add(dict, s, char *);
+
+ array = talloc_zero(dict, sl_array_t);
+ s = dalloc_strdup(array, v->v_path);
+ dalloc_add(array, s, char *);
+ dalloc_add(dict, array, sl_array_t);
+
+ /* key/val 3 */
+ s = dalloc_strdup(dict, "kMDSStoreUUID");
+ dalloc_add(dict, s, char *);
+
+ memcpy(uuid.sl_uuid, v->v_uuid, 16);
+ dalloc_add_copy(dict, &uuid, sl_uuid_t);
+
+ /* key/val 4 */
+ s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
+ dalloc_add(dict, s, char *);
+ sl_bool_t b = true;
+ dalloc_add_copy(dict, &b, sl_bool_t);
+
+ dalloc_add(reply, dict, sl_dict_t);
+
+EC_CLEANUP:
+ EC_EXIT;
}
-static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
+static int sl_rpc_openQuery(AFPObj *obj,
+ const DALLOC_CTX *query,
+ DALLOC_CTX *reply,
+ struct vol *v)
{
- int count, i;
- uint64_t query_data64;
- sl_time_t t;
+ EC_INIT;
+ char *sl_query;
+ uint64_t *uint64;
+ DALLOC_CTX *reqinfo;
+ sl_array_t *array;
+ sl_cnids_t *cnids;
+ slq_t *slq;
+ char slq_host[MAXPATHLEN + 1];
+ uint16_t convflags = v->v_mtou_flags;
+ uint64_t result;
+ gchar *sparql_query;
+ GError *error = NULL;
+ bool ok;
+
+ array = talloc_zero(reply, sl_array_t);
+
+ if (obj->sl_ctx->tracker_con == NULL) {
+ LOG(log_error, logtype_sl, "no tracker connection");
+ EC_FAIL;
+ }
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- count = query_data64 >> 32;
- offset += 8;
+ /* Allocate and initialize query object */
+ slq = talloc_zero(obj->sl_ctx, slq_t);
+ slq->slq_state = SLQ_STATE_NEW;
+ slq->slq_obj = obj;
+ slq->slq_vol = v;
+ slq->slq_allow_expr = obj->options.flags & OPTION_SPOTLIGHT_EXPR ? true : false;
+ slq->slq_result_limit = obj->options.sparql_limit;
+ talloc_set_destructor(slq, slq_free_cb);
+
+ LOG(log_debug, logtype_sl, "Spotlight: expr: %s, limit: %" PRIu64,
+ slq->slq_allow_expr ? "yes" : "no", slq->slq_result_limit);
+
+ /* convert spotlight query charset to host charset */
+ sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0,
+ "DALLOC_CTX", 1,
+ "kMDQueryString");
+ if (sl_query == NULL) {
+ EC_FAIL;
+ }
+ ret = convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset,
+ sl_query, strlen(sl_query), slq_host, MAXPATHLEN,
+ &convflags);
+ if (ret == -1) {
+ LOG(log_error, logtype_sl, "charset conversion failed");
+ EC_FAIL;
+ }
+ slq->slq_qstring = talloc_strdup(slq, slq_host);
+ LOG(log_debug, logtype_sl, "Spotlight query: \"%s\"", slq->slq_qstring);
- i = 0;
- while (i++ < count) {
- query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
- t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
- t.tv_usec = 0;
- dalloc_add(query, &t, sl_time_t);
- offset += 8;
+ slq->slq_time = time(NULL);
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
+ if (uint64 == NULL) {
+ EC_FAIL;
}
+ slq->slq_ctx1 = *uint64;
- return count;
-}
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ slq->slq_ctx2 = *uint64;
-static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
-{
- int count, i;
- uint64_t query_data64;
- sl_uuid_t uuid;
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- count = query_data64 >> 32;
- offset += 8;
+ reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1,
+ "kMDAttributeArray");
+ if (reqinfo == NULL) {
+ EC_FAIL;
+ }
+ slq->slq_reqinfo = talloc_steal(slq, reqinfo);
- i = 0;
- while (i++ < count) {
- memcpy(uuid.sl_uuid, buf + offset, 16);
- dalloc_add(query, &uuid, sl_uuid_t);
- offset += 16;
+ cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1,
+ "kMDQueryItemArray");
+ if (cnids) {
+ EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
}
- return count;
-}
+ ret = map_spotlight_to_sparql_query(slq, &sparql_query);
+ if (ret != 0) {
+ LOG(log_debug, logtype_sl, "mapping retured non-zero");
+ EC_FAIL;
+ }
+ LOG(log_debug, logtype_sl, "SPARQL query: \"%s\"", sparql_query);
+
+ tracker_sparql_connection_query_async(obj->sl_ctx->tracker_con,
+ sparql_query,
+ slq->slq_obj->sl_ctx->cancellable,
+ tracker_query_cb,
+ slq);
+ if (error) {
+ LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
+ error->message);
+ g_clear_error(&error);
+ EC_FAIL;
+ }
-static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
-{
- int count, i;
- uint64_t query_data64;
- double fval;
- union {
- double d;
- uint32_t w[2];
- } ieee_fp_union;
-
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- count = query_data64 >> 32;
- offset += 8;
-
- i = 0;
- while (i++ < count) {
- if (encoding == SL_ENC_LITTLE_ENDIAN) {
-#ifdef WORDS_BIGENDIAN
- ieee_fp_union.w[0] = IVAL(buf, offset + 4);
- ieee_fp_union.w[1] = IVAL(buf, offset);
-#else
- ieee_fp_union.w[0] = IVAL(buf, offset);
- ieee_fp_union.w[1] = IVAL(buf, offset + 4);
-#endif
- } else {
-#ifdef WORDS_BIGENDIAN
- ieee_fp_union.w[0] = RIVAL(buf, offset);
- ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
-#else
- ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
- ieee_fp_union.w[1] = RIVAL(buf, offset);
-#endif
- }
- dalloc_add(query, &ieee_fp_union.d, double);
- offset += 8;
+ slq->slq_state = SLQ_STATE_RUNNING;
+
+ ok = create_result_handle(slq);
+ if (!ok) {
+ LOG(log_error, logtype_sl, "create_result_handle error");
+ slq->slq_state = SLQ_STATE_ERROR;
+ EC_FAIL;
}
- return count;
+ slq_add(slq);
+
+EC_CLEANUP:
+ if (ret != 0) {
+ slq_destroy(slq);
+ result = UINT64_MAX;
+ ret = 0;
+ } else {
+ result = 0;
+ }
+
+ dalloc_add_copy(array, &result, uint64_t);
+ dalloc_add(reply, array, sl_array_t);
+ EC_EXIT;
}
-static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
+static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj,
+ const DALLOC_CTX *query,
+ DALLOC_CTX *reply,
+ const struct vol *v)
{
EC_INIT;
- int count;
- uint64_t query_data64;
- sl_cnids_t cnids;
+ slq_t *slq = NULL;
+ uint64_t *uint64, ctx1, ctx2, status;
+ sl_array_t *array;
+ bool ok;
- EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
+ array = talloc_zero(reply, sl_array_t);
+ if (array == NULL) {
+ return false;
+ }
- if (length <= 16)
- /* that's permitted, it's an empty array */
- goto EC_CLEANUP;
-
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- count = query_data64 & 0xffff;
+ /* Context */
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ ctx1 = *uint64;
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ ctx2 = *uint64;
- cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
- cnids.ca_context = query_data64 >> 32;
+ /* Get query for context */
+ slq = slq_for_ctx(ctx1, ctx2);
+ if (slq == NULL) {
+ EC_FAIL;
+ }
+
+ switch (slq->slq_state) {
+ case SLQ_STATE_RUNNING:
+ case SLQ_STATE_RESULTS:
+ case SLQ_STATE_FULL:
+ case SLQ_STATE_DONE:
+ ok = add_results(array, slq);
+ if (!ok) {
+ LOG(log_error, logtype_sl, "error adding results");
+ EC_FAIL;
+ }
+ if (slq->slq_state == SLQ_STATE_FULL) {
+ slq->slq_state = SLQ_STATE_RESULTS;
+
+ tracker_sparql_cursor_next_async(
+ slq->tracker_cursor,
+ slq->slq_obj->sl_ctx->cancellable,
+ tracker_cursor_cb,
+ slq);
+ }
+ break;
- offset += 8;
+ case SLQ_STATE_ERROR:
+ LOG(log_error, logtype_sl, "query in error state");
+ EC_FAIL;
- while (count --) {
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
- offset += 8;
+ default:
+ LOG(log_error, logtype_sl, "unexpected query state %d", slq->slq_state);
+ EC_FAIL;
}
- dalloc_add(query, &cnids, sl_cnids_t);
+ dalloc_add(reply, array, sl_array_t);
+ EC_EXIT;
EC_CLEANUP:
+ slq_destroy(slq);
+ status = UINT64_MAX;
+ dalloc_add_copy(array, &status, uint64_t);
+ dalloc_add(reply, array, sl_array_t);
EC_EXIT;
}
-static const char *spotlight_get_qtype_string(uint64_t query_type)
+static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj,
+ const DALLOC_CTX *query,
+ DALLOC_CTX *reply,
+ const struct vol *vol)
{
- switch (query_type) {
- case SQ_TYPE_NULL:
- return "null";
- case SQ_TYPE_COMPLEX:
- return "complex";
- case SQ_TYPE_INT64:
- return "int64";
- case SQ_TYPE_BOOL:
- return "bool";
- case SQ_TYPE_FLOAT:
- return "float";
- case SQ_TYPE_DATA:
- return "data";
- case SQ_TYPE_CNIDS:
- return "CNIDs";
- default:
- return "unknown";
+ EC_INIT;
+ uint64_t uint64;
+ sl_array_t *array;
+ sl_cnids_t *cnids;
+ sl_time_t *sl_time;
+ cnid_t id;
+ char *path;
+ struct dir *dir;
+
+ EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2) );
+ memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
+ id = (cnid_t)uint64;
+ LOG(log_debug, logtype_sl, "CNID: %" PRIu32, id);
+
+ if (htonl(id) == DIRDID_ROOT) {
+ path = vol->v_path;
+ } else if (id < CNID_START) {
+ EC_FAIL;
+ } else {
+ cnid_t did;
+ char buffer[12 + MAXPATHLEN + 1];
+
+ did = htonl(id);
+ EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
+ EC_NULL_LOG( dir = dirlookup(vol, did) );
+ EC_NEG1_LOG( movecwd(vol, dir) );
}
+
+ /*
+ * We're possibly supposed to update attributes in two places: the
+ * database and the filesystem. Due to the lack of documentation
+ * and not yet implemented database updates, we cherry pick attributes
+ * that seems to be candidates for updating filesystem metadata.
+ */
+
+ if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemFSContentChangeDate"))) {
+ struct utimbuf utimes;
+ utimes.actime = utimes.modtime = sl_time->tv_sec;
+ utime(path, &utimes);
+ }
+
+ array = talloc_zero(reply, sl_array_t);
+ uint64_t sl_res = 0;
+ dalloc_add_copy(array, &sl_res, uint64_t);
+ dalloc_add(reply, array, sl_array_t);
+
+EC_CLEANUP:
+ EC_EXIT;
}
-static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
+static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
{
- switch (cpx_query_type) {
- case SQ_CPX_TYPE_ARRAY:
- return "array";
- case SQ_CPX_TYPE_STRING:
- return "string";
- case SQ_CPX_TYPE_UTF16_STRING:
- return "utf-16 string";
- case SQ_CPX_TYPE_DICT:
- return "dictionary";
- case SQ_CPX_TYPE_CNIDS:
- return "CNIDs";
- case SQ_CPX_TYPE_FILEMETA:
- return "FileMeta";
- default:
- return "unknown";
+ EC_INIT;
+ uint64_t uint64;
+ sl_cnids_t *cnids;
+ cnid_t id;
+ char *path;
+ struct dir *dir;
+
+ EC_NULL_LOG( cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1) );
+ memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
+ id = (cnid_t)uint64;
+ LOG(log_debug, logtype_sl, "sl_rpc_fetchAttributeNamesForOIDArray: CNID: %" PRIu32, id);
+
+ if (htonl(id) == DIRDID_ROOT) {
+ path = vol->v_path;
+ } else if (id < CNID_START) {
+ EC_FAIL;
+ } else {
+ cnid_t did;
+ char buffer[12 + MAXPATHLEN + 1];
+
+ did = htonl(id);
+ EC_NULL_LOG( path = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
+ EC_NULL_LOG( dir = dirlookup(vol, did) );
+ EC_NEG1_LOG( movecwd(vol, dir) );
}
+
+ /* Result array */
+ sl_array_t *array = talloc_zero(reply, sl_array_t);
+ dalloc_add(reply, array, sl_array_t);
+
+ /* Return result value 0 */
+ uint64_t sl_res = 0;
+ dalloc_add_copy(array, &sl_res, uint64_t);
+
+ /* Return CNID array */
+ sl_cnids_t *replycnids = talloc_zero(reply, sl_cnids_t);
+ replycnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
+ replycnids->ca_unkn1 = 0xfec;
+ replycnids->ca_context = cnids->ca_context;
+ uint64 = (uint64_t)id;
+ dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
+ dalloc_add(array, replycnids, sl_cnids_t);
+
+ /* Return filemeta array */
+
+ /*
+ * FIXME: this should return the real attributes from all known metadata sources
+ * (Tracker and filesystem)
+ */
+ sl_array_t *mdattrs = talloc_zero(reply, sl_array_t);
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSName"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemDisplayName"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSSize"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerUserID"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSOwnerGroupID"), "char *");
+ dalloc_add(mdattrs, dalloc_strdup(mdattrs, "kMDItemFSContentChangeDate"), "char *");
+
+ sl_filemeta_t *fmeta = talloc_zero(reply, sl_filemeta_t);
+ dalloc_add(fmeta, mdattrs, sl_array_t);
+ dalloc_add(array, fmeta, sl_filemeta_t);
+
+EC_CLEANUP:
+ EC_EXIT;
}
-static int spotlight_dissect_loop(DALLOC_CTX *query,
- const char *buf,
- uint offset,
- uint count,
- const uint toc_offset,
- const uint encoding)
+static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol)
{
EC_INIT;
- int i, toc_index, query_length;
- uint subcount, cpx_query_type, cpx_query_count;
- uint64_t query_data64, query_type;
- uint unicode_encoding;
- uint8_t mark_exists;
- char *p;
- int padding, slen;
-
- while (count > 0 && (offset < toc_offset)) {
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- query_length = (query_data64 & 0xffff) * 8;
- query_type = (query_data64 & 0xffff0000) >> 16;
- if (query_length == 0)
- EC_FAIL;
+ uint64_t uint64;
+ sl_cnids_t *cnids, *replycnids;
+ cnid_t id, did;
+ struct dir *dir;
+ sl_array_t *array, *reqinfo, *fm_array;
+ char buffer[12 + MAXPATHLEN + 1];
+ char *name, *path;
+ sl_filemeta_t *fm;
+ sl_nil_t nil;
+ uint64_t sl_res;
+ struct stat sb;
+
+ array = talloc_zero(reply, sl_array_t);
+ replycnids = talloc_zero(reply, sl_cnids_t);
+ replycnids->ca_cnids = talloc_zero(replycnids, DALLOC_CTX);
+ fm = talloc_zero(array, sl_filemeta_t);
+ fm_array = talloc_zero(fm, sl_array_t);
+
+ if (array == NULL || replycnids == NULL || replycnids->ca_cnids == NULL
+ || fm == NULL || fm_array == NULL) {
+ EC_FAIL;
+ }
- switch (query_type) {
- case SQ_TYPE_COMPLEX:
- toc_index = (query_data64 >> 32) - 1;
- query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
- cpx_query_type = (query_data64 & 0xffff0000) >> 16;
- cpx_query_count = query_data64 >> 32;
-
- switch (cpx_query_type) {
- case SQ_CPX_TYPE_ARRAY: {
- sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
- EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
- dalloc_add(query, sl_arrary, sl_array_t);
- break;
- }
+ reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1);
+ if (reqinfo == NULL) {
+ EC_FAIL;
+ }
+ cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2);
+ if (cnids == NULL) {
+ EC_FAIL;
+ }
- case SQ_CPX_TYPE_DICT: {
- sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
- EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
- dalloc_add(query, sl_dict, sl_dict_t);
- break;
- }
- case SQ_CPX_TYPE_STRING:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
- if ((padding = 8 - (query_data64 >> 32)) < 0)
- EC_FAIL;
- if ((slen = query_length - 16 - padding) < 1)
- EC_FAIL;
- p = talloc_strndup(query, buf + offset + 16, slen);
- dalloc_add(query, &p, char *);
- break;
-
- case SQ_CPX_TYPE_UTF16_STRING:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
- if ((padding = 8 - (query_data64 >> 32)) < 0)
- EC_FAIL;
- if ((slen = query_length - 16 - padding) < 1)
- EC_FAIL;
-
- unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
- mark_exists = (unicode_encoding & SL_ENC_UTF_16);
- unicode_encoding &= ~SL_ENC_UTF_16;
-
- EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
- dalloc_add(query, &p, char *);
- break;
-
- case SQ_CPX_TYPE_FILEMETA:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
-
- if (query_length <= 8) {
- EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
- } else {
- EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
- }
- break;
+ memcpy(&uint64, cnids->ca_cnids->dd_talloc_array[0], sizeof(uint64_t));
+ id = (cnid_t)uint64;
- case SQ_CPX_TYPE_CNIDS:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
- EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
- break;
- } /* switch (cpx_query_type) */
+ if (htonl(id) == DIRDID_ROOT) {
+ path = talloc_strdup(reply, vol->v_path);
+ } else if (id < CNID_START) {
+ EC_FAIL;
+ } else {
+ did = htonl(id);
+ EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) );
+ EC_NULL( dir = dirlookup(vol, did) );
+ EC_NULL( path = talloc_asprintf(reply, "%s/%s", bdata(dir->d_fullpath), name) );
+ }
- count--;
- break;
+ EC_ZERO( stat(path, &sb) );
- case SQ_TYPE_NULL: {
- subcount = query_data64 >> 32;
- if (subcount > 64)
- EC_FAIL;
- sl_nil_t nil = 0;
- for (i = 0; i < subcount; i++)
- dalloc_add(query, &nil, sl_nil_t);
- count -= subcount;
- break;
- }
- case SQ_TYPE_BOOL: {
- sl_bool_t b = query_data64 >> 32;
- dalloc_add(query, &b, sl_bool_t);
- count--;
- break;
- }
- case SQ_TYPE_INT64:
- EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
- count -= subcount;
- break;
- case SQ_TYPE_UUID:
- EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
- count -= subcount;
- break;
- case SQ_TYPE_FLOAT:
- EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
- count -= subcount;
- break;
- case SQ_TYPE_DATE:
- EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
- count -= subcount;
- break;
- default:
- EC_FAIL;
- }
+ sl_res = 0;
+ dalloc_add_copy(array, &sl_res, uint64_t);
- offset += query_length;
- }
+ replycnids->ca_unkn1 = 0xfec;
+ replycnids->ca_context = cnids->ca_context;
+ uint64 = (uint64_t)id;
+ dalloc_add_copy(replycnids->ca_cnids, &uint64, uint64_t);
+ dalloc_add(array, replycnids, sl_cnids_t);
+ dalloc_add(fm, fm_array, fm_array_t);
+ dalloc_add_copy(fm_array, &nil, sl_nil_t);
+ add_filemeta(reqinfo, fm_array, path, &sb);
+
+ /* Now add result */
+ dalloc_add(array, fm, sl_filemeta_t);
+ dalloc_add(reply, array, sl_array_t);
+ EC_EXIT;
EC_CLEANUP:
- if (ret != 0) {
- offset = -1;
- }
- return offset;
+ sl_res = UINT64_MAX;
+ dalloc_add_copy(array, &sl_res, uint64_t);
+ dalloc_add(array, fm, sl_filemeta_t);
+ dalloc_add(reply, array, sl_array_t);
+ EC_EXIT;
}
-static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
+static int sl_rpc_closeQueryForContext(const AFPObj *obj,
+ const DALLOC_CTX *query,
+ DALLOC_CTX *reply,
+ const struct vol *v)
{
EC_INIT;
- int encoding, i, toc_entries;
- uint64_t toc_offset, tquerylen, toc_entry;
+ slq_t *slq = NULL;
+ uint64_t *uint64, ctx1, ctx2;
+ sl_array_t *array;
+ uint64_t sl_result;
- if (strncmp(buf, "md031234", 8) == 0)
- encoding = SL_ENC_BIG_ENDIAN;
- else
- encoding = SL_ENC_LITTLE_ENDIAN;
+ array = talloc_zero(reply, sl_array_t);
- buf += 8;
+ /* Context */
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ ctx1 = *uint64;
+ uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2);
+ if (uint64 == NULL) {
+ EC_FAIL;
+ }
+ ctx2 = *uint64;
- toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
- if (toc_offset < 0 || (toc_offset > 65000)) {
+ /* Get query for context and free it */
+ slq = slq_for_ctx(ctx1, ctx2);
+ if (slq == NULL) {
EC_FAIL;
}
- buf += 8;
+ switch (slq->slq_state) {
+ case SLQ_STATE_FULL:
+ case SLQ_STATE_DONE:
+ case SLQ_STATE_ERROR:
+ LOG(log_debug, logtype_sl, "close: destroying query: state %s",
+ slq_state_names[slq->slq_state].state_name);
+ slq_destroy(slq);
+ break;
+
+ case SLQ_STATE_RUNNING:
+ case SLQ_STATE_RESULTS:
+ LOG(log_debug, logtype_sl, "close: cancel query: state %s",
+ slq_state_names[slq->slq_state].state_name);
+ slq_cancel(slq);
+ break;
- toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
+ default:
+ LOG(log_error, logtype_sl, "Unexpected state %d", slq->slq_state);
+ EC_FAIL;
+ }
- EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
+ sl_result = 0;
EC_CLEANUP:
+ if (ret != 0) {
+ sl_result = UINT64_MAX;
+ }
+ dalloc_add_copy(array, &sl_result, uint64_t);
+ dalloc_add(reply, array, sl_array_t);
EC_EXIT;
}
-/**************************************************************************************************
- * Spotlight RPC functions
- **************************************************************************************************/
+/******************************************************************************
+ * Spotlight functions
+ ******************************************************************************/
-static int sl_rpc_fetchPropertiesForContext(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
+int spotlight_init(AFPObj *obj)
{
- EC_INIT;
+ static bool initialized = false;
+ const char *attributes;
+ struct sl_ctx *sl_ctx;
- char *s;
- sl_dict_t *dict;
- sl_array_t *array;
- sl_uuid_t uuid;
-
- if (!v->v_uuid)
- EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
-
- dict = talloc_zero(reply, sl_dict_t);
-
- /* key/val 1 */
- s = talloc_strdup(dict, "kMDSStoreMetaScopes");
- dalloc_add(dict, &s, char *);
-
- array = talloc_zero(dict, sl_array_t);
- s = talloc_strdup(array, "kMDQueryScopeComputer");
- dalloc_add(array, &s, char *);
- dalloc_add(dict, array, sl_array_t);
+ if (initialized) {
+ return 0;
+ }
- /* key/val 2 */
- s = talloc_strdup(dict, "kMDSStorePathScopes");
- dalloc_add(dict, &s, char *);
+ LOG(log_info, logtype_sl, "Initializing Spotlight");
- array = talloc_zero(dict, sl_array_t);
- s = talloc_strdup(array, v->v_path);
- dalloc_add(array, &s, char *);
- dalloc_add(dict, array, sl_array_t);
+ sl_ctx = talloc_zero(NULL, struct sl_ctx);
+ obj->sl_ctx = sl_ctx;
- /* key/val 3 */
- s = talloc_strdup(dict, "kMDSStoreUUID");
- dalloc_add(dict, &s, char *);
+ attributes = atalk_iniparser_getstring(obj->iniconfig, INISEC_GLOBAL,
+ "spotlight attributes", NULL);
+ if (attributes) {
+ configure_spotlight_attributes(attributes);
+ }
- memcpy(uuid.sl_uuid, v->v_uuid, 16);
- dalloc_add(dict, &uuid, sl_uuid_t);
+ /*
+ * Tracker uses glibs event dispatching, so we need a mainloop
+ */
+#if ((GLIB_MAJOR_VERSION <= 2) && (GLIB_MINOR_VERSION < 36))
+ g_type_init();
+#endif
+ sl_ctx->mainloop = g_main_loop_new(NULL, false);
+ sl_ctx->cancellable = g_cancellable_new();
- /* key/val 4 */
- s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
- dalloc_add(dict, &s, char *);
- sl_bool_t b = true;
- dalloc_add(dict, &b, sl_bool_t);
+ setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=" _PATH_STATEDIR "spotlight.ipc", 1);
+ setenv("XDG_DATA_HOME", _PATH_STATEDIR, 0);
+ setenv("XDG_CACHE_HOME", _PATH_STATEDIR, 0);
+ setenv("TRACKER_USE_LOG_FILES", "1", 0);
- dalloc_add(reply, dict, sl_dict_t);
+ tracker_sparql_connection_get_async(sl_ctx->cancellable,
+ tracker_con_cb, sl_ctx);
-EC_CLEANUP:
- EC_EXIT;
+ initialized = true;
+ return 0;
}
-/**************************************************************************************************
+/******************************************************************************
* AFP functions
- **************************************************************************************************/
-int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
+ ******************************************************************************/
+
+int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen,
+ char *rbuf, size_t *rbuflen)
{
EC_INIT;
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
uint16_t vid;
int cmd;
- int endianess = SL_ENC_LITTLE_ENDIAN;
struct vol *vol;
DALLOC_CTX *query;
+ DALLOC_CTX *reply;
+ char *rpccmd;
+ int len;
+ bool event;
*rbuflen = 0;
+ if (!(obj->options.flags & OPTION_SPOTLIGHT)) {
+ return AFPERR_NOOP;
+ }
+
+ spotlight_init(obj);
+ slq_dump();
+
+ /*
+ * Process finished glib events
+ */
+ event = true;
+ while (event) {
+ event = g_main_context_iteration(NULL, false);
+ }
+ slq_cancelled_cleanup();
+
ibuf += 2;
ibuflen -= 2;
switch (cmd) {
- case SPOTLIGHT_CMD_VOLPATH: {
+ case SPOTLIGHT_CMD_OPEN:
+ case SPOTLIGHT_CMD_OPEN2:
RSIVAL(rbuf, 0, ntohs(vid));
RSIVAL(rbuf, 4, 0);
- int len = strlen(vol->v_path) + 1;
+ len = strlen(vol->v_path) + 1;
strncpy(rbuf + 8, vol->v_path, len);
*rbuflen += 8 + len;
break;
- }
+
case SPOTLIGHT_CMD_FLAGS:
- RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
+ RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
*rbuflen += 4;
break;
- case SPOTLIGHT_CMD_RPC: {
- DALLOC_CTX *query;
+ case SPOTLIGHT_CMD_RPC:
EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
- DALLOC_CTX *reply;
EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
+ EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
- EC_ZERO( dissect_spotlight(query, ibuf + 22) );
- dd_dump(query, 0);
+ LOG(log_debug, logtype_sl, "Spotlight RPC request:\n%s",
+ dd_dump(query, 0));
- char **cmd;
- EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
+ EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
-
- if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
+ if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
- } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
- uint64_t *p;
- if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
- LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
- }
+ } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
+ EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
+ } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
+ EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
+ } else if (STRCMP(rpccmd, ==, "storeAttributes:forOIDArray:context:")) {
+ EC_ZERO_LOG( sl_rpc_storeAttributesForOIDArray(obj, query, reply, vol) );
+ } else if (STRCMP(rpccmd, ==, "fetchAttributeNamesForOIDArray:context:")) {
+ EC_ZERO_LOG( sl_rpc_fetchAttributeNamesForOIDArray(obj, query, reply, vol) );
+ } else if (STRCMP(rpccmd, ==, "fetchAttributes:forOIDArray:context:")) {
+ EC_ZERO_LOG( sl_rpc_fetchAttributesForOIDArray(obj, query, reply, vol) );
+ } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
+ EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
+ } else {
+ LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
}
- /* Spotlight RPC status code ? 0 in all traces, we use 0xffffffff for an error, never seen from Apple */
- if (ret == 0)
- memset(rbuf, 0, 4);
- else
- memset(rbuf, 0xff, 4);
+ LOG(log_debug, logtype_sl, "Spotlight RPC reply dump:\n%s",
+ dd_dump(reply, 0));
+
+ memset(rbuf, 0, 4);
*rbuflen += 4;
- int len;
EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
*rbuflen += len;
break;
}
- }
EC_CLEANUP:
talloc_free(tmp_ctx);
}
EC_EXIT;
}
-
-/**************************************************************************************************
- * Testing
- **************************************************************************************************/
-
-#ifdef SPOT_TEST_MAIN
-
-int main(int argc, char **argv)
-{
- EC_INIT;
- TALLOC_CTX *mem_ctx = talloc_new(NULL);
- DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
- int64_t i;
-
- set_processname("spot");
- setuplog("default:info,spotlight:debug", "/dev/tty");
-
- LOG(log_info, logtype_sl, "Start");
-
-#if 0
- i = 2;
- dalloc_add(dd, &i, int64_t);
-
- i = 1;
- dalloc_add(dd, &i, int64_t);
-
-
- char *str = talloc_strdup(dd, "hello world");
- dalloc_add(dd, &str, char *);
-
- sl_bool_t b = true;
- dalloc_add(dd, &b, sl_bool_t);
-
- b = false;
- dalloc_add(dd, &b, sl_bool_t);
-
-
- /* add a nested array */
- DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
- i = 3;
- dalloc_add(nested, &i, int64_t);
- dalloc_add(dd, nested, DALLOC_CTX);
-
- /* test an allocated CNID array */
- uint32_t id = 16;
- sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
-
- cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
-
- cnids->ca_unkn1 = 1;
- cnids->ca_unkn2 = 2;
-
- dalloc_add(cnids->ca_cnids, &id, uint32_t);
- dalloc_add(dd, cnids, sl_cnids_t);
-
-#endif
-
- /* Now the Spotlight types */
- sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
- i = 1234;
- dalloc_add(sl_arrary, &i, int64_t);
-
- sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
- i = 5678;
- dalloc_add(sl_dict, &i, int64_t);
- dalloc_add(sl_arrary, sl_dict, sl_dict_t);
-
- dalloc_add(dd, sl_arrary, sl_array_t);
- dd_dump(dd, 0);
-
-
-#if 0
- /* now parse a real spotlight packet */
- char ibuf[8192];
- char rbuf[8192];
- int fd;
- size_t len;
- DALLOC_CTX *query;
-
- EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
-
- EC_NEG1_LOG( fd = open("spotlight-packet.bin", O_RDONLY) );
- EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
- close(fd);
- EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
-
- /* Now dump the whole thing */
- dd_dump(query, 0);
-
- int qlen;
- char buf[MAX_SLQ_DAT];
- EC_NEG1_LOG( qlen = sl_pack(query, buf) );
-
- EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
- lseek(fd, 24, SEEK_SET);
- write(fd, buf, qlen);
- close(fd);
-#endif
-
-EC_CLEANUP:
- if (mem_ctx) {
- talloc_free(mem_ctx);
- mem_ctx = NULL;
- }
- EC_EXIT;
-}
-#endif