36575bff |
1 | #include <unistd.h> |
2 | #include <fcntl.h> |
3 | #include <gio/gio.h> |
4 | |
5 | static GKeyFile *config; |
d099276b |
6 | static int config_descr; |
36575bff |
7 | |
8 | static const gchar *CONFIG_KEYS[] = { |
9 | "PrettyHostname", |
10 | "IconName", |
11 | "ChassisType" |
12 | }; |
13 | |
14 | /* NULL if key doesn't exist */ |
15 | gchar *get_option(gchar *key, gchar *group) { |
16 | |
17 | if(!group) |
18 | group = "default"; |
19 | |
20 | return g_key_file_get_string(config, group, key, NULL); |
21 | } |
22 | |
23 | /* false if key isn't already defined or value is invalid */ |
24 | gboolean set_option(gchar *key, gchar *value, gchar *group) { |
25 | |
26 | if(!group) |
27 | group = "default"; |
28 | |
29 | if(!g_key_file_get_string(config, group, key, NULL)) |
30 | return FALSE; |
31 | |
32 | //TODO safteycheck value |
33 | g_key_file_set_string(config, group, key, value); |
34 | return TRUE; |
35 | } |
36 | |
37 | /* initial load/check */ |
36575bff |
38 | gboolean config_init() { |
39 | |
36575bff |
40 | if(config) |
39df6847 |
41 | return TRUE; //already init'd |
42 | |
43 | config = g_key_file_new(); |
44 | |
45 | const gchar *config_path; |
46 | GStatBuf *config_lstat; |
47 | |
48 | config_path = "/etc/systemd_compat.conf"; |
49 | |
50 | /* does conf exist? */ |
51 | if(g_lstat(config_path, config_lstat)) { |
52 | |
53 | /* if not, can we write it */ |
54 | if(g_access("/etc/", W_OK)) { |
55 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
56 | return FALSE; |
57 | } |
58 | |
39df6847 |
59 | config_descr = g_open(config_path, O_CREAT, 644); |
60 | |
61 | gchar *posix_hostname; |
62 | posix_hostname = g_malloc(255); |
63 | |
64 | gethostname(posix_hostname, 255); |
65 | |
66 | g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname); |
67 | g_key_file_set_string(config, "hostnamed", "PrettyHostname", ""); |
68 | g_key_file_set_string(config, "hostnamed", "IconName", "Computer"); |
69 | g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly |
70 | |
71 | if(!g_key_file_save_to_file(config, config_path, NULL)) { |
72 | g_printf("failed to write config to %s!\n", config_path); |
73 | g_free(posix_hostname); |
74 | return FALSE; |
75 | } |
76 | |
77 | g_printf("wrote config to %s\n", config_path); |
78 | |
79 | g_free(posix_hostname); |
80 | |
36575bff |
81 | return TRUE; |
82 | |
39df6847 |
83 | /* it does exist, read it */ |
84 | } else { |
36575bff |
85 | |
39df6847 |
86 | if(!g_access(config_path, W_OK)) { |
87 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
88 | return FALSE; |
d099276b |
89 | } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) { |
90 | config_descr = g_open(config_path, O_RDWR, 644); |
36575bff |
91 | return TRUE; |
d099276b |
92 | } |
36575bff |
93 | |
39df6847 |
94 | g_printf("could not read config at %s! exiting..", config_path); |
95 | return FALSE; |
96 | } |
36575bff |
97 | } |
d099276b |
98 | |
99 | void clean_config() { |
100 | |
101 | if(config) |
102 | g_free(config); |
103 | |
104 | if(config_descr) |
105 | g_close(config_descr, NULL); |
106 | |
107 | } |