From 456eeb47ee9874693d18df121eecd2d16187bd06 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Wed, 17 Apr 2013 08:48:12 +0200 Subject: [PATCH 1/3] User: hold on to our keyfile When updating a User object from keyfile, keep reference on the keyfile object passed in and modify the Daemon not to destroy it. When saving a User, instead of creating a new keyfile, reuse the one we stored on the object. We still record the properties from the User object into the keyfile in the previous way (and include a modification to clear out the 'User' group before doing this). The intention here is to allow other groups stored in the keyfile to be preserved. These other groups are likely to correspond to extension interfaces (which will be introduced in future patches). An alternative approach would have been to only preserve groups for extensions that we currently have loaded but this was abandoned as being excessively brittle since the result of a temporarily missing extension file would be the destruction of data. https://bugs.freedesktop.org/show_bug.cgi?id=63733 --- src/daemon.c | 2 +- src/user.c | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index ec5cc82..81dcbde 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -491,7 +491,7 @@ entry_generator_cachedir (GHashTable *users, key_file = g_key_file_new (); if (g_key_file_load_from_file (key_file, filename, 0, NULL)) user_update_from_keyfile (user, key_file); - g_key_file_free (key_file); + g_key_file_unref (key_file); g_free (filename); } diff --git a/src/user.c b/src/user.c index b71ee02..ec11876 100644 --- a/src/user.c +++ b/src/user.c @@ -78,6 +78,8 @@ struct User { Daemon *daemon; + GKeyFile *keyfile; + uid_t uid; gid_t gid; gchar *user_name; @@ -360,6 +362,9 @@ user_update_from_keyfile (User *user, } } + g_clear_pointer (&user->keyfile, g_key_file_unref); + user->keyfile = g_key_file_ref (keyfile); + g_object_thaw_notify (G_OBJECT (user)); } @@ -387,6 +392,8 @@ static void user_save_to_keyfile (User *user, GKeyFile *keyfile) { + g_key_file_remove_group (keyfile, "User", NULL); + if (user->email) g_key_file_set_string (keyfile, "User", "Email", user->email); @@ -412,15 +419,13 @@ static void save_extra_data (User *user) { gchar *filename; - GKeyFile *keyfile; gchar *data; GError *error; - keyfile = g_key_file_new (); - user_save_to_keyfile (user, keyfile); + user_save_to_keyfile (user, user->keyfile); error = NULL; - data = g_key_file_to_data (keyfile, NULL, &error); + data = g_key_file_to_data (user->keyfile, NULL, &error); if (error == NULL) { filename = g_build_filename (USERDIR, user->user_name, @@ -433,7 +438,6 @@ save_extra_data (User *user) user->user_name, error->message); g_error_free (error); } - g_key_file_free (keyfile); } static void @@ -1816,6 +1820,8 @@ user_finalize (GObject *object) user = USER (object); + g_clear_pointer (&user->keyfile, g_key_file_unref); + g_free (user->object_path); g_free (user->user_name); g_free (user->real_name); @@ -2034,4 +2040,5 @@ user_init (User *user) user->automatic_login = FALSE; user->system_account = FALSE; user->login_history = NULL; + user->keyfile = g_key_file_new (); } -- 1.8.2.1