From de46ace438056c7e63fcf3527fc9a3e6cc5c976e Mon Sep 17 00:00:00 2001 From: kremlin Date: Mon, 25 Aug 2014 02:03:19 -0500 Subject: [PATCH] add config_[set,get]() in util.c, replace ini-style config configs are now key->value pairs delimited by newlines, seperated by '=' signs. replace all instances of g_key_file ini-like configs --- conf/machine-info | 3 - src/interfaces/hostnamed/hostnamed.c | 63 +++++---------------- src/util.c | 82 ++++++++++++++++++++++++++++ src/util.h | 3 + 4 files changed, 98 insertions(+), 53 deletions(-) diff --git a/conf/machine-info b/conf/machine-info index a06d400..264c7f2 100644 --- a/conf/machine-info +++ b/conf/machine-info @@ -1,6 +1,3 @@ -# XXX this file should not be an ini file; -# it should contain parsable variables -[hostnamed] PRETTY_HOSTNAME= ICON_NAME= CHASSIS= diff --git a/src/interfaces/hostnamed/hostnamed.c b/src/interfaces/hostnamed/hostnamed.c index f3a6372..fb17f4f 100644 --- a/src/interfaces/hostnamed/hostnamed.c +++ b/src/interfaces/hostnamed/hostnamed.c @@ -112,6 +112,7 @@ const gchar *server_archs[] = { static const gchar *DEFAULT_DOMAIN = ".home.network"; static const gchar *OS_HOSTNAME_PATH = "/etc/myname"; +static const gchar *OS_CONFIG_PATH = "/etc/machine-info"; /* --- begin method/property/dbus signal code --- */ @@ -267,7 +268,7 @@ on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, bsd_hostname_try = get_bsd_hostname(STATIC_HOSTNAME); GError *debug_error; if(!bsd_hostname_try || !g_file_set_contents(OS_HOSTNAME_PATH, bsd_hostname_try, -1, &debug_error)) - g_printf("failed to write to %s! are you root?\n", OS_HOSTNAME_PATH); + g_printf("could not to write to %s! are you root?\n", OS_HOSTNAME_PATH); if(bsd_hostname_try) g_free(bsd_hostname_try); @@ -293,9 +294,7 @@ on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, gboolean policykit_auth, ret, try_to_set; size_t check_length; check_auth_result is_authed; - GKeyFile *config; - config = g_key_file_new(); proposed_pretty_hostname = NULL; ret = try_to_set = FALSE; @@ -354,26 +353,11 @@ on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, hostname1_complete_set_pretty_hostname(hn1_passed_interf, invoc); ret = TRUE; - if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL)) { - - g_key_file_set_string(config, "hostnamed", "PRETTY_HOSTNAME", valid_pretty_hostname_buf); - - /* if((computed_static_hostname = g_hostname_to_ascii(PRETTY_HOSTNAME))) { - - g_strdelimit(computed_static_hostname, " ", '-'); - hostname1_set_static_hostname(hn1_passed_interf, computed_static_hostname); - STATIC_HOSTNAME = computed_static_hostname; - g_ptr_array_add(hostnamed_freeable, computed_static_hostname); - g_key_file_set_string(config, "hostnamed", "StaticHostname", computed_static_hostname); - - } */ - } + if(!config_set(OS_CONFIG_PATH, "PRETTY_HOSTNAME", PRETTY_HOSTNAME)) + g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH); } } - g_key_file_save_to_file(config, "/etc/machine-info", NULL); - g_key_file_unref(config); - return ret; } @@ -388,9 +372,7 @@ on_handle_set_chassis(Hostname1 *hn1_passed_interf, const gchar *bus_name; gboolean policykit_auth, ret, try_to_set; check_auth_result is_authed; - GKeyFile *config; - config = g_key_file_new(); proposed_chassis_name = NULL; ret = try_to_set = FALSE; valid_chassis_name_buf = (gchar *)g_malloc0(8192); @@ -448,19 +430,13 @@ on_handle_set_chassis(Hostname1 *hn1_passed_interf, hostname1_set_chassis(hn1_passed_interf, CHASSIS); g_ptr_array_add(hostnamed_freeable, valid_chassis_name_buf); hostname1_complete_set_chassis(hn1_passed_interf, invoc); + ret = TRUE; - if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL)) { - - ret = TRUE; - g_key_file_set_string(config, "hostnamed", "ChassisType", valid_chassis_name_buf); - - } + if(!config_set(OS_CONFIG_PATH, "CHASSIS", valid_chassis_name_buf)) + g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH); } } - g_key_file_save_to_file(config, "/etc/machine-info", NULL); - g_key_file_unref(config); - return ret; } @@ -475,9 +451,7 @@ on_handle_set_icon_name(Hostname1 *hn1_passed_interf, const gchar *bus_name; gboolean policykit_auth, ret, try_to_set; check_auth_result is_authed; - GKeyFile *config; - config = g_key_file_new(); proposed_icon_name = NULL; ret = try_to_set = FALSE; @@ -529,19 +503,14 @@ on_handle_set_icon_name(Hostname1 *hn1_passed_interf, hostname1_set_icon_name(hn1_passed_interf, ICON); g_ptr_array_add(hostnamed_freeable, valid_icon_name_buf); hostname1_complete_set_icon_name(hn1_passed_interf, invoc); + ret = TRUE; - if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL)) { - - ret = TRUE; - g_key_file_set_string(config, "hostnamed", "IconName", valid_icon_name_buf); + if(!config_set(OS_CONFIG_PATH, "ICON_NAME", valid_icon_name_buf)) + g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH); - } } } - g_key_file_save_to_file(config, "/etc/machine-info", NULL); - g_key_file_unref(config); - return ret; } @@ -799,15 +768,12 @@ gboolean set_names() { /* (1) set up */ gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf; - GKeyFile *config; size_t hostname_divider; hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); static_hostname_buf = (gchar*) g_malloc0(4096); pretty_hostname_buf = (gchar*) g_malloc0(4096); - config = g_key_file_new(); - g_ptr_array_add(hostnamed_freeable, hostname_buf); g_ptr_array_add(hostnamed_freeable, static_hostname_buf); g_ptr_array_add(hostnamed_freeable, pretty_hostname_buf); @@ -823,23 +789,20 @@ gboolean set_names() { strncpy(ret, hostname_buf, hostname_divider); */ /* (3) set PRETTY_HOSTNAME */ - if(g_key_file_load_from_file(config, "/etc/machine-info", G_KEY_FILE_NONE, NULL) - && (pretty_hostname_buf = g_key_file_get_value(config, "hostnamed", "PRETTY_HOSTNAME", NULL))) + if((pretty_hostname_buf = config_get(OS_CONFIG_PATH, "PRETTY_HOSTNAME"))) PRETTY_HOSTNAME = pretty_hostname_buf; + else PRETTY_HOSTNAME = ""; /* (4) set STATIC_HOSTNAME */ - if((static_hostname_buf = g_key_file_get_value(config, "hostnamed", "STATIC_HOSTNAME", NULL))) + if(!gethostname(static_hostname_buf, MAXHOSTNAMELEN)) STATIC_HOSTNAME = static_hostname_buf; else STATIC_HOSTNAME = ""; - if(config) - g_key_file_unref(config); - return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE; } diff --git a/src/util.c b/src/util.c index f228c9a..bb5670a 100644 --- a/src/util.c +++ b/src/util.c @@ -24,6 +24,88 @@ #include "util.h" +const gint MAX_TOKENS = 20; + +/* return must be g_free()'d */ +gchar *config_get(const gchar *path, gchar *key) { + + gchar *content, **split_content, *cur, **cur_split, *ret; + GError *err; + int incr = 0; + gboolean breaker = TRUE; + + ret = (gchar *) g_malloc0(4096); + + if(!g_file_get_contents(path, &content, NULL, &err)) + return NULL; + + split_content = g_strsplit(content, "\n", MAX_TOKENS); + + while(breaker && (cur = split_content[incr]) && (cur_split = g_strsplit(cur, "=", 2))) { + + if(!g_strcmp0(key, cur_split[0])) { + + g_strlcpy(ret, cur_split[1], 2048); + breaker = FALSE; + } + + incr++; + g_strfreev(cur_split); + } + + if(split_content) + g_strfreev(split_content); + if(content) + g_free(content); + + return (ret ? ret : NULL); +} + +gboolean config_set(const gchar *path, gchar *key, gchar *value) { + + gchar *content, **split_content, *cur, **cur_split, *rewrite; + GError *err_set, *err_get; + gboolean ret = FALSE; + int incr = 0; + gboolean breaker = TRUE; + + err_get = err_set = NULL; + + if(!g_file_get_contents(path, &content, NULL, &err_get)) + return FALSE; + + split_content = g_strsplit(content, "\n", MAX_TOKENS); + + while(breaker && (cur = split_content[incr]) && (cur_split = g_strsplit(cur, "=", 2))) { + + if(!g_strcmp0(key, cur_split[0])) { + + cur_split[1] = value; + split_content[incr] = g_strjoinv("=", cur_split); + ret = TRUE; + breaker = FALSE; + } + + incr++; + } + + if(ret) { + + rewrite = g_strjoinv("\n", split_content); + ret = g_file_set_contents(path, rewrite, -1, &err_set); + g_free(rewrite); + } + + if(cur_split) + g_strfreev(cur_split); + if(split_content) + g_strfreev(split_content); + if(content) + g_free(content); + + return ret; +} + static gboolean is_valid_action(GList *action_list, const gchar *action) { PolkitActionDescription *action_descr; diff --git a/src/util.h b/src/util.h index 5775fb4..c80dc07 100644 --- a/src/util.h +++ b/src/util.h @@ -21,3 +21,6 @@ typedef enum { } check_auth_result; check_auth_result polkit_try_auth(const gchar *bus, const gchar *action, gboolean prompt); + +gchar * config_get(const gchar *path, gchar *key); +gboolean config_set(const gchar *path, gchar *key, gchar *value); -- 2.41.0