storage.c 20.9 KB
Newer Older
1 2 3 4
/*
 *
 *  Connection Manager
 *
5
 *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
30
#include <sys/inotify.h>
31 32 33 34 35 36 37 38 39 40 41

#include <connman/storage.h>

#include "connman.h"

#define SETTINGS	"settings"
#define DEFAULT		"default.profile"

#define MODE		(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
			S_IXGRP | S_IROTH | S_IXOTH)

42 43 44 45
#define NAME_MASK (IN_ACCESS | IN_ATTRIB | IN_CLOSE_WRITE | IN_CLOSE_NOWRITE | \
			IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | \
			IN_MOVED_TO | IN_OPEN)

46 47 48 49 50 51 52
struct storage_subdir {
	gchar *name;
	gboolean has_settings;
};

struct storage_dir_context {
	gboolean initialized;
53
	gboolean vpn_initialized;
54 55 56
	GList *subdirs;
};

57 58 59 60 61
enum storage_dir_type {
	STORAGE_DIR_TYPE_MAIN = 0,
	STORAGE_DIR_TYPE_VPN,
};

62 63
static struct storage_dir_context storage = {
	.initialized = FALSE,
64
	.vpn_initialized = FALSE,
65 66 67
	.subdirs = NULL
};

68 69 70 71 72 73 74
struct keyfile_record {
	gchar *pathname;
	GKeyFile *keyfile;
};

static GHashTable *keyfile_hash = NULL;

75 76
static void storage_dir_cleanup(const char *storagedir,
					enum storage_dir_type type);
77 78 79 80 81

static void storage_inotify_subdir_cb(struct inotify_event *event,
					const char *ident,
					gpointer user_data);

82 83 84 85
static void keyfile_inotify_cb(struct inotify_event *event,
				const char *ident,
				gpointer user_data);

86 87 88 89
gboolean is_service_dir_name(const char *name);

gboolean is_provider_dir_name(const char *name);

90 91 92 93
gboolean is_vpn_dir_name(const char *name);

gboolean is_vpn_dir(const char *name);

94 95
static const char* storagedir_for(const char *name);

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static void debug_subdirs(void)
{
	GList *l;

	if (!storage.initialized) {
		DBG("Storage subdirs not initialized.");
		return;
	}

	DBG("Storage subdirs: {");
	for (l = storage.subdirs; l; l = l->next) {
		struct storage_subdir *subdir = l->data;
		DBG("\t%s[%s]", subdir->name,
			subdir->has_settings ? (
				is_service_dir_name(subdir->name) ? "S" :
				is_provider_dir_name(subdir->name) ? "P" :
112
				is_vpn_dir_name(subdir->name) ? "V" :
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
				"X") : "-");
	}
	DBG("}");
}

static void debug_inotify_event(struct inotify_event *event)
{
	static const char *flags[] = {
		"IN_ACCESS", 		// 1
		"IN_MODIFY", 		// 2
		"IN_ATTRIB", 		// 4
		"IN_CLOSE_WRITE",	// 8

		"IN_CLOSE_NOWRITE",	// 10
		"IN_OPEN",		// 20
		"IN_MOVED_FROM",	// 40
		"IN_MOVED_TO",		// 80

		"IN_CREATE",		// 100
		"IN_DELETE",		// 200
		"IN_DELETE_SELF",	// 400
		"IN_MOVE_SELF",		// 800

		"UNDEFINED_1000",
		"IN_UNMOUNT",		// 2000
		"IN_Q_OVERFLOW",	// 4000
		"IN_IGNORED",		// 8000

		"UNDEFINED_10000",
		"UNDEFINED_20000",
		"UNDEFINED_40000",
		"UNDEFINED_80000",

		"UNDEFINED_100000",
		"UNDEFINED_200000",
		"UNDEFINED_400000",
		"UNDEFINED_800000",

		"IN_ONLYDIR",           // 1000000
		"IN_DONT_FOLLOW",	// 2000000
		"IN_EXCL_UNLINK",	// 4000000
		"UNDEFINED_800000",

		"UNDEFINED_1000000",
		"IN_MASK_ADD",		// 20000000
		"IN_ISDIR",		// 40000000
		"IN_ONESHOT",		// 80000000
	};
	int i;

	DBG("Event flags: ");
	for (i = 0; i < 32; i++) {
		if (event->mask & (1 << i))
			DBG("\t%s", flags[i]);
	}

	if (event->mask & NAME_MASK)
		DBG("Event name: %s", event->name);
}

173 174 175 176 177 178 179 180 181 182
bool service_id_is_valid(const char *id)
{
	char *check = g_strdup_printf("%s/service/%s", CONNMAN_PATH, id);
	bool valid = dbus_validate_path(check, NULL) == TRUE;
	if (!valid)
		DBG("Service ID '%s' is not valid.", id);
	g_free(check);
	return valid;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
gboolean is_service_dir_name(const char *name)
{
	DBG("name %s", name);

	if (strncmp(name, "provider_", 9) == 0 || !service_id_is_valid(name))
		return FALSE;

	return TRUE;
}

gboolean is_provider_dir_name(const char *name)
{
	DBG("name %s", name);

	if (strncmp(name, "provider_", 9) == 0)
		return TRUE;

	return FALSE;
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
gboolean is_vpn_dir_name(const char *name)
{
	DBG("name %s", name);

	if (strncmp(name, "vpn_", 4) == 0)
		return TRUE;

	return FALSE;
}

gboolean is_vpn_dir(const char *name)
{
	if (is_vpn_dir_name(name) || is_provider_dir_name(name))
		return TRUE;

	return FALSE;
}

221 222 223 224 225
static const char *storagedir_for(const char *name)
{
	return is_vpn_dir(name) ? VPN_STORAGEDIR : STORAGEDIR;
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
gint storage_subdir_cmp(gconstpointer a, gconstpointer b)
{
	const struct storage_subdir *d1 = a;
	const struct storage_subdir *d2 = b;

	DBG("name1 %s name2 %s", d1->name, d2->name);

	return g_strcmp0(d1->name, d2->name);
}

static void storage_subdir_free(gpointer data)
{
	struct storage_subdir *subdir = data;
	DBG("%s", subdir->name);
	storage.subdirs = g_list_remove(storage.subdirs, subdir);
	g_free(subdir->name);
	g_free(subdir);
}

static void storage_subdir_unregister(gpointer data)
{
	struct storage_subdir *subdir = data;
	gchar *str;

	DBG("%s", subdir->name);
251 252
	str = g_strdup_printf("%s/%s", storagedir_for(subdir->name),
				subdir->name);
253 254 255 256 257 258 259 260 261
	connman_inotify_unregister(str, storage_inotify_subdir_cb, subdir);
	g_free(str);
}

static void storage_subdir_append(const char *name)
{
	struct storage_subdir *subdir;
	struct stat buf;
	gchar *str;
262
	const char *storagedir;
263 264 265 266 267 268 269
	int ret;

	DBG("%s", name);

	subdir = g_new0(struct storage_subdir, 1);
	subdir->name = g_strdup(name);

270
	storagedir = storagedir_for(subdir->name);
271
	str = g_strdup_printf("%s/%s/%s", storagedir, subdir->name, SETTINGS);
272 273 274 275 276 277 278
	ret = stat(str, &buf);
	g_free(str);
	if (ret == 0)
		subdir->has_settings = TRUE;

	storage.subdirs = g_list_prepend(storage.subdirs, subdir);

279
	str = g_strdup_printf("%s/%s", storagedir, subdir->name);
280 281 282 283 284

	if (connman_inotify_register(str, storage_inotify_subdir_cb, subdir,
				storage_subdir_free) != 0)
		storage_subdir_free(subdir);

285 286 287 288 289 290 291 292 293 294
	g_free(str);
}

static void storage_inotify_subdir_cb(struct inotify_event *event,
					const char *ident,
					gpointer user_data)
{
	struct storage_subdir *subdir = user_data;

	DBG("name %s", subdir->name);
295
	debug_inotify_event(event);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

	/* Only interested in files here */
	if (event->mask & IN_ISDIR)
		return;

	if ((event->mask & IN_DELETE) || (event->mask & IN_MOVED_FROM)) {
		DBG("delete/move-from %s", event->name);
		if (!g_strcmp0(event->name, SETTINGS))
			subdir->has_settings = FALSE;
		return;
	}

	if ((event->mask & IN_CREATE) || (event->mask & IN_MOVED_TO)) {
		DBG("create/move-to %s", event->name);
		if (!g_strcmp0(event->name, SETTINGS)) {
			struct stat st;
			gchar *pathname;
313

314 315
			pathname = g_strdup_printf("%s/%s/%s",
						storagedir_for(subdir->name),
316 317 318 319 320 321 322 323 324 325 326 327 328 329
						subdir->name, event->name);
			if (stat(pathname, &st) == 0 && S_ISREG(st.st_mode)) {
				subdir->has_settings = TRUE;
			}
			g_free(pathname);
		}
		return;
	}
}

