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 gchar **connman_storage_get_services(void)
221 gchar **services = NULL;
225 dir = opendir(STORAGEDIR);
229 result = g_string_new(NULL);
231 while ((d = readdir(dir))) {
232 if (strcmp(d->d_name, ".") == 0 ||
233 strcmp(d->d_name, "..") == 0 ||
234 strncmp(d->d_name, "provider_", 9) == 0)
241 * If the settings file is not found, then
242 * assume this directory is not a services dir.
244 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
246 ret = stat(str, &buf);
251 g_string_append_printf(result, "%s/", d->d_name);
258 str = g_string_free(result, FALSE);
259 if (str && str[0] != '\0') {
261 * Remove the trailing separator so that services doesn't end up
262 * with an empty element.
264 str[strlen(str) - 1] = '\0';
265 services = g_strsplit(str, "/", -1);
272 GKeyFile *connman_storage_load_service(const char *service_id)
275 GKeyFile *keyfile = NULL;
277 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
281 keyfile = storage_load(pathname);
287 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
290 gchar *pathname, *dirname;
292 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
296 /* If the dir doesn't exist, create it */
297 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
298 if (mkdir(dirname, MODE) < 0) {
299 if (errno != EEXIST) {
306 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
310 ret = storage_save(keyfile, pathname);
317 static bool remove_file(const char *service_id, const char *file)
322 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
326 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
328 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) {
337 static bool remove_dir(const char *service_id)
342 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
346 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
348 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) {
357 bool __connman_storage_remove_service(const char *service_id)
361 /* Remove service configuration file */
362 removed = remove_file(service_id, SETTINGS);
366 /* Remove the statistics file also */
367 removed = remove_file(service_id, "data");
371 removed = remove_dir(service_id);
375 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
380 GKeyFile *__connman_storage_load_provider(const char *identifier)
385 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
386 identifier, SETTINGS);
390 keyfile = storage_load(pathname);
396 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
398 gchar *pathname, *dirname;
400 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
401 "provider", identifier);
405 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
406 mkdir(dirname, MODE) < 0) {
411 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
414 storage_save(keyfile, pathname);
418 static bool remove_all(const char *id)
422 remove_file(id, SETTINGS);
423 remove_file(id, "data");
425 removed = remove_dir(id);
432 bool __connman_storage_remove_provider(const char *identifier)
437 id = g_strdup_printf("%s_%s", "provider", identifier);
442 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
446 id = g_strdup_printf("%s_%s", "vpn", identifier);
450 if ((removed = remove_all(id)))
451 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
458 gchar **__connman_storage_get_providers(void)
470 dir = opendir(STORAGEDIR);
474 while ((d = readdir(dir))) {
475 if (strcmp(d->d_name, ".") == 0 ||
476 strcmp(d->d_name, "..") == 0 ||
477 strncmp(d->d_name, "provider_", 9) != 0)
480 if (d->d_type == DT_DIR) {
481 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
483 ret = stat(str, &buf);
487 list = g_slist_prepend(list, g_strdup(d->d_name));
494 providers = g_try_new0(char *, num + 1);
495 for (iter = list; iter; iter = g_slist_next(iter)) {
497 providers[i] = iter->data;