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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/* -*- indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* LD_PRELOAD functions to make libgconf read defaults out of local schemas
* Copyright (C) 2009 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE /* for RTLD_NEXT */
#include <dlfcn.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "gconf-defaults-hack.h"
gboolean
gconf_defaults_hack_verbose (void)
{
static int verbose = -1;
if (verbose < 0)
{
const char *str = g_getenv ("GCONF_DEFAULTS_VERBOSE");
verbose = (str && str[0]);
}
return verbose;
}
GConfValueType
gconf_defaults_hack_value_type_from_string (const char *str)
{
if (str == NULL)
return GCONF_VALUE_INVALID;
switch (str[0])
{
case 'b':
if (strcmp (str, "bool") == 0)
return GCONF_VALUE_BOOL;
break;
case 'f':
if (strcmp (str, "float") == 0)
return GCONF_VALUE_FLOAT;
break;
case 'i':
if (strcmp (str, "int") == 0)
return GCONF_VALUE_INT;
break;
case 'l':
if (strcmp (str, "list") == 0)
return GCONF_VALUE_LIST;
break;
case 'p':
if (strcmp (str, "pair") == 0)
return GCONF_VALUE_PAIR;
break;
case 's':
if (strcmp (str, "string") == 0)
return GCONF_VALUE_STRING;
break;
}
return GCONF_VALUE_INVALID;
}
static time_t
get_mtime (const char *filename)
{
struct stat st;
if (stat (filename, &st) == 0)
{
return st.st_mtime;
}
else
return (time_t)-1;
}
static time_t
get_dir_mtime (const char *dir_filename)
{
GDir *dir = NULL;
GError *error = NULL;
time_t mtime;
dir = g_dir_open (dir_filename, 0, &error);
if (!dir)
{
g_error_free (error);
return (time_t)-1;
}
mtime = get_mtime (dir_filename);
while (TRUE)
{
char *filename;
const char *name = g_dir_read_name (dir);
time_t file_mtime;
if (!name)
break;
if (!g_str_has_suffix (name, ".schemas"))
continue;
filename = g_build_filename (dir_filename, name, NULL);
file_mtime = get_mtime (filename);
if (file_mtime != (time_t)-1 && file_mtime > mtime)
mtime = file_mtime;
g_free (filename);
}
g_dir_close (dir);
return mtime;
}
static GConfDefaultsHackDatabase *
open_database (void)
{
GConfDefaultsHackDatabase *database = NULL;
const char *schemas_dir;
gboolean schemas_dir_writable;
char *db_file;
time_t db_mtime;
time_t dir_mtime;
schemas_dir = g_getenv ("GCONF_DEFAULTS_SCHEMAS_DIR");
if (schemas_dir == NULL)
schemas_dir = SYSCONFDIR "/gconf/schemas";
schemas_dir_writable = access (schemas_dir, W_OK) == 0;
if (schemas_dir_writable)
{
db_file = g_build_filename (schemas_dir, ".gconfdefaults.sqlite", NULL);
}
else
{
GString *tmp = g_string_new (".gconfdefaults.");
const char *p;
gboolean last_was_slash = FALSE;
for (p = schemas_dir; *p; p++)
{
if (*p == '/')
{
if (!last_was_slash)
g_string_append_c (tmp, '_');
last_was_slash = TRUE;
}
else
{
if (*p == '_')
g_string_append (tmp, "__");
else
g_string_append_c (tmp, *p);
last_was_slash = FALSE;
}
}
g_string_append (tmp, ".sqlite");
db_file = g_build_filename (g_get_home_dir(), tmp->str, NULL);
g_string_free (tmp, TRUE);
}
if (gconf_defaults_hack_verbose ())
{
g_printerr("gconf-defaults-hack: schemas_dir=%s, db_file=%s\n",
schemas_dir, db_file);
}
db_mtime = get_mtime (db_file);
dir_mtime = get_dir_mtime (schemas_dir);
if (dir_mtime != (time_t)-1)
{
database = gconf_defaults_hack_database_open (db_file);
if (database &&
(db_mtime == (time_t)-1 || db_mtime < dir_mtime))
{
if (gconf_defaults_hack_verbose ())
g_printerr ("Rebuilding database: db_mtime=%ld, dir_mtime=%ld\n",
(long)db_mtime, (long)dir_mtime);
gconf_defaults_hack_rebuild_database (database, schemas_dir);
}
}
g_free (db_file);
return database;
}
static GConfDefaultsHackDatabase *
get_database (void)
{
static GConfDefaultsHackDatabase *database;
static gboolean initialized = FALSE;
if (!initialized)
{
initialized = TRUE;
database = open_database ();
}
return database;
}
static GConfValueType
validate_element_type (const char *str,
GError **error)
{
GConfValueType type;
if (str == NULL)
{
g_set_error (error, GCONF_ERROR, GCONF_ERROR_CORRUPT,
"Missing element type in defaults database");
return GCONF_VALUE_INVALID;
}
type = gconf_defaults_hack_value_type_from_string (str);
if (type == GCONF_VALUE_INVALID ||
type == GCONF_VALUE_LIST ||
type == GCONF_VALUE_PAIR)
{
g_set_error (error, GCONF_ERROR, GCONF_ERROR_CORRUPT,
"Bad element type '%s' in defaults database", str);
return GCONF_VALUE_INVALID;
}
return type;
}
static GConfValue *
get_default_from_database (const char *key,
GError **error)
{
GConfDefaultsHackDatabase *database = get_database ();
GConfDefaultsHackEntry *entry;
GConfValue *value = NULL;
GConfValueType type;
if (database == NULL)
goto out;
entry = gconf_defaults_hack_database_get (database, key);
if (!entry)
goto out;
if (entry->default_value == NULL)
{
g_set_error (error, GCONF_ERROR, GCONF_ERROR_CORRUPT,
"Defaults database entry is missing a value");
goto out;
}
type = gconf_defaults_hack_value_type_from_string (entry->type);
switch (type)
{
case GCONF_VALUE_INVALID:
case GCONF_VALUE_SCHEMA:
g_set_error (error, GCONF_ERROR, GCONF_ERROR_CORRUPT,
"Bad type '%s' in defaults database",
entry->type ? entry->type : "<none>");
break;
case GCONF_VALUE_STRING:
case GCONF_VALUE_INT:
case GCONF_VALUE_FLOAT:
case GCONF_VALUE_BOOL:
value = gconf_value_new_from_string (type, entry->default_value, error);
break;
case GCONF_VALUE_LIST:
{
GConfValueType list_type;
list_type = validate_element_type (entry->list_type, error);
if (list_type == GCONF_VALUE_INVALID)
break;
value = gconf_defaults_hack_value_new_list_from_string (list_type,
entry->default_value,
error);
}
case GCONF_VALUE_PAIR:
{
GConfValueType car_type;
GConfValueType cdr_type;
car_type = validate_element_type (entry->car_type, error);
if (car_type == GCONF_VALUE_INVALID)
break;
cdr_type = validate_element_type (entry->cdr_type, error);
if (cdr_type == GCONF_VALUE_INVALID)
break;
value = gconf_defaults_hack_value_new_pair_from_string (car_type, cdr_type,
entry->default_value,
error);
}
}
out:
if (entry)
gconf_defaults_hack_entry_free (entry);
return value;
}
GConfValue *
gconf_engine_get_fuller (GConfEngine *conf,
const gchar *key,
const gchar *locale,
gboolean use_schema_default,
gboolean *is_default_p,
gboolean *is_writable_p,
gchar **schema_name_p,
GError **err)
{
static GConfValue * (*next) (GConfEngine *conf,
const gchar *key,
const gchar *locale,
gboolean use_schema_default,
gboolean *is_default_p,
gboolean *is_writable_p,
gchar **schema_name_p,
GError **err);
GConfValue *result;
if (next == NULL)
next = dlsym (RTLD_NEXT, "gconf_engine_get_fuller");
result = (*next) (conf, key, locale, use_schema_default,
is_default_p, is_writable_p, schema_name_p, err);
if (result != NULL ||
!use_schema_default ||
conf != gconf_engine_get_default ())
return result;
g_clear_error (err);
result = get_default_from_database (key, err);
if (result)
{
if (is_default_p)
*is_default_p = TRUE;
if (is_writable_p)
*is_writable_p = TRUE; /* A guess */
if (schema_name_p)
*schema_name_p = g_strconcat ("/schemas/", key, NULL); /* probably right... */
}
return result;
}
GConfValue*
gconf_engine_get_default_from_schema (GConfEngine* conf,
const gchar* key,
GError** err)
{
static GConfValue* (*next) (GConfEngine* conf,
const gchar* key,
GError** err);
GConfValue *result;
if (next == NULL)
next = dlsym (RTLD_NEXT, "gconf_engine_get_default_from_schema");
result = (*next) (conf, key, err);
if (result != NULL ||
conf != gconf_engine_get_default ())
return result;
g_clear_error (err);
return get_default_from_database (key, err);
}
|