static void storage_inotify_cb(struct inotify_event *event, const char *ident,
				gpointer user_data)
{
	DBG("");
330
	debug_inotify_event(event);
331 332 333

	if (event->mask & IN_DELETE_SELF) {
		DBG("delete self");
334 335
		storage_dir_cleanup(STORAGEDIR, STORAGE_DIR_TYPE_MAIN);
		storage_dir_cleanup(VPN_STORAGEDIR, STORAGE_DIR_TYPE_VPN);
336 337 338 339 340 341 342 343 344 345 346 347 348 349
		return;
	}

	/* Only interested in subdirectories here */
	if (!(event->mask & IN_ISDIR))
		return;

	if ((event->mask & IN_DELETE) || (event->mask & IN_MOVED_FROM)) {
		struct storage_subdir key = { .name = event->name };
		GList *pos;

		DBG("delete/move-from %s", event->name);
		pos = g_list_find_custom(storage.subdirs, &key,
					storage_subdir_cmp);
350
		if (pos) {
351
			storage_subdir_unregister(pos->data);
352 353
			debug_subdirs();
		}
354 355 356 357 358 359 360

		return;
	}

	if ((event->mask & IN_CREATE) || (event->mask & IN_MOVED_TO)) {
		DBG("create %s", event->name);
		storage_subdir_append(event->name);
361
		debug_subdirs();
362 363 364 365
		return;
	}
}

