2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
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.
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.
22 static GKeyFile
*config
;
23 static int config_descr
;
24 static gchar
*data_dir
;
26 /* NULL if key doesn't exist */
27 gchar
*get_option(gchar
*key
, gchar
*group
) {
32 return g_key_file_get_string(config
, group
, key
, NULL
);
35 /* false if key isn't already defined or value is invalid */
36 gboolean
set_option(gchar
*key
, gchar
*value
, gchar
*group
) {
41 if(!g_key_file_get_string(config
, group
, key
, NULL
))
44 //TODO safteycheck value
45 g_key_file_set_string(config
, group
, key
, value
);
49 /* initial load/check */
50 gboolean
config_init() {
52 config
= g_key_file_new();
54 const gchar
*config_path
;
55 GStatBuf
*config_lstat
;
58 config_path
= "/etc/systemd_compat.conf";
59 config_lstat_ret
= g_lstat(config_path
, config_lstat
);
61 /* does conf exist? */
62 if(config_lstat_ret
) {
64 /* if not, can we write to where it should be? */
65 if(g_access("/etc/", W_OK
)) {
66 g_printf("%s\n", "no write permissions for /etc/! exiting..");
71 config_descr
= g_open(config_path
, O_CREAT
, 644);
73 gchar
*posix_hostname
;
74 posix_hostname
= g_malloc(HOST_NAME_MAX
);
76 gethostname(posix_hostname
, HOST_NAME_MAX
);
78 g_key_file_set_string(config
, "hostnamed", "Hostname", posix_hostname
);
79 g_key_file_set_string(config
, "hostnamed", "PrettyHostname", "");
80 g_key_file_set_string(config
, "hostnamed", "IconName", "Computer");
81 g_key_file_set_string(config
, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
83 if(!g_key_file_save_to_file(config
, config_path
, NULL
)) {
84 g_printf("failed to write config to %s!\n", config_path
);
85 g_free(posix_hostname
);
89 g_printf("wrote config to %s\n", config_path
);
91 g_free(posix_hostname
);
95 /* it does exist, read it */
98 if(g_access(config_path
, W_OK
)) {
99 g_printf("%s\n", "no write permissions for /etc/! exiting..");
101 } else if(g_key_file_load_from_file(config
, config_path
, G_KEY_FILE_KEEP_COMMENTS
, NULL
))
104 g_printf("could not read config at %s! exiting..", config_path
);
109 gboolean
init_xml() {
111 const gchar
* const *data_dir_prefix
;
115 data_dir_prefix
= g_get_system_data_dirs();
116 data_dir
= g_strconcat(data_dir_prefix
[0], "systemd_compat", NULL
);
118 xml_lstat_ret
= g_lstat(data_dir
, xml_lstat
);
120 /* does xml dir exist? */
123 /* if not, can we write to where it should be? */
124 if(g_access(data_dir_prefix
[0], W_OK
)) {
125 g_printf("no write permissions for %s! exiting...\n", data_dir_prefix
[0]);
129 g_printf("creating xml data directory %s...\n", data_dir
);
131 if(g_mkdir(data_dir
, 644) || g_access
) {
132 g_printf("failed to create dir %s...\n", data_dir
);
136 /* read in xml files from conf/ */
137 if(!read_xml_from_installconf()) {
138 g_printf("failed to read xml configs in conf/...\n");
142 /* write our configs to system data dir */
143 if(!populate_xml_data_dir()) {
144 g_printf("failed to write xml configs to %s...\n", data_dir
);
148 /* get descriptors from freshly-installed configs */
149 if(!set_xml_descriptors()) {
150 g_printf("failed to fopen xml configs...\n");
156 /* it does exist, read it */
159 if(!set_xml_descriptors()) {
160 g_printf("failed to fopen xml configs...\n");
168 void clean_config() {
170 //TODO g_ptr_array all of this
173 g_close(config_descr
, NULL
);