clean up config.c
[systembsd.git] / src / config.c
CommitLineData
3b82e3c1 1/*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
36575bff 17#include <unistd.h>
18#include <fcntl.h>
19#include <gio/gio.h>
d90aa67d 20#include "config.h"
36575bff 21
22static GKeyFile *config;
d099276b 23static int config_descr;
5b435dd1 24static gchar *data_dir;
25
36575bff 26/* NULL if key doesn't exist */
27gchar *get_option(gchar *key, gchar *group) {
28
1cd5e6fe 29 if(!group)
30 group = "default";
36575bff 31
1cd5e6fe 32 return g_key_file_get_string(config, group, key, NULL);
36575bff 33}
34
35/* false if key isn't already defined or value is invalid */
36gboolean set_option(gchar *key, gchar *value, gchar *group) {
37
1cd5e6fe 38 if(!group)
39 group = "default";
36575bff 40
1cd5e6fe 41 if(!g_key_file_get_string(config, group, key, NULL))
42 return FALSE;
36575bff 43
1cd5e6fe 44 //TODO safteycheck value
45 g_key_file_set_string(config, group, key, value);
46 return TRUE;
36575bff 47}
48
49/* initial load/check */
36575bff 50gboolean config_init() {
51
7f67c069 52 /* config is already set */
53 if(config)
54 return TRUE;
55
1cd5e6fe 56 config = g_key_file_new();
57
58 const gchar *config_path;
59 GStatBuf *config_lstat;
7f67c069 60 int config_lstat_ret;
39df6847 61
1cd5e6fe 62 config_path = "/etc/systemd_compat.conf";
7f67c069 63 config_lstat_ret = g_lstat(config_path, config_lstat);
39df6847 64
1cd5e6fe 65 /* does conf exist? */
d90aa67d 66 if(config_lstat_ret) {
39df6847 67
d90aa67d 68 /* if not, can we write to where it should be? */
1cd5e6fe 69 if(g_access("/etc/", W_OK)) {
70 g_printf("%s\n", "no write permissions for /etc/! exiting..");
71 return FALSE;
72 }
39df6847 73
7f67c069 74 int config_descr; /*TODO add to descr array for sigints, etc */
1cd5e6fe 75 config_descr = g_open(config_path, O_CREAT, 644);
39df6847 76
1cd5e6fe 77 gchar *posix_hostname;
78 posix_hostname = g_malloc(HOST_NAME_MAX);
39df6847 79
1cd5e6fe 80 gethostname(posix_hostname, HOST_NAME_MAX);
39df6847 81
1cd5e6fe 82 g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname);
83 g_key_file_set_string(config, "hostnamed", "PrettyHostname", "");
84 g_key_file_set_string(config, "hostnamed", "IconName", "Computer");
7f67c069 85 g_key_file_set_string(config, "hostnamed", "ChassisType","laptop"); /*TODO set these correctly */
39df6847 86
1cd5e6fe 87 if(!g_key_file_save_to_file(config, config_path, NULL)) {
88 g_printf("failed to write config to %s!\n", config_path);
89 g_free(posix_hostname);
90 return FALSE;
91 }
39df6847 92
1cd5e6fe 93 g_printf("wrote config to %s\n", config_path);
39df6847 94
1cd5e6fe 95 g_free(posix_hostname);
39df6847 96
1cd5e6fe 97 return TRUE;
36575bff 98
1cd5e6fe 99 /* it does exist, read it */
100 } else {
36575bff 101
881cd13b 102 if(g_access(config_path, W_OK)) {
1cd5e6fe 103 g_printf("%s\n", "no write permissions for /etc/! exiting..");
104 return FALSE;
105 } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL))
106 return TRUE;
36575bff 107
1cd5e6fe 108 g_printf("could not read config at %s! exiting..", config_path);
109 return FALSE;
110 }
446ae274 111}
8f18585d 112
113gboolean init_xml() {
114
7f67c069 115 const gchar * const *data_dir_prefix;
116 int xml_lstat_ret;
117 GStatBuf *xml_lstat;
118
119 data_dir_prefix = g_get_system_data_dirs();
120 data_dir = g_strconcat(data_dir_prefix[0], "systemd_compat", NULL);
121
122 xml_lstat_ret = g_lstat(data_dir, xml_lstat);
123
124 /* does xml dir exist? */
125 if(xml_lstat_ret) {
126
127 /* if not, can we write to where it should be? */
128 if(g_access(data_dir_prefix[0], W_OK)) {
129 g_printf("no write permissions for %s! exiting...\n", data_dir_prefix[0]);
130 return FALSE;
131 }
132
133 g_printf("creating xml data directory %s...\n", data_dir);
134
135 if(g_mkdir(data_dir, 644) || g_access) {
136 g_printf("failed to create dir %s...\n", data_dir);
137 return FALSE;
138 }
139
140 /* read in xml files from conf/ */
141 if(!read_xml_from_installconf()) {
142 g_printf("failed to read xml configs in conf/...\n");
143 return FALSE;
144 }
145
146 /* write our configs to system data dir */
147 if(!populate_xml_data_dir()) {
148 g_printf("failed to write xml configs to %s...\n", data_dir);
149 return FALSE;
150 }
151
152 /* get descriptors from freshly-installed configs */
153 if(!set_xml_descriptors()) {
154 g_printf("failed to fopen xml configs...\n");
155 return FALSE;
156 }
157
158 return TRUE;
159
160 /* it does exist, read it */
161 } else {
162
163 if(!set_xml_descriptors()) {
164 g_printf("failed to fopen xml configs...\n");
165 return FALSE;
166 }
167
168 return TRUE;
169 }
8f18585d 170}
171
172void clean_config() {
173
7f67c069 174 //TODO g_ptr_array all of this
175 g_free(config);
176 g_free(data_dir);
177 g_close(config_descr, NULL);
446ae274 178}