366
static void storage_dir_init(const char *storagedir, enum storage_dir_type type)
367 368 369 370
{
	DIR *dir;
	struct dirent *d;

371
	if (!storagedir)
372 373
		return;

374 375 376 377 378 379 380 381 382 383 384 385 386
	switch (type) {
	case STORAGE_DIR_TYPE_MAIN:
		if (storage.initialized)
			return;

		break;
	case STORAGE_DIR_TYPE_VPN:
		if (storage.vpn_initialized)
			return;

		break;
	}

387 388
	DBG("Initializing storage directories.");

389
	dir = opendir(storagedir);
390 391 392 393 394 395 396 397 398 399 400 401
	if (!dir)
		return;

	while ((d = readdir(dir))) {

		if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
			continue;

		switch (d->d_type) {
		case DT_DIR:
		case DT_UNKNOWN:
			storage_subdir_append(d->d_name);
402
			debug_subdirs();
403 404 405 406 407 408
			break;
		}
	}

	closedir(dir);

409
	connman_inotify_register(storagedir, storage_inotify_cb, NULL, NULL);
410

411 412 413 414 415 416 417 418
	switch (type) {
	case STORAGE_DIR_TYPE_MAIN:
		storage.initialized = TRUE;
		break;
	case STORAGE_DIR_TYPE_VPN:
		storage.vpn_initialized = TRUE;
		break;
	}
419 420 421 422

	DBG("Initialization done.");
}

423 424
static void storage_dir_cleanup(const char *storagedir,
						enum storage_dir_type type)
