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



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

    registrybackend: handle writability notifications

 gio/gregistrysettingsbackend.c |   43 +++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 7 deletions(-)
---
diff --git a/gio/gregistrysettingsbackend.c b/gio/gregistrysettingsbackend.c
index e05ebdf..c44ad90 100644
--- a/gio/gregistrysettingsbackend.c
+++ b/gio/gregistrysettingsbackend.c
@@ -378,12 +378,15 @@ typedef struct
 
   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;
@@ -402,6 +405,7 @@ registry_cache_add_item (GNode         *parent,
   item->subscription_count = 0;
   item->block_count = 0;
   item->readable = FALSE;
+  item->writable = writable;
 
   trace ("\treg cache: adding %s to %s\n",
          name, ((RegistryCacheItem *)parent->data)->name);
@@ -1226,6 +1230,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;
 
 static void
@@ -1310,12 +1315,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;
@@ -1326,13 +1341,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, event);
           child_item = subkey_node->data;
           child_item->readable = TRUE;
+          child_item->writable = writable;
+
+          if (event != NULL)
+            event->writability_changed = event->writability_changed ||
+                                         child_item->writable != writable;
 
           RegCloseKey (hsubpath);
         }
@@ -1355,7 +1376,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;
 
@@ -1371,7 +1392,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
@@ -1409,6 +1430,7 @@ registry_cache_update (GRegistryBackend *self,
                            registry_cache_remove_deleted, event);
 
   trace ("registry cache update complete.\n");
+
   g_free (key_name);
 }
 
@@ -1422,7 +1444,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);
 }
 
@@ -1437,7 +1461,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);
@@ -1699,6 +1727,7 @@ 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,
@@ -1849,7 +1878,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]