]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight.c
Move and rename Spotlight module
[netatalk.git] / etc / afpd / spotlight.c
index 241775b4c72a2b7c9b37104dfd03b566a27f9acf..eb4a125bf76b629db220df69fc84936399683c09 100644 (file)
@@ -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 */
@@ -25,6 +27,7 @@
 #include <inttypes.h>
 #include <time.h>
 
+#include <atalk/list.h>
 #include <atalk/errchk.h>
 #include <atalk/util.h>
 #include <atalk/logger.h>
@@ -33,9 +36,7 @@
 #include <atalk/byteorder.h>
 #include <atalk/netatalk_conf.h>
 #include <atalk/volume.h>
-#include <atalk/queue.h>
-
-#include "spotlight.h"
+#include <atalk/spotlight.h>
 
 static TALLOC_CTX *sl_ctx;
 static void *sl_module;
@@ -98,30 +99,35 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
  * 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 +137,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;
+
+    list_for_each(p, &sl_queries) {
+        q = list_entry(p, slq_t, slq_list);
 
-    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);
+        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 ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2))
-        q = slq_active;
-    else
+        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
  **************************************************************************************************/
@@ -210,16 +231,17 @@ 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);
+    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;
     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;
+
     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;
@@ -234,14 +256,16 @@ static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *re
     (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 +273,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,19 +284,21 @@ 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 */
-    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*/
-    sl_module_export->sl_mod_fetch_result(slq);
+    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) {
-        dalloc_add(reply, array, sl_array_t);
+    if (ret != 0) {
+        slq_error(slq);
     }
     EC_EXIT;
 }
@@ -281,7 +306,7 @@ EC_CLEANUP:
 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 +318,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 +346,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: %s", path, mod_error());
         EC_FAIL;
     }
 
@@ -334,9 +363,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 +391,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;