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



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

    registrybackend: handle writability notifications

 gio/gregistrysettingsbackend.c |   55 ++++++++++++++++++++++++++++++++--------
 1 files changed, 44 insertions(+), 11 deletions(-)
---
diff --git a/gio/gregistrysettingsbackend.c b/gio/gregistrysettingsbackend.c
index a5eeb4b..e3bb0c0 100644
--- a/gio/gregistrysettingsbackend.c
+++ b/gio/gregistrysettingsbackend.c
@@ -378,12 +378,15 @@ typedef struct
 
   gint32 touched : 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->touched = FALSE;
+  item->writable = writable;
 
   trace ("\treg cache: adding %s to %s\n",
          name, ((RegistryCacheItem *)parent->data)->name);
@@ -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;
@@ -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->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
@@ -1360,13 +1381,15 @@ registry_cache_update (GRegistryBackend *self,
 
       child_item = cache_child_node->data;
       child_item->touched = TRUE;
-      if (changed == TRUE && changes != NULL)
+      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);
         }
     }
@@ -1379,6 +1402,7 @@ registry_cache_update (GRegistryBackend *self,
                            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]