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_load_provider_config(const char *ident)
153 GKeyFile *keyfile = NULL;
155 pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
156 if (pathname == NULL)
159 keyfile = storage_load(pathname);
166 GKeyFile *__connman_storage_open_service(const char *service_id)
169 GKeyFile *keyfile = NULL;
171 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
175 keyfile = storage_load(pathname);
183 keyfile = g_key_file_new();
188 gchar **connman_storage_get_services(void)
194 gchar **services = NULL;
198 dir = opendir(STORAGEDIR);
202 result = g_string_new(NULL);
204 while ((d = readdir(dir))) {
205 if (strcmp(d->d_name, ".") == 0 ||
206 strcmp(d->d_name, "..") == 0 ||
207 strncmp(d->d_name, "provider_", 9) == 0)
214 * If the settings file is not found, then
215 * assume this directory is not a services dir.
217 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
219 ret = stat(str, &buf);
224 g_string_append_printf(result, "%s/", d->d_name);
231 str = g_string_free(result, FALSE);
232 if (str && str[0] != '\0') {
234 * Remove the trailing separator so that services doesn't end up
235 * with an empty element.
237 str[strlen(str) - 1] = '\0';
238 services = g_strsplit(str, "/", -1);
245 GKeyFile *connman_storage_load_service(const char *service_id)
248 GKeyFile *keyfile = NULL;
250 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
254 keyfile = storage_load(pathname);
260 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
263 gchar *pathname, *dirname;
265 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
269 /* If the dir doesn't exist, create it */
270 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
271 if(mkdir(dirname, MODE) < 0) {
272 if (errno != EEXIST) {
279 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
283 ret = storage_save(keyfile, pathname);
290 static gboolean remove_file(const char *service_id, const char *file)
293 gboolean ret = FALSE;
295 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
299 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
301 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
310 static gboolean remove_dir(const char *service_id)
313 gboolean ret = FALSE;
315 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
319 if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
321 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
330 gboolean __connman_storage_remove_service(const char *service_id)
334 /* Remove service configuration file */
335 removed = remove_file(service_id, SETTINGS);
336 if (removed == FALSE)
339 /* Remove the statistics file also */
340 removed = remove_file(service_id, "data");
341 if (removed == FALSE)
344 removed = remove_dir(service_id);
345 if (removed == FALSE)
348 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
353 GKeyFile *__connman_storage_load_provider(const char *identifier)
358 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
359 identifier, SETTINGS);
360 if (pathname == NULL)
363 keyfile = storage_load(pathname);
369 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
371 gchar *pathname, *dirname;
373 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
374 "provider", identifier);
378 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
379 mkdir(dirname, MODE) < 0) {
384 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
387 storage_save(keyfile, pathname);
391 static gboolean remove_all(const char *id)
395 remove_file(id, SETTINGS);
396 remove_file(id, "data");
398 removed = remove_dir(id);
399 if (removed == FALSE)
405 gboolean __connman_storage_remove_provider(const char *identifier)
410 id = g_strdup_printf("%s_%s", "provider", identifier);
414 if (remove_all(id) == TRUE)
415 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
419 id = g_strdup_printf("%s_%s", "vpn", identifier);
423 if ((removed = remove_all(id)) == TRUE)
424 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
431 gchar **__connman_storage_get_providers(void)
443 dir = opendir(STORAGEDIR);
447 while ((d = readdir(dir))) {
448 if (strcmp(d->d_name, ".") == 0 ||
449 strcmp(d->d_name, "..") == 0 ||
450 strncmp(d->d_name, "provider_", 9) != 0)
453 if (d->d_type == DT_DIR) {
454 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
456 ret = stat(str, &buf);
460 list = g_slist_prepend(list, g_strdup(d->d_name));
467 providers = g_try_new0(char *, num + 1);
468 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
469 if (providers != NULL)
470 providers[i] = iter->data;