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 keyfile = g_key_file_new();
49 if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
50 DBG("Unable to load %s: %s", pathname, error->message);
51 g_clear_error(&error);
53 g_key_file_free(keyfile);
60 static int storage_save(GKeyFile *keyfile, char *pathname)
67 data = g_key_file_to_data(keyfile, &length, NULL);
69 if (!g_file_set_contents(pathname, data, length, &error)) {
70 DBG("Failed to store information: %s", error->message);
78 fp = fopen(pathname, "a+");
83 DBG("sync the file to disk");
93 static void storage_delete(const char *pathname)
95 DBG("file path %s", pathname);
97 if (unlink(pathname) < 0)
98 connman_error("Failed to remove %s", pathname);
101 GKeyFile *__connman_storage_load_global(void)
104 GKeyFile *keyfile = NULL;
106 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
110 keyfile = storage_load(pathname);
117 int __connman_storage_save_global(GKeyFile *keyfile)
122 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
126 ret = storage_save(keyfile, pathname);
133 void __connman_storage_delete_global(void)
137 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
141 storage_delete(pathname);
146 GKeyFile *__connman_storage_load_config(const char *ident)
149 GKeyFile *keyfile = NULL;
151 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
155 keyfile = storage_load(pathname);
162 GKeyFile *__connman_storage_load_provider_config(const char *ident)
165 GKeyFile *keyfile = NULL;
167 pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
171 keyfile = storage_load(pathname);
178 GKeyFile *__connman_storage_open_service(const char *service_id)
181 GKeyFile *keyfile = NULL;
183 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
187 keyfile = storage_load(pathname);
195 keyfile = g_key_file_new();
200 gchar **connman_storage_get_services(void)
206 gchar **services = NULL;
210 dir = opendir(STORAGEDIR);
214 result = g_string_new(NULL);
216 while ((d = readdir(dir))) {
217 if (strcmp(d->d_name, ".") == 0 ||
218 strcmp(d->d_name, "..") == 0 ||
219 strncmp(d->d_name, "provider_", 9) == 0)
226 * If the settings file is not found, then
227 * assume this directory is not a services dir.
229 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
231 ret = stat(str, &buf);
236 g_string_append_printf(result, "%s/", d->d_name);
243 str = g_string_free(result, FALSE);
244 if (str && str[0] != '\0') {
246 * Remove the trailing separator so that services doesn't end up
247 * with an empty element.
249 str[strlen(str) - 1] = '\0';
250 services = g_strsplit(str, "/", -1);
257 GKeyFile *connman_storage_load_service(const char *service_id)
260 GKeyFile *keyfile = NULL;
262 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
266 keyfile = storage_load(pathname);
272 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
275 gchar *pathname, *dirname;
277 dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
281 /* If the dir doesn't exist, create it */
282 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
283 if (mkdir(dirname, MODE) < 0) {
284 if (errno != EEXIST) {
291 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
295 ret = storage_save(keyfile, pathname);
302 static bool remove_file(const char *service_id, const char *file)
307 pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
311 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
313 } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) {
322 static bool remove_dir(const char *service_id)
327 pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
331 if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
333 } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) {
342 bool __connman_storage_remove_service(const char *service_id)
346 /* Remove service configuration file */
347 removed = remove_file(service_id, SETTINGS);
351 /* Remove the statistics file also */
352 removed = remove_file(service_id, "data");
356 removed = remove_dir(service_id);
360 DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
365 GKeyFile *__connman_storage_load_provider(const char *identifier)
370 pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
371 identifier, SETTINGS);
375 keyfile = storage_load(pathname);
381 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
383 gchar *pathname, *dirname;
385 dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
386 "provider", identifier);
390 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
391 mkdir(dirname, MODE) < 0) {
396 pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
399 storage_save(keyfile, pathname);
403 static bool remove_all(const char *id)
407 remove_file(id, SETTINGS);
408 remove_file(id, "data");
410 removed = remove_dir(id);
417 bool __connman_storage_remove_provider(const char *identifier)
422 id = g_strdup_printf("%s_%s", "provider", identifier);
427 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
431 id = g_strdup_printf("%s_%s", "vpn", identifier);
435 if ((removed = remove_all(id)))
436 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
443 gchar **__connman_storage_get_providers(void)
455 dir = opendir(STORAGEDIR);
459 while ((d = readdir(dir))) {
460 if (strcmp(d->d_name, ".") == 0 ||
461 strcmp(d->d_name, "..") == 0 ||
462 strncmp(d->d_name, "provider_", 9) != 0)
465 if (d->d_type == DT_DIR) {
466 str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
468 ret = stat(str, &buf);
472 list = g_slist_prepend(list, g_strdup(d->d_name));
479 providers = g_try_new0(char *, num + 1);
480 for (iter = list; iter; iter = g_slist_next(iter)) {
482 providers[i] = iter->data;