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 | |
22 | static GKeyFile *config; |
7d10ea85 |
23 | static int config_descr; |
4241079f |
24 | static gchar *data_dir; |
25 | |
1c38000a |
26 | /* NULL if key doesn't exist */ |
27 | gchar *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 */ |
36 | gboolean 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 |
50 | gboolean 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 | |
36961b20 |
65 | if(g_access(config_path, W_OK)) { |
1c38000a |
66 | |
94dc7ef0 |
67 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
fbc0295f |
68 | return FALSE; |
94304e78 |
69 | |
36961b20 |
70 | } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) |
71 | return TRUE; |
72 | |
73 | g_printf("could not read config at %s! exiting..", config_path); |
74 | return FALSE; |
75 | } |
94304e78 |
76 | |
77 | void clean_config() { |
78 | |
6981850a |
79 | /* TODO g_ptr_array all of this */ |
8636b04c |
80 | g_free(config); |
81 | g_free(data_dir); |
82 | g_close(config_descr, NULL); |
e3756876 |
83 | } |