registrybackend: more cleanups

This commit is contained in:
Ignacio Casal Quinteiro 2016-01-26 09:00:35 +01:00
parent 7256f2289a
commit a159bc939d

View File

@ -192,7 +192,8 @@ trace (const char *format,
{ {
#ifdef TRACE #ifdef TRACE
va_list va; va_start (va, format); va_list va; va_start (va, format);
vprintf (format, va); fflush (stdout); vprintf (format, va);
fflush (stdout);
va_end (va); va_end (va);
#endif #endif
} }
@ -202,7 +203,7 @@ trace (const char *format,
* result from programmer error (Microsoft programmers don't count), instead * result from programmer error (Microsoft programmers don't count), instead
* they will mostly occur from people messing with the registry by hand. */ * they will mostly occur from people messing with the registry by hand. */
static void static void
g_message_win32_error (DWORD result_code, g_message_win32_error (DWORD result_code,
const gchar *format, const gchar *format,
...) ...)
{ {
@ -216,10 +217,12 @@ g_message_win32_error (DWORD result_code,
va_start (va, format); va_start (va, format);
pos = g_vsnprintf (win32_message, 512, format, va); pos = g_vsnprintf (win32_message, 512, format, va);
win32_message[pos++] = ':'; win32_message[pos++] = ' '; win32_message[pos++] = ':';
win32_message[pos++] = ' ';
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, result_code, 0, (LPTSTR)(win32_message+pos), FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL,
1023 - pos, NULL); result_code, 0, (LPTSTR)(win32_message + pos),
1023 - pos, NULL);
if (result_code == ERROR_KEY_DELETED) if (result_code == ERROR_KEY_DELETED)
trace ("(%s)", win32_message); trace ("(%s)", win32_message);
@ -256,7 +259,7 @@ parse_key (const gchar *key_name,
if (*c == '/') if (*c == '/')
{ {
*c = '\\'; *c = '\\';
(*value_name) = c; *value_name = c;
} }
} }
@ -311,8 +314,8 @@ handle_read_error (LONG result,
{ {
/* file not found means key value not set, this isn't an error for us. */ /* file not found means key value not set, this isn't an error for us. */
if (result != ERROR_FILE_NOT_FOUND) if (result != ERROR_FILE_NOT_FOUND)
g_message_win32_error (result, "Unable to query value %s/%s: %s.\n", g_message_win32_error (result, "Unable to query value %s/%s: %s.\n",
path_name, value_name); path_name, value_name);
} }
/*************************************************************************** /***************************************************************************
@ -365,22 +368,22 @@ registry_value_free (RegistryValue value)
typedef struct typedef struct
{ {
/* Component of path that this node represents */ /* Component of path that this node represents */
gchar *name; gchar *name;
/* If a watch is subscribed at this point (subscription_count > 0) we can /* If a watch is subscribed at this point (subscription_count > 0) we can
* block its next notification. This is useful because if two watches cover * block its next notification. This is useful because if two watches cover
* the same path, both will trigger when it changes. It also allows changes * the same path, both will trigger when it changes. It also allows changes
* done by the application to be ignored by the watch thread. * done by the application to be ignored by the watch thread.
*/ */
gint32 block_count : 8; gint32 block_count : 8;
/* Number of times g_settings_subscribe has been called for this location /* Number of times g_settings_subscribe has been called for this location
* (I guess you can't subscribe more than 16383 times) */ * (I guess you can't subscribe more than 16383 times) */
gint32 subscription_count : 14; gint32 subscription_count : 14;
gint32 ref_count : 9; gint32 ref_count : 9;
gint32 touched : 1; gint32 touched : 1;
RegistryValue value; RegistryValue value;
} RegistryCacheItem; } RegistryCacheItem;
@ -404,7 +407,9 @@ registry_cache_add_item (GNode *parent,
item->subscription_count = 0; item->subscription_count = 0;
item->block_count = 0; item->block_count = 0;
item->touched = FALSE; item->touched = FALSE;
trace ("\treg cache: adding %s to %s\n", name, ((RegistryCacheItem *)parent->data)->name);
trace ("\treg cache: adding %s to %s\n",
name, ((RegistryCacheItem *)parent->data)->name);
cache_node = g_node_new (item); cache_node = g_node_new (item);
g_node_append (parent, cache_node); g_node_append (parent, cache_node);
@ -440,7 +445,7 @@ registry_cache_ref_tree (GNode *tree)
g_node_children_foreach (tree, G_TRAVERSE_ALL, g_node_children_foreach (tree, G_TRAVERSE_ALL,
(GNodeForeachFunc)_ref_down, NULL); (GNodeForeachFunc)_ref_down, NULL);
for (node=tree->parent; node; node = node->parent) for (node = tree->parent; node; node = node->parent)
{ {
item = node->data; item = node->data;
item->ref_count ++; item->ref_count ++;
@ -451,6 +456,7 @@ static void
_free_cache_item (RegistryCacheItem *item) _free_cache_item (RegistryCacheItem *item)
{ {
trace ("\t -- Free node %s\n", item->name); trace ("\t -- Free node %s\n", item->name);
g_free (item->name); g_free (item->name);
registry_value_free (item->value); registry_value_free (item->value);
g_slice_free (RegistryCacheItem, item); g_slice_free (RegistryCacheItem, item);
@ -462,7 +468,7 @@ _unref_node (GNode *node)
{ {
RegistryCacheItem *item = node->data; RegistryCacheItem *item = node->data;
item->ref_count --; item->ref_count--;
g_warn_if_fail (item->ref_count >= 0); g_warn_if_fail (item->ref_count >= 0);
@ -557,6 +563,7 @@ registry_cache_find_immediate_child (GNode *node,
search.result = NULL; search.result = NULL;
search.name = name; search.name = name;
g_node_traverse (node, G_POST_ORDER, G_TRAVERSE_ALL, 2, g_node_traverse (node, G_POST_ORDER, G_TRAVERSE_ALL, 2,
registry_cache_find_compare, &search); registry_cache_find_compare, &search);
@ -570,8 +577,8 @@ registry_cache_get_node_for_key_recursive (GNode *node,
gint n_parent_watches) gint n_parent_watches)
{ {
RegistryCacheItem *item; RegistryCacheItem *item;
gchar *component = key_name, gchar *component = key_name;
*c = strchr (component, '/'); gchar *c = strchr (component, '/');
GNode *child; GNode *child;
if (c != NULL) if (c != NULL)
@ -599,7 +606,7 @@ registry_cache_get_node_for_key_recursive (GNode *node,
} }
/* We are done if there are no more path components. Allow for a trailing /. */ /* We are done if there are no more path components. Allow for a trailing /. */
if (child==NULL || c == NULL || *(c+1)==0) if (child==NULL || c == NULL || *(c + 1) == 0)
return child; return child;
trace ("get node for key recursive: next: %s.\n", c + 1); trace ("get node for key recursive: next: %s.\n", c + 1);
@ -614,8 +621,8 @@ registry_cache_get_node_for_key (GNode *root,
const gchar *key_name, const gchar *key_name,
gboolean create_if_not_found) gboolean create_if_not_found)
{ {
GNode *child = NULL, GNode *child = NULL;
*result = NULL; GNode *result = NULL;
gchar *component, *c; gchar *component, *c;
g_return_val_if_fail (key_name != NULL, NULL); g_return_val_if_fail (key_name != NULL, NULL);
@ -645,7 +652,7 @@ registry_cache_get_node_for_key (GNode *root,
if (c == NULL) if (c == NULL)
result = root; result = root;
else if (*(c + 1)==0) else if (*(c + 1) == 0)
result = child; result = child;
else if (child != NULL) else if (child != NULL)
result = registry_cache_get_node_for_key_recursive (child, c + 1, result = registry_cache_get_node_for_key_recursive (child, c + 1,
@ -958,7 +965,7 @@ g_registry_backend_write_one (const char *key_name,
{ {
GString *value_string; GString *value_string;
value_string = g_variant_print_string (variant, NULL, FALSE); value_string = g_variant_print_string (variant, NULL, FALSE);
value_data_size = value_string->len+1; value_data_size = value_string->len + 1;
value.ptr = value_data = g_string_free (value_string, FALSE); value.ptr = value_data = g_string_free (value_string, FALSE);
} }
break; break;
@ -1164,18 +1171,22 @@ registry_cache_destroy_tree (GNode *node,
if (item->subscription_count > 0) if (item->subscription_count > 0)
{ {
gint i; gint i;
/* There must be some watches active if this node is a watch point */ /* There must be some watches active if this node is a watch point */
g_warn_if_fail (self->cache_nodes->len > 1); g_warn_if_fail (self->cache_nodes->len > 1);
/* This is a watch point that has been deleted. Let's free the watch! */ /* This is a watch point that has been deleted. Let's free the watch! */
for (i=1; i<self->cache_nodes->len; i++) for (i = 1; i < self->cache_nodes->len; i++)
if (g_ptr_array_index (self->cache_nodes, i) == node) {
break; if (g_ptr_array_index (self->cache_nodes, i) == node)
break;
}
if (i >= self->cache_nodes->len) if (i >= self->cache_nodes->len)
g_warning ("watch thread: a watch point was deleted, but unable to " g_warning ("watch thread: a watch point was deleted, but unable to "
"find '%s' in the list of %i watch nodes\n", item->name, "find '%s' in the list of %i watch nodes\n", item->name,
self->cache_nodes->len-1); self->cache_nodes->len - 1);
else else
{ {
_free_watch (self, i, node); _free_watch (self, i, node);
@ -1373,6 +1384,7 @@ static gboolean
watch_handler (RegistryEvent *event) watch_handler (RegistryEvent *event)
{ {
gint i; gint i;
trace ("Watch handler: got event in %s, items %i.\n", event->prefix, event->items->len); trace ("Watch handler: got event in %s, items %i.\n", event->prefix, event->items->len);
/* GSettings requires us to NULL-terminate the array. */ /* GSettings requires us to NULL-terminate the array. */
@ -1477,9 +1489,11 @@ watch_thread_handle_message (WatchThreadState *self)
gint i; gint i;
for (i = 1; i < self->prefixes->len; i++) for (i = 1; i < self->prefixes->len; i++)
if (strcmp (g_ptr_array_index (self->prefixes, i), {
self->message.watch.prefix) == 0) if (strcmp (g_ptr_array_index (self->prefixes, i),
self->message.watch.prefix) == 0)
break; break;
}
if (i >= self->prefixes->len) if (i >= self->prefixes->len)
{ {
@ -1714,6 +1728,7 @@ watch_stop_unlocked (GRegistryBackend *self)
{ {
WatchThreadState *watch = self->watch; WatchThreadState *watch = self->watch;
DWORD result; DWORD result;
g_return_if_fail (watch != NULL); g_return_if_fail (watch != NULL);
watch->message.type = WATCH_THREAD_STOP; watch->message.type = WATCH_THREAD_STOP;
@ -1844,7 +1859,7 @@ watch_remove_notify (GRegistryBackend *self,
if (g_atomic_int_get (&watch->watches_remaining) >= MAX_WATCHES) if (g_atomic_int_get (&watch->watches_remaining) >= MAX_WATCHES)
/* Stop it before any new ones can get added and confuse things */ /* Stop it before any new ones can get added and confuse things */
watch_stop_unlocked (self); watch_stop_unlocked (self);
else else
LeaveCriticalSection (watch->message_lock); LeaveCriticalSection (watch->message_lock);
} }