]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight_module.c
Add close RPC call
[netatalk.git] / etc / afpd / spotlight_module.c
index d6ccb0740eba3887b9cc457763f380ef67ddf6df..4b4d8a6592f9c22f578ca9ff28d1313c8bda7cfe 100644 (file)
 
 static TrackerSparqlConnection *connection;
 
+const char *tracker_to_unix_path(const char *path)
+{
+    /* just skip 'file://' */
+    return path + 7;
+}
+
 static int sl_mod_init(void *p)
 {
     EC_INIT;
@@ -66,7 +72,7 @@ static const gchar *map_spotlight_to_sparql_query(slq_t *slq)
 
     EC_NULL_LOG( word = strstr(slquery, "*==") );
     word += 4; /* skip *== and the left enclosing quote */
-    EC_NULL( word = talloc_strdup(slq, word) );
+    EC_NULL( word = dalloc_strdup(slq, word) );
     /* Search asterisk */
     EC_NULL_LOG( p = strchr(word, '*') );
     *p = 0;
@@ -130,25 +136,108 @@ EC_CLEANUP:
     EC_EXIT;
 }
 
+static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
+{
+    EC_INIT;
+    sl_array_t *meta;
+    sl_nil_t nil = 0;
+    int i, metacount;
+
+    if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0)
+        EC_FAIL;
+
+    LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
+
+    meta = talloc_zero(fm_array, sl_array_t);
+
+    for (i = 0; i < metacount; i++) {
+        if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
+            char *p, *name;
+            if ((p = strrchr(path, '/'))) {
+                name = dalloc_strdup(meta, p + 1);
+                dalloc_add(meta, name, "char *");
+            }
+        } else {
+            dalloc_add_copy(meta, &nil, sl_nil_t);
+        }
+    }
+
+    dalloc_add(fm_array, meta, sl_array_t);
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
 static int sl_mod_fetch_result(void *p)
 {
     EC_INIT;
     slq_t *slq = p;
     GError *error = NULL;
+    int i = 0;
+    cnid_t did, id;
+    const char *path;
+    sl_cnids_t *cnids;
+    sl_filemeta_t *fm;
+    sl_array_t *fm_array;
+    uint64_t uint64;
+
+    if (!slq->slq_tracker_cursor) {
+        LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
+        goto EC_CLEANUP;
+    }
 
-    if (slq->slq_tracker_cursor) {
-        int i = 0;
-        while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
-            LOG(log_debug, logtype_sl, "Result [%d]: %s",
-                i++, tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL));
+    /* Prepare CNIDs */
+    cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
+    cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
+    cnids->ca_unkn1 = 0xadd;
+    cnids->ca_context = slq->slq_ctx2;
+
+    /* Prepare FileMeta */
+    fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
+    fm_array = talloc_zero(fm, sl_array_t);
+    dalloc_add(fm, fm_array, sl_array_t);
+
+    /* For some reason the list of results always starts with a nil entry */
+    sl_nil_t nil;
+    dalloc_add_copy(fm_array, &nil, sl_nil_t);
+
+    while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
+        EC_NULL_LOG( path = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL) );
+        path = tracker_to_unix_path(path);
+        LOG(log_debug, logtype_sl, "sl_mod_fetch_result: path(volpath: %s): \"%s\"", slq->slq_vol->v_path, path);
+        if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
+            LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
+            continue;
         }
-    } else {
-        LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
+        LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
+        uint64 = ntohl(id);
+        dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
+        add_filemeta(slq->slq_reqinfo, fm_array, id, path);
+        //dalloc_add_copy(fm_array, &nil, sl_nil_t);
     }
+    dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
+    dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
 
 EC_CLEANUP:
-    if (slq->slq_tracker_cursor)
+    if (slq->slq_tracker_cursor) {
+        g_object_unref(slq->slq_tracker_cursor);
+        slq->slq_tracker_cursor = NULL;
+    }
+    EC_EXIT;
+}
+
+/* Free ressources allocated in this module */
+static int sl_mod_close_query(void *p)
+{
+    EC_INIT;
+    slq_t *slq = p;
+
+    if (slq->slq_tracker_cursor) {
         g_object_unref(slq->slq_tracker_cursor);
+        slq->slq_tracker_cursor = NULL;
+    }
+
+EC_CLEANUP:
     EC_EXIT;
 }
 
@@ -157,5 +246,5 @@ struct sl_module_export sl_mod = {
     sl_mod_init,
     sl_mod_start_search,
     sl_mod_fetch_result,
-    NULL
+    sl_mod_close_query
 };