Check for resolvconf binary
[connman] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "connman.h"
27
28 int __connman_storage_init(void)
29 {
30         DBG("");
31
32         return 0;
33 }
34
35 void __connman_storage_cleanup(void)
36 {
37         DBG("");
38 }
39
40 static int do_load(GKeyFile *keyfile, struct connman_element *element)
41 {
42         const gchar *value;
43
44         DBG("element %p name %s", element, element->name);
45
46         value = g_key_file_get_string(keyfile, element->path,
47                                                 "Policy", NULL);
48         if (value != NULL)
49                 element->policy = __connman_element_string2policy(value);
50
51         if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
52                 element->remember = g_key_file_get_boolean(keyfile,
53                                         element->path, "Remember", NULL);
54
55         value = g_key_file_get_string(keyfile, element->path,
56                                                 "WiFi.Security", NULL);
57         if (value != NULL)
58                 connman_element_set_property(element,
59                                 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
60
61         value = g_key_file_get_string(keyfile, element->path,
62                                                 "WiFi.Passphrase", NULL);
63         if (value != NULL)
64                 connman_element_set_property(element,
65                                 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
66
67         return 0;
68 }
69
70 int __connman_element_load(struct connman_element *element)
71 {
72         GKeyFile *keyfile;
73         gchar *pathname, *data = NULL;
74         gsize length;
75
76         DBG("element %p name %s", element, element->name);
77
78         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
79         if (pathname == NULL)
80                 return -ENOMEM;
81
82         keyfile = g_key_file_new();
83
84         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
85                 g_free(pathname);
86                 return -ENOENT;
87         }
88
89         g_free(pathname);
90
91         if (g_key_file_load_from_data(keyfile, data, length,
92                                                         0, NULL) == FALSE) {
93                 g_free(data);
94                 return -EILSEQ;
95         }
96
97         g_free(data);
98
99         do_load(keyfile, element);
100
101         g_key_file_free(keyfile);
102
103         return 0;
104 }
105
106 static void do_update(GKeyFile *keyfile, struct connman_element *element)
107 {
108         GSList *list;
109         char *value;
110         const char *str;
111
112         DBG("element %p name %s", element, element->name);
113
114         g_key_file_set_string(keyfile, element->path, "Name", element->name);
115
116         str = __connman_element_policy2string(element->policy);
117         if (str != NULL)
118                 g_key_file_set_string(keyfile, element->path, "Policy", str);
119
120         //g_key_file_set_boolean(keyfile, element->path, "Enabled",
121         //                                              element->enabled);
122
123         if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
124                 g_key_file_set_boolean(keyfile, element->path, "Remember",
125                                                         element->remember);
126
127         __connman_element_lock(element);
128
129         for (list = element->properties; list; list = list->next) {
130                 struct connman_property *property = list->data;
131
132                 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
133                         continue;
134
135                 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
136                         continue;
137
138                 if (property->type == DBUS_TYPE_STRING)
139                         g_key_file_set_string(keyfile, element->path,
140                                         property->name, property->value);
141         }
142
143         __connman_element_unlock(element);
144
145         if (connman_element_get_value(element,
146                         CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
147                 g_key_file_set_string(keyfile, element->path,
148                                                 "WiFi.Security", value);
149
150         if (connman_element_get_value(element,
151                         CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
152                 g_key_file_set_string(keyfile, element->path,
153                                                 "WiFi.Passphrase", value);
154 }
155
156 int __connman_element_store(struct connman_element *element)
157 {
158         GKeyFile *keyfile;
159         gchar *pathname, *data = NULL;
160         gsize length;
161
162         DBG("element %p name %s", element, element->name);
163
164         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
165                                 element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
166                 return -EINVAL;
167
168         if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE ||
169                         element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
170                 return -EINVAL;
171
172         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
173         if (pathname == NULL)
174                 return -ENOMEM;
175
176         keyfile = g_key_file_new();
177
178         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
179                 goto update;
180
181         if (length > 0) {
182                 if (g_key_file_load_from_data(keyfile, data, length,
183                                                         0, NULL) == FALSE)
184                         goto done;
185         }
186
187         g_free(data);
188
189 update:
190         do_update(keyfile, element);
191
192         data = g_key_file_to_data(keyfile, &length, NULL);
193
194         g_file_set_contents(pathname, data, length, NULL);
195
196 done:
197         g_free(data);
198
199         g_key_file_free(keyfile);
200
201         g_free(pathname);
202
203         return 0;
204 }