add config_[set,get]() in util.c, replace ini-style config
authorkremlin <kremlin@juliana.kremlin.cc>
Mon, 25 Aug 2014 07:03:19 +0000 (02:03 -0500)
committerkremlin <kremlin@juliana.kremlin.cc>
Mon, 25 Aug 2014 07:03:19 +0000 (02:03 -0500)
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
src/interfaces/hostnamed/hostnamed.c
src/util.c
src/util.h

index a06d400abe8d999485c7f65bdfc99e85c07123f3..264c7f2dd2ef058d521c4cb80b438f7b4252445e 100644 (file)
@@ -1,6 +1,3 @@
-# XXX this file should not be an ini file;
-# it should contain parsable variables
-[hostnamed]
 PRETTY_HOSTNAME=
 ICON_NAME=
 CHASSIS=
index f3a6372a979ae00dbec2693bfbd7933371c747fe..fb17f4f80a764aaf4d69fa2d2097cd31bd783e7c 100644 (file)
@@ -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;
 }
 
index f228c9a941d044f37d4fdd3612e1c0037771af6a..bb5670a18d2ebcd8762197f60ab9769cae934d9e 100644 (file)
 
 #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;
index 5775fb406fc092e9ad1dec4301cfdf08405a3ec5..c80dc07e031568d332a60cc16dd01e574ec50b0d 100644 (file)
@@ -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);