425
{
426
	if (!storagedir)
427 428
		return;

429 430 431 432 433 434 435 436 437 438 439 440 441
	switch (type) {
	case STORAGE_DIR_TYPE_MAIN:
		if (!storage.initialized)
			return;

		break;
	case STORAGE_DIR_TYPE_VPN:
		if (!storage.vpn_initialized)
			return;

		break;
	}

442 443
	DBG("Cleaning up storage directories.");

444
	connman_inotify_unregister(storagedir, storage_inotify_cb, NULL);
445 446 447 448 449

	while (storage.subdirs)
		storage_subdir_unregister(storage.subdirs->data);
	storage.subdirs = NULL;

450 451 452 453 454 455 456 457
	switch (type) {
	case STORAGE_DIR_TYPE_MAIN:
		storage.initialized = FALSE;
		break;
	case STORAGE_DIR_TYPE_VPN:
		storage.vpn_initialized = FALSE;
		break;
	}
458 459 460 461

	DBG("Cleanup done.");
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
static void keyfile_free(gpointer data)
{
	struct keyfile_record *record = data;
	DBG("Freeing record %p for %s", record, record->pathname);
	g_hash_table_remove(keyfile_hash, record->pathname);
	g_key_file_unref(record->keyfile);
	g_free(record->pathname);
	g_free(record);
}

static void keyfile_unregister(gpointer data)
{
	struct keyfile_record *record = data;
	char *str = g_strdup(record->pathname);
	connman_inotify_unregister(str, keyfile_inotify_cb, record);
	g_free(str);
}

static void keyfile_insert(const char *pathname, GKeyFile *keyfile)
{
	struct keyfile_record *record = g_new0(struct keyfile_record, 1);
	record->pathname = g_strdup(pathname);
	record->keyfile = g_key_file_ref(keyfile);
	g_hash_table_insert(keyfile_hash, record->pathname, record);
486 487 488 489

	if (connman_inotify_register(pathname, keyfile_inotify_cb, record,
				keyfile_free) != 0)
		keyfile_free(record);
490 491 492 493 494 495 496 497
}

static void keyfile_inotify_cb(struct inotify_event *event,
				const char *ident,
				gpointer user_data)
{
	struct keyfile_record *record = user_data;

498 499 500
	DBG("name %s", record->pathname);
	debug_inotify_event(event);

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	if ((event->mask & IN_DELETE_SELF) || (event->mask & IN_MOVE_SELF) ||
		(event->mask & IN_MODIFY) || (event->mask & IN_IGNORED)) {
		DBG("File for record %p path %s changed, dropping from cache.",
			record, record->pathname);
		keyfile_unregister(record);
		return;
	}
}

static void keyfile_init(void)
{
	if (keyfile_hash)
		return;

	DBG("Creating keyfile hash.");
	keyfile_hash = g_hash_table_new(g_str_hash, g_str_equal);
}

static void keyfile_cleanup(void)
{
	GHashTableIter iter;
	gpointer key, value;

	if (!keyfile_hash)
		return;

	g_hash_table_iter_init(&iter, keyfile_hash);
	while (g_hash_table_iter_next(&iter, &key, &value)) {
		g_hash_table_iter_steal(&iter);
		keyfile_unregister(value);
	}
	g_hash_table_unref(keyfile_hash);
	keyfile_hash = NULL;
}

536 537
static GKeyFile *storage_load(const char *pathname)
{
538
	struct keyfile_record *record = NULL;
539
	GError *error = NULL;
540
	GKeyFile *keyfile = NULL;
541 542 543

	DBG("Loading %s", pathname);

544 545 546 547 548 549 550
	record = g_hash_table_lookup(keyfile_hash, pathname);
	if (record) {
		DBG("Found record %p for %s from cache.", record, pathname);
		return g_key_file_ref(record->keyfile);
        }

	DBG("No keyfile in cache for %s", pathname);
551 552 553 554 555 556
	keyfile = g_key_file_new();

	if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
		DBG("Unable to load %s: %s", pathname, error->message);
		g_clear_error(&error);

557
		g_key_file_unref(keyfile);
558
		keyfile = NULL;
559 560 561
	} else {
		DBG("Storing record for %s into cache.", pathname);
		keyfile_insert(pathname, keyfile);
562 563 564 565 566 567 568 569 570 571 572
	}

	return keyfile;
}

