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
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 keyfile = g_key_file_new();
48 if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
49 DBG("Unable to load %s: %s", pathname, error->message);
50 g_clear_error(&error);
52 g_key_file_free(keyfile);
59 static int storage_save(GKeyFile *keyfile, char *pathname)
66 data = g_key_file_to_data(keyfile, &length, NULL);
68 if (!g_file_set_contents(pathname, data, length, &error)) {
69 DBG("Failed to store information: %s", error->message);
79 static void storage_delete(const char *pathname)
81 DBG("file path %s", pathname);
83 if (unlink(pathname) < 0)
84 connman_error("Failed to remove %s", pathname);
87 GKeyFile *__connman_storage_load_global(void)
90 GKeyFile *keyfile = NULL;
92 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
96 keyfile = storage_load(pathname);
103 int __connman_storage_save_global(GKeyFile *keyfile)
108 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
112 ret = storage_save(keyfile, pathname);
119 void __connman_storage_delete_global(void)
123 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
127 storage_delete(pathname);
132 GKeyFile *__connman_storage_load_config(const char *ident)
135 GKeyFile *keyfile = NULL;
137 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
141 keyfile = storage_load(pathname);
148 GKeyFile *__connman_storage_load_provider_config(const char *ident)
151 GKeyFile *keyfile = NULL;
153 pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
157 keyfile = storage_load(pathname);
164 gchar **connman_storage_get_services(void)
170 gchar **services = NULL;
174 dir = opendir(STORAGEDIR);
178 result = g_string_new(NULL);
180 while ((d = readdir(dir))) {
181 if (strcmp(d->d_name, ".") == 0 ||
182 strcmp(d->d_name, "..") == 0 ||
183 strncmp(d->d_name, "provider_", 9) == 0)
190 * If the settings file is not found, then
191 * assume this directory is not a services dir.
193 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
195 ret = stat(str, &buf);
200 g_string_append_printf(result, "%s/", d->d_name);
207 str = g_string_free(result, FALSE);
208 if (str && str[0] != '\0') {
210 * Remove the trailing separator so that services doesn't end up
211 * with an empty element.
213 str[strlen(str) - 1] = '\0';
214 services = g_strsplit(str, "/", -1);
221 GKeyFile *connman_storage_load_service(const char *service_id)
224 GKeyFile *keyfile = NULL;
226 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
230 keyfile = storage_load(pathname);
236 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
239 gchar *pathname, *dirname;
241 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
245 /* If the dir doesn't exist, create it */
246 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
247 if (mkdir(dirname, MODE) < 0) {
248 if (errno != EEXIST) {
255 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
259 ret = storage_save(keyfile, pathname);
266 static bool remove_file(const char *service_id, const char *file)
271 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
275 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
277 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) {
286 static bool remove_dir(const char *service_id)
291 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
295 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
297 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) {
306 bool __connman_storage_remove_service(const char *service_id)
310 /* Remove service configuration file */
311 removed = remove_file(service_id, SETTINGS);
315 /* Remove the statistics file also */
316 removed = remove_file(service_id, "data");
320 removed = remove_dir(service_id);
324 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
329 GKeyFile *__connman_storage_load_provider(const char *identifier)
334 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
335 identifier, SETTINGS);
339 keyfile = storage_load(pathname);
345 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
347 gchar *pathname, *dirname;
349 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
350 "provider", identifier);
354 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
355 mkdir(dirname, MODE) < 0) {
360 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
363 storage_save(keyfile, pathname);
367 static bool remove_all(const char *id)
371 remove_file(id, SETTINGS);
372 remove_file(id, "data");
374 removed = remove_dir(id);
381 bool __connman_storage_remove_provider(const char *identifier)
386 id = g_strdup_printf("%s_%s", "provider", identifier);
391 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
395 id = g_strdup_printf("%s_%s", "vpn", identifier);
399 if ((removed = remove_all(id)))
400 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
407 gchar **__connman_storage_get_providers(void)
419 dir = opendir(STORAGEDIR);
423 while ((d = readdir(dir))) {
424 if (strcmp(d->d_name, ".") == 0 ||
425 strcmp(d->d_name, "..") == 0 ||
426 strncmp(d->d_name, "provider_", 9) != 0)
429 if (d->d_type == DT_DIR) {
430 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
432 ret = stat(str, &buf);
436 list = g_slist_prepend(list, g_strdup(d->d_name));
443 providers = g_try_new0(char *, num + 1);
444 for (iter = list; iter; iter = g_slist_next(iter)) {
446 providers[i] = iter->data;