From: Frank Lahm Date: Wed, 24 Oct 2012 07:38:31 +0000 (+0200) Subject: Fix searching from OS X cmdline X-Git-Url: https://arthur.barton.de/gitweb/?p=netatalk.git;a=commitdiff_plain;h=4350426e2528a5765642f388e06e9ac6ff75fafd Fix searching from OS X cmdline Searching from OS X cmdline with `mdfind` will generate queries with an empty query attribute array. In that case we must add a nil element for every query match to the filemeta array. Also fixed: * marshalling of empty CNID arrays and filemeta * return 0x23 instead of 0 when returning a non empty result list, this is was traces of an OS X AFP server show --- diff --git a/etc/afpd/spotlight.c b/etc/afpd/spotlight.c index cede06d2..dc1682bb 100644 --- a/etc/afpd/spotlight.c +++ b/etc/afpd/spotlight.c @@ -276,7 +276,6 @@ static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CT EC_INIT; slq_t *slq = NULL; uint64_t *uint64, ctx1, ctx2; - sl_array_t *array; /* Context */ EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); @@ -291,17 +290,12 @@ static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CT } /* Create and pass reply handle */ - array = talloc_zero(reply, sl_array_t); - uint64_t sl_res = 0; - dalloc_add_copy(array, &sl_res, uint64_t); - slq->slq_reply = array; + EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) ); - if (slq->slq_state == SLQ_STATE_RUNNING) { - /* Fetch Tracker results*/ - EC_ZERO( sl_module_export->sl_mod_fetch_result(slq) ); - } + /* Fetch Tracker results*/ + EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) ); - dalloc_add(reply, array, sl_array_t); + dalloc_add(reply, slq->slq_reply, sl_array_t); EC_CLEANUP: if (ret != 0) { @@ -325,6 +319,8 @@ static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *quer /* Get query for context and free it */ EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) ); + if (slq->slq_state != SLQ_STATE_DONE) + LOG(log_warning, logtype_sl, "Closing active query"); sl_module_export->sl_mod_end_search(slq); slq_remove(slq); talloc_free(slq); diff --git a/etc/afpd/spotlight_marshalling.c b/etc/afpd/spotlight_marshalling.c index e3bfc268..a30145e9 100644 --- a/etc/afpd/spotlight_marshalling.c +++ b/etc/afpd/spotlight_marshalling.c @@ -222,7 +222,7 @@ EC_CLEANUP: static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx) { EC_INIT; - int len = 0, off = 0; + int off = 0, len; int cnid_count = talloc_array_length(cnids->ca_cnids->dd_talloc_array); uint64_t id; @@ -231,7 +231,11 @@ static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, *toc_idx += 1; offset += 8; - EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */)) ); + len = cnid_count + 1; + if (cnid_count > 0) + len ++; + + EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_CNIDS, len, 8 /* unknown meaning, but always 8 */)) ); offset += 8; if (cnid_count > 0) { @@ -302,7 +306,13 @@ static int sl_pack_filemeta(sl_filemeta_t *fm, char *buf, int offset, char *toc_ offset += 16; EC_NEG1( fmlen = sl_pack(fm, buf + offset) ); - offset += fmlen; + + /* Check for empty filemeta array, if it's only 40 bytes, it's only the header but no content */ + LOG(log_debug, logtype_sl, "fmlen: %d", fmlen); + if (fmlen > 40) + offset += fmlen; + else + fmlen = 0; EC_ZERO( slvalc(buf, saveoff + 8, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_DATA, (fmlen / 8) + 1, 8 /* unknown meaning, but always 8 */)) ); diff --git a/etc/afpd/spotlight_module.c b/etc/afpd/spotlight_module.c index cdc36404..52e4e8c4 100644 --- a/etc/afpd/spotlight_module.c +++ b/etc/afpd/spotlight_module.c @@ -191,8 +191,10 @@ static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, co sl_nil_t nil = 0; int i, metacount; - if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) - EC_FAIL; + if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) { + dalloc_add_copy(fm_array, &nil, sl_nil_t); + goto EC_CLEANUP; + } LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount); @@ -228,8 +230,9 @@ static int sl_mod_fetch_result(void *p) sl_cnids_t *cnids; sl_filemeta_t *fm; sl_array_t *fm_array; + sl_nil_t nil; uint64_t uint64; - gboolean qres; + gboolean qres, firstmatch = true; if (!slq->slq_tracker_cursor) { LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found"); @@ -247,13 +250,9 @@ static int sl_mod_fetch_result(void *p) fm_array = talloc_zero(fm, sl_array_t); dalloc_add(fm, fm_array, sl_array_t); - /* For some reason the list of results always starts with a nil entry */ - sl_nil_t nil; - dalloc_add_copy(fm_array, &nil, sl_nil_t); - LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor"); - while (i <= MAX_SL_RESULTS) { + while ((slq->slq_state == SLQ_STATE_RUNNING) && (i <= MAX_SL_RESULTS)) { become_root(); qres = tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error); unbecome_root(); @@ -261,12 +260,16 @@ static int sl_mod_fetch_result(void *p) if (!qres) break; + if (firstmatch) { + /* For some reason the list of results always starts with a nil entry */ + dalloc_add_copy(fm_array, &nil, sl_nil_t); + firstmatch = false; + } + become_root(); uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL); unbecome_root(); - LOG(log_debug, logtype_sl, "uri: \"%s\"", uri); - EC_NULL_LOG( path = tracker_to_unix_path(uri) ); if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) { @@ -294,12 +297,14 @@ static int sl_mod_fetch_result(void *p) if (i < MAX_SL_RESULTS) slq->slq_state = SLQ_STATE_DONE; + uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */ + dalloc_add_copy(slq->slq_reply, &uint64, uint64_t); 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) { - if ((ret != 0) || (slq->slq_state == SLQ_STATE_DONE)) { + if (ret != 0) { + if (slq->slq_tracker_cursor) { g_object_unref(slq->slq_tracker_cursor); slq->slq_tracker_cursor = NULL; }