static int storage_save(GKeyFile *keyfile, char *pathname)
{
	gchar *data = NULL;
	gsize length = 0;
	GError *error = NULL;
	int ret = 0;
573 574
	const mode_t perm = STORAGE_FILE_MODE;
	const mode_t old_mask = umask(~perm & 0777);
575 576 577 578 579 580 581 582 583

	data = g_key_file_to_data(keyfile, &length, NULL);

	if (!g_file_set_contents(pathname, data, length, &error)) {
		DBG("Failed to store information: %s", error->message);
		g_error_free(error);
		ret = -EIO;
	}

584 585 586 587 588 589 590 591 592
	if (ret == 0) {
		ret = chmod(pathname, perm);
		if (ret < 0) {
			ret = -errno;
			DBG("Failed to set permissions 0%o on %s: %s",
					perm, pathname, strerror(errno));
		}
	}

593
	g_free(data);
594
	umask(old_mask);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

	return ret;
}

static void storage_delete(const char *pathname)
{
	DBG("file path %s", pathname);

	if (unlink(pathname) < 0)
		connman_error("Failed to remove %s", pathname);
}

GKeyFile *__connman_storage_load_global(void)
{
	gchar *pathname;
	GKeyFile *keyfile = NULL;

	pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
613
	if (!pathname)
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		return NULL;

	keyfile = storage_load(pathname);

	g_free(pathname);

	return keyfile;
}

int __connman_storage_save_global(GKeyFile *keyfile)
{
	gchar *pathname;
	int ret;

	pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
629
	if (!pathname)
630 631 632 633 634 635 636 637 638 639 640 641 642 643
		return -ENOMEM;

	ret = storage_save(keyfile, pathname);

	g_free(pathname);

	return ret;
}

void __connman_storage_delete_global(void)
{
	gchar *pathname;

	pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
644
	if (!pathname)
645 646 647 648 649 650 651 652 653 654 655 656
		return;

	storage_delete(pathname);

	g_free(pathname);
}

GKeyFile *__connman_storage_load_config(const char *ident)
{
	gchar *pathname;
	GKeyFile *keyfile = NULL;

657 658
	pathname = g_strdup_printf("%s/%s.config", storagedir_for(ident),
				ident);
659
	if (!pathname)
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
		return NULL;

	keyfile = storage_load(pathname);

	g_free(pathname);

	return keyfile;
}

GKeyFile *__connman_storage_load_provider_config(const char *ident)
{
	gchar *pathname;
	GKeyFile *keyfile = NULL;

	pathname = g_strdup_printf("%s/%s.config", VPN_STORAGEDIR, ident);
675
	if (!pathname)
676 677 678 679 680 681 682 683 684 685 686 687 688 689
		return NULL;

	keyfile = storage_load(pathname);

	g_free(pathname);

	return keyfile;
}

GKeyFile *__connman_storage_open_service(const char *service_id)
{
	gchar *pathname;
	GKeyFile *keyfile = NULL;

690 691 692
	if (!service_id_is_valid(service_id))
		return NULL;

693 694
	pathname = g_strdup_printf("%s/%s/%s", storagedir_for(service_id),
				service_id, SETTINGS);
695
	if (!pathname)
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
		return NULL;

	keyfile =  storage_load(pathname);
	if (keyfile) {
		g_free(pathname);
		return keyfile;
	}

	g_free(pathname);

	keyfile = g_key_file_new();

	return keyfile;
}

gchar **connman_storage_get_services(void)
{
713 714 715
	gchar **result = NULL;
	GList *l;
	int sum, pos;
716

717
	DBG("");
718

719
	if (!storage.initialized) {
720
		storage_dir_init(STORAGEDIR, STORAGE_DIR_TYPE_MAIN);
721 722 723
		if (!storage.initialized)
			return NULL;
	}
724

725 726 727 728
	for (sum = 0, l = storage.subdirs; l; l = l->next) {
		struct storage_subdir *subdir = l->data;
		if (is_service_dir_name(subdir->name) && subdir->has_settings)
			sum++;
729 730
	}

731
	result = g_new0(gchar *, sum + 1);
732

733 734 735 736
	for (pos = 0, l = storage.subdirs; l; l = l->next) {
		struct storage_subdir *subdir = l->data;
		if (is_service_dir_name(subdir->name) && subdir->has_settings)
			result[pos++] = g_strdup(subdir->name);
737 738
	}

739
	return result;
740 741 742 743 744 745 746
}

