forked from dlitz/xdg-shared-mime-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-mime-database.c
3981 lines (3309 loc) · 84.5 KB
/
update-mime-database.c
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
#include <config.h>
#define N_(x) x
#define _(x) (x)
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gstdio.h>
#include <errno.h>
#include <dirent.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#define XML_NS XML_XML_NAMESPACE
#define XMLNS_NS "http://www.w3.org/2000/xmlns/"
#define FREE_NS (xmlChar *)"http://www.freedesktop.org/standards/shared-mime-info"
#define COPYING \
N_("Copyright (C) 2003 Thomas Leonard.\n" \
"update-mime-database comes with ABSOLUTELY NO WARRANTY,\n" \
"to the extent permitted by law.\n" \
"You may redistribute copies of update-mime-database\n" \
"under the terms of the GNU General Public License.\n" \
"For more information about these matters, " \
"see the file named COPYING.\n")
#define MIME_ERROR g_quark_from_static_string("mime-error-quark")
#define NOGLOBS "__NOGLOBS__"
#define NOMAGIC "__NOMAGIC__"
#ifndef PATH_SEPARATOR
# ifdef _WIN32
# define PATH_SEPARATOR ";"
# else
# define PATH_SEPARATOR ":"
# endif
#endif
/* This is the list of directories to scan when finding old type files to
* delete. It is also used to warn about invalid MIME types.
*/
const char *media_types[] = {
"all",
"uri",
"print",
"text",
"application",
"image",
"audio",
"inode",
"video",
"message",
"model",
"multipart",
"x-content",
"x-epoc",
"x-scheme-handler",
"font",
};
/* Represents a MIME type */
typedef struct _Type Type;
/* A parsed <magic> element */
typedef struct _Magic Magic;
/* A parsed <match> element */
typedef struct _Match Match;
/* A parsed <treemagic> element */
typedef struct _TreeMagic TreeMagic;
/* A parsed <treematch> element */
typedef struct _TreeMatch TreeMatch;
/* A parsed <glob> element */
typedef struct _Glob Glob;
struct _Type {
char *media;
char *subtype;
/* Contains xmlNodes for elements that are being copied to the output.
* That is, <comment>, <sub-class-of> and <alias> nodes, and anything
* with an unknown namespace.
*/
xmlDoc *output;
};
struct _Glob {
int weight;
char *pattern;
Type *type;
gboolean noglob;
gboolean case_sensitive;
};
struct _Magic {
int priority;
Type *type;
GList *matches;
gboolean nomagic;
};
struct _Match {
long range_start;
int range_length;
char word_size;
int data_length;
char *data;
char *mask;
GList *matches;
};
struct _TreeMagic {
int priority;
Type *type;
GList *matches;
};
struct _TreeMatch {
char *path;
gboolean match_case;
gboolean executable;
gboolean non_empty;
gint type;
char *mimetype;
GList *matches;
};
/* Maps MIME type names to Types */
static GHashTable *types = NULL;
/* Maps "namespaceURI localName" strings to Types */
static GHashTable *namespace_hash = NULL;
/* Maps glob patterns to Types */
static GHashTable *globs_hash = NULL;
/* 'magic' nodes */
static GPtrArray *magic_array = NULL;
/* 'treemagic' nodes */
static GPtrArray *tree_magic_array = NULL;
/* Maps MIME type names to superclass names */
static GHashTable *subclass_hash = NULL;
/* Maps aliases to Types */
static GHashTable *alias_hash = NULL;
/* Maps MIME type names to icon names */
static GHashTable *icon_hash = NULL;
/* Maps MIME type names to icon names */
static GHashTable *generic_icon_hash = NULL;
/* Lists enabled log levels */
static GLogLevelFlags enabled_log_levels = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING;
/* Static prototypes */
static Magic *magic_new(xmlNode *node, Type *type, GError **error);
static Match *match_new(void);
static TreeMagic *tree_magic_new(xmlNode *node, Type *type, GError **error);
static void g_log_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer unused_data)
{
if (log_level & enabled_log_levels) {
g_printf("%s\n", message);
}
}
static void
fatal_gerror (GError *error) G_GNUC_NORETURN;
static void
fatal_gerror (GError *error)
{
g_assert(error != NULL);
g_printerr("%s\n", error->message);
g_error_free(error);
exit (EXIT_FAILURE);
}
static void usage(const char *name)
{
g_fprintf(stderr, _("Usage: %s [-hvVn] MIME-DIR\n"), name);
}
static void free_type(gpointer data)
{
Type *type = (Type *) data;
g_free(type->media);
g_free(type->subtype);
xmlFreeDoc(type->output);
g_free(type);
}
/* Ugly workaround to shut up gcc4 warnings about signedness issues
* (xmlChar is typedef'ed to unsigned char)
*/
static char *my_xmlGetNsProp (xmlNodePtr node,
const char *name,
const xmlChar *namespace)
{
return (char *)xmlGetNsProp (node, (xmlChar *)name, namespace);
}
/* If we've seen this type before, return the existing object.
* Otherwise, create a new one. Checks that the name looks sensible;
* if not, sets error and returns NULL.
* Also warns about unknown media types, but does not set error.
*/
static Type *get_type(const char *name, GError **error)
{
xmlNode *root;
xmlNs *ns;
const char *slash;
Type *type;
int i;
slash = strchr(name, '/');
if (!slash || strchr(slash + 1, '/'))
{
g_set_error(error, MIME_ERROR, 0,
_("Invalid MIME-type '%s'"), name);
return NULL;
}
type = g_hash_table_lookup(types, name);
if (type)
return type;
type = g_new(Type, 1);
type->media = g_strndup(name, slash - name);
type->subtype = g_strdup(slash + 1);
g_hash_table_insert(types, g_strdup(name), type);
type->output = xmlNewDoc((xmlChar *)"1.0");
root = xmlNewDocNode(type->output, NULL, (xmlChar *)"mime-type", NULL);
ns = xmlNewNs(root, FREE_NS, NULL);
xmlSetNs(root, ns);
xmlDocSetRootElement(type->output, root);
xmlSetNsProp(root, NULL, (xmlChar *)"type", (xmlChar *)name);
xmlAddChild(root, xmlNewDocComment(type->output,
(xmlChar *)"Created automatically by update-mime-database. DO NOT EDIT!"));
for (i = 0; i < G_N_ELEMENTS(media_types); i++)
{
if (strcmp(media_types[i], type->media) == 0)
return type;
}
g_message("Unknown media type in type '%s'", name);
return type;
}
/* Test that this node has the expected name and namespace */
static gboolean match_node(xmlNode *node,
const char *namespaceURI,
const char *localName)
{
if (namespaceURI)
return node->ns &&
strcmp((char *)node->ns->href, namespaceURI) == 0 &&
strcmp((char *)node->name, localName) == 0;
else
return strcmp((char *)node->name, localName) == 0 && !node->ns;
}
static int get_int_attribute(xmlNode *node, const char *name)
{
char *prio_string;
int p;
prio_string = my_xmlGetNsProp(node, name, NULL);
if (prio_string)
{
char *end;
p = strtol(prio_string, &end, 10);
if (*prio_string == '\0' || *end != '\0')
p = -1;
xmlFree(prio_string);
if (p < 0 || p > 100)
return -1;
return p;
}
else
return 50;
}
/* Return the priority of a <magic> node.
* Returns 50 if no priority is given, or -1 if a priority is given but
* is invalid.
*/
static int get_priority(xmlNode *node)
{
return get_int_attribute (node, "priority");
}
/* Return the weight a <glob> node.
* Returns 50 if no weight is given, or -1 if a weight is given but
* is invalid.
*/
static int get_weight(xmlNode *node)
{
return get_int_attribute (node, "weight");
}
/* Return the value of a false/true attribute, which defaults to false.
* Returns 0 or 1.
*/
static gboolean get_boolean_attribute(xmlNode *node, const char* name)
{
char *attr;
attr = my_xmlGetNsProp(node, name, NULL);
if (attr)
{
if (strcmp (attr, "true") == 0)
{
return TRUE;
}
xmlFree(attr);
}
return FALSE;
}
/* Process a <root-XML> element by adding a rule to namespace_hash */
static void add_namespace(Type *type, const char *namespaceURI,
const char *localName, GError **error)
{
g_return_if_fail(type != NULL);
if (!namespaceURI)
{
g_set_error(error, MIME_ERROR, 0,
_("Missing 'namespaceURI' attribute'"));
return;
}
if (!localName)
{
g_set_error(error, MIME_ERROR, 0,
_("Missing 'localName' attribute'"));
return;
}
if (!*namespaceURI && !*localName)
{
g_set_error(error, MIME_ERROR, 0,
_("namespaceURI and localName attributes can't "
"both be empty"));
return;
}
if (strpbrk(namespaceURI, " \n") || strpbrk(localName, " \n"))
{
g_set_error(error, MIME_ERROR, 0,
_("namespaceURI and localName cannot contain "
"spaces or newlines"));
return;
}
g_hash_table_insert(namespace_hash,
g_strconcat(namespaceURI, " ", localName, NULL),
type);
}
/* 'field' was found in the definition of 'type' and has the freedesktop.org
* namespace. If it's a known field, process it and return TRUE, else
* return FALSE to add it to the output XML document.
* On error, returns FALSE and sets 'error'.
*/
static gboolean process_freedesktop_node(Type *type, xmlNode *field,
GError **error)
{
gboolean copy_to_xml;
copy_to_xml = FALSE;
if (strcmp((char *)field->name, "glob") == 0)
{
gchar *pattern;
gint weight;
gboolean case_sensitive;
weight = get_weight(field);
case_sensitive = get_boolean_attribute(field, "case-sensitive");
if (weight == -1)
{
g_set_error(error, MIME_ERROR, 0,
_("Bad weight (%d) in <glob> element"), weight);
}
pattern = my_xmlGetNsProp(field, "pattern", NULL);
if (pattern && *pattern)
{
Glob *glob;
char *pat = case_sensitive ? g_strdup (pattern) : g_ascii_strdown (pattern, -1);
GList *list = g_hash_table_lookup (globs_hash, pat);
glob = g_new0 (Glob, 1);
glob->pattern = pat;
glob->type = type;
glob->weight = weight;
glob->case_sensitive = case_sensitive;
list = g_list_append (list, glob);
g_hash_table_insert(globs_hash, g_strdup (glob->pattern), list);
xmlFree(pattern);
copy_to_xml = TRUE;
}
else
{
if (pattern)
xmlFree(pattern);
g_set_error(error, MIME_ERROR, 0,
_("Missing 'pattern' attribute in <glob> "
"element"));
}
}
else if (strcmp((char *)field->name, "glob-deleteall") == 0)
{
Glob *glob;
GList *list = g_hash_table_lookup (globs_hash, NOGLOBS);
glob = g_new0 (Glob, 1);
glob->pattern = g_strdup (NOGLOBS);
glob->type = type;
glob->weight = 0;
glob->noglob = TRUE;
glob->case_sensitive = FALSE;
list = g_list_append (list, glob);
g_hash_table_insert(globs_hash, g_strdup (glob->pattern), list);
copy_to_xml = TRUE;
}
else if (strcmp((char *)field->name, "magic") == 0)
{
Magic *magic;
magic = magic_new(field, type, error);
if (!*error)
{
g_return_val_if_fail(magic != NULL, FALSE);
g_ptr_array_add(magic_array, magic);
}
else
g_return_val_if_fail(magic == NULL, FALSE);
}
else if (strcmp((char *)field->name, "magic-deleteall") == 0)
{
Magic *magic;
Match *match;
magic = g_new0(Magic, 1);
magic->priority = 0;
magic->type = type;
magic->nomagic = TRUE;
match = match_new ();
match->data = g_strdup (NOMAGIC);
match->data_length = strlen (NOMAGIC);
magic->matches = g_list_prepend (NULL, match);
g_ptr_array_add(magic_array, magic);
}
else if (strcmp((char *)field->name, "treemagic") == 0)
{
TreeMagic *magic;
magic = tree_magic_new(field, type, error);
if (!*error)
{
g_return_val_if_fail(magic != NULL, FALSE);
g_ptr_array_add(tree_magic_array, magic);
}
else
g_return_val_if_fail(magic == NULL, FALSE);
}
else if (strcmp((char *)field->name, "comment") == 0 ||
strcmp((char *)field->name, "acronym") == 0 ||
strcmp((char *)field->name, "expanded-acronym") == 0)
copy_to_xml = TRUE;
else if (strcmp((char *)field->name, "alias") == 0 ||
strcmp((char *)field->name, "sub-class-of") == 0)
{
char *other_type;
gboolean valid;
GSList *list, *nlist;
other_type = my_xmlGetNsProp(field, "type", NULL);
valid = other_type && strchr(other_type, '/');
if (valid)
{
char *typename;
typename = g_strdup_printf("%s/%s",
type->media,
type->subtype);
if (strcmp((char *)field->name, "alias") == 0)
g_hash_table_insert(alias_hash,
g_strdup(other_type), type);
else
{
list = g_hash_table_lookup(subclass_hash, typename);
nlist = g_slist_append (list, g_strdup(other_type));
if (list == NULL)
g_hash_table_insert(subclass_hash,
g_strdup(typename), nlist);
}
g_free(typename);
xmlFree(other_type);
copy_to_xml = TRUE; /* Copy through */
}
else
{
xmlFree(other_type);
g_set_error(error, MIME_ERROR, 0,
_("Incorrect or missing 'type' attribute "
"in <%s>"), field->name);
}
}
else if (strcmp((char *)field->name, "root-XML") == 0)
{
char *namespaceURI, *localName;
namespaceURI = my_xmlGetNsProp(field, "namespaceURI", NULL);
localName = my_xmlGetNsProp(field, "localName", NULL);
add_namespace(type, namespaceURI, localName, error);
if (namespaceURI)
xmlFree(namespaceURI);
if (localName)
xmlFree(localName);
}
else if (strcmp((char *)field->name, "generic-icon") == 0 ||
strcmp((char *)field->name, "icon") == 0)
{
char *icon;
char *typename;
icon = my_xmlGetNsProp(field, "name", NULL);
if (icon)
{
typename = g_strdup_printf("%s/%s",
type->media,
type->subtype);
if (strcmp((char *)field->name, "icon") == 0)
g_hash_table_insert(icon_hash,
typename, g_strdup (icon));
else
g_hash_table_insert(generic_icon_hash,
typename, g_strdup (icon));
xmlFree (icon);
copy_to_xml = TRUE; /* Copy through */
}
}
if (*error)
return FALSE;
return !copy_to_xml;
}
/* Checks to see if 'node' has the given value for xml:lang.
* If 'lang' is NULL, checks that 'node' doesn't have an xml:lang.
*/
static gboolean has_lang(xmlNode *node, const char *lang)
{
char *lang2;
lang2 = my_xmlGetNsProp(node, "lang", XML_NS);
if (!lang2)
return !lang;
if (lang && strcmp(lang, lang2) == 0)
{
xmlFree(lang2);
return TRUE;
}
xmlFree(lang2);
return FALSE;
}
/* We're about to add 'new' to the list of fields to be output for the
* type. Remove any existing nodes which it replaces.
*/
static void remove_old(Type *type, xmlNode *new)
{
xmlNode *field, *fields;
char *lang;
if (new->ns == NULL || xmlStrcmp(new->ns->href, FREE_NS) != 0)
return; /* No idea what we're doing -- leave it in! */
if (strcmp((char *)new->name, "comment") != 0)
return;
lang = my_xmlGetNsProp(new, "lang", XML_NS);
fields = xmlDocGetRootElement(type->output);
for (field = fields->xmlChildrenNode; field; field = field->next)
{
if (match_node(field, (char *)FREE_NS, "comment") &&
has_lang(field, lang))
{
xmlUnlinkNode(field);
xmlFreeNode(field);
break;
}
}
xmlFree(lang);
}
/* 'node' is a <mime-type> node from a source file, whose type is 'type'.
* Process all the child elements, setting 'error' if anything goes wrong.
*/
static void load_type(Type *type, xmlNode *node, GError **error)
{
xmlNode *field;
g_return_if_fail(type != NULL);
g_return_if_fail(node != NULL);
g_return_if_fail(error != NULL);
for (field = node->xmlChildrenNode; field; field = field->next)
{
xmlNode *copy;
if (field->type != XML_ELEMENT_NODE)
continue;
if (field->ns && xmlStrcmp(field->ns->href, FREE_NS) == 0)
{
if (process_freedesktop_node(type, field, error))
{
g_return_if_fail(*error == NULL);
continue;
}
}
if (*error)
return;
copy = xmlDocCopyNode(field, type->output, 1);
/* Ugly hack to stop the xmlns= attributes appearing on
* every node...
*/
if (copy->ns && copy->ns->prefix == NULL &&
xmlStrcmp(copy->ns->href, FREE_NS) == 0)
{
if (copy->nsDef)
{
/* Still used somewhere... */
/* xmlFreeNsList(copy->nsDef); */
/* (this leaks) */
copy->nsDef = NULL;
}
}
remove_old(type, field);
xmlAddChild(xmlDocGetRootElement(type->output), copy);
}
}
/* Parse 'filename' as an XML file and add all the information to the
* database. If called more than once, information read in later calls
* overrides information read previously.
*/
static void load_source_file(const char *filename)
{
xmlDoc *doc;
xmlNode *root, *node;
doc = xmlParseFile(filename);
if (!doc)
{
g_warning(_("Failed to parse '%s'"), filename);
return;
}
g_message("Parsing source file %s...", filename);
root = xmlDocGetRootElement(doc);
if (root->ns == NULL || xmlStrcmp(root->ns->href, FREE_NS) != 0)
{
g_warning("Wrong namespace on document element in '%s' (should be %s)", filename, FREE_NS);
goto out;
}
if (strcmp((char *)root->name, "mime-info") != 0)
{
g_warning("Root element <%s> is not <mime-info> (in '%s')", root->name, filename);
goto out;
}
for (node = root->xmlChildrenNode; node; node = node->next)
{
Type *type = NULL;
char *type_name = NULL;
GError *error = NULL;
if (node->type != XML_ELEMENT_NODE)
continue;
if (!match_node(node, (char *)FREE_NS, "mime-type"))
g_set_error(&error, MIME_ERROR, 0,
_("Excepted <mime-type>, but got wrong name "
"or namespace"));
if (!error)
{
type_name = my_xmlGetNsProp(node, "type", NULL);
if (!type_name)
g_set_error(&error, MIME_ERROR, 0,
_("<mime-type> element has no 'type' "
"attribute"));
}
if (type_name)
{
type = get_type(type_name, &error);
xmlFree(type_name);
}
if (!error)
{
g_return_if_fail(type != NULL);
load_type(type, node, &error);
}
else
g_return_if_fail(type == NULL);
if (error)
{
g_warning("Error in type '%s/%s' (in %s): %s.",
type ? type->media : _("unknown"),
type ? type->subtype : _("unknown"),
filename, error->message);
g_error_free(error);
}
}
out:
xmlFreeDoc(doc);
}
/* Used as the sort function for sorting GPtrArrays */
static gint strcmp2(gconstpointer a, gconstpointer b)
{
const char *aa = *(char **) a;
const char *bb = *(char **) b;
return strcmp(aa, bb);
}
/* 'path' should be a 'packages' directory. Loads the information from
* every file in the directory.
*/
static void scan_source_dir(const char *path)
{
DIR *dir;
struct dirent *ent;
char *filename;
GPtrArray *files;
int i;
gboolean have_override = FALSE;
dir = opendir(path);
if (!dir)
{
perror("scan_source_dir");
exit(EXIT_FAILURE);
}
files = g_ptr_array_new();
while ((ent = readdir(dir)))
{
int l;
l = strlen(ent->d_name);
if (l < 4 || strcmp(ent->d_name + l - 4, ".xml") != 0)
continue;
if (strcmp(ent->d_name, "Override.xml") == 0)
{
have_override = TRUE;
continue;
}
g_ptr_array_add(files, g_strdup(ent->d_name));
}
closedir(dir);
g_ptr_array_sort(files, strcmp2);
if (have_override)
g_ptr_array_add(files, g_strdup("Override.xml"));
for (i = 0; i < files->len; i++)
{
gchar *leaf = (gchar *) files->pdata[i];
filename = g_strconcat(path, "/", leaf, NULL);
load_source_file(filename);
g_free(filename);
}
for (i = 0; i < files->len; i++)
g_free(files->pdata[i]);
g_ptr_array_free(files, TRUE);
}
static gboolean save_xml_file(xmlDocPtr doc, const gchar *filename, GError **error)
{
#if LIBXML_VERSION > 20400
if (xmlSaveFormatFileEnc(filename, doc, "utf-8", 1) < 0)
{
g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Failed to write XML file; For permission problems, try rerunning as root");
return FALSE;
}
#else
FILE *out;
out = fopen_gerror(filename, error);
if (!out)
return FALSE;
xmlDocDump(out, doc); /* Some versions return void */
if (!fclose_gerror(out, error))
return FALSE;
#endif
return TRUE;
}
/* Write out globs for one pattern to the 'globs' file */
static void write_out_glob(GList *globs, FILE *stream)
{
GList *list;
Glob *glob;
Type *type;
for (list = globs; list; list = list->next) {
glob = (Glob *)list->data;
type = glob->type;
if (strchr(glob->pattern, '\n'))
g_warning("Glob patterns can't contain literal newlines "
"(%s in type %s/%s)", glob->pattern,
type->media, type->subtype);
else
g_fprintf(stream, "%s/%s:%s\n",
type->media, type->subtype, glob->pattern);
}
}
/* Write out globs and weights for one pattern to the 'globs2' file */
static void write_out_glob2(GList *globs, FILE *stream)
{
GList *list;
Glob *glob;
Type *type;
gboolean need_flags;
for (list = globs ; list; list = list->next) {
glob = (Glob *)list->data;
type = glob->type;
if (strchr(glob->pattern, '\n'))
g_warning("Glob patterns can't contain literal newlines "
"(%s in type %s/%s)", glob->pattern,
type->media, type->subtype);
else
{
need_flags = FALSE;
if (glob->case_sensitive)
need_flags = TRUE;
if (need_flags) {
g_fprintf(stream, "%d:%s/%s:%s%s\n",
glob->weight, type->media, type->subtype, glob->pattern,
glob->case_sensitive ? ":cs" : "");
}
/* Always write the line without the flags, for older parsers */
g_fprintf(stream, "%d:%s/%s:%s\n",
glob->weight, type->media, type->subtype, glob->pattern);
}
}
}
static void collect_glob2(gpointer key, gpointer value, gpointer data)
{
GList **listp = data;
*listp = g_list_concat (*listp, g_list_copy ((GList *)value));
}
static int compare_glob_by_weight (gpointer a, gpointer b)
{
Glob *ag = (Glob *)a;
Glob *bg = (Glob *)b;
if (ag->noglob || bg->noglob)
return bg->noglob - ag->noglob;
return bg->weight - ag->weight;
}
static void
set_error_from_errno (GError **error)
{
int errsv = errno;
g_set_error_literal(error, G_FILE_ERROR, g_file_error_from_errno(errsv),
g_strerror(errsv));
}
#ifdef HAVE_FDATASYNC
static gboolean
sync_enabled(void)
{
const char *env;
env = g_getenv("PKGSYSTEM_ENABLE_FSYNC");
if (!env)
return TRUE;
return atoi(env);
}
#endif
static int
sync_file(const gchar *pathname, GError **error)
{
#ifdef HAVE_FDATASYNC
int fd;
if (!sync_enabled())
return 0;
fd = open(pathname, O_RDWR);
if (fd == -1)
{
set_error_from_errno(error);
return -1;
}
if (fdatasync(fd) == -1)
{
set_error_from_errno(error);
close(fd);
return -1;
}
if (close(fd) == -1)
{
set_error_from_errno(error);
return -1;
}
#endif
return 0;
}
/* Renames pathname by removing the .new extension */
static gboolean atomic_update(const gchar *pathname, GError **error)
{
gboolean ret = FALSE;
gchar *new_name = NULL;
int len;
len = strlen(pathname);
g_return_val_if_fail(strcmp(pathname + len - 4, ".new") == 0, FALSE);