Don't store fake or network device elements
[platform/upstream/connman.git] / 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                                                 "WiFi.Security", NULL);
48         if (value != NULL)
49                 connman_element_set_property(element,
50                                 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
51
52         value = g_key_file_get_string(keyfile, element->path,
53                                                 "WiFi.Passphrase", NULL);
54         if (value != NULL)
55                 connman_element_set_property(element,
56                                 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
57
58         return 0;
59 }
60
61 int __connman_element_load(struct connman_element *element)
62 {
63         GKeyFile *keyfile;
64         gchar *pathname, *data = NULL;
65         gsize length;
66
67         DBG("element %p name %s", element, element->name);
68
69         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
70         if (pathname == NULL)
71                 return -ENOMEM;
72
73         keyfile = g_key_file_new();
74
75         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
76                 g_free(pathname);
77                 return -ENOENT;
78         }
79
80         g_free(pathname);
81
82         if (g_key_file_load_from_data(keyfile, data, length,
83                                                         0, NULL) == FALSE) {
84                 g_free(data);
85                 return -EILSEQ;
86         }
87
88         g_free(data);
89
90         do_load(keyfile, element);
91
92         g_key_file_free(keyfile);
93
94         return 0;
95 }
96
97 static void do_update(GKeyFile *keyfile, struct connman_element *element)
98 {
99         GSList *list;
100         char *value;
101
102         DBG("element %p name %s", element, element->name);
103
104         g_key_file_set_string(keyfile, element->path, "Name", element->name);
105
106         g_key_file_set_boolean(keyfile, element->path, "Enabled",
107                                                         element->enabled);
108
109         __connman_element_lock(element);
110
111         for (list = element->properties; list; list = list->next) {
112                 struct connman_property *property = list->data;
113
114                 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
115                         continue;
116
117                 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
118                         continue;
119
120                 if (property->type == DBUS_TYPE_STRING)
121                         g_key_file_set_string(keyfile, element->path,
122                                         property->name, property->value);
123         }
124
125         __connman_element_unlock(element);
126
127         if (connman_element_get_value(element,
128                         CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
129                 g_key_file_set_string(keyfile, element->path,
130                                                 "WiFi.Security", value);
131
132         if (connman_element_get_value(element,
133                         CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
134                 g_key_file_set_string(keyfile, element->path,
135                                                 "WiFi.Passphrase", value);
136 }
137
138 int __connman_element_store(struct connman_element *element)
139 {
140         GKeyFile *keyfile;
141         gchar *pathname, *data = NULL;
142         gsize length;
143
144         DBG("element %p name %s", element, element->name);
145
146         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
147                                 element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
148                 return -EINVAL;
149
150         if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE ||
151                         element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
152                 return -EINVAL;
153
154         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
155         if (pathname == NULL)
156                 return -ENOMEM;
157
158         keyfile = g_key_file_new();
159
160         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
161                 goto update;
162
163         if (length > 0) {
164                 if (g_key_file_load_from_data(keyfile, data, length,
165                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
166                         goto done;
167         }
168
169         g_free(data);
170
171 update:
172         do_update(keyfile, element);
173
174         data = g_key_file_to_data(keyfile, &length, NULL);
175
176         g_file_set_contents(pathname, data, length, NULL);
177
178 done:
179         g_free(data);
180
181         g_key_file_free(keyfile);
182
183         g_free(pathname);
184
185         return 0;
186 }