[rygel] tracker: Escape the regex for SPARQL



commit 1993f0246cafdb05ddf2e4530cdbcc5fb1b2be3e
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Sun Aug 22 02:05:53 2010 +0300

    tracker: Escape the regex for SPARQL

 src/plugins/tracker/Makefile.am                    |    2 +-
 .../tracker/rygel-tracker-search-container.vala    |   59 +++++++++++++++++++-
 2 files changed, 57 insertions(+), 4 deletions(-)
---
diff --git a/src/plugins/tracker/Makefile.am b/src/plugins/tracker/Makefile.am
index 5ee9124..bb787d6 100644
--- a/src/plugins/tracker/Makefile.am
+++ b/src/plugins/tracker/Makefile.am
@@ -40,7 +40,7 @@ librygel_media_tracker_la_SOURCES = rygel-tracker-root-container.vala \
 librygel_media_tracker_la_VALAFLAGS = \
 	--vapidir=$(top_srcdir)/src/rygel \
 	--pkg rygel-1.0 --pkg rygel-build-config --pkg gconf-2.0 \
-	--pkg gupnp-1.0 --pkg gupnp-av-1.0 \
+	--pkg gupnp-1.0 --pkg gupnp-av-1.0 --pkg posix \
 	--pkg dbus-glib-1 --pkg gee-1.0 --pkg gstreamer-0.10 -g
 
 librygel_media_tracker_la_LIBADD = $(LIBGUPNP_LIBS) \
diff --git a/src/plugins/tracker/rygel-tracker-search-container.vala b/src/plugins/tracker/rygel-tracker-search-container.vala
index 744b626..52fcde3 100644
--- a/src/plugins/tracker/rygel-tracker-search-container.vala
+++ b/src/plugins/tracker/rygel-tracker-search-container.vala
@@ -3,6 +3,7 @@
  * Copyright (C) 2008 Nokia Corporation.
  *
  * Author: Zeeshan Ali <zeenix gmail com>
+ *         Ivan Frade <ivan frade nokia com>
  *
  * This file is part of Rygel.
  *
@@ -253,9 +254,10 @@ public class Rygel.Tracker.SearchContainer : Rygel.MediaContainer {
                 filter = variable + " = " + value;
                 break;
             case SearchCriteriaOp.CONTAINS:
-                filter = "regex(" + variable + ", " +
-                                    Regex.escape_string (value) +
-                         ")";
+                // We need to escape this twice for Tracker
+                var regex = this.escape_string (Regex.escape_string (value));
+
+                filter = "regex(" + variable + ", \"" + regex + "\")";
                 break;
         }
 
@@ -303,5 +305,56 @@ public class Rygel.Tracker.SearchContainer : Rygel.MediaContainer {
 
         return slice;
     }
+
+    /**
+     * tracker_sparql_escape_string:
+     * @literal: a string to escape
+     *
+     * Escapes a string so that it can be used in a SPARQL query. Copied from
+     * Tracker project.
+     *
+     * Returns: a newly-allocated string with the escaped version of @literal.
+     *  The returned string should be freed with g_free() when no longer needed.
+     */
+    private string escape_string (string literal) {
+        StringBuilder str = new StringBuilder ();
+        char *p = literal;
+
+        while (*p != '\0') {
+            size_t len = Posix.strcspn ((string) p, "\t\n\r\b\f\"\\");
+            str.append_len ((string) p, (long) len);
+            p += len;
+
+            switch (*p) {
+                case '\t':
+                    str.append ("\\t");
+                    break;
+                case '\n':
+                    str.append ("\\n");
+                    break;
+                case '\r':
+                    str.append ("\\r");
+                    break;
+                case '\b':
+                    str.append ("\\b");
+                    break;
+                case '\f':
+                    str.append ("\\f");
+                    break;
+                case '"':
+                    str.append ("\\\"");
+                    break;
+                case '\\':
+                    str.append ("\\\\");
+                    break;
+                default:
+                    continue;
+            }
+
+            p++;
+        }
+
+        return str.str;
+    }
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]