]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight.c
Convert charset of query
[netatalk.git] / etc / afpd / spotlight.c
index 60d3679ae6b554f0251f160d10985da925e1b53f..1edecb6e4f1acce39e3d81c7214a6ceb5331bfef 100644 (file)
@@ -36,8 +36,7 @@
 #include <atalk/byteorder.h>
 #include <atalk/netatalk_conf.h>
 #include <atalk/volume.h>
-
-#include "spotlight.h"
+#include <atalk/spotlight.h>
 
 static TALLOC_CTX *sl_ctx;
 static void *sl_module;
@@ -225,6 +224,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 +263,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 +271,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 +291,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) );
 
@@ -276,7 +320,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) );
@@ -286,20 +329,17 @@ 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) {
-        EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't runnnig");
+    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 */
-    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) );
 
     /* Fetch Tracker results*/
-    EC_ZERO( sl_module_export->sl_mod_fetch_result(slq) );
+    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) {
@@ -323,6 +363,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);
@@ -351,7 +393,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, "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;
     }
 
@@ -366,9 +408,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;
@@ -384,6 +436,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;