5 * Copyright (C) 2007-2013 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
32 #include <connman/storage.h>
36 #define SETTINGS "settings"
37 #define DEFAULT "default.profile"
39 #define INS_SETTINGS "settings.ins"
42 #define MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
43 S_IXGRP | S_IROTH | S_IXOTH)
45 static GKeyFile *storage_load(const char *pathname)
47 GKeyFile *keyfile = NULL;
50 keyfile = g_key_file_new();
52 if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
53 DBG("Unable to load %s: %s", pathname, error->message);
54 g_clear_error(&error);
56 g_key_file_free(keyfile);
63 static int storage_save(GKeyFile *keyfile, char *pathname)
70 data = g_key_file_to_data(keyfile, &length, NULL);
72 if (!g_file_set_contents(pathname, data, length, &error)) {
73 DBG("Failed to store information: %s", error->message);
81 fp = fopen(pathname, "a+");
86 DBG("sync the file to disk");
96 static void storage_delete(const char *pathname)
98 DBG("file path %s", pathname);
100 if (unlink(pathname) < 0)
101 connman_error("Failed to remove %s", pathname);
104 GKeyFile *__connman_storage_load_global(void)
107 GKeyFile *keyfile = NULL;
109 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
113 keyfile = storage_load(pathname);
120 int __connman_storage_save_global(GKeyFile *keyfile)
125 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
129 ret = storage_save(keyfile, pathname);
136 #if defined TIZEN_EXT
137 GKeyFile *__connman_storage_load_ins(void)
140 GKeyFile *keyfile = NULL;
142 pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS);
146 keyfile = storage_load(pathname);
153 int __connman_storage_save_ins(GKeyFile *keyfile)
158 pathname = g_strdup_printf("%s/%s", STORAGEDIR, INS_SETTINGS);
162 ret = storage_save(keyfile, pathname);
170 void __connman_storage_delete_global(void)
174 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
178 storage_delete(pathname);
183 GKeyFile *__connman_storage_load_config(const char *ident)
186 GKeyFile *keyfile = NULL;
188 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
192 keyfile = storage_load(pathname);
199 GKeyFile *__connman_storage_load_provider_config(const char *ident)
202 GKeyFile *keyfile = NULL;
204 pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
208 keyfile = storage_load(pathname);
215 GKeyFile *__connman_storage_open_service(const char *service_id)
218 GKeyFile *keyfile = NULL;
220 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
224 keyfile = storage_load(pathname);
232 keyfile = g_key_file_new();
237 gchar **connman_storage_get_services(void)
243 gchar **services = NULL;
247 dir = opendir(STORAGEDIR);
251 result = g_string_new(NULL);
253 while ((d = readdir(dir))) {
254 if (strcmp(d->d_name, ".") == 0 ||
255 strcmp(d->d_name, "..") == 0 ||
256 strncmp(d->d_name, "provider_", 9) == 0)
263 * If the settings file is not found, then
264 * assume this directory is not a services dir.
266 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
268 ret = stat(str, &buf);
273 g_string_append_printf(result, "%s/", d->d_name);
280 str = g_string_free(result, FALSE);
281 if (str && str[0] != '\0') {
283 * Remove the trailing separator so that services doesn't end up
284 * with an empty element.
286 str[strlen(str) - 1] = '\0';
287 services = g_strsplit(str, "/", -1);
294 GKeyFile *connman_storage_load_service(const char *service_id)
297 GKeyFile *keyfile = NULL;
299 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
303 keyfile = storage_load(pathname);
309 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
312 gchar *pathname, *dirname;
314 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
318 /* If the dir doesn't exist, create it */
319 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
320 if (mkdir(dirname, MODE) < 0) {
321 if (errno != EEXIST) {
328 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
332 ret = storage_save(keyfile, pathname);
339 static bool remove_file(const char *service_id, const char *file)
344 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
348 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
350 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) {
359 static bool remove_dir(const char *service_id)
364 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
368 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
370 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) {
379 bool __connman_storage_remove_service(const char *service_id)
383 /* Remove service configuration file */
384 removed = remove_file(service_id, SETTINGS);
388 /* Remove the statistics file also */
389 removed = remove_file(service_id, "data");
393 removed = remove_dir(service_id);
397 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
402 GKeyFile *__connman_storage_load_provider(const char *identifier)
407 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
408 identifier, SETTINGS);
412 keyfile = storage_load(pathname);
418 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
420 gchar *pathname, *dirname;
422 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
423 "provider", identifier);
427 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
428 mkdir(dirname, MODE) < 0) {
433 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
436 storage_save(keyfile, pathname);
440 static bool remove_all(const char *id)
444 remove_file(id, SETTINGS);
445 remove_file(id, "data");
447 removed = remove_dir(id);
454 bool __connman_storage_remove_provider(const char *identifier)
459 id = g_strdup_printf("%s_%s", "provider", identifier);
464 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
468 id = g_strdup_printf("%s_%s", "vpn", identifier);
472 if ((removed = remove_all(id)))
473 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
480 gchar **__connman_storage_get_providers(void)
492 dir = opendir(STORAGEDIR);
496 while ((d = readdir(dir))) {
497 if (strcmp(d->d_name, ".") == 0 ||
498 strcmp(d->d_name, "..") == 0 ||
499 strncmp(d->d_name, "provider_", 9) != 0)
502 if (d->d_type == DT_DIR) {
503 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
505 ret = stat(str, &buf);
509 list = g_slist_prepend(list, g_strdup(d->d_name));
516 providers = g_try_new0(char *, num + 1);
517 for (iter = list; iter; iter = g_slist_next(iter)) {
519 providers[i] = iter->data;