GKeyFile *connman_storage_load_service(const char *service_id)
{
	gchar *pathname;
	GKeyFile *keyfile = NULL;

747 748 749
	if (!service_id_is_valid(service_id))
		return NULL;

750 751
	pathname = g_strdup_printf("%s/%s/%s", storagedir_for(service_id),
				service_id, SETTINGS);
752
	if (!pathname)
753 754 755 756 757 758 759 760 761 762 763 764 765
		return NULL;

	keyfile =  storage_load(pathname);
	g_free(pathname);

	return keyfile;
}

int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
{
	int ret = 0;
	gchar *pathname, *dirname;

766 767 768
	if (!service_id_is_valid(service_id))
		return -EINVAL;

769 770
	dirname = g_strdup_printf("%s/%s", storagedir_for(service_id),
				service_id);
771
	if (!dirname)
772 773 774 775
		return -ENOMEM;

	/* If the dir doesn't exist, create it */
	if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
776
		if (mkdir(dirname, STORAGE_DIR_MODE) < 0) {
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
			if (errno != EEXIST) {
				g_free(dirname);
				return -errno;
			}
		}
	}

	pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);

	g_free(dirname);

	ret = storage_save(keyfile, pathname);

	g_free(pathname);

	return ret;
}

795
static bool remove_file(const char *service_id, const char *file)
796 797
{
	gchar *pathname;
798
	bool ret = false;
799

800 801
	pathname = g_strdup_printf("%s/%s/%s", storagedir_for(service_id),
				service_id, file);
802 803
	if (!pathname)
		return false;
804

805 806 807
	if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
		ret = true;
	} else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR)) {
808
		unlink(pathname);
809
		ret = true;
810 811 812 813 814 815
	}

	g_free(pathname);
	return ret;
}

816
static bool remove_dir(const char *service_id)
817 818
{
	gchar *pathname;
819
	bool ret = false;
820

821 822
	pathname = g_strdup_printf("%s/%s", storagedir_for(service_id),
				service_id);
823 824
	if (!pathname)
		return false;
825

826 827 828
	if (!g_file_test(pathname, G_FILE_TEST_EXISTS)) {
		ret = true;
	} else if (g_file_test(pathname, G_FILE_TEST_IS_DIR)) {
829
		rmdir(pathname);
830
		ret = true;
831 832 833 834 835 836
	}

	g_free(pathname);
	return ret;
}

837
bool __connman_storage_remove_service(const char *service_id)
838
{
839
	bool removed = false;
840 841 842
	gchar *pathname;
	DIR *dir;

843 844
	pathname = g_strdup_printf("%s/%s", storagedir_for(service_id),
				service_id);
845
	dir = opendir(pathname);
846 847 848 849 850 851 852 853 854 855 856

	if (dir) {
		struct dirent *d;

		/* Remove the configuration files */
		while ((d = readdir(dir)) != NULL) {
			if (strcmp(d->d_name, ".") != 0 &&
					strcmp(d->d_name, "..") != 0) {
				remove_file(service_id, d->d_name);
			}
		}
857

858 859 860 861 862
		closedir(dir);
		remove_dir(service_id);
		DBG("Removed service dir %s", pathname);
		removed = true;
	}
863

864 865
	g_free(pathname);
	return removed;
866 867 868 869 870 871 872
}

GKeyFile *__connman_storage_load_provider(const char *identifier)
{
	gchar *pathname;
	GKeyFile *keyfile;

873
	pathname = g_strdup_printf("%s/%s_%s/%s", VPN_STORAGEDIR, "provider",
874
			identifier, SETTINGS);
875
	if (!pathname)
876 877 878 879 880 881 882 883 884 885 886 887
		return NULL;

	keyfile = storage_load(pathname);
	g_free(pathname);

	return keyfile;
}

