storage: remove storage module framework
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <unistd.h>
28
29 #include "connman.h"
30
31 #define PROFILE_SUFFIX  "profile"
32 #define CONFIG_SUFFIX   "config"
33
34 GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
35 {
36         GKeyFile *keyfile;
37         gchar *pathname, *data = NULL;
38         gboolean result;
39         gsize length;
40
41         DBG("ident %s suffix %s", ident, suffix);
42
43         pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
44         if (pathname == NULL)
45                 return NULL;
46
47         result = g_file_get_contents(pathname, &data, &length, NULL);
48
49         g_free(pathname);
50
51         keyfile = g_key_file_new();
52
53         if (result == FALSE)
54                 goto done;
55
56         if (length > 0)
57                 g_key_file_load_from_data(keyfile, data, length, 0, NULL);
58
59         g_free(data);
60
61 done:
62         DBG("keyfile %p", keyfile);
63
64         return keyfile;
65 }
66
67 void __connman_storage_close(const char *ident, const char *suffix,
68                                         GKeyFile *keyfile, gboolean save)
69 {
70         gchar *pathname, *data = NULL;
71         gsize length = 0;
72
73         DBG("ident %s suffix %s keyfile %p save %d",
74                                         ident, suffix, keyfile, save);
75
76         if (save == FALSE) {
77                 g_key_file_free(keyfile);
78                 return;
79         }
80
81         pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
82         if (pathname == NULL)
83                 return;
84
85         data = g_key_file_to_data(keyfile, &length, NULL);
86
87         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
88                 connman_error("Failed to store information");
89
90         g_free(data);
91
92         g_free(pathname);
93
94         g_key_file_free(keyfile);
95 }
96
97 void __connman_storage_delete(const char *ident, const char *suffix)
98 {
99         gchar *pathname;
100
101         DBG("ident %s suffix %s", ident, suffix);
102
103         pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
104         if (pathname == NULL)
105                 return;
106
107         if (unlink(pathname) < 0)
108                 connman_error("Failed to remove %s", pathname);
109 }
110
111 GKeyFile *__connman_storage_open_profile(const char *ident)
112 {
113         return __connman_storage_open(ident, PROFILE_SUFFIX);
114 }
115
116 void __connman_storage_close_profile(const char *ident,
117                                         GKeyFile *keyfile, gboolean save)
118 {
119         __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
120 }
121
122 void __connman_storage_delete_profile(const char *ident)
123 {
124         __connman_storage_delete(ident, PROFILE_SUFFIX);
125 }
126
127 GKeyFile *__connman_storage_open_config(const char *ident)
128 {
129         return __connman_storage_open(ident, CONFIG_SUFFIX);
130 }
131
132 void __connman_storage_close_config(const char *ident,
133                                         GKeyFile *keyfile, gboolean save)
134 {
135         __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
136 }
137
138 void __connman_storage_delete_config(const char *ident)
139 {
140         __connman_storage_delete(ident, CONFIG_SUFFIX);
141 }