]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight.c
Fix dalloc_value_for_key usage
[netatalk.git] / etc / afpd / spotlight.c
index dc1682bba52465a50ac3c47a968e6d0bf682814c..fff5a36ba244def42fed80134ae2664b603cfbd9 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdbool.h>
 #include <inttypes.h>
 #include <time.h>
+#include <utime.h>
 
 #include <atalk/list.h>
 #include <atalk/errchk.h>
@@ -36,8 +37,9 @@
 #include <atalk/byteorder.h>
 #include <atalk/netatalk_conf.h>
 #include <atalk/volume.h>
+#include <atalk/spotlight.h>
 
-#include "spotlight.h"
+#include "directory.h"
 
 static TALLOC_CTX *sl_ctx;
 static void *sl_module;
@@ -82,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));
@@ -96,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
  **************************************************************************************************/
@@ -225,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;
@@ -232,6 +275,7 @@ static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *re
     uint64_t *uint64;
     DALLOC_CTX *reqinfo;
     sl_array_t *array;
+    sl_cnids_t *cnids;
     slq_t *slq = NULL;
 
     /* Allocate and initialize query object */
@@ -239,9 +283,18 @@ static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *re
     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);
+    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) );
@@ -250,12 +303,15 @@ 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 */
     EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
 
@@ -304,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, "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);
+    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;
@@ -349,7 +580,7 @@ int sl_mod_load(const char *path)
     sl_ctx = talloc_new(NULL);
 
     if ((sl_module = mod_open(path)) == NULL) {
-        LOG(log_error, logtype_sl, "Failed to load: %s", path, mod_error());
+        LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", path, mod_error());
         EC_FAIL;
     }
 
@@ -369,7 +600,8 @@ EC_CLEANUP:
  **/
 void sl_index_file(const char *path)
 {
-    sl_module_export->sl_mod_index_file(path);
+    if (sl_module_export && sl_module_export->sl_mod_index_file)
+        sl_module_export->sl_mod_index_file(path);
 }
 
 /**************************************************************************************************
@@ -445,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 {
@@ -470,6 +708,7 @@ EC_CLEANUP:
     }
     EC_EXIT;
 }
+#endif
 
 /**************************************************************************************************
  * Testing