From e3fc43de05107cd8f7d9c7676cf84cb4d9eee088 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Wed, 7 Aug 2013 11:28:57 +0200 Subject: [PATCH] Add new Spotlight RPC functions --- etc/afpd/spotlight.c | 194 +++++++++++++++++++++++++++++++++++ etc/spotlight/slmod_sparql.c | 59 ++++++++++- include/atalk/spotlight.h | 39 +++---- 3 files changed, 271 insertions(+), 21 deletions(-) diff --git a/etc/afpd/spotlight.c b/etc/afpd/spotlight.c index 1edecb6e..7e5ec145 100644 --- a/etc/afpd/spotlight.c +++ b/etc/afpd/spotlight.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -38,6 +39,8 @@ #include #include +#include "directory.h" + static TALLOC_CTX *sl_ctx; static void *sl_module; static struct sl_module_export *sl_module_export; @@ -81,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)); @@ -95,6 +106,7 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel) LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]); } +#ifndef SPOT_TEST_MAIN /************************************************************************************************** * Spotlight queries **************************************************************************************************/ @@ -348,6 +360,181 @@ EC_CLEANUP: 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, "sl_array_t", 1, "sl_dict_t", 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); + dalloc_add(reply, array, sl_array_t); + +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: + 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; @@ -490,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 { @@ -515,6 +708,7 @@ EC_CLEANUP: } EC_EXIT; } +#endif /************************************************************************************************** * Testing diff --git a/etc/spotlight/slmod_sparql.c b/etc/spotlight/slmod_sparql.c index 63d1cade..00ed90d6 100644 --- a/etc/spotlight/slmod_sparql.c +++ b/etc/spotlight/slmod_sparql.c @@ -148,12 +148,14 @@ EC_CLEANUP: EC_EXIT; } -static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path) +static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path, const struct stat *sp) { EC_INIT; sl_array_t *meta; sl_nil_t nil = 0; int i, metacount; + uint64_t uint64; + sl_time_t sl_time; if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) { dalloc_add_copy(fm_array, &nil, sl_nil_t); @@ -165,12 +167,25 @@ static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, co meta = talloc_zero(fm_array, sl_array_t); for (i = 0; i < metacount; i++) { - if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) { + if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName") || + STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSName")) { char *p, *name; if ((p = strrchr(path, '/'))) { name = dalloc_strdup(meta, p + 1); dalloc_add(meta, name, "char *"); } + } else if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSSize")) { + uint64 = sp->st_size; + dalloc_add_copy(meta, &uint64, uint64_t); + } else if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSOwnerUserID")) { + uint64 = sp->st_uid; + dalloc_add_copy(meta, &uint64, uint64_t); + } else if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemFSOwnerGroupID")) { + uint64 = sp->st_gid; + dalloc_add_copy(meta, &uint64, uint64_t); + } else if (STRCMP(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); } @@ -223,6 +238,8 @@ static int sl_mod_fetch_result(void *p) /* Prepare FileMeta */ fm = talloc_zero(slq->slq_reply, sl_filemeta_t); fm_array = talloc_zero(fm, sl_array_t); + /* For some reason the list of results always starts with a nil entry */ + dalloc_add_copy(fm_array, &nil, sl_nil_t); dalloc_add(fm, fm_array, sl_array_t); LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor"); @@ -235,11 +252,13 @@ static int sl_mod_fetch_result(void *p) if (!qres) break; +#if 0 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; } +#endif become_root(); uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL); @@ -259,8 +278,12 @@ static int sl_mod_fetch_result(void *p) goto loop_cleanup; } + struct stat sb; + if (stat(path, &sb) != 0) + goto loop_cleanup; + dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t); - add_filemeta(slq->slq_reqinfo, fm_array, id, path); + add_filemeta(slq->slq_reqinfo, fm_array, id, path, &sb); loop_cleanup: g_free(path); @@ -307,6 +330,35 @@ EC_CLEANUP: EC_EXIT; } +static int sl_mod_fetch_attrs(void *p) +{ + EC_INIT; + slq_t *slq = p; + sl_filemeta_t *fm; + sl_array_t *fm_array; + sl_nil_t nil; + + LOG(log_debug, logtype_sl, "sl_mod_fetch_attrs(\"%s\")", slq->slq_path); + + struct stat sb; + EC_ZERO( stat(slq->slq_path, &sb) ); + + /* Prepare FileMeta */ + fm = talloc_zero(slq->slq_reply, sl_filemeta_t); + fm_array = talloc_zero(fm, sl_array_t); + dalloc_add(fm, fm_array, fm_array_t); + /* For some reason the list of results always starts with a nil entry */ + dalloc_add_copy(fm_array, &nil, sl_nil_t); + + add_filemeta(slq->slq_reqinfo, fm_array, CNID_INVALID, slq->slq_path, &sb); + + /* Now add result */ + dalloc_add(slq->slq_reply, fm, sl_filemeta_t); + +EC_CLEANUP: + EC_EXIT; +} + static int sl_mod_error(void *p) { EC_INIT; @@ -371,6 +423,7 @@ struct sl_module_export sl_mod = { sl_mod_start_search, sl_mod_fetch_result, sl_mod_close_query, + sl_mod_fetch_attrs, sl_mod_error, sl_mod_index_file }; diff --git a/include/atalk/spotlight.h b/include/atalk/spotlight.h index 56e09306..9aba3201 100644 --- a/include/atalk/spotlight.h +++ b/include/atalk/spotlight.h @@ -38,6 +38,7 @@ struct sl_module_export { int (*sl_mod_start_search)(void *); int (*sl_mod_fetch_result)(void *); int (*sl_mod_end_search) (void *); + int (*sl_mod_fetch_attrs) (void *); int (*sl_mod_error) (void *); int (*sl_mod_index_file) (const void *); }; @@ -84,31 +85,33 @@ typedef enum { SLQ_STATE_NEW = 1, /* Query received from client */ SLQ_STATE_RUNNING = 2, /* Query dispatched to Tracker */ SLQ_STATE_DONE = 3, /* Tracker finished */ - SLQ_STATE_END = 4 /* Query results returned to client */ + SLQ_STATE_END = 4, /* Query results returned to client */ + SLQ_STATE_ATTRS = 5 /* Fetch metadata for an object */ } slq_state_t; /* Internal query data structure */ typedef struct _slq_t { - struct list_head slq_list; /* queries are stored in a list */ - 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 */ - sl_array_t *slq_reqinfo; /* array with requested metadata */ - const char *slq_qstring; /* the Spotlight query string */ - uint64_t *slq_cnids; /* Pointer to array with CNIDs to which a query applies */ - size_t slq_cnids_num; /* Size of slq_cnids array */ + struct list_head slq_list; /* queries are stored in a list */ + 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 */ + sl_array_t *slq_reqinfo; /* array with requested metadata */ + const char *slq_qstring; /* the Spotlight query string */ + uint64_t *slq_cnids; /* Pointer to array with CNIDs to which a query applies */ + size_t slq_cnids_num; /* Size of slq_cnids array */ + const char *slq_path; /* Path to file or dir, used in fetchAttributes */ #ifdef HAVE_TRACKER_SPARQL - void *slq_tracker_cursor; /* Tracker SPARQL query result cursor */ + void *slq_tracker_cursor; /* Tracker SPARQL query result cursor */ #endif #ifdef HAVE_TRACKER_RDF - char *slq_trackerquery; /* RDF query string*/ - char *slq_fts; /* FTS search string */ - int slq_service; /* Tracker service */ - int slq_offset; /* search offset */ + char *slq_trackerquery; /* RDF query string */ + char *slq_fts; /* FTS search string */ + int slq_service; /* Tracker service */ + int slq_offset; /* search offset */ #endif } slq_t; -- 2.39.2