From: Frank Lahm Date: Thu, 20 Sep 2012 14:52:15 +0000 (+0200) Subject: Return query results, CNIDs is working, FileMeta marshalling is missing X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=commitdiff_plain;h=842593b24200537927aeb6f749782a9de46c313b Return query results, CNIDs is working, FileMeta marshalling is missing --- diff --git a/etc/afpd/spotlight.c b/etc/afpd/spotlight.c index 2742d002..d83b7571 100644 --- a/etc/afpd/spotlight.c +++ b/etc/afpd/spotlight.c @@ -82,7 +82,7 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel) } else if (STRCMP(type, ==, "sl_cnids_t")) { sl_cnids_t cnids; memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t)); - LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32, + LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32, neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context); if (cnids.ca_cnids) dd_dump(cnids.ca_cnids, nestinglevel + 1); @@ -191,28 +191,32 @@ EC_CLEANUP: EC_EXIT; } -static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) +static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v) { EC_INIT; char **sl_query; uint64_t *uint64; - DALLOC_CTX *dalloc_ctx; + DALLOC_CTX *reqinfo; sl_array_t *array; slq_t *slq = talloc_zero(sl_ctx, slq_t); /* Allocate and initialize query object */ + slq->slq_obj = obj; + slq->slq_vol = v; EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") ); LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", *sl_query); slq->slq_qstring = talloc_steal(slq, *sl_query); slq->slq_state = SLQ_STATE_NEW; slq->slq_time = time(NULL); - EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); + EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); slq->slq_ctx1 = *uint64; - EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) ); + EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) ); slq->slq_ctx2 = *uint64; - EC_NULL_LOG (dalloc_ctx = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") ); - slq->slq_reqinfo = talloc_steal(slq, dalloc_ctx); + EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") ); + slq->slq_reqinfo = talloc_steal(slq, reqinfo); + slq->slq_metacount = dalloc_size(slq->slq_reqinfo); + LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:"); dd_dump(slq->slq_reqinfo, 0); @@ -236,6 +240,10 @@ static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CT slq_t *slq; uint64_t *uint64, ctx1, ctx2; sl_array_t *array; + + array = talloc_zero(reply, sl_array_t); + uint64_t sl_res = 0; + dalloc_add(array, &sl_res, uint64_t); /* Context */ EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); @@ -246,13 +254,14 @@ static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CT /* Get query for context */ EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) ); + /* Pass reply handle */ + slq->slq_reply = array; + /* Fetch Tracker results*/ sl_module_export->sl_mod_fetch_result(slq); EC_CLEANUP: - array = talloc_zero(reply, sl_array_t); - uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX; - dalloc_add(array, &sl_res, uint64_t); + dalloc_add(reply, array, sl_array_t); EC_EXIT; @@ -342,6 +351,7 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_ EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) ); EC_NEG1_LOG( sl_unpack(query, ibuf + 22) ); + LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:"); dd_dump(query, 0); char **cmd; @@ -350,16 +360,12 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_ if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) { EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) ); } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) { - uint64_t *ctx1, *ctx2; - EC_NULL_LOG (ctx1 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); - EC_NULL_LOG (ctx2 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) ); - LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *ctx1, *ctx2); - EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) ); } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) { EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) ); } + LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:"); dd_dump(reply, 0); memset(rbuf, 0, 4); diff --git a/etc/afpd/spotlight.h b/etc/afpd/spotlight.h index adcac14d..da7aaf65 100644 --- a/etc/afpd/spotlight.h +++ b/etc/afpd/spotlight.h @@ -24,6 +24,7 @@ #include #include +#include /************************************************************************************************** * Spotlight module stuff @@ -87,11 +88,15 @@ typedef enum { /* Internal query data structure */ typedef struct { slq_state_t slq_state; /* State */ + AFPObj *slq_obj; /* global AFPObj handle */ + const struct vol *slq_vol; /* volume handle */ + DALLOC_CTX *slq_reply; /* reply handle */ time_t slq_time; /* timestamp where we received this query */ uint64_t slq_ctx1; /* client context 1 */ uint64_t slq_ctx2; /* client context 2 */ const char *slq_qstring; /* the Spotlight query string */ DALLOC_CTX *slq_reqinfo; /* array with requested metadata */ + int slq_metacount; /* number of requested metadata attributes in slq_reqinfo */ void *slq_tracker_cursor; /* Tracker query result cursor */ } slq_t; diff --git a/etc/afpd/spotlight_module.c b/etc/afpd/spotlight_module.c index d6ccb074..1d19a322 100644 --- a/etc/afpd/spotlight_module.c +++ b/etc/afpd/spotlight_module.c @@ -28,6 +28,12 @@ static TrackerSparqlConnection *connection; +const char *tracker_to_unix_path(const char *path) +{ + /* just skip 'file://' */ + return path + 7; +} + static int sl_mod_init(void *p) { EC_INIT; @@ -130,21 +136,71 @@ EC_CLEANUP: EC_EXIT; } +static int add_filemeta(DALLOC_CTX *reqinfo, const int metacount, sl_array_t *fm_array, cnid_t id, const char *path) +{ + EC_INIT; + sl_array_t *meta; + sl_nil_t nil = 0; + int i = metacount; + + LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount); + + meta = talloc_zero(fm_array, sl_array_t); + + while (i--) { + dalloc_add(meta, &nil, sl_nil_t); + } + + dalloc_add(fm_array, meta, sl_array_t); + +EC_CLEANUP: + EC_EXIT; +} + static int sl_mod_fetch_result(void *p) { EC_INIT; slq_t *slq = p; GError *error = NULL; + int i = 0; + cnid_t did, id; + const char *path; + sl_cnids_t *cnids; + sl_filemeta_t *fm; + sl_array_t *fm_array; + uint64_t uint64; + + if (!slq->slq_tracker_cursor) { + LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found"); + goto EC_CLEANUP; + } - if (slq->slq_tracker_cursor) { - int i = 0; - while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) { - LOG(log_debug, logtype_sl, "Result [%d]: %s", - i++, tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL)); + /* Prepare CNIDs */ + cnids = talloc_zero(slq->slq_reply, sl_cnids_t); + cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX); + cnids->ca_unkn1 = 0xadd; + cnids->ca_context = slq->slq_ctx2; + + /* Prepare FileMeta */ + fm = talloc_zero(slq->slq_reply, sl_filemeta_t); + fm_array = talloc_zero(fm, sl_array_t); + dalloc_add(fm, fm_array, sl_array_t); + + while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) { + EC_NULL_LOG( path = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL) ); + path = tracker_to_unix_path(path); + LOG(log_debug, logtype_sl, "sl_mod_fetch_result: path(volpath: %s): \"%s\"", slq->slq_vol->v_path, path); + if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) { + LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error"); + continue; } - } else { - LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found"); + LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path); + uint64 = ntohl(id); + dalloc_add(cnids->ca_cnids, &uint64, uint64_t); + add_filemeta(slq->slq_reqinfo, slq->slq_metacount, fm_array, id, path); } + dalloc_add(slq->slq_reply, cnids, sl_cnids_t); + dalloc_add(slq->slq_reply, fm, sl_filemeta_t); EC_CLEANUP: if (slq->slq_tracker_cursor)