summaryrefslogtreecommitdiff
path: root/src/storage.c
blob: 271d49cc88dfe9dc08bf1fca7233688a810d5f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
 *
 *  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 "connman.h"

#define PROFILE_SUFFIX	"profile"
#define CONFIG_SUFFIX	"config"

GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
{
	GKeyFile *keyfile;
	gchar *pathname, *data = NULL;
	gboolean result;
	gsize length;

	DBG("ident %s suffix %s", ident, suffix);

	pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
	if (pathname == NULL)
		return NULL;

	result = g_file_get_contents(pathname, &data, &length, NULL);

	g_free(pathname);

	keyfile = g_key_file_new();

	if (result == FALSE)
		goto done;

	if (length > 0)
		g_key_file_load_from_data(keyfile, data, length, 0, NULL);

	g_free(data);

done:
	DBG("keyfile %p", keyfile);

	return keyfile;
}

void __connman_storage_close(const char *ident, const char *suffix,
					GKeyFile *keyfile, gboolean save)
{
	gchar *pathname, *data = NULL;
	gsize length = 0;

	DBG("ident %s suffix %s keyfile %p save %d",
					ident, suffix, keyfile, save);

	if (save == FALSE) {
		g_key_file_free(keyfile);
		return;
	}

	pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
	if (pathname == NULL)
		return;

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

	if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
		connman_error("Failed to store information");

	g_free(data);

	g_free(pathname);

	g_key_file_free(keyfile);
}

void __connman_storage_delete(const char *ident, const char *suffix)
{
	gchar *pathname;

	DBG("ident %s suffix %s", ident, suffix);

	pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
	if (pathname == NULL)
		return;

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

GKeyFile *__connman_storage_open_profile(const char *ident)
{
	return __connman_storage_open(ident, PROFILE_SUFFIX);
}

void __connman_storage_close_profile(const char *ident,
					GKeyFile *keyfile, gboolean save)
{
	__connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
}

void __connman_storage_delete_profile(const char *ident)
{
	__connman_storage_delete(ident, PROFILE_SUFFIX);
}

GKeyFile *__connman_storage_open_config(const char *ident)
{
	return __connman_storage_open(ident, CONFIG_SUFFIX);
}

void __connman_storage_close_config(const char *ident,
					GKeyFile *keyfile, gboolean save)
{
	__connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
}

void __connman_storage_delete_config(const char *ident)
{
	__connman_storage_delete(ident, CONFIG_SUFFIX);
}