/
update-category-database.c
1941 lines (1599 loc) · 41.2 KB
/
update-category-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
/* This is cut-and-pasted from shared-mime-info and modified. */
#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 <errno.h>
#include <dirent.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <sys/stat.h>
#include <sys/types.h>
#define JUST_WRITE_CATEGORIES
#define XML_NS XML_XML_NAMESPACE
#define XMLNS_NS "http://www.w3.org/2000/xmlns/"
#define FREE_NS "http://www.freedesktop.org/standards/shared-mime-info"
#define OSSO_NS "http://nokia.com/osso/mime-categories"
#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")
/* 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[] = {
"text",
"application",
"image",
"audio",
"inode",
"video",
"message",
"model",
"multipart",
"x-content",
"x-epoc",
"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;
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 _Magic {
int priority;
Type *type;
GList *matches;
};
struct _Match {
long range_start;
int range_length;
char word_size;
int data_length;
char *data;
char *mask;
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;
/* Maps MIME type names to superclass names */
static GHashTable *subclass_hash = NULL;
/* Maps aliases to Types */
static GHashTable *alias_hash = NULL;
/* Maps categories to types */
static GHashTable *categories_hash = NULL;
/* Static prototypes */
static Magic *magic_new(xmlNode *node, Type *type, GError **error);
static void usage(const char *name)
{
fprintf(stderr, _("Usage: %s [-hv] 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);
}
/* 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("1.0");
root = xmlNewDocNode(type->output, NULL, "mime-type", NULL);
ns = xmlNewNs(root, FREE_NS, NULL);
xmlSetNs(root, ns);
xmlDocSetRootElement(type->output, root);
xmlSetNsProp(root, NULL, "type", name);
xmlAddChild(root, xmlNewDocComment(type->output,
"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_print("* Warning: Unknown media type in type '%s'\n", 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(node->ns->href, namespaceURI) == 0 &&
strcmp(node->name, localName) == 0;
else
return strcmp(node->name, localName) == 0 && !node->ns;
}
/* 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)
{
char *prio_string;
int p;
prio_string = xmlGetNsProp(node, "priority", 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;
}
/* Process a <root-XML> element by adding a rule to namespace_hash */
static void add_namespace(Type *type, const guchar *namespaceURI,
const guchar *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)
{
if (strcmp(field->name, "glob") == 0)
{
gchar *pattern;
pattern = xmlGetNsProp(field, "pattern", NULL);
if (pattern && *pattern)
{
g_hash_table_insert(globs_hash,
g_strdup(pattern), type);
xmlFree(pattern);
}
else
{
if (pattern)
xmlFree(pattern);
g_set_error(error, MIME_ERROR, 0,
_("Missing 'pattern' attribute in <glob> "
"element"));
}
}
else if (strcmp(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, "comment") == 0 ||
strcmp((char *)field->name, "acronym") == 0 ||
strcmp((char *)field->name, "generic-icon") == 0 ||
strcmp((char *)field->name, "treemagic") == 0 ||
strcmp((char *)field->name, "expanded-acronym") == 0)
return FALSE;
else if (strcmp(field->name, "alias") == 0 ||
strcmp(field->name, "sub-class-of") == 0)
{
char *other_type;
gboolean valid;
GSList *list, *nlist;
other_type = 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(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);
return FALSE; /* Copy through */
}
xmlFree(other_type);
g_set_error(error, MIME_ERROR, 0,
_("Incorrect or missing 'type' attribute "
"in <%s>"), field->name);
}
else if (strcmp(field->name, "root-XML") == 0)
{
char *namespaceURI, *localName;
namespaceURI = xmlGetNsProp(field, "namespaceURI", NULL);
localName = xmlGetNsProp(field, "localName", NULL);
add_namespace(type, namespaceURI, localName, error);
if (namespaceURI)
xmlFree(namespaceURI);
if (localName)
xmlFree(localName);
}
else
{
g_set_error(error, MIME_ERROR, 0,
_("Unknown freedesktop.org field '%s'"),
field->name);
}
return !*error;
}
/* 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 = 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;
gchar *lang;
if (new->ns == NULL || strcmp(new->ns->href, FREE_NS) != 0)
return; /* No idea what we're doing -- leave it in! */
if (strcmp(new->name, "comment") != 0)
return;
lang = xmlGetNsProp(new, "lang", XML_NS);
fields = xmlDocGetRootElement(type->output);
for (field = fields->xmlChildrenNode; field; field = field->next)
{
if (match_node(field, FREE_NS, "comment") &&
has_lang(field, lang))
{
xmlUnlinkNode(field);
xmlFreeNode(field);
break;
}
}
xmlFree(lang);
}
static gboolean category_is_valid (const char *category)
{
int i;
const char *categories [] = {
"audio",
"bookmarks",
"contacts",
"documents",
"emails",
"images",
"media",
"video"
};
for (i = 0; i < G_N_ELEMENTS (categories); i++)
if (strcmp (categories[i], category) == 0)
return TRUE;
return FALSE;
}
static gint compare_types (gconstpointer a, gconstpointer b)
{
const Type *ta = a;
const Type *tb = b;
gint rv;
if ((rv = strcmp (ta->media, tb->media)) == 0)
rv = strcmp (ta->subtype, tb->subtype);
return rv;
}
static void append_category_type(const char *category_name, Type *type)
{
GSList *list, *nlist;
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, categories_hash);
while (g_hash_table_iter_next (&iter, &key, &value))
{
GSList *l = g_slist_find_custom (value, type, compare_types);
if (l)
{
if (strcmp (key, category_name) != 0)
{
g_print("* Warning: Mime type '%s/%s'' already in category '%s'\n",
type->media, type->subtype, (char *)key);
}
return;
}
}
list = g_hash_table_lookup (categories_hash, category_name);
nlist = g_slist_append (list, type);
if (list == NULL)
{
g_hash_table_insert (categories_hash, g_strdup (category_name),
nlist);
}
}
static char *get_type_category(Type *type)
{
char *category_name = NULL;
if (strcmp (type->media, "image") == 0)
category_name = "images";
else if (strcmp (type->media, "audio") == 0)
category_name = "audio";
else if (strcmp (type->media, "video") == 0)
category_name = "video";
else if (strcmp (type->media, "message") == 0)
category_name = "emails";
else if (strcmp (type->media, "application") == 0)
{
if (strcmp (type->subtype, "x-xbel") == 0 ||
strcmp (type->subtype, "x-mozilla-bookmarks") == 0)
{
category_name = "bookmarks";
}
}
if (category_name)
category_name = g_strdup (category_name);
return category_name;
}
static char *get_generic_icon_category(xmlNode *field)
{
char *category_name = NULL;
char *name;
name = xmlGetNsProp(field, "name", NULL);
if (strcmp (name, "audio-x-generic") == 0)
category_name = "audio";
else if (strcmp (name, "image-x-generic") == 0)
category_name = "images";
else if (strcmp (name, "video-x-generic") == 0)
category_name = "video";
else if (strcmp (name, "x-office-address-book") == 0)
category_name = "contacts";
else if (strcmp (name, "x-office-calendar") == 0 ||
strcmp (name, "x-office-document") == 0 ||
strcmp (name, "x-office-presentation") == 0 ||
strcmp (name, "x-office-spreadsheet") == 0)
{
category_name = "documents";
}
xmlFree (name);
if (category_name)
category_name = g_strdup (category_name);
return category_name;
}
/* '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;
char *category_name;
char *osso_category_name = NULL;
g_return_if_fail(type != NULL);
g_return_if_fail(node != NULL);
g_return_if_fail(error != NULL);
category_name = get_type_category(type);
for (field = node->xmlChildrenNode; field; field = field->next)
{
xmlNode *copy;
if (field->type != XML_ELEMENT_NODE)
continue;
if (field->ns && strcmp(field->ns->href, FREE_NS) == 0)
{
if (!category_name &&
strcmp ((char *)field->name, "generic-icon") == 0)
{
category_name =
get_generic_icon_category (field);
}
if (process_freedesktop_node(type, field, error))
{
g_return_if_fail(*error == NULL);
continue;
}
}
else if (field->ns && strcmp(field->ns->href, OSSO_NS) == 0)
{
char *category_name;
category_name = xmlGetNsProp (field, "name", NULL);
if (!category_name)
{
g_set_error(error, MIME_ERROR, 0,
_("Missing 'name' attribute in <osso:category> "
"element"));
}
else if (!category_is_valid (category_name))
{
g_set_error(error, MIME_ERROR, 0,
_("Invalid category name '%s' in <osso:category> "
"element"), category_name);
}
else
osso_category_name = g_strdup (category_name);
xmlFree (category_name);
}
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 &&
strcmp(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);
}
if (category_name)
{
if (osso_category_name)
g_free (osso_category_name);
osso_category_name = category_name;
}
if (osso_category_name)
{
append_category_type (osso_category_name, type);
g_free (osso_category_name);
}
}
/* 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'\n"), filename);
return;
}
root = xmlDocGetRootElement(doc);
if (root->ns == NULL || strcmp(root->ns->href, FREE_NS) != 0)
{
g_print("* Wrong namespace on document element\n"
"* in '%s'\n"
"* (should be %s)\n", filename, FREE_NS);
goto out;
}
if (strcmp(root->name, "mime-info") != 0)
{
g_print("* Root element <%s> is not <mime-info>\n"
"* (in '%s')\n", root->name, filename);
goto out;
}
for (node = root->xmlChildrenNode; node; node = node->next)
{
Type *type = NULL;
guchar *type_name = NULL;
GError *error = NULL;
if (node->type != XML_ELEMENT_NODE)
continue;
if (!match_node(node, FREE_NS, "mime-type"))
g_set_error(&error, MIME_ERROR, 0,
_("Excepted <mime-type>, but got wrong name "
"or namespace"));
if (!error)
{
type_name = 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_print("* Error in type '%s/%s'\n"
"* (in %s):\n"
"* %s.\n",
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);
}
/* Save doc as XML as filename, 0 on success or -1 on failure */
static int save_xml_file(xmlDocPtr doc, const gchar *filename)
{
#if LIBXML_VERSION > 20400
if (xmlSaveFormatFileEnc(filename, doc, "utf-8", 1) < 0)
return 1;
#else
FILE *out;
out = fopen(filename, "w");
if (!out)
return 1;
xmlDocDump(out, doc); /* Some versions return void */
if (fclose(out))
return 1;
#endif
return 0;
}
#ifndef JUST_WRITE_CATEGORIES
/* Write out one line of the 'globs' file */
static void write_out_glob(gpointer key, gpointer value, gpointer data)
{
const gchar *pattern = (gchar *) key;
Type *type = (Type *) value;
FILE *stream = (FILE *) data;
if (strchr(pattern, '\n'))
g_print("* Glob patterns can't contain literal newlines "
"(%s in type %s/%s)\n", pattern,
type->media, type->subtype);
else
fprintf(stream, "%s/%s:%s\n",
type->media, type->subtype, pattern);
}
#endif
/* Write out one line of the 'globs' file */
static void write_out_category(gpointer key, gpointer value, gpointer data)
{
const gchar *category = (gchar *) key;
GList *l = value;
FILE *stream = (FILE *) data;
fprintf (stream, "%s:", category);
for (; l; l = l->next)
{
Type *type = l->data;
fprintf (stream, " %s/%s", type->media, type->subtype);
}
fputc('\n', stream);
}
/* Renames pathname by removing the .new extension */
static void atomic_update(const guchar *pathname)
{
guchar *new_name;
int len;
len = strlen(pathname);
g_return_if_fail(strcmp(pathname + len - 4, ".new") == 0);
new_name = g_strndup(pathname, len - 4);
if (rename(pathname, new_name))
g_warning("Failed to rename %s as %s\n", pathname, new_name);
g_free(new_name);
}
/* Write out an XML file for one type */
static void write_out_type(gpointer key, gpointer value, gpointer data)
{
Type *type = (Type *) value;
const char *mime_dir = (char *) data;
char *media, *filename;
media = g_strconcat(mime_dir, "/", type->media, NULL);
mkdir(media, 0755);
filename = g_strconcat(media, "/", type->subtype, ".xml.new", NULL);
g_free(media);
media = NULL;
if (save_xml_file(type->output, filename) != 0)
g_warning("Failed to write out '%s'\n", filename);
atomic_update(filename);
g_free(filename);
}
#ifndef JUST_WRITE_CATEGORIES
/* Comparison function to get the magic rules in priority order */
static gint cmp_magic(gconstpointer a, gconstpointer b)
{
Magic *aa = *(Magic **) a;
Magic *bb = *(Magic **) b;
int retval;
if (aa->priority > bb->priority)
return -1;
else if (aa->priority < bb->priority)
return 1;
retval = strcmp(aa->type->media, bb->type->media);
if (!retval)
retval = strcmp(aa->type->subtype, bb->type->subtype);
return retval;
}
/* Write out 'n' as a two-byte big-endian number to 'stream' */
static void write16(FILE *stream, guint32 n)
{
guint16 big = GUINT16_TO_BE(n);
g_return_if_fail(n <= 0xffff);
fwrite(&big, sizeof(big), 1, stream);
}
#endif
/* Single hex char to int; -1 if not a hex char.
* From file(1).
*/
static int hextoint(int c)
{
if (!isascii((unsigned char) c))
return -1;
if (isdigit((unsigned char) c))
return c - '0';
if ((c >= 'a')&&(c <= 'f'))
return c + 10 - 'a';
if (( c>= 'A')&&(c <= 'F'))
return c + 10 - 'A';
return -1;
}
/*
* Convert a string containing C character escapes. Stop at an unescaped
* space or tab.
* Copy the converted version to "p", returning its length in *slen.
* Return updated scan pointer as function result.
* Stolen from file(1) and heavily modified.
*/
static void getstr(const char *s, GString *out)
{
int c;
int val;
while ((c = *s++) != '\0') {
if(c == '\\') {
switch(c = *s++) {
case '\0':
return;
default:
g_string_append_c(out, (char) c);
break;
case 'n':
g_string_append_c(out, '\n');
break;
case 'r':
g_string_append_c(out, '\r');
break;
case 'b':
g_string_append_c(out, '\b');
break;
case 't':
g_string_append_c(out, '\t');
break;
case 'f':
g_string_append_c(out, '\f');
break;
case 'v':
g_string_append_c(out, '\v');
break;
/* \ and up to 3 octal digits */
case '0':