[glib/wip/nacho/registry-writable] registrybackend: handle writability notifications



commit 78f72683680275811ca84d86530ea3ac9dc232d9
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Feb 2 10:24:10 2016 +0100

    registrybackend: handle writability notifications

 gio/gregistrysettingsbackend.c |   77 ++++++++++++++++++++++++++++-----------
 1 files changed, 55 insertions(+), 22 deletions(-)
---
diff --git a/gio/gregistrysettingsbackend.c b/gio/gregistrysettingsbackend.c
index a5eeb4b..bbc1686 100644
--- a/gio/gregistrysettingsbackend.c
+++ b/gio/gregistrysettingsbackend.c
@@ -376,14 +376,17 @@ typedef struct
   
   gint32 ref_count : 9;
 
-  gint32 touched : 1;
+  gint32 readable : 1;
   RegistryValue value;
+
+  gint32 writable : 1;
 } RegistryCacheItem;
 
 static GNode *
 registry_cache_add_item (GNode         *parent,
                          gchar         *name,
                          RegistryValue  value,
+                         gboolean       writable,
                          gint           ref_count)
 {
   RegistryCacheItem *item;
@@ -401,7 +404,8 @@ registry_cache_add_item (GNode         *parent,
   item->value = value;
   item->subscription_count = 0;
   item->block_count = 0;
-  item->touched = FALSE;
+  item->readable = FALSE;
+  item->writable = writable;
 
   trace ("\treg cache: adding %s to %s\n",
          name, ((RegistryCacheItem *)parent->data)->name);
@@ -1175,11 +1179,11 @@ _free_watch (WatchThreadState *self,
              GNode            *cache_node);
 
 static void
-registry_cache_item_reset_touched (GNode    *node,
-                                   gpointer  data)
+registry_cache_item_reset_readable (GNode    *node,
+                                    gpointer  data)
 {
   RegistryCacheItem *item = node->data;
-  item->touched = FALSE;
+  item->readable = FALSE;
 }
 
 /* Delete a node and any children, for when it has been deleted from the registry */
@@ -1226,7 +1230,7 @@ registry_cache_remove_deleted (GNode    *node,
 {
   RegistryCacheItem *item = node->data;
 
-  if (!item->touched)
+  if (!item->readable)
     registry_cache_destroy_tree (node, data);
 }
 
@@ -1251,7 +1255,8 @@ registry_cache_update (GRegistryBackend *self,
                        const gchar      *partial_key_name,
                        GNode            *cache_node,
                        int               n_watches, 
-                       GPtrArray        *changes)
+                       GPtrArray        *changes,
+                       gboolean         *writability_changed)
 {
   gchar buffer[MAX_KEY_NAME_LENGTH + 1];
   gchar *key_name;
@@ -1270,11 +1275,11 @@ registry_cache_update (GRegistryBackend *self,
   trace ("registry cache update: %s. Node %x has %i children\n", key_name,
          cache_node, g_node_n_children (cache_node));
 
-  /* Start by zeroing 'touched' flag. When the registry traversal is done, any untouched nodes
+  /* Start by zeroing 'readable' flag. When the registry traversal is done, any unreadable nodes
    * must have been deleted from the registry.
    */
   g_node_children_foreach (cache_node, G_TRAVERSE_ALL,
-                           registry_cache_item_reset_touched, NULL);
+                           registry_cache_item_reset_readable, NULL);
 
   /* Recurse into each subpath at the current level, if any */
   i = 0;
@@ -1282,12 +1287,22 @@ registry_cache_update (GRegistryBackend *self,
     {
       DWORD buffer_size = MAX_KEY_NAME_LENGTH;
       HKEY  hsubpath;
+      gboolean writable = TRUE;
 
       result = RegEnumKeyEx (hpath, i++, buffer, &buffer_size, NULL, NULL, NULL, NULL);
       if (result != ERROR_SUCCESS)
         break;
 
-      result = RegOpenKeyEx (hpath, buffer, 0, KEY_READ, &hsubpath);
+      /* First try to open the key for writing so we can understand if the path
+       * is writable, if not we fallback to just read from it.
+       */
+      result = RegOpenKeyEx (hpath, buffer, 0, KEY_READ | KEY_WRITE, &hsubpath);
+      if (result == ERROR_ACCESS_DENIED)
+        {
+          result = RegOpenKeyEx (hpath, buffer, 0, KEY_READ, &hsubpath);
+          writable = FALSE;
+        }
+
       if (result == ERROR_SUCCESS)
         {
           GNode             *subkey_node;
@@ -1298,13 +1313,19 @@ registry_cache_update (GRegistryBackend *self,
             {
               RegistryValue null_value = {REG_NONE, {0}};
               subkey_node = registry_cache_add_item (cache_node, buffer,
-                                                     null_value, n_watches);
+                                                     null_value, writable,
+                                                     n_watches);
             }
 
           registry_cache_update (self, hsubpath, prefix, buffer, subkey_node,
-                                 n_watches, changes);
+                                 n_watches, changes, writability_changed);
+
           child_item = subkey_node->data;
-          child_item->touched = TRUE;
+          child_item->readable = TRUE;
+          child_item->writable = writable;
+
+          if (writability_changed)
+            *writability_changed = *writability_changed || child_item->writable != writable;
 
           RegCloseKey (hsubpath);
         }
@@ -1327,7 +1348,7 @@ registry_cache_update (GRegistryBackend *self,
       if (result != ERROR_SUCCESS)
         break;
 
-      if (buffer[0]==0)
+      if (buffer[0] == 0)
         /* This is the key's 'default' value, for which we have no use. */
         continue;
 
@@ -1343,7 +1364,7 @@ registry_cache_update (GRegistryBackend *self,
         {
           /* This is a new value */
           cache_child_node = registry_cache_add_item (cache_node, buffer, value,
-                                                      n_watches);
+                                                      item->writable, n_watches);
           changed = TRUE;
         }
       else
@@ -1359,14 +1380,16 @@ registry_cache_update (GRegistryBackend *self,
         }
 
       child_item = cache_child_node->data;
-      child_item->touched = TRUE;
-      if (changed == TRUE && changes != NULL)
+      child_item->readable = TRUE;
+      if (changed && changes != NULL)
         {
           gchar *item;
+
           if (partial_key_name == NULL)
             item = g_strdup (buffer);
           else
             item = g_build_path ("/", partial_key_name, buffer, NULL);
+
           g_ptr_array_add (changes, item);
         }
     }
@@ -1374,11 +1397,12 @@ registry_cache_update (GRegistryBackend *self,
   if (result != ERROR_NO_MORE_ITEMS)
     g_message_win32_error (result, "gregistrybackend: error enumerating values for cache");
 
-  /* Any nodes now left untouched must have been deleted, remove them from cache */
+  /* Any nodes now left unreadable must have been deleted, remove them from cache */
   g_node_children_foreach (cache_node, G_TRAVERSE_ALL,
                            registry_cache_remove_deleted, self->watch);
 
   trace ("registry cache update complete.\n");
+
   g_free (key_name);
 }
 
@@ -1392,7 +1416,9 @@ registry_watch_key (HKEY   hpath,
                     HANDLE event)
 {
   return RegNotifyChangeKeyValue (hpath, TRUE,
-                                  REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET,
+                                  REG_NOTIFY_CHANGE_NAME |
+                                  REG_NOTIFY_CHANGE_LAST_SET |
+                                  REG_NOTIFY_CHANGE_SECURITY,
                                   event, TRUE);
 }
 
@@ -1402,6 +1428,7 @@ typedef struct
   GRegistryBackend *self;
   gchar *prefix;          /* prefix is a gsettings path, all items are subkeys of this. */
   GPtrArray *items;       /* each item is a subkey below prefix that has changed. */
+  gboolean writability_changed; /* whether the writability of any of the items from prefix changed */
 } RegistryEvent;
 
 /* This handler runs in the main thread to emit the changed signals */
@@ -1415,7 +1442,11 @@ watch_handler (RegistryEvent *event)
   /* GSettings requires us to NULL-terminate the array. */
   g_ptr_array_add (event->items, NULL);
   g_settings_backend_keys_changed (G_SETTINGS_BACKEND (event->self), event->prefix,
-                                   (gchar const **)event->items->pdata, NULL);
+                                   (gchar const * const *)event->items->pdata, NULL);
+
+  if (event->writability_changed)
+    g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (event->self),
+                                              event->prefix);
 
   g_ptr_array_free (event->items, TRUE);
   g_free (event->prefix);
@@ -1677,10 +1708,12 @@ watch_thread_function (LPVOID parameter)
           g_object_ref (self->owner);
 
           event->items = g_ptr_array_new_with_free_func (g_free);
+          event->writability_changed = FALSE;
 
           EnterCriticalSection (G_REGISTRY_BACKEND (self->owner)->cache_lock);
           registry_cache_update (G_REGISTRY_BACKEND (self->owner), hpath,
-                                 prefix, NULL, cache_node, 0, event->items);
+                                 prefix, NULL, cache_node, 0,
+                                 event->items, &event->writability_changed);
           LeaveCriticalSection (G_REGISTRY_BACKEND (self->owner)->cache_lock);
 
           if (event->items->len > 0)
@@ -1827,7 +1860,7 @@ watch_add_notify (GRegistryBackend *self,
     }
 
   registry_cache_ref_tree (cache_node);
-  registry_cache_update (self, hpath, gsettings_prefix, NULL, cache_node, 0, NULL);
+  registry_cache_update (self, hpath, gsettings_prefix, NULL, cache_node, 0, NULL, NULL);
   //registry_cache_dump (self->cache_root, NULL);
   LeaveCriticalSection (self->cache_lock);
 


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