| 1 | #include <gio/gio.h> |
| 2 | #include "xml_defs.h" |
| 3 | |
| 4 | //expects valid input, always free() output |
| 5 | const gchar **get_prefixs(gchar *path, gchar *dname) { |
| 6 | gsize size = sizeof(path) + sizeof(dname) + 11; //11 extra bytes for file suffix |
| 7 | const gchar **ret = (gchar**) g_malloc0(2 * size); |
| 8 | |
| 9 | g_snprintf(ret[0], size, "%s%s-ispect.xml", path, dname); |
| 10 | g_snprintf(ret[1], size, "%s%s-config.xml", path, dname); |
| 11 | |
| 12 | return ret; |
| 13 | } |
| 14 | |
| 15 | const GString *strjoin(gchar *sep, GString strings[]) { |
| 16 | GString *cur; |
| 17 | GString *out; |
| 18 | gint n; |
| 19 | |
| 20 | while((cur = &strings[n]) != NULL) { |
| 21 | g_string_append(out, cur); |
| 22 | n++; |
| 23 | } |
| 24 | |
| 25 | return out; |
| 26 | } |
| 27 | |
| 28 | gboolean install_conf() { |
| 29 | gchar *our_conf_uri = "systemd-utl/xml-conf/"; |
| 30 | gchar **data_dir = g_get_system_data_dirs(); |
| 31 | |
| 32 | if(g_strcmp0(data_dir[0], NULL)) { |
| 33 | const gchar our_conf_path[256]; |
| 34 | g_snprintf(our_conf_path, sizeof our_conf_path, "%s%s", *data_dir, our_conf_uri); |
| 35 | GError **errors; |
| 36 | |
| 37 | /*TODO permissions w/ this */ |
| 38 | g_mkdir_with_parents(our_conf_path, 0644); |
| 39 | |
| 40 | const gchar **hostnamed_prefixs = get_prefixs(our_conf_path, "hostnamed"); |
| 41 | const GString *hostnamed_ispect_xml = strjoin("\n", hostnamed_ispect_xml_def); |
| 42 | const GString *hostnamed_config_xml = strjoin("\n", hostnamed_config_xml_def); |
| 43 | |
| 44 | const gchar **timedated_prefixs = get_prefixs(our_conf_path, "timedated"); |
| 45 | const GString *timedated_ispect_xml = strjoin("\n", timedated_ispect_xml_def); |
| 46 | const GString *timedated_config_xml = strjoin("\n", timedated_config_xml_def); |
| 47 | |
| 48 | const gchar **localed_prefixs = get_prefixs(our_conf_path, "localed"); |
| 49 | const GString *localed_ispect_xml = strjoin("\n", localed_ispect_xml_def); |
| 50 | const GString *localed_config_xml = strjoin("\n", localed_config_xml_def); |
| 51 | |
| 52 | const gchar **logind_prefixs = get_prefixs(our_conf_path, "logind"); |
| 53 | const GString *logind_ispect_xml = strjoin("\n", logind_ispect_xml_def); |
| 54 | const GString *logind_config_xml = strjoin("\n", logind_config_xml_def); |
| 55 | |
| 56 | g_file_set_contents(*hostnamed_prefixs[0], hostnamed_config_xml, -1, errors); |
| 57 | g_file_set_contents(*hostnamed_prefixs[1], hostnamed_config_xml, -1, errors); |
| 58 | |
| 59 | g_file_set_contents(*timedated_prefixs[0], timedated_config_xml, -1, errors); |
| 60 | g_file_set_contents(*timedated_prefixs[1], timedated_config_xml, -1, errors); |
| 61 | |
| 62 | g_file_set_contents(*localed_prefixs[0], localed_config_xml, -1, errors); |
| 63 | g_file_set_contents(*localed_prefixs[1], localed_config_xml, -1, errors); |
| 64 | |
| 65 | g_file_set_contents(*logind_prefixs[0], logind_config_xml, -1, errors); |
| 66 | g_file_set_contents(*logind_prefixs[1], logind_config_xml, -1, errors); |
| 67 | |
| 68 | /* TODO: free() strings */ |
| 69 | |
| 70 | return (errors[0] == NULL); |
| 71 | |
| 72 | } else |
| 73 | return FALSE; |
| 74 | } |