void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
{
	gchar *pathname, *dirname;

888
	dirname = g_strdup_printf("%s/%s_%s", VPN_STORAGEDIR,
889
			"provider", identifier);
890
	if (!dirname)
891 892
		return;

893
	if (!g_file_test(dirname, G_FILE_TEST_IS_DIR) &&
894 895 896 897 898 899 900 901 902 903 904 905
			mkdir(dirname, MODE) < 0) {
		g_free(dirname);
		return;
	}

	pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
	g_free(dirname);

	storage_save(keyfile, pathname);
	g_free(pathname);
}

906
static bool remove_all(const char *id)
907
{
908
	bool removed;
909 910 911 912 913

	remove_file(id, SETTINGS);
	remove_file(id, "data");

	removed = remove_dir(id);
914 915
	if (!removed)
		return false;
916

917
	return true;
918 919
}

920
bool __connman_storage_remove_provider(const char *identifier)
921
{
922
	bool removed;
923 924 925
	gchar *id;

	id = g_strdup_printf("%s_%s", "provider", identifier);
926 927
	if (!id)
		return false;
928

929
	if (remove_all(id))
930
		DBG("Removed provider dir %s/%s", VPN_STORAGEDIR, id);
931 932 933 934

	g_free(id);

	id = g_strdup_printf("%s_%s", "vpn", identifier);
935 936
	if (!id)
		return false;
937

938
	if ((removed = remove_all(id)))
939
		DBG("Removed vpn dir %s/%s", VPN_STORAGEDIR, id);
940 941 942 943 944 945 946 947

	g_free(id);

	return removed;
}

gchar **__connman_storage_get_providers(void)
{
948 949 950
	gchar **result = NULL;
	GList *l;
	int sum, pos;
951

952
	DBG("");
953

954 955 956
	if (!storage.vpn_initialized) {
		storage_dir_init(VPN_STORAGEDIR, STORAGE_DIR_TYPE_VPN);
		if (!storage.vpn_initialized)
957 958
			return NULL;
	}
959

960 961 962 963
	for (sum = 0, l = storage.subdirs; l; l = l->next) {
		struct storage_subdir *subdir = l->data;
		if (is_provider_dir_name(subdir->name) && subdir->has_settings)
			sum++;
964 965
	}

966
	result = g_new0(gchar *, sum + 1);
967

968 969 970 971
	for (pos = 0, l = storage.subdirs; l; l = l->next) {
		struct storage_subdir *subdir = l->data;
		if (is_provider_dir_name(subdir->name) && subdir->has_settings)
			result[pos++] = g_strdup(subdir->name);
972 973
	}

974
	return result;
975
}
976

977 978
static char *storage_dir = NULL;
static char *vpn_storage_dir = NULL;
979 980
static int storage_dir_mode;
static int storage_file_mode;
981

982
const char *connman_storage_dir(void)
983
{
984 985 986
	return storage_dir;
}

987
const char *connman_storage_vpn_dir(void)
988 989 990 991
{
	return vpn_storage_dir;
}

992 993 994 995 996 997 998 999 1000 1001 1002
int __connman_storage_dir_mode(void)
{
	return storage_dir_mode;
}

int __connman_storage_file_mode(void)
{
	return storage_file_mode;
}

int __connman_storage_init(const char *dir, int dir_mode, int file_mode)
1003 1004 1005
{
	const char *root = dir ? dir : DEFAULT_STORAGE_ROOT;

1006
	DBG("%s 0%o 0%o", root, dir_mode, file_mode);
1007 1008
	storage_dir = g_strconcat(root, "/connman", NULL);
	vpn_storage_dir = g_strconcat(root, "/connman-vpn", NULL);
1009 1010
	storage_dir_mode = dir_mode;
	storage_file_mode = file_mode;
1011
	keyfile_init();
1012 1013 1014 1015 1016
	return 0;
}

void __connman_storage_cleanup(void)
{
1017
	DBG("");
1018 1019
	storage_dir_cleanup(storage_dir, STORAGE_DIR_TYPE_MAIN);
	storage_dir_cleanup(vpn_storage_dir, STORAGE_DIR_TYPE_VPN);
1020
	keyfile_cleanup();
1021 1022
	g_free(storage_dir);
	g_free(vpn_storage_dir);
1023
}