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 MODE (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
40 S_IXGRP | S_IROTH | S_IXOTH)
42 static GKeyFile *storage_load(const char *pathname)
44 GKeyFile *keyfile = NULL;
47 DBG("Loading %s", pathname);
49 keyfile = g_key_file_new();
51 if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
52 DBG("Unable to load %s: %s", pathname, error->message);
53 g_clear_error(&error);
55 g_key_file_free(keyfile);
62 static int storage_save(GKeyFile *keyfile, char *pathname)
69 data = g_key_file_to_data(keyfile, &length, NULL);
71 if (!g_file_set_contents(pathname, data, length, &error)) {
72 DBG("Failed to store information: %s", error->message);
80 fp = fopen(pathname, "a+");
85 DBG("sync the file to disk");
95 static void storage_delete(const char *pathname)
97 DBG("file path %s", pathname);
99 if (unlink(pathname) < 0)
100 connman_error("Failed to remove %s", pathname);
103 GKeyFile *__connman_storage_load_global(void)
106 GKeyFile *keyfile = NULL;
108 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
112 keyfile = storage_load(pathname);
119 int __connman_storage_save_global(GKeyFile *keyfile)
124 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
128 ret = storage_save(keyfile, pathname);
135 void __connman_storage_delete_global(void)
139 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
143 storage_delete(pathname);
148 GKeyFile *__connman_storage_load_config(const char *ident)
151 GKeyFile *keyfile = NULL;
153 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
157 keyfile = storage_load(pathname);
164 GKeyFile *__connman_storage_load_provider_config(const char *ident)
167 GKeyFile *keyfile = NULL;
169 pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
173 keyfile = storage_load(pathname);
180 GKeyFile *__connman_storage_open_service(const char *service_id)
183 GKeyFile *keyfile = NULL;
185 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
189 keyfile = storage_load(pathname);
197 keyfile = g_key_file_new();
202 gchar **connman_storage_get_services(void)
208 gchar **services = NULL;
212 dir = opendir(STORAGEDIR);
216 result = g_string_new(NULL);
218 while ((d = readdir(dir))) {
219 if (strcmp(d->d_name, ".") == 0 ||
220 strcmp(d->d_name, "..") == 0 ||
221 strncmp(d->d_name, "provider_", 9) == 0)
228 * If the settings file is not found, then
229 * assume this directory is not a services dir.
231 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
233 ret = stat(str, &buf);
238 g_string_append_printf(result, "%s/", d->d_name);
245 str = g_string_free(result, FALSE);
246 if (str && str[0] != '\0') {
248 * Remove the trailing separator so that services doesn't end up
249 * with an empty element.
251 str[strlen(str) - 1] = '\0';
252 services = g_strsplit(str, "/", -1);
259 GKeyFile *connman_storage_load_service(const char *service_id)
262 GKeyFile *keyfile = NULL;
264 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
268 keyfile = storage_load(pathname);
274 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
277 gchar *pathname, *dirname;
279 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
283 /* If the dir doesn't exist, create it */
284 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
285 if (mkdir(dirname, MODE) < 0) {
286 if (errno != EEXIST) {
293 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
297 ret = storage_save(keyfile, pathname);
304 static bool remove_file(const char *service_id, const char *file)
309 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
313 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
315 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) {
324 static bool remove_dir(const char *service_id)
329 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
333 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
335 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) {
344 bool __connman_storage_remove_service(const char *service_id)
348 /* Remove service configuration file */
349 removed = remove_file(service_id, SETTINGS);
353 /* Remove the statistics file also */
354 removed = remove_file(service_id, "data");
358 removed = remove_dir(service_id);
362 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
367 GKeyFile *__connman_storage_load_provider(const char *identifier)
372 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
373 identifier, SETTINGS);
377 keyfile = storage_load(pathname);
383 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
385 gchar *pathname, *dirname;
387 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
388 "provider", identifier);
392 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
393 mkdir(dirname, MODE) < 0) {
398 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
401 storage_save(keyfile, pathname);
405 static bool remove_all(const char *id)
409 remove_file(id, SETTINGS);
410 remove_file(id, "data");
412 removed = remove_dir(id);
419 bool __connman_storage_remove_provider(const char *identifier)
424 id = g_strdup_printf("%s_%s", "provider", identifier);
429 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
433 id = g_strdup_printf("%s_%s", "vpn", identifier);
437 if ((removed = remove_all(id)))
438 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
445 gchar **__connman_storage_get_providers(void)
457 dir = opendir(STORAGEDIR);
461 while ((d = readdir(dir))) {
462 if (strcmp(d->d_name, ".") == 0 ||
463 strcmp(d->d_name, "..") == 0 ||
464 strncmp(d->d_name, "provider_", 9) != 0)
467 if (d->d_type == DT_DIR) {
468 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
470 ret = stat(str, &buf);
474 list = g_slist_prepend(list, g_strdup(d->d_name));
481 providers = g_try_new0(char *, num + 1);
482 for (iter = list; iter; iter = g_slist_next(iter)) {
484 providers[i] = iter->data;