86f14c9082baddb9e7bce67fc62f764a76b34f4b
[petitboot] / lib / pb-config / pb-config.c
1
2 #include <string.h>
3
4 #include <log/log.h>
5 #include <types/types.h>
6 #include <talloc/talloc.h>
7
8 #include "pb-config.h"
9
10 static struct interface_config *config_copy_interface(struct config *ctx,
11                 struct interface_config *src)
12 {
13         struct interface_config *dest = talloc_zero(ctx,
14                                                 struct interface_config);
15
16         memcpy(dest->hwaddr, src->hwaddr, sizeof(src->hwaddr));
17         dest->ignore = src->ignore;
18
19         if (dest->ignore)
20                 return dest;
21
22         dest->method = src->method;
23
24         switch (src->method) {
25         case CONFIG_METHOD_DHCP:
26                 break;
27         case CONFIG_METHOD_STATIC:
28                 dest->static_config.address =
29                         talloc_strdup(dest, src->static_config.address);
30                 dest->static_config.gateway =
31                         talloc_strdup(dest, src->static_config.gateway);
32                 dest->static_config.url =
33                         talloc_strdup(dest, src->static_config.url);
34                 break;
35         }
36         dest->override = src->override;
37
38         return dest;
39 }
40
41 struct config *config_copy(void *ctx, const struct config *src)
42 {
43         struct config *dest;
44         unsigned int i;
45
46         dest = talloc_zero(ctx, struct config);
47         dest->autoboot_enabled = src->autoboot_enabled;
48         dest->autoboot_timeout_sec = src->autoboot_timeout_sec;
49         dest->safe_mode = src->safe_mode;
50
51         dest->network.n_interfaces = src->network.n_interfaces;
52         dest->network.interfaces = talloc_array(dest, struct interface_config *,
53                                         dest->network.n_interfaces);
54         dest->network.n_dns_servers = src->network.n_dns_servers;
55         dest->network.dns_servers = talloc_array(dest, const char *,
56                                         dest->network.n_dns_servers);
57
58         for (i = 0; i < src->network.n_interfaces; i++)
59                 dest->network.interfaces[i] = config_copy_interface(dest,
60                                 src->network.interfaces[i]);
61
62         for (i = 0; i < src->network.n_dns_servers; i++)
63                 dest->network.dns_servers[i] = talloc_strdup(dest,
64                                 src->network.dns_servers[i]);
65
66         dest->n_autoboot_opts = src->n_autoboot_opts;
67         dest->autoboot_opts = talloc_array(dest, struct autoboot_option,
68                                         dest->n_autoboot_opts);
69
70         for (i = 0; i < src->n_autoboot_opts; i++) {
71                 dest->autoboot_opts[i].boot_type =
72                         src->autoboot_opts[i].boot_type;
73                 if (src->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
74                         dest->autoboot_opts[i].type =
75                                 src->autoboot_opts[i].type;
76                 else
77                         dest->autoboot_opts[i].uuid =
78                                 talloc_strdup(dest, src->autoboot_opts[i].uuid);
79         }
80
81         dest->ipmi_bootdev = src->ipmi_bootdev;
82         dest->ipmi_bootdev_persistent = src->ipmi_bootdev_persistent;
83
84         dest->allow_writes = src->allow_writes;
85
86         dest->n_consoles = src->n_consoles;
87         if (src->consoles)
88                 dest->consoles = talloc_array(dest, char *, src->n_consoles);
89         for (i = 0; i < src->n_consoles && src->n_consoles; i++)
90                 dest->consoles[i] = talloc_strdup(dest->consoles,
91                                                 src->consoles[i]);
92
93         if (src->boot_console)
94                 dest->boot_console = talloc_strdup(dest, src->boot_console);
95
96         if (src->lang && strlen(src->lang))
97                 dest->lang = talloc_strdup(dest, src->lang);
98         else
99                 dest->lang = NULL;
100
101         return dest;
102 }