[tracker/wip/carlosg/namespace-api-updates: 2/4] libtracker-sparql: Seal TrackerNamespaceManagers belonging to a connection




commit acad3ccab990c68f70753bb71934d667d162df70
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Nov 13 14:23:18 2021 +0100

    libtracker-sparql: Seal TrackerNamespaceManagers belonging to a connection
    
    This is a slight API break, but one that is a) unused to my knowledge, and
    b) prone to introduce further brokenness if misused. Since any namespace
    added this way is still not part of the ontology, it could only be used to
    produce RDF that cannot be consumed later on.
    
    Also, there's the implicit assumption that TrackerSparqlConnection is
    multithread-aware, but this object would potentially break if it ends up
    used and modified from different threads.
    
    So, make tracker_namespace_manager_add_prefix() reserved for namespaces
    created through tracker_namespace_manager_new(), and make those coming from
    tracker_sparql_connection_get_namespace_manager() immutable.

 src/libtracker-sparql/tracker-connection.c        |  7 ++++++-
 src/libtracker-sparql/tracker-namespace-manager.c | 15 +++++++++++++++
 src/libtracker-sparql/tracker-private.h           |  2 ++
 3 files changed, 23 insertions(+), 1 deletion(-)
---
diff --git a/src/libtracker-sparql/tracker-connection.c b/src/libtracker-sparql/tracker-connection.c
index ea197d923..e2585d440 100644
--- a/src/libtracker-sparql/tracker-connection.c
+++ b/src/libtracker-sparql/tracker-connection.c
@@ -666,9 +666,14 @@ tracker_sparql_connection_update_resource_finish (TrackerSparqlConnection  *conn
 TrackerNamespaceManager *
 tracker_sparql_connection_get_namespace_manager (TrackerSparqlConnection *connection)
 {
+       TrackerNamespaceManager *manager;
+
        g_return_val_if_fail (TRACKER_IS_SPARQL_CONNECTION (connection), NULL);
 
-       return TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->get_namespace_manager (connection);
+       manager = TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->get_namespace_manager (connection);
+       tracker_namespace_manager_seal (manager);
+
+       return manager;
 }
 
 /**
diff --git a/src/libtracker-sparql/tracker-namespace-manager.c 
b/src/libtracker-sparql/tracker-namespace-manager.c
index f89e16f9e..13c1d2187 100644
--- a/src/libtracker-sparql/tracker-namespace-manager.c
+++ b/src/libtracker-sparql/tracker-namespace-manager.c
@@ -26,6 +26,7 @@
 
 #include "tracker-namespace-manager.h"
 #include "tracker-ontologies.h"
+#include "tracker-private.h"
 
 #define MAX_PREFIX_LENGTH 100
 
@@ -36,6 +37,7 @@ struct _TrackerNamespaceManager {
 typedef struct {
        GHashTable *prefix_to_namespace;
        GHashTable *namespace_to_prefix;
+       gboolean sealed;
 } TrackerNamespaceManagerPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (TrackerNamespaceManager, tracker_namespace_manager, G_TYPE_OBJECT)
@@ -206,6 +208,10 @@ tracker_namespace_manager_lookup_prefix (TrackerNamespaceManager *self,
  *
  * Only one prefix is allowed for a given namespace, and all prefixes must
  * be unique.
+ *
+ * Since 3.3, This function may not be used on #TrackerNamespaceManager
+ * instances that were obtained through
+ * tracker_sparql_connection_get_namespace_manager().
  */
 void
 tracker_namespace_manager_add_prefix (TrackerNamespaceManager *self,
@@ -220,6 +226,7 @@ tracker_namespace_manager_add_prefix (TrackerNamespaceManager *self,
        g_return_if_fail (ns != NULL);
 
        priv = GET_PRIVATE (TRACKER_NAMESPACE_MANAGER (self));
+       g_return_if_fail (priv->sealed == FALSE);
 
        if (strlen (prefix) > MAX_PREFIX_LENGTH) {
                g_error ("Prefix is too long: max %i characters.", MAX_PREFIX_LENGTH);
@@ -372,3 +379,11 @@ tracker_namespace_manager_foreach (TrackerNamespaceManager *self,
 
        g_hash_table_foreach (priv->prefix_to_namespace, func, user_data);
 }
+
+void
+tracker_namespace_manager_seal (TrackerNamespaceManager *self)
+{
+       TrackerNamespaceManagerPrivate *priv = GET_PRIVATE (self);
+
+       priv->sealed = TRUE;
+}
diff --git a/src/libtracker-sparql/tracker-private.h b/src/libtracker-sparql/tracker-private.h
index 18f4fd273..448581acd 100644
--- a/src/libtracker-sparql/tracker-private.h
+++ b/src/libtracker-sparql/tracker-private.h
@@ -265,4 +265,6 @@ void tracker_sparql_cursor_set_connection (TrackerSparqlCursor     *cursor,
                                            TrackerSparqlConnection *connection);
 GError * _translate_internal_error (GError *error);
 
+void tracker_namespace_manager_seal (TrackerNamespaceManager *namespaces);
+
 #endif /* __TRACKER_PRIVATE_H__ */


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