retab
[systembsd.git] / src / config.c
CommitLineData
baa40e28 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
1c38000a 17#include <unistd.h>
18#include <fcntl.h>
19#include <gio/gio.h>
e0453635 20#include "config.h"
1c38000a 21
22static GKeyFile *config;
7d10ea85 23static int config_descr;
4241079f 24static gchar *data_dir;
25
1c38000a 26/* NULL if key doesn't exist */
27gchar *get_option(gchar *key, gchar *group) {
28
fbc0295f 29 if(!group)
30 group = "default";
1c38000a 31
fbc0295f 32 return g_key_file_get_string(config, group, key, NULL);
1c38000a 33}
34
35/* false if key isn't already defined or value is invalid */
36gboolean set_option(gchar *key, gchar *value, gchar *group) {
37
fbc0295f 38 if(!group)
39 group = "default";
1c38000a 40
fbc0295f 41 if(!g_key_file_get_string(config, group, key, NULL))
42 return FALSE;
1c38000a 43
6981850a 44 /* TODO safteycheck value */
fbc0295f 45 g_key_file_set_string(config, group, key, value);
46 return TRUE;
1c38000a 47}
48
49/* initial load/check */
1c38000a 50gboolean config_init() {
51
8636b04c 52 /* config is already set */
53 if(config)
54 return TRUE;
55
fbc0295f 56 config = g_key_file_new();
57
58 const gchar *config_path;
59 GStatBuf *config_lstat;
8636b04c 60 int config_lstat_ret;
c3b84b0a 61
fbc0295f 62 config_path = "/etc/systemd_compat.conf";
8636b04c 63 config_lstat_ret = g_lstat(config_path, config_lstat);
c3b84b0a 64
94dc7ef0 65 /* this should all be handled in the makefile
36961b20 66 * does conf exist?
e0453635 67 if(config_lstat_ret) {
c3b84b0a 68
fbc0295f 69 if(g_access("/etc/", W_OK)) {
70 g_printf("%s\n", "no write permissions for /etc/! exiting..");
71 return FALSE;
72 }
c3b84b0a 73
36961b20 74 int config_descr;
fbc0295f 75 config_descr = g_open(config_path, O_CREAT, 644);
c3b84b0a 76
fbc0295f 77 gchar *posix_hostname;
78 posix_hostname = g_malloc(HOST_NAME_MAX);
c3b84b0a 79
fbc0295f 80 gethostname(posix_hostname, HOST_NAME_MAX);
c3b84b0a 81
fbc0295f 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");
36961b20 85 g_key_file_set_string(config, "hostnamed", "ChassisType","laptop");
c3b84b0a 86
fbc0295f 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 }
c3b84b0a 92
fbc0295f 93 g_printf("wrote config to %s\n", config_path);
c3b84b0a 94
fbc0295f 95 g_free(posix_hostname);
c3b84b0a 96
36961b20 97 return TRUE;
1c38000a 98
36961b20 99 } else { */
1c38000a 100
36961b20 101 if(g_access(config_path, W_OK)) {
1c38000a 102
94dc7ef0 103 g_printf("%s\n", "no write permissions for /etc/! exiting..");
fbc0295f 104 return FALSE;
94304e78 105
36961b20 106 } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL))
107 return TRUE;
108
109 g_printf("could not read config at %s! exiting..", config_path);
110 return FALSE;
111}
112/* this definitely does not need to exist
94304e78 113gboolean init_xml() {
114
8636b04c 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
8636b04c 124 if(xml_lstat_ret) {
125
8636b04c 126 if(g_access(data_dir_prefix[0], W_OK)) {
127 g_printf("no write permissions for %s! exiting...\n", data_dir_prefix[0]);
128 return FALSE;
129 }
130
131 g_printf("creating xml data directory %s...\n", data_dir);
132
133 if(g_mkdir(data_dir, 644) || g_access) {
134 g_printf("failed to create dir %s...\n", data_dir);
135 return FALSE;
136 }
137
8636b04c 138 if(!read_xml_from_installconf()) {
139 g_printf("failed to read xml configs in conf/...\n");
140 return FALSE;
141 }
142
8636b04c 143 if(!populate_xml_data_dir()) {
144 g_printf("failed to write xml configs to %s...\n", data_dir);
145 return FALSE;
146 }
147
8636b04c 148 if(!set_xml_descriptors()) {
149 g_printf("failed to fopen xml configs...\n");
150 return FALSE;
151 }
152
153 return TRUE;
154
8636b04c 155 } else {
156
157 if(!set_xml_descriptors()) {
158 g_printf("failed to fopen xml configs...\n");
159 return FALSE;
160 }
161
162 return TRUE;
163 }
36961b20 164} */
94304e78 165
166void clean_config() {
167
6981850a 168 /* TODO g_ptr_array all of this */
8636b04c 169 g_free(config);
170 g_free(data_dir);
171 g_close(config_descr, NULL);
e3756876 172}