5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
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.
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.
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
31 #include <connman/storage.h>
35 #define SETTINGS "settings"
36 #define DEFAULT "default.profile"
38 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
39 S_IXGRP | S_IROTH | S_IXOTH)
41 static GKeyFile *storage_load(const char *pathname)
43 GKeyFile *keyfile = NULL;
46 DBG("Loading %s", pathname);
48 keyfile = g_key_file_new();
50 if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
51 DBG("Unable to load %s: %s", pathname, error->message);
52 g_clear_error(&error);
54 g_key_file_free(keyfile);
61 static int storage_save(GKeyFile *keyfile, char *pathname)
68 data = g_key_file_to_data(keyfile, &length, NULL);
70 if (!g_file_set_contents(pathname, data, length, &error)) {
71 DBG("Failed to store information: %s", error->message);
81 static void storage_delete(const char *pathname)
83 DBG("file path %s", pathname);
85 if (unlink(pathname) < 0)
86 connman_error("Failed to remove %s", pathname);
89 GKeyFile *__connman_storage_load_global()
92 GKeyFile *keyfile = NULL;
94 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
98 keyfile = storage_load(pathname);
105 int __connman_storage_save_global(GKeyFile *keyfile)
110 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
114 ret = storage_save(keyfile, pathname);
121 void __connman_storage_delete_global()
125 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
129 storage_delete(pathname);
134 GKeyFile *__connman_storage_load_config(const char *ident)
137 GKeyFile *keyfile = NULL;
139 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
143 keyfile = storage_load(pathname);
150 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
154 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
158 storage_save(keyfile, pathname);
161 void __connman_storage_delete_config(const char *ident)
165 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
169 storage_delete(pathname);
174 GKeyFile *__connman_storage_open_service(const char *service_id)
177 GKeyFile *keyfile = NULL;
179 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
183 keyfile = storage_load(pathname);
191 keyfile = g_key_file_new();
196 gchar **connman_storage_get_services()
202 gchar **services = NULL;
206 dir = opendir(STORAGEDIR);
210 result = g_string_new(NULL);
212 while ((d = readdir(dir))) {
213 if (strcmp(d->d_name, ".") == 0 ||
214 strcmp(d->d_name, "..") == 0 ||
215 strncmp(d->d_name, "provider_", 9) == 0)
221 * If the settings file is not found, then
222 * assume this directory is not a services dir.
224 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
226 ret = stat(str, &buf);
231 g_string_append_printf(result, "%s/", d->d_name);
238 str = g_string_free(result, FALSE);
240 str[strlen(str) - 1] = '\0';
241 services = g_strsplit(str, "/", -1);
248 GKeyFile *connman_storage_load_service(const char *service_id)
251 GKeyFile *keyfile = NULL;
253 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
257 keyfile = storage_load(pathname);
263 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
266 gchar *pathname, *dirname;
268 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
272 /* If the dir doesn't exist, create it */
273 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
274 if(mkdir(dirname, MODE) < 0) {
275 if (errno != EEXIST) {
282 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
286 ret = storage_save(keyfile, pathname);
293 GKeyFile *__connman_storage_load_provider(const char *identifier)
298 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
299 identifier, SETTINGS);
300 if (pathname == NULL)
303 keyfile = storage_load(pathname);
309 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
311 gchar *pathname, *dirname;
313 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
314 "provider", identifier);
318 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
319 mkdir(dirname, MODE) < 0) {
324 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
327 storage_save(keyfile, pathname);
331 gchar **__connman_storage_get_providers(void)
343 dir = opendir(STORAGEDIR);
347 while ((d = readdir(dir))) {
348 if (strcmp(d->d_name, ".") == 0 ||
349 strcmp(d->d_name, "..") == 0 ||
350 strncmp(d->d_name, "provider_", 9) != 0)
353 if (d->d_type == DT_DIR) {
354 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
356 ret = stat(str, &buf);
360 list = g_slist_prepend(list, g_strdup(d->d_name));
367 providers = g_try_new0(char *, num + 1);
368 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
369 if (providers != NULL)
370 providers[i] = iter->data;
381 * This function migrates keys from default.profile to settings file.
382 * This can be removed once the migration is over.
384 void __connman_storage_migrate()
387 GKeyFile *keyfile_def = NULL;
388 GKeyFile *keyfile = NULL;
389 GError *error = NULL;
392 /* If setting file exists, migration has been done. */
393 keyfile = __connman_storage_load_global();
395 g_key_file_free(keyfile);
399 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
403 /* If default.profile exists, create new settings file */
404 keyfile_def = storage_load(pathname);
405 if (keyfile_def == NULL)
408 /* Copy global settings from default.profile to settings. */
409 keyfile = g_key_file_new();
411 val = g_key_file_get_boolean(keyfile_def, "global",
412 "OfflineMode", &error);
414 g_clear_error(&error);
418 g_key_file_set_boolean(keyfile, "global",
421 val = g_key_file_get_boolean(keyfile_def, "WiFi",
424 g_clear_error(&error);
428 g_key_file_set_boolean(keyfile, "WiFi",
431 val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
434 g_clear_error(&error);
438 g_key_file_set_boolean(keyfile, "Bluetooth",
441 val = g_key_file_get_boolean(keyfile_def, "Wired",
444 g_clear_error(&error);
448 g_key_file_set_boolean(keyfile, "Wired",
451 val = g_key_file_get_boolean(keyfile_def, "Cellular",
454 g_clear_error(&error);
458 g_key_file_set_boolean(keyfile, "Cellular",
461 val = g_key_file_get_boolean(keyfile_def, "WiMAX",
464 g_clear_error(&error);
468 g_key_file_set_boolean(keyfile, "WiMAX",
471 __connman_storage_save_global(keyfile);
473 g_key_file_free(keyfile);
475 g_key_file_free(keyfile_def);