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(void)
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(void)
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 GKeyFile *__connman_storage_open_service(const char *service_id)
153 GKeyFile *keyfile = NULL;
155 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
159 keyfile = storage_load(pathname);
167 keyfile = g_key_file_new();
172 gchar **connman_storage_get_services(void)
178 gchar **services = NULL;
182 dir = opendir(STORAGEDIR);
186 result = g_string_new(NULL);
188 while ((d = readdir(dir))) {
189 if (strcmp(d->d_name, ".") == 0 ||
190 strcmp(d->d_name, "..") == 0 ||
191 strncmp(d->d_name, "provider_", 9) == 0)
198 * If the settings file is not found, then
199 * assume this directory is not a services dir.
201 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
203 ret = stat(str, &buf);
208 g_string_append_printf(result, "%s/", d->d_name);
215 str = g_string_free(result, FALSE);
216 if (str && str[0] != '\0') {
218 * Remove the trailing separator so that services doesn't end up
219 * with an empty element.
221 str[strlen(str) - 1] = '\0';
222 services = g_strsplit(str, "/", -1);
229 GKeyFile *connman_storage_load_service(const char *service_id)
232 GKeyFile *keyfile = NULL;
234 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
238 keyfile = storage_load(pathname);
244 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
247 gchar *pathname, *dirname;
249 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
253 /* If the dir doesn't exist, create it */
254 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
255 if(mkdir(dirname, MODE) < 0) {
256 if (errno != EEXIST) {
263 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
267 ret = storage_save(keyfile, pathname);
274 static gboolean remove_file(const char *service_id, const char *file)
277 gboolean ret = FALSE;
279 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
283 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
285 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
294 static gboolean remove_dir(const char *service_id)
297 gboolean ret = FALSE;
299 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
303 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
305 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
314 gboolean __connman_storage_remove_service(const char *service_id)
318 /* Remove service configuration file */
319 removed = remove_file(service_id, SETTINGS);
320 if (removed == FALSE)
323 /* Remove the statistics file also */
324 removed = remove_file(service_id, "data");
325 if (removed == FALSE)
328 removed = remove_dir(service_id);
329 if (removed == FALSE)
332 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
337 GKeyFile *__connman_storage_load_provider(const char *identifier)
342 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
343 identifier, SETTINGS);
344 if (pathname == NULL)
347 keyfile = storage_load(pathname);
353 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
355 gchar *pathname, *dirname;
357 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
358 "provider", identifier);
362 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
363 mkdir(dirname, MODE) < 0) {
368 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
371 storage_save(keyfile, pathname);
375 static gboolean remove_all(const char *id)
379 remove_file(id, SETTINGS);
380 remove_file(id, "data");
382 removed = remove_dir(id);
383 if (removed == FALSE)
389 gboolean __connman_storage_remove_provider(const char *identifier)
394 id = g_strdup_printf("%s_%s", "provider", identifier);
398 if (remove_all(id) == TRUE)
399 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
403 id = g_strdup_printf("%s_%s", "vpn", identifier);
407 if ((removed = remove_all(id)) == TRUE)
408 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
415 gchar **__connman_storage_get_providers(void)
427 dir = opendir(STORAGEDIR);
431 while ((d = readdir(dir))) {
432 if (strcmp(d->d_name, ".") == 0 ||
433 strcmp(d->d_name, "..") == 0 ||
434 strncmp(d->d_name, "provider_", 9) != 0)
437 if (d->d_type == DT_DIR) {
438 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
440 ret = stat(str, &buf);
444 list = g_slist_prepend(list, g_strdup(d->d_name));
451 providers = g_try_new0(char *, num + 1);
452 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
453 if (providers != NULL)
454 providers[i] = iter->data;