Adding a fallback to vconftool
[platform/core/appfw/ail.git] / src / ail_vconf.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #if !defined(NO_VCONF_BUXTON_FALLBACK)
23
24 #define _GNU_SOURCE
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29
30 #include <vconf.h>
31 #include "ail_vconf.h"
32
33 #define VCONFTOOL               "/usr/bin/vconftool"
34 #define CMD_VCONF_GET_STR       VCONFTOOL " -q get '%s'"
35 #define CMD_VCONF_SET_STR       VCONFTOOL " -q set -t string '%s' '%s' -f"
36
37 /*
38
39  Reads the content of the input 'stream' (it should be a text without nul
40 characters) in a feshly allocated buffer, using allocations of 'block_size'
41 increment.
42
43  Returns the read string or NULL in case of error.
44 */
45 static char *_pread_(FILE *stream, size_t block_size)
46 {
47         char *result = NULL;
48         size_t alloc = 0;
49         size_t length = 0;
50         char *string;
51
52         for(;;) {
53                 /* is on error ? */
54                 if (ferror(stream) != 0) {
55                         free(result);
56                         return NULL;
57                 }
58                 /* allocate enough memory */
59                 /* assert(length <= alloc) */
60                 if (length >= alloc) {
61                         alloc += block_size;
62                         string = realloc(result, alloc + 1); /* one more for ending null */
63                         if (string == NULL) {
64                                 free(result);
65                                 return NULL;
66                         }
67                         result = string;
68                 }
69                 /* assert(length < alloc) */
70                 /* assert(result != NULL); */
71                 /* is at end ? */
72                 if (feof(stream) != 0) {
73                         result[length] = 0;
74                         return result;
75                 }
76                 length += fread(result + length, 1, alloc - length, stream);
77         }
78 }
79
80 /*
81  Runs the command given by 'cmddef' and its arguments formated as printf.
82
83  The resulting output stream of the command is return as a freshly allocated
84 string in '*readen'.
85
86  Retruns 0 in case of success or -1 in case of error.
87 */
88 static int _ail_vconf_exec_(char **readen, const char *cmddef, ...)
89 {
90         int result;
91         FILE *stream;
92         char *command;
93         va_list ap;
94
95         *readen = NULL;
96         va_start(ap, cmddef);
97         result = vasprintf(&command, cmddef, ap);
98         va_end(ap);
99         if (result >= 0) {
100                 result = -1;
101                 stream = popen(command, "r");
102                 if (stream != NULL) {
103                         *readen = _pread_(stream, 1024);
104                         if (pclose(stream) != -1 && *readen != NULL) {
105                                 result = 0;
106                         }
107                 }
108                 free(command);
109         }
110         return result;
111 }
112
113 /*
114  vconf_get_str with fallback to the command vconftool.
115 */
116 char *ail_vconf_get_str(const char *keyname)
117 {
118         char *result;
119         char *data;
120         int status;
121         size_t length;
122
123         result = vconf_get_str(keyname);
124         if (result == NULL) {
125                 status = _ail_vconf_exec_(&data, CMD_VCONF_GET_STR, keyname);
126                 if (status == 0) {
127                         /* the string data is of the form 'key, value = ...\n' */
128                         result = strstr(data, " = ");
129                         if (result != NULL) {
130                                 /* skips the prefix " = " */
131                                 result = result + 3;
132                                 /* remove trailing '\n' */
133                                 length = strlen(result);
134                                 if (length > 0 && result[length-1] == '\n') {
135                                         result[length-1] = 0;
136                                 }
137                                 /* get the final result */
138                                 result = strdup(result);
139                         }
140                 }
141                 free(data);
142         }
143         return result;
144 }
145
146 /*
147  vconf_set_str with fallback to the command vconftool.
148 */
149 int ail_vconf_set_str(const char *keyname, const char *strval)
150 {
151         int result;
152         char *data;
153
154         result = vconf_set_str(keyname, strval);
155         if (result != 0) {
156                 result = _ail_vconf_exec_(&data, CMD_VCONF_SET_STR, keyname, strval);
157                 free(data);
158         }
159         return result;
160 }
161
162 #endif
163
164
165