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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
|
/* GConf
* Copyright (C) 1999, 2000 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "xml-dir.h"
#include "xml-entry.h"
#include <libxml/parser.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <gconf/gconf-internals.h>
/* This makes hash table safer when debugging */
#ifndef GCONF_ENABLE_DEBUG
#define safe_g_hash_table_insert g_hash_table_insert
#else
static void
safe_g_hash_table_insert(GHashTable* ht, gpointer key, gpointer value)
{
gpointer oldkey = NULL, oldval = NULL;
if (g_hash_table_lookup_extended(ht, key, &oldkey, &oldval))
{
gconf_log(GCL_WARNING, "Hash key `%s' is already in the table!",
(gchar*)key);
return;
}
else
{
g_hash_table_insert(ht, key, value);
}
}
#endif
static xmlDocPtr my_xml_parse_file (const char *filename,
GError **err);
static gboolean dir_rescan_subdirs (Dir* d, GError** err);
struct _Dir {
gchar* key;
gchar* parent_key;
gchar* fs_dirname;
gchar* xml_filename;
guint root_dir_len;
GTime last_access; /* so we know when to un-cache */
xmlDocPtr doc;
GHashTable* entry_cache; /* store key-value entries */
guint dir_mode;
guint file_mode;
GSList *subdir_names;
guint dirty : 1;
guint need_rescan_subdirs : 1;
};
static void
dir_load_doc(Dir* d, GError** err);
static Entry* dir_make_new_entry(Dir* d, const gchar* relative_key);
static gboolean dir_forget_entry_if_useless(Dir* d, Entry* e);
static Dir*
dir_blank(const gchar* key)
{
Dir* d;
d = g_new0(Dir, 1);
#ifdef GCONF_ENABLE_DEBUG
{
gchar* why;
if (!gconf_valid_key(key, &why)) {
gconf_log(GCL_DEBUG, "key `%s' invalid: %s",
key, why);
}
g_assert(gconf_valid_key(key, NULL));
}
#endif
d->key = g_strdup (key);
d->parent_key = gconf_key_directory (key);
d->last_access = time(NULL);
d->doc = NULL;
d->entry_cache = g_hash_table_new (g_str_hash, g_str_equal);
d->dirty = FALSE;
d->need_rescan_subdirs = TRUE;
d->subdir_names = NULL;
d->dir_mode = 0700;
d->file_mode = 0600;
return d;
}
Dir*
dir_new (const gchar *keyname,
const gchar *xml_root_dir,
guint dir_mode,
guint file_mode)
{
Dir* d;
d = dir_blank(keyname);
/* sync with dir_load() */
d->fs_dirname = gconf_concat_dir_and_key(xml_root_dir, keyname);
d->xml_filename = g_strconcat(d->fs_dirname, "/%gconf.xml", NULL);
d->root_dir_len = strlen(xml_root_dir);
d->dir_mode = dir_mode;
d->file_mode = file_mode;
return d;
}
Dir*
dir_load (const gchar* key, const gchar* xml_root_dir, GError** err)
{
Dir* d;
gchar* fs_dirname;
gchar* xml_filename;
guint dir_mode = 0700;
guint file_mode = 0600;
g_return_val_if_fail(gconf_valid_key(key, NULL), NULL);
fs_dirname = gconf_concat_dir_and_key(xml_root_dir, key);
xml_filename = g_strconcat(fs_dirname, "/%gconf.xml", NULL);
{
struct stat s;
gboolean notfound = FALSE;
if (g_stat(xml_filename, &s) != 0)
{
if (errno != ENOENT)
{
gconf_set_error (err, GCONF_ERROR_FAILED,
_("Could not stat `%s': %s"),
xml_filename, g_strerror(errno));
}
notfound = TRUE;
}
else if (S_ISDIR(s.st_mode))
{
gconf_set_error (err, GCONF_ERROR_FAILED,
_("XML filename `%s' is a directory"),
xml_filename);
notfound = TRUE;
}
if (notfound)
{
gconf_log(GCL_DEBUG, "dir file %s not found", xml_filename);
g_free(fs_dirname);
g_free(xml_filename);
return NULL;
}
else
{
/* Take directory mode from the xml_root_dir, if possible */
if (g_stat (xml_root_dir, &s) == 0)
{
dir_mode = _gconf_mode_t_to_mode (s.st_mode);
}
file_mode = dir_mode & ~0111; /* turn off search bits */
}
}
d = dir_blank(key);
/* sync with dir_new() */
d->fs_dirname = fs_dirname;
d->xml_filename = xml_filename;
d->root_dir_len = strlen(xml_root_dir);
d->dir_mode = dir_mode;
d->file_mode = file_mode;
gconf_log (GCL_DEBUG, "loaded dir %s", fs_dirname);
return d;
}
static void
entry_destroy_foreach(const gchar* name, Entry* e, gpointer data)
{
entry_destroy (e);
}
void
dir_destroy(Dir* d)
{
g_free (d->key);
g_free (d->parent_key);
g_free (d->fs_dirname);
g_free (d->xml_filename);
g_slist_foreach (d->subdir_names, (GFunc) g_free, NULL);
g_slist_free (d->subdir_names);
g_hash_table_foreach (d->entry_cache, (GHFunc)entry_destroy_foreach,
NULL);
g_hash_table_destroy (d->entry_cache);
if (d->doc != NULL)
xmlFreeDoc (d->doc);
g_free (d);
}
static gboolean
create_fs_dir(const gchar* dir, const gchar* xml_filename,
guint root_dir_len,
guint dir_mode, guint file_mode,
GError** err);
gboolean
dir_ensure_exists (Dir* d,
GError** err)
{
if (!create_fs_dir(d->fs_dirname, d->xml_filename, d->root_dir_len,
d->dir_mode, d->file_mode,
err))
{
/* check that error is set */
g_return_val_if_fail( (err == NULL) || (*err != NULL), FALSE );
return FALSE;
}
else
{
return TRUE;
}
}
static void
entry_sync_foreach(const gchar* name, Entry* e, gpointer data)
{
entry_sync_to_node(e);
}
gboolean
dir_sync_pending (Dir *d)
{
return d->dirty;
}
void
dir_child_removed (Dir *d,
const char *child_name)
{
GSList *tmp;
/* dirty because we need to consider removing
* this directory, it may have become empty.
*/
d->dirty = TRUE;
if (d->need_rescan_subdirs)
return; /* subdir_names is totally invalid anyhow */
tmp = d->subdir_names;
while (tmp != NULL)
{
if (strcmp (tmp->data, child_name) == 0)
{
char *tofree = tmp->data;
d->subdir_names = g_slist_remove (d->subdir_names,
tofree);
g_free (tofree);
break;
}
tmp = tmp->next;
}
}
void
dir_child_added (Dir *d,
const char *child_name)
{
if (d->need_rescan_subdirs)
return;
if (g_slist_find_custom (d->subdir_names,
child_name,
(GCompareFunc) strcmp) == NULL)
d->subdir_names = g_slist_prepend (d->subdir_names,
g_strdup (child_name));
}
/* directories auto-disappear when they're empty */
static gboolean
dir_useless (Dir *d)
{
if (d->doc == NULL)
dir_load_doc (d, NULL);
if (d->need_rescan_subdirs)
dir_rescan_subdirs (d, NULL);
return
d->subdir_names == NULL &&
g_hash_table_size (d->entry_cache) == 0;
}
/* for info on why this is used rather than xmlDocDump or xmlSaveFile
* and friends, see http://bugzilla.gnome.org/show_bug.cgi?id=108329 */
static int
gconf_xml_doc_dump (FILE *fp, xmlDocPtr doc)
{
char *xmlbuf;
int fd, n;
xmlDocDumpFormatMemory (doc, (xmlChar **) &xmlbuf, &n, TRUE);
if (n <= 0)
{
errno = ENOMEM;
return -1;
}
if (fwrite (xmlbuf, sizeof (xmlChar), n, fp) < n)
{
xmlFree (xmlbuf);
return -1;
}
xmlFree (xmlbuf);
/* From the fflush(3) man page:
*
* Note that fflush() only flushes the user space buffers provided by the
* C library. To ensure that the data is physically stored on disk the
* kernel buffers must be flushed too, e.g. with sync(2) or fsync(2).
*/
/* flush user-space buffers */
if (fflush (fp) != 0)
return -1;
if ((fd = fileno (fp)) == -1)
return -1;
#ifdef HAVE_FSYNC
/* sync kernel-space buffers to disk */
if (fsync (fd) == -1)
return -1;
#endif
return 0;
}
gboolean
dir_sync (Dir *d,
gboolean *deleted,
GError **err)
{
gboolean retval = TRUE;
if (deleted)
*deleted = FALSE;
if (!d->dirty)
return TRUE;
gconf_log (GCL_DEBUG, "Syncing dir \"%s\"", d->key);
d->last_access = time (NULL);
if (dir_useless (d))
{
gconf_log (GCL_DEBUG, "Deleting useless dir \"%s\"",
d->key);
if (g_unlink (d->xml_filename) != 0)
{
gconf_set_error (err, GCONF_ERROR_FAILED, _("Failed to delete \"%s\": %s"),
d->xml_filename, g_strerror (errno));
return FALSE;
}
if (strcmp (d->key, "/") != 0) /* don't delete root dir */
{
if (g_rmdir (d->fs_dirname) != 0)
{
gconf_set_error (err, GCONF_ERROR_FAILED, _("Failed to delete \"%s\": %s"),
d->fs_dirname, g_strerror (errno));
return FALSE;
}
}
if (deleted)
*deleted = TRUE;
}
else
{
gboolean old_existed = FALSE;
gchar* tmp_filename;
gchar* old_filename;
FILE* outfile;
/* We should have a doc if deleted is FALSE */
g_assert(d->doc != NULL);
/* First make sure entry values are synced to their
XML nodes */
g_hash_table_foreach(d->entry_cache, (GHFunc)entry_sync_foreach, NULL);
tmp_filename = g_strconcat(d->fs_dirname, "/%gconf.xml.tmp", NULL);
old_filename = g_strconcat(d->fs_dirname, "/%gconf.xml.old", NULL);
outfile = g_fopen (tmp_filename, "w");
if (outfile == NULL)
{
/* Try to solve the problem by creating the FS dir */
if (!g_file_test (d->fs_dirname, G_FILE_TEST_EXISTS))
{
if (create_fs_dir(d->fs_dirname, d->xml_filename,
d->root_dir_len,
d->dir_mode, d->file_mode,
err))
outfile = g_fopen (tmp_filename, "w");
}
if (outfile == NULL)
{
/* Don't set error if it's already set by some
* earlier failure.
*/
if (err && *err == NULL)
gconf_set_error(err, GCONF_ERROR_FAILED, _("Failed to write file `%s': %s"),
tmp_filename, g_strerror(errno));
retval = FALSE;
goto failed_end_of_sync;
}
}
#ifdef HAVE_FCHMOD
/* Set permissions on the new file */
if (fchmod (fileno (outfile), d->file_mode) != 0)
{
gconf_set_error(err, GCONF_ERROR_FAILED,
_("Failed to set mode on `%s': %s"),
tmp_filename, g_strerror(errno));
retval = FALSE;
goto failed_end_of_sync;
}
#endif
if (gconf_xml_doc_dump (outfile, d->doc) < 0)
{
gconf_set_error (err, GCONF_ERROR_FAILED,
_("Failed to write XML data to `%s': %s"),
tmp_filename, g_strerror (errno));
retval = FALSE;
goto failed_end_of_sync;
}
if (fclose (outfile) < 0)
{
gconf_set_error (err, GCONF_ERROR_FAILED,
_("Failed to close file `%s': %s"),
tmp_filename, g_strerror (errno));
retval = FALSE;
outfile = NULL;
goto failed_end_of_sync;
}
outfile = NULL;
#ifndef HAVE_FCHMOD
/* Set permissions on the new file */
if (chmod (tmp_filename, d->file_mode) != 0)
{
gconf_set_error(err, GCONF_ERROR_FAILED,
_("Failed to set mode on `%s': %s"),
tmp_filename, g_strerror(errno));
retval = FALSE;
goto failed_end_of_sync;
}
#endif
old_existed = g_file_test (d->xml_filename, G_FILE_TEST_EXISTS);
if (old_existed)
{
if (g_rename(d->xml_filename, old_filename) < 0)
{
gconf_set_error(err, GCONF_ERROR_FAILED,
_("Failed to rename `%s' to `%s': %s"),
d->xml_filename, old_filename, g_strerror(errno));
retval = FALSE;
goto failed_end_of_sync;
}
}
if (g_rename(tmp_filename, d->xml_filename) < 0)
{
gconf_set_error(err, GCONF_ERROR_FAILED, _("Failed to rename `%s' to `%s': %s"),
tmp_filename, d->xml_filename, g_strerror(errno));
/* Put the original file back, so this isn't a total disaster. */
if (g_rename(old_filename, d->xml_filename) < 0)
{
gconf_set_error(err, GCONF_ERROR_FAILED, _("Failed to restore `%s' from `%s': %s"),
d->xml_filename, old_filename, g_strerror(errno));
}
retval = FALSE;
goto failed_end_of_sync;
}
if (old_existed)
{
if (g_unlink(old_filename) < 0)
{
gconf_log(GCL_WARNING, _("Failed to delete old file `%s': %s"),
old_filename, g_strerror(errno));
/* Not a failure, just leaves cruft around. */
}
}
failed_end_of_sync:
g_free(old_filename);
g_free(tmp_filename);
if (outfile)
fclose (outfile);
}
if (retval)
d->dirty = FALSE;
return retval;
}
void
dir_set_value (Dir* d, const gchar* relative_key,
const GConfValue* value, GError** err)
{
Entry* e;
if (d->doc == NULL)
dir_load_doc(d, err);
if (d->doc == NULL)
{
g_return_if_fail( (err == NULL) || (*err != NULL) );
return;
}
e = g_hash_table_lookup(d->entry_cache, relative_key);
if (e == NULL)
e = dir_make_new_entry(d, relative_key);
entry_set_value(e, value);
d->last_access = time(NULL);
entry_set_mod_time(e, d->last_access);
entry_set_mod_user(e, g_get_user_name());
d->dirty = TRUE;
}
GTime
dir_get_last_access (Dir *d)
{
return d->last_access;
}
GConfValue*
dir_get_value (Dir* d,
const gchar* relative_key,
const gchar** locales,
gchar** schema_name,
GError** err)
{
Entry* e;
if (d->doc == NULL)
dir_load_doc(d, err);
if (d->doc == NULL)
{
g_return_val_if_fail( (err == NULL) || (*err != NULL), NULL );
return NULL;
}
e = g_hash_table_lookup(d->entry_cache, relative_key);
d->last_access = time(NULL);
if (e == NULL)
{
/* No entry; return */
return NULL;
}
else
{
GConfValue* val;
g_assert(e != NULL);
val = entry_get_value (e, locales, err);
/* Get schema name if requested */
if (schema_name && entry_get_schema_name (e))
*schema_name = g_strdup (entry_get_schema_name (e));
/* return copy of the value */
if (val != NULL)
return gconf_value_copy(val);
else
return NULL;
}
}
const gchar*
dir_get_name (Dir *d)
{
g_return_val_if_fail (d != NULL, NULL);
return d->key;
}
const char*
dir_get_parent_name (Dir *d)
{
g_return_val_if_fail (d != NULL, NULL);
return d->parent_key;
}
GConfMetaInfo*
dir_get_metainfo(Dir* d, const gchar* relative_key, GError** err)
{
Entry* e;
d->last_access = time(NULL);
if (d->doc == NULL)
dir_load_doc(d, err);
if (d->doc == NULL)
{
g_return_val_if_fail( (err == NULL) || (*err != NULL), NULL );
return NULL;
}
e = g_hash_table_lookup(d->entry_cache, relative_key);
if (e == NULL)
return NULL;
else
return entry_get_metainfo(e);
}
void
dir_unset_value (Dir* d, const gchar* relative_key,
const gchar* locale, GError** err)
{
Entry* e;
d->last_access = time(NULL);
if (d->doc == NULL)
dir_load_doc(d, err);
if (d->doc == NULL)
{
g_return_if_fail( (err == NULL) || (*err != NULL) );
return;
}
e = g_hash_table_lookup(d->entry_cache, relative_key);
if (e == NULL) /* nothing to change */
return;
if (entry_unset_value(e, locale))
{
/* If entry_unset() returns TRUE then
the entry was changed (not already unset) */
d->dirty = TRUE;
if (dir_forget_entry_if_useless(d, e))
{
/* entry is destroyed */
return;
}
else
{
entry_set_mod_time(e, d->last_access);
entry_set_mod_user(e, g_get_user_name());
}
}
else
{
/* Check uselessness anyway; this ensures that if it was useless
when the daemon started or we otherwise missed its lack of
utility, we clean it up if the user does an explicit unset */
dir_forget_entry_if_useless(d, e);
}
}
typedef struct _ListifyData ListifyData;
struct _ListifyData {
GSList* list;
const gchar* name;
const gchar** locales;
};
static void
listify_foreach(const gchar* key, Entry* e, ListifyData* ld)
{
GConfValue* val;
GConfEntry* entry;
GError* error = NULL;
val = entry_get_value (e, ld->locales, &error);
if (error != NULL)
{
g_assert (val == NULL);
g_error_free (error);
return;
}
entry = gconf_entry_new_nocopy (g_strdup(key),
val ? gconf_value_copy(val) : NULL);
if (entry_get_schema_name (e))
{
gconf_entry_set_schema_name (entry, entry_get_schema_name (e));
}
ld->list = g_slist_prepend(ld->list, entry);
}
GSList*
dir_all_entries (Dir* d, const gchar** locales, GError** err)
{
ListifyData ld;
if (d->doc == NULL)
dir_load_doc(d, err);
if (d->doc == NULL)
{
g_return_val_if_fail( (err == NULL) || (*err != NULL), NULL );
return NULL;
}
ld.list = NULL;
ld.name = d->key;
ld.locales = locales;
g_hash_table_foreach(d->entry_cache, (GHFunc)listify_foreach,
&ld);
return ld.list;
}
static GSList*
copy_string_list (GSList *src)
{
GSList *copy;
GSList *tmp;
copy = NULL;
tmp = src;
while (tmp != NULL)
{
copy = g_slist_prepend (copy, g_strdup (tmp->data));
tmp = tmp->next;
}
copy = g_slist_reverse (copy);
return copy;
}
static gboolean
dir_rescan_subdirs (Dir* d, GError** err)
{
GDir* dp;
const char* dent;
struct stat statbuf;
GSList* retval = NULL;
gchar* fullpath;
gchar* fullpath_end;
guint len;
guint subdir_len;
if (d->doc == NULL)
dir_load_doc (d, err);
if (d->doc == NULL)
{
g_return_val_if_fail ((err == NULL) || (*err != NULL), FALSE);
return FALSE;
}
if (!d->need_rescan_subdirs)
return TRUE;
g_slist_foreach (d->subdir_names, (GFunc) g_free, NULL);
g_slist_free (d->subdir_names);
d->subdir_names = NULL;
dp = g_dir_open (d->fs_dirname, 0, NULL);
if (dp == NULL)
{
d->need_rescan_subdirs = FALSE;
return TRUE;
}
len = strlen(d->fs_dirname);
subdir_len = PATH_MAX - len;
fullpath = g_malloc0(subdir_len + len + 20); /* ensure null termination */
strcpy(fullpath, d->fs_dirname);
fullpath_end = fullpath + len;
*fullpath_end = '/';
++fullpath_end;
*fullpath_end = '\0';
while ((dent = g_dir_read_name(dp)) != NULL)
{
/* ignore all dot-files */
if (dent[0] == '.')
continue;
len = strlen(dent);
if (len < subdir_len)
{
strcpy(fullpath_end, dent);
strncpy(fullpath_end+len, "/%gconf.xml", subdir_len - len);
}
else
continue; /* Shouldn't ever happen since PATH_MAX is available */
if (g_stat(fullpath, &statbuf) < 0)
{
/* This is some kind of cruft, not an XML directory */
continue;
}
retval = g_slist_prepend (retval, g_strdup(dent));
}
/* if this fails, we really can't do a thing about it
* and it's not a meaningful error
*/
g_dir_close (dp);
g_free (fullpath);
d->subdir_names = retval;
d->need_rescan_subdirs = FALSE;
return TRUE;
}
GSList*
dir_all_subdirs (Dir* d, GError** err)
{
if (!dir_rescan_subdirs (d, err))
return NULL;
return copy_string_list (d->subdir_names);
}
void
dir_set_schema (Dir *d,
const gchar *relative_key,
const gchar *schema_key,
GError **err)
{
Entry* e;
if (d->doc == NULL)
dir_load_doc (d, err);
if (d->doc == NULL)
{
g_return_if_fail ((err == NULL) || (*err != NULL));
return;
}
d->dirty = TRUE;
d->last_access = time (NULL);
e = g_hash_table_lookup (d->entry_cache, relative_key);
if (e == NULL)
e = dir_make_new_entry (d, relative_key);
entry_set_mod_time (e, d->last_access);
entry_set_schema_name (e, schema_key);
if (schema_key == NULL)
dir_forget_entry_if_useless (d, e);
}
GTime
dir_last_access (Dir* d)
{
return d->last_access;
}
/* private Dir functions */
static void
dir_fill_cache_from_doc(Dir* d);
static void
dir_load_doc(Dir* d, GError** err)
{
gboolean xml_already_exists = TRUE;
gboolean need_backup = FALSE;
struct stat statbuf;
g_return_if_fail(d->doc == NULL);
if (stat(d->xml_filename, &statbuf) < 0)
{
switch (errno)
{
case ENOENT:
xml_already_exists = FALSE;
break;
case ENOTDIR:
#ifdef ELOOP
case ELOOP:
#endif
case EFAULT:
case EACCES:
case ENOMEM:
case ENAMETOOLONG:
default:
/* These are all fatal errors */
gconf_set_error(err, GCONF_ERROR_FAILED, _("Failed to stat `%s': %s"),
d->xml_filename, g_strerror(errno));
return;
break;
}
}
if (statbuf.st_size == 0)
{
xml_already_exists = FALSE;
}
if (xml_already_exists)
{
GError *tmp_err;
gboolean error_was_fatal;
error_was_fatal = FALSE;
tmp_err = NULL;
d->doc = my_xml_parse_file (d->xml_filename, &tmp_err);
if (tmp_err != NULL)
{
gconf_log (GCL_WARNING,
"%s", tmp_err->message);
/* file errors are assumed to be some kind of
* blowup, like out of file descriptors, so
* we play it safe and don't touch anything
*/
if (tmp_err->domain == G_FILE_ERROR)
error_was_fatal = TRUE;
g_error_free (tmp_err);
}
if (error_was_fatal)
return;
}
/* We recover from parse errors instead of passing them up */
/* This has the potential to just blow away an entire corrupted
* config file; but I think that is better than the alternatives
* (disabling config for a directory because the document is mangled).
*
* Parse errors really should not happen from an XML file we created
* ourselves anyway...
*/
/* Also we create empty %gconf.xml files when we create a new dir,
* and those return a parse error, though they should be trapped
* by the statbuf.st_size == 0 check above.
*/
if (d->doc == NULL)
{
if (xml_already_exists)
need_backup = TRUE; /* rather uselessly save whatever broken stuff was in the file */
/* Create a new doc */
d->doc = xmlNewDoc((xmlChar *)"1.0");
}
if (d->doc->xmlRootNode == NULL)
{
/* fill it in */
d->doc->xmlRootNode = xmlNewDocNode(d->doc, NULL, "gconf", NULL);
}
else if (strcmp((char*)d->doc->xmlRootNode->name, "gconf") != 0)
{
xmlFreeDoc(d->doc);
d->doc = xmlNewDoc((xmlChar*)"1.0");
d->doc->xmlRootNode = xmlNewDocNode(d->doc, NULL, (xmlChar *)"gconf", NULL);
need_backup = TRUE; /* save broken stuff */
}
else
{
/* We had an initial doc with a valid root */
/* Fill child_cache from entries */
dir_fill_cache_from_doc(d);
}
if (need_backup)
{
/* Back up the file we failed to parse, if it exists,
we aren't going to be able to do anything if this call
fails
*/
gchar* backup = g_strconcat(d->xml_filename, ".bak", NULL);
int fd;
g_rename(d->xml_filename, backup);
/* Recreate %gconf.xml to maintain our integrity and be sure
all_subdirs works */
/* If we failed to rename, we just give up and truncate the file */
fd = g_open(d->xml_filename, O_CREAT | O_WRONLY | O_TRUNC, d->file_mode);
if (fd >= 0)
close(fd);
g_free(backup);
}
g_assert(d->doc != NULL);
g_assert(d->doc->xmlRootNode != NULL);
}
static Entry*
dir_make_new_entry(Dir* d, const gchar* relative_key)
{
Entry* e;
g_return_val_if_fail(d->doc != NULL, NULL);
g_return_val_if_fail(d->doc->xmlRootNode != NULL, NULL);
e = entry_new(relative_key);
entry_set_node(e, xmlNewChild(d->doc->xmlRootNode, NULL, (xmlChar *)"entry", NULL));
safe_g_hash_table_insert(d->entry_cache, (gchar*)entry_get_name(e), e);
return e;
}
static gboolean
dir_forget_entry_if_useless(Dir* d, Entry* e)
{
GConfValue* val;
if (entry_get_schema_name(e) != NULL)
return FALSE;
val = entry_get_value(e, NULL, NULL);
if (val != NULL)
return FALSE; /* not useless */
g_hash_table_remove(d->entry_cache, entry_get_name(e));
entry_destroy(e);
return TRUE;
}
static void
dir_fill_cache_from_doc(Dir* d)
{
xmlNodePtr node;
if (d->doc == NULL ||
d->doc->xmlRootNode == NULL ||
d->doc->xmlRootNode->xmlChildrenNode == NULL)
{
/* Empty document - just return. */
return;
}
node = d->doc->xmlRootNode->xmlChildrenNode;
while (node != NULL)
{
if (node->type == XML_ELEMENT_NODE &&
(strcmp((xmlChar *)node->name, "entry") == 0))
{
gchar* attr = my_xmlGetProp(node, "name");
if (attr != NULL)
{
if (g_hash_table_lookup(d->entry_cache, attr) != NULL)
{
gconf_log(GCL_WARNING,
_("Duplicate entry `%s' in `%s', ignoring"),
attr, d->xml_filename);
}
else
{
Entry* e;
e = entry_new(attr);
entry_set_node(e, node);
entry_fill_from_node(e);
safe_g_hash_table_insert(d->entry_cache,
(gchar*)entry_get_name(e), e);
}
free(attr);
}
else
{
gconf_log(GCL_WARNING,
_("Entry with no name in XML file `%s', ignoring"),
d->xml_filename);
}
}
else
{
if (node->type == XML_ELEMENT_NODE)
gconf_log(GCL_WARNING,
_("A toplevel node in XML file `%s' is <%s> rather than <entry>, ignoring"),
d->xml_filename,
node->name ? (char*) node->name : "unknown");
}
node = node->next;
}
}
/*
* Misc
*/
static gboolean
create_fs_dir(const gchar* dir, const gchar* xml_filename,
guint root_dir_len, guint dir_mode, guint file_mode,
GError** err)
{
g_return_val_if_fail(xml_filename != NULL, FALSE);
gconf_log(GCL_DEBUG, "Enter create_fs_dir: %s", dir);
if (g_file_test(xml_filename, G_FILE_TEST_IS_REGULAR))
{
gconf_log(GCL_DEBUG, "XML backend file %s already exists", xml_filename);
return TRUE;
}
/* Don't create anything above the root directory */
if (strlen(dir) > root_dir_len)
{
gchar* parent;
parent = _gconf_parent_dir (dir);
gconf_log (GCL_DEBUG, "Parent dir is %s", parent);
if (parent != NULL)
{
gchar* parent_xml = NULL;
gboolean success = FALSE;
if (xml_filename)
parent_xml = g_strconcat(parent, "/%gconf.xml", NULL);
success = create_fs_dir(parent, parent_xml, root_dir_len,
dir_mode, file_mode, err);
if (success)
gconf_log(GCL_DEBUG, "created parent: %s", parent);
else
gconf_log(GCL_DEBUG, "failed parent: %s", parent);
g_free(parent);
if (parent_xml)
g_free(parent_xml);
if (!success)
return FALSE;
}
else
{
gconf_log(GCL_DEBUG, "%s has no parent", dir);
}
}
gconf_log(GCL_DEBUG, "Making directory %s", dir);
if (g_mkdir(dir, dir_mode) < 0)
{
if (errno != EEXIST)
{
gconf_set_error(err, GCONF_ERROR_FAILED,
_("Could not make directory \"%s\": %s"),
(gchar*)dir, g_strerror(errno));
return FALSE;
}
}
if (xml_filename != NULL)
{
int fd;
/* don't truncate the file, it may well already exist */
fd = g_open(xml_filename, O_CREAT | O_WRONLY, file_mode);
gconf_log(GCL_DEBUG, "Creating XML file %s", xml_filename);
if (fd < 0)
{
gconf_set_error(err, GCONF_ERROR_FAILED, _("Failed to create file `%s': %s"),
xml_filename, g_strerror(errno));
return FALSE;
}
if (close(fd) < 0)
{
gconf_set_error(err, GCONF_ERROR_FAILED, _("Failed to close file `%s': %s"),
xml_filename, g_strerror(errno));
return FALSE;
}
}
else
{
gconf_log(GCL_DEBUG, "No XML filename passed to create_fs_dir() for %s", dir);
}
return TRUE;
}
gchar*
_gconf_parent_dir (const gchar* dir)
{
/* We assume the dir doesn't have a trailing slash, since that's our
standard canonicalization in GConf */
gchar* parent;
gchar* last_slash;
g_return_val_if_fail(*dir != '\0', NULL);
if (dir[1] == '\0')
{
g_assert(dir[0] == '/');
return NULL;
}
parent = g_strdup(dir);
last_slash = strrchr(parent, '/');
/* dir must have had at least the root slash in it */
g_assert(last_slash != NULL);
if (last_slash != parent)
*last_slash = '\0';
else
{
++last_slash;
*last_slash = '\0';
}
return parent;
}
/* util */
guint
_gconf_mode_t_to_mode(mode_t orig)
{
/* I don't think this is portable. */
guint mode = 0;
guint fullmask = S_IRWXG | S_IRWXU | S_IRWXO;
mode = orig & fullmask;
g_return_val_if_fail(mode <= 0777, 0700);
return mode;
}
static xmlDocPtr
my_xml_parse_file (const char *filename,
GError **err)
{
char *text;
gsize length;
xmlDocPtr doc;
text = NULL;
length = 0;
if (!g_file_get_contents (filename,
&text,
&length,
err))
return NULL;
doc = xmlParseMemory (text, length);
g_free (text);
if (doc == NULL)
{
g_set_error (err,
GCONF_ERROR,
GCONF_ERROR_PARSE_ERROR,
_("Failed to parse XML file \"%s\""),
filename);
return NULL;
}
return doc;
}
void
xml_test_dir (void)
{
#ifndef GCONF_DISABLE_TESTS
#endif
}
|