X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Fspotlight.c;h=fff5a36ba244def42fed80134ae2664b603cfbd9;hb=b85e88cc0e408268df912f6c10607ad72991c21e;hp=241775b4c72a2b7c9b37104dfd03b566a27f9acf;hpb=745435619841daa9aeb0ce09377383b347e49bbd;p=netatalk.git diff --git a/etc/afpd/spotlight.c b/etc/afpd/spotlight.c index 241775b4..fff5a36b 100644 --- a/etc/afpd/spotlight.c +++ b/etc/afpd/spotlight.c @@ -12,6 +12,8 @@ GNU General Public License for more details. */ +#define USE_LIST + #ifdef HAVE_CONFIG_H #include "config.h" #endif /* HAVE_CONFIG_H */ @@ -24,7 +26,9 @@ #include #include #include +#include +#include #include #include #include @@ -33,9 +37,9 @@ #include #include #include -#include +#include -#include "spotlight.h" +#include "directory.h" static TALLOC_CTX *sl_ctx; static void *sl_module; @@ -80,6 +84,14 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel) LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false"); } else if (STRCMP(type, ==, "sl_nil_t")) { LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]); + } else if (STRCMP(type, ==, "sl_time_t")) { + sl_time_t t; + struct tm *tm; + char datestring[256]; + 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); + LOG(log_debug, logtype_sl, "%ssl_time_t: %s.%06d", neststrings[nestinglevel + 1], datestring, t.tv_usec); } else if (STRCMP(type, ==, "sl_cnids_t")) { sl_cnids_t cnids; memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t)); @@ -94,34 +106,40 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel) LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]); } +#ifndef SPOT_TEST_MAIN /************************************************************************************************** * Spotlight queries **************************************************************************************************/ -static q_t *sl_queries; -static slq_t *slq_active; +static ATALK_LIST_HEAD(sl_queries); /*! * Add a query to the list of active queries */ static int slq_add(slq_t *slq) { - EC_INIT; - - if (slq_active) - talloc_free(slq_active); - slq_active = slq; - -EC_CLEANUP: - EC_EXIT; + list_add(&(slq->slq_list), &sl_queries); + return 0; } static int slq_remove(slq_t *slq) { EC_INIT; + struct list_head *p; + slq_t *q = NULL; + + 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; + } + q = NULL; + } - if ((slq_active->slq_ctx1 == slq->slq_ctx1) && (slq_active->slq_ctx2 == slq->slq_ctx2)) { - slq_active = NULL; + if (q == NULL) { + /* The SL query 'slq' was not found in the list, this is not supposed to happen! */ + LOG(log_warning, logtype_sl, "slq_remove: slq not in active query list"); } EC_CLEANUP: @@ -131,23 +149,38 @@ EC_CLEANUP: static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2) { EC_INIT; - slq_t *q; + slq_t *q = NULL; + struct list_head *p; - LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64 - "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, - ctx1, ctx2, slq_active->slq_ctx1, slq_active->slq_ctx2); + list_for_each(p, &sl_queries) { + q = list_entry(p, slq_t, slq_list); - if ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2)) - q = slq_active; - else + LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64 + "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, + ctx1, ctx2, q->slq_ctx1, q->slq_ctx2); + + if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) { + break; + } q = NULL; - + } + EC_CLEANUP: if (ret != 0) q = NULL; return q; } +/* Error handling for queries */ +static void slq_error(slq_t *slq) +{ + if (!slq) + return; + sl_module_export->sl_mod_error(slq); + slq_remove(slq); + talloc_free(slq); +} + /************************************************************************************************** * Spotlight RPC functions **************************************************************************************************/ @@ -203,6 +236,38 @@ EC_CLEANUP: EC_EXIT; } +static int cnid_comp_fn(const void *p1, const void *p2) +{ + const uint64_t *cnid1 = p1, *cnid2 = p2; + if (*cnid1 == *cnid2) + return 0; + if (*cnid1 < *cnid2) + return -1; + else + return 1; +} + +static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p) +{ + EC_INIT; + uint64_t *cnids = NULL; + + EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) ); + 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_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v) { EC_INIT; @@ -210,16 +275,27 @@ static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *re uint64_t *uint64; DALLOC_CTX *reqinfo; sl_array_t *array; - - slq_t *slq = talloc_zero(sl_ctx, slq_t); + sl_cnids_t *cnids; + slq_t *slq = NULL; /* Allocate and initialize query object */ + slq = talloc_zero(sl_ctx, slq_t); + slq->slq_state = SLQ_STATE_NEW; slq->slq_obj = obj; slq->slq_vol = v; + + /* convert spotlight query charset to host charset */ 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; + char slq_host[MAXPATHLEN + 1]; + uint16_t convflags = v->v_mtou_flags; + size_t slq_maclen; + if (convert_charset(CH_UTF8_MAC, v->v_volcharset, v->v_maccharset, sl_query, strlen(sl_query), slq_host, MAXPATHLEN, &convflags) == -1) { + LOG(log_error, logtype_afpd, "sl_rpc_openQuery(\"%s\"): charset conversion failed", sl_query); + EC_FAIL; + } + slq->slq_qstring = talloc_strdup(slq, slq_host); + LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", slq->slq_qstring); + slq->slq_time = time(NULL); EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) ); slq->slq_ctx1 = *uint64; @@ -227,21 +303,26 @@ static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *re slq->slq_ctx2 = *uint64; EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") ); slq->slq_reqinfo = talloc_steal(slq, reqinfo); - + if ((cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryItemArray"))) { + EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) ); + } + LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:"); dd_dump(slq->slq_reqinfo, 0); (void)slq_add(slq); - + /* Run the query */ - sl_module_export->sl_mod_start_search(slq); + EC_ZERO( sl_module_export->sl_mod_start_search(slq) ); + + array = talloc_zero(reply, sl_array_t); + uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX; + dalloc_add_copy(array, &sl_res, uint64_t); + dalloc_add(reply, array, sl_array_t); EC_CLEANUP: - if (ret == 0) { - array = talloc_zero(reply, sl_array_t); - uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX; - dalloc_add_copy(array, &sl_res, uint64_t); - dalloc_add(reply, array, sl_array_t); + if (ret != 0) { + slq_error(slq); } EC_EXIT; } @@ -249,10 +330,9 @@ EC_CLEANUP: static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) { EC_INIT; - slq_t *slq; + 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) ); ctx1 = *uint64; @@ -261,27 +341,204 @@ static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CT /* Get query for context */ EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) ); + if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) { + EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active"); + } /* Create and pass reply handle */ + EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) ); + + /* Fetch Tracker results*/ + EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) ); + + dalloc_add(reply, slq->slq_reply, sl_array_t); + +EC_CLEANUP: + if (ret != 0) { + slq_error(slq); + } + EC_EXIT; +} + +static int sl_rpc_storeAttributesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol) +{ + 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, "sl_rpc_storeAttributesForOIDArray: 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) ); + } + + if ((sl_time = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "DALLOC_CTX", 1, "kMDItemLastUsedDate"))) { + 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); - slq->slq_reply = array; + dalloc_add(reply, array, sl_array_t); - /* Fetch Tracker results*/ - sl_module_export->sl_mod_fetch_result(slq); +EC_CLEANUP: + EC_EXIT; +} + +static int sl_rpc_fetchAttributeNamesForOIDArray(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol) +{ + EC_INIT; + uint64_t uint64; + 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", 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: - if (ret == 0) { - dalloc_add(reply, array, sl_array_t); + EC_EXIT; +} + +static int sl_rpc_fetchAttributesForOIDArray(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *vol) +{ + EC_INIT; + slq_t *slq = NULL; + uint64_t uint64; + sl_cnids_t *cnids; + sl_time_t *sl_time; + cnid_t id; + struct dir *dir; + sl_array_t *reqinfo; + + + + /* Allocate and initialize query object */ + slq = talloc_zero(reply, slq_t); + slq->slq_state = SLQ_STATE_ATTRS; + slq->slq_obj = obj; + slq->slq_vol = vol; + EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) ); + EC_NULL( reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1) ); + slq->slq_reqinfo = talloc_steal(slq, reqinfo); + + 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; + + if (htonl(id) == DIRDID_ROOT) { + slq->slq_path = talloc_strdup(slq, vol->v_path); + } else if (id < CNID_START) { + EC_FAIL; + } else { + cnid_t did; + char buffer[12 + MAXPATHLEN + 1]; + char *name; + did = htonl(id); + EC_NULL( name = cnid_resolve(vol->v_cdb, &did, buffer, sizeof(buffer)) ); + EC_NULL( dir = dirlookup(vol, did) ); + EC_NULL( slq->slq_path = talloc_asprintf(slq, "%s/%s", bdata(dir->d_fullpath), name) ); } + + /* Return result value 0 */ + uint64_t sl_res = 0; + dalloc_add_copy(slq->slq_reply, &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(slq->slq_reply, replycnids, sl_cnids_t); + + /* Fetch attributes from module */ + EC_ZERO_LOG( sl_module_export->sl_mod_fetch_attrs(slq) ); + + dalloc_add(reply, slq->slq_reply, sl_array_t); + +EC_CLEANUP: EC_EXIT; } static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) { EC_INIT; - slq_t *slq; + slq_t *slq = NULL; uint64_t *uint64, ctx1, ctx2; sl_array_t *array; @@ -293,16 +550,21 @@ 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); + slq = NULL; + + 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: - if (ret == 0) { - 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); + if (ret != 0) { + slq_error(slq); } EC_EXIT; } @@ -316,10 +578,9 @@ int sl_mod_load(const char *path) EC_INIT; sl_ctx = talloc_new(NULL); - sl_queries = queue_init(); if ((sl_module = mod_open(path)) == NULL) { - LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error()); + LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", path, mod_error()); EC_FAIL; } @@ -334,9 +595,19 @@ EC_CLEANUP: EC_EXIT; } +/** + * Index a file + **/ +void sl_index_file(const char *path) +{ + if (sl_module_export && sl_module_export->sl_mod_index_file) + sl_module_export->sl_mod_index_file(path); +} + /************************************************************************************************** * AFP functions **************************************************************************************************/ + int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen) { EC_INIT; @@ -352,6 +623,9 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_ *rbuflen = 0; + if (sl_module == NULL) + return AFPERR_NOOP; + ibuf += 2; ibuflen -= 2; @@ -403,6 +677,12 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_ 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 { @@ -428,6 +708,7 @@ EC_CLEANUP: } EC_EXIT; } +#endif /************************************************************************************************** * Testing