1
/* -*- mode: C; c-file-style: "gnu" -*- */
2
/* xdgmime.c: XDG Mime Spec mime resolver. Based on version 0.11 of the spec.
4
* More info can be found at http://www.freedesktop.org/standards/
6
* Copyright (C) 2003,2004 Red Hat, Inc.
7
* Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
9
* Licensed under the Academic Free License version 2.0
10
* Or under the following terms:
12
* This library is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU Lesser General Public
14
* License as published by the Free Software Foundation; either
15
* version 2 of the License, or (at your option) any later version.
17
* This library is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
* Lesser General Public License for more details.
22
* You should have received a copy of the GNU Lesser General Public
23
* License along with this library; if not, write to the
24
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25
* Boston, MA 02111-1307, USA.
33
#include "xdgmimeint.h"
34
#include "xdgmimeglob.h"
35
#include "xdgmimemagic.h"
36
#include "xdgmimealias.h"
37
#include "xdgmimeparent.h"
38
#include "xdgmimecache.h"
42
#include <sys/types.h>
47
typedef struct XdgDirTimeList XdgDirTimeList;
48
typedef struct XdgCallbackList XdgCallbackList;
50
static int need_reread = TRUE;
51
static time_t last_stat_time = 0;
53
static XdgGlobHash *global_hash = NULL;
54
static XdgMimeMagic *global_magic = NULL;
55
static XdgAliasList *alias_list = NULL;
56
static XdgParentList *parent_list = NULL;
57
static XdgDirTimeList *dir_time_list = NULL;
58
static XdgCallbackList *callback_list = NULL;
60
XdgMimeCache **_xdg_mime_caches = NULL;
61
static int n_caches = 0;
63
const char xdg_mime_type_unknown[] = "application/octet-stream";
68
XDG_CHECKED_UNCHECKED,
82
struct XdgCallbackList
84
XdgCallbackList *next;
85
XdgCallbackList *prev;
87
XdgMimeCallback callback;
89
XdgMimeDestroy destroy;
92
/* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
93
* directories aren't looked at */
94
typedef int (*XdgDirectoryFunc) (const char *directory,
97
static XdgDirTimeList *
98
xdg_dir_time_list_new (void)
100
XdgDirTimeList *retval;
102
retval = calloc (1, sizeof (XdgDirTimeList));
103
retval->checked = XDG_CHECKED_UNCHECKED;
109
xdg_dir_time_list_free (XdgDirTimeList *list)
111
XdgDirTimeList *next;
116
free (list->directory_name);
123
xdg_mime_init_from_directory (const char *directory)
127
XdgDirTimeList *list;
129
assert (directory != NULL);
131
file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
132
strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
133
if (stat (file_name, &st) == 0)
135
XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
139
list = xdg_dir_time_list_new ();
140
list->directory_name = file_name;
141
list->mtime = st.st_mtime;
142
list->next = dir_time_list;
144
dir_time_list = list;
146
_xdg_mime_caches = realloc (_xdg_mime_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
147
_xdg_mime_caches[n_caches] = cache;
148
_xdg_mime_caches[n_caches + 1] = NULL;
156
file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
157
strcpy (file_name, directory); strcat (file_name, "/mime/globs");
158
if (stat (file_name, &st) == 0)
160
_xdg_mime_glob_read_from_file (global_hash, file_name);
162
list = xdg_dir_time_list_new ();
163
list->directory_name = file_name;
164
list->mtime = st.st_mtime;
165
list->next = dir_time_list;
166
dir_time_list = list;
173
file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
174
strcpy (file_name, directory); strcat (file_name, "/mime/magic");
175
if (stat (file_name, &st) == 0)
177
_xdg_mime_magic_read_from_file (global_magic, file_name);
179
list = xdg_dir_time_list_new ();
180
list->directory_name = file_name;
181
list->mtime = st.st_mtime;
182
list->next = dir_time_list;
183
dir_time_list = list;
190
file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
191
strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
192
_xdg_mime_alias_read_from_file (alias_list, file_name);
195
file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
196
strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
197
_xdg_mime_parent_read_from_file (parent_list, file_name);
200
return FALSE; /* Keep processing */
203
/* Runs a command on all the directories in the search path */
205
xdg_run_command_on_dirs (XdgDirectoryFunc func,
208
const char *xdg_data_home;
209
const char *xdg_data_dirs;
212
xdg_data_home = getenv ("XDG_DATA_HOME");
215
if ((func) (xdg_data_home, user_data))
222
home = getenv ("HOME");
225
char *guessed_xdg_home;
228
guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
229
strcpy (guessed_xdg_home, home);
230
strcat (guessed_xdg_home, "/.local/share/");
231
stop_processing = (func) (guessed_xdg_home, user_data);
232
free (guessed_xdg_home);
239
xdg_data_dirs = getenv ("XDG_DATA_DIRS");
240
if (xdg_data_dirs == NULL)
241
xdg_data_dirs = "/usr/local/share/:/usr/share/";
245
while (*ptr != '\000')
253
while (*end_ptr != ':' && *end_ptr != '\000')
265
len = end_ptr - ptr + 1;
266
dir = malloc (len + 1);
267
strncpy (dir, ptr, len);
269
stop_processing = (func) (dir, user_data);
279
static XdgMimeCache *
280
xdg_lookup_cache_for_file (const char *file_path)
282
XdgDirTimeList *list;
284
for (list = dir_time_list; list; list = list->next)
285
if (! strcmp (list->directory_name, file_path))
291
/* Checks file_path to make sure it has the same mtime as last time it was
292
* checked. If it has a different mtime, or if the file doesn't exist, it
295
* FIXME: This doesn't protect against permission changes.
298
xdg_check_file (const char *file_path)
302
/* If the file exists */
303
if (stat (file_path, &st) == 0)
305
XdgDirTimeList *list;
307
for (list = dir_time_list; list; list = list->next)
309
if (! strcmp (list->directory_name, file_path) &&
310
st.st_mtime == list->mtime)
312
if (list->checked == XDG_CHECKED_UNCHECKED)
313
list->checked = XDG_CHECKED_VALID;
314
else if (list->checked == XDG_CHECKED_VALID)
315
list->checked = XDG_CHECKED_INVALID;
317
return (list->checked != XDG_CHECKED_VALID);
327
xdg_check_dir (const char *directory,
328
int *invalid_dir_list)
330
int invalid, has_cache;
333
assert (directory != NULL);
335
/* Check the mime.cache file */
336
file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
337
strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
338
invalid = xdg_check_file (file_name);
339
has_cache = xdg_lookup_cache_for_file (file_name) != NULL;
346
*invalid_dir_list = TRUE;
353
/* Check the globs file */
354
file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
355
strcpy (file_name, directory); strcat (file_name, "/mime/globs");
356
invalid = xdg_check_file (file_name);
360
*invalid_dir_list = TRUE;
364
/* Check the magic file */
365
file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
366
strcpy (file_name, directory); strcat (file_name, "/mime/magic");
367
invalid = xdg_check_file (file_name);
371
*invalid_dir_list = TRUE;
375
return FALSE; /* Keep processing */
378
/* Walks through all the mime files stat()ing them to see if they've changed.
379
* Returns TRUE if they have. */
381
xdg_check_dirs (void)
383
XdgDirTimeList *list;
384
int invalid_dir_list = FALSE;
386
for (list = dir_time_list; list; list = list->next)
387
list->checked = XDG_CHECKED_UNCHECKED;
389
xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
392
if (invalid_dir_list)
395
for (list = dir_time_list; list; list = list->next)
397
if (list->checked != XDG_CHECKED_VALID)
404
/* We want to avoid stat()ing on every single mime call, so we only look for
405
* newer files every 5 seconds. This will return TRUE if we need to reread the
406
* mime data from disk.
409
xdg_check_time_and_dirs (void)
415
gettimeofday (&tv, NULL);
416
current_time = tv.tv_sec;
418
if (current_time >= last_stat_time + 5)
420
retval = xdg_check_dirs ();
421
last_stat_time = current_time;
427
/* Called in every public function. It reloads the hash function if need be.
432
if (xdg_check_time_and_dirs ())
434
xdg_mime_shutdown ();
439
global_hash = _xdg_glob_hash_new ();
440
global_magic = _xdg_mime_magic_new ();
441
alias_list = _xdg_mime_alias_list_new ();
442
parent_list = _xdg_mime_parent_list_new ();
444
xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
452
xdg_mime_get_mime_type_for_data (const void *data,
455
const char *mime_type;
459
if (_xdg_mime_caches)
460
return _xdg_mime_cache_get_mime_type_for_data (data, len);
462
mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, NULL, 0);
467
return XDG_MIME_TYPE_UNKNOWN;
471
xdg_mime_get_mime_type_for_file (const char *file_name,
472
struct stat *statbuf)
474
const char *mime_type;
475
/* Used to detect whether multiple MIME types match file_name */
476
const char *mime_types[2];
482
const char *base_name;
485
if (file_name == NULL)
487
if (! _xdg_utf8_validate (file_name))
492
if (_xdg_mime_caches)
493
return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
495
base_name = _xdg_get_base_name (file_name);
496
n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 2);
499
return mime_types[0];
503
if (stat (file_name, &buf) != 0)
504
return XDG_MIME_TYPE_UNKNOWN;
509
if (!S_ISREG (statbuf->st_mode))
510
return XDG_MIME_TYPE_UNKNOWN;
512
/* FIXME: Need to make sure that max_extent isn't totally broken. This could
513
* be large and need getting from a stream instead of just reading it all
515
max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
516
data = malloc (max_extent);
518
return XDG_MIME_TYPE_UNKNOWN;
520
file = fopen (file_name, "r");
524
return XDG_MIME_TYPE_UNKNOWN;
527
bytes_read = fread (data, 1, max_extent, file);
532
return XDG_MIME_TYPE_UNKNOWN;
535
mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read,
544
return XDG_MIME_TYPE_UNKNOWN;
548
xdg_mime_get_mime_type_from_file_name (const char *file_name)
550
const char *mime_types[2];
554
if (_xdg_mime_caches)
555
return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
557
if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, 2) == 1)
558
return mime_types[0];
560
return XDG_MIME_TYPE_UNKNOWN;
564
xdg_mime_is_valid_mime_type (const char *mime_type)
566
/* FIXME: We should make this a better test
568
return _xdg_utf8_validate (mime_type);
572
xdg_mime_shutdown (void)
574
XdgCallbackList *list;
576
/* FIXME: Need to make this (and the whole library) thread safe */
579
xdg_dir_time_list_free (dir_time_list);
580
dir_time_list = NULL;
585
_xdg_glob_hash_free (global_hash);
590
_xdg_mime_magic_free (global_magic);
596
_xdg_mime_alias_list_free (alias_list);
602
_xdg_mime_parent_list_free (parent_list);
606
if (_xdg_mime_caches)
609
for (i = 0; i < n_caches; i++)
610
_xdg_mime_cache_unref (_xdg_mime_caches[i]);
611
free (_xdg_mime_caches);
612
_xdg_mime_caches = NULL;
616
for (list = callback_list; list; list = list->next)
617
(list->callback) (list->data);
623
xdg_mime_get_max_buffer_extents (void)
627
if (_xdg_mime_caches)
628
return _xdg_mime_cache_get_max_buffer_extents ();
630
return _xdg_mime_magic_get_buffer_extents (global_magic);
634
_xdg_mime_unalias_mime_type (const char *mime_type)
638
if (_xdg_mime_caches)
639
return _xdg_mime_cache_unalias_mime_type (mime_type);
641
if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
648
xdg_mime_unalias_mime_type (const char *mime_type)
652
return _xdg_mime_unalias_mime_type (mime_type);
656
_xdg_mime_mime_type_equal (const char *mime_a,
659
const char *unalias_a, *unalias_b;
661
unalias_a = _xdg_mime_unalias_mime_type (mime_a);
662
unalias_b = _xdg_mime_unalias_mime_type (mime_b);
664
if (strcmp (unalias_a, unalias_b) == 0)
671
xdg_mime_mime_type_equal (const char *mime_a,
676
return _xdg_mime_mime_type_equal (mime_a, mime_b);
680
_xdg_mime_media_type_equal (const char *mime_a,
687
sep = strchr (mime_a, '/');
689
if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
696
xdg_mime_media_type_equal (const char *mime_a,
701
return _xdg_mime_media_type_equal (mime_a, mime_b);
706
xdg_mime_is_super_type (const char *mime)
711
length = strlen (mime);
712
type = &(mime[length - 2]);
714
if (strcmp (type, "/*") == 0)
722
_xdg_mime_mime_type_subclass (const char *mime,
725
const char *umime, *ubase;
726
const char **parents;
728
if (_xdg_mime_caches)
729
return _xdg_mime_cache_mime_type_subclass (mime, base);
731
umime = _xdg_mime_unalias_mime_type (mime);
732
ubase = _xdg_mime_unalias_mime_type (base);
734
if (strcmp (umime, ubase) == 0)
738
/* Handle supertypes */
739
if (xdg_mime_is_super_type (ubase) &&
740
_xdg_mime_media_type_equal (umime, ubase))
744
/* Handle special cases text/plain and application/octet-stream */
745
if (strcmp (ubase, "text/plain") == 0 &&
746
strncmp (umime, "text/", 5) == 0)
749
if (strcmp (ubase, "application/octet-stream") == 0)
752
parents = _xdg_mime_parent_list_lookup (parent_list, umime);
753
for (; parents && *parents; parents++)
755
if (_xdg_mime_mime_type_subclass (*parents, ubase))
763
xdg_mime_mime_type_subclass (const char *mime,
768
return _xdg_mime_mime_type_subclass (mime, base);
772
xdg_mime_list_mime_parents (const char *mime)
774
const char **parents;
778
if (_xdg_mime_caches)
779
return _xdg_mime_cache_list_mime_parents (mime);
781
parents = xdg_mime_get_mime_parents (mime);
786
for (i = 0; parents[i]; i++) ;
788
n = (i + 1) * sizeof (char *);
789
result = (char **) malloc (n);
790
memcpy (result, parents, n);
796
xdg_mime_get_mime_parents (const char *mime)
802
umime = _xdg_mime_unalias_mime_type (mime);
804
return _xdg_mime_parent_list_lookup (parent_list, umime);
810
printf ("*** ALIASES ***\n\n");
811
_xdg_mime_alias_list_dump (alias_list);
812
printf ("\n*** PARENTS ***\n\n");
813
_xdg_mime_parent_list_dump (parent_list);
814
printf ("\n*** CACHE ***\n\n");
815
_xdg_glob_hash_dump (global_hash);
819
/* Registers a function to be called every time the mime database reloads its files
822
xdg_mime_register_reload_callback (XdgMimeCallback callback,
824
XdgMimeDestroy destroy)
826
XdgCallbackList *list_el;
827
static int callback_id = 1;
829
/* Make a new list element */
830
list_el = calloc (1, sizeof (XdgCallbackList));
831
list_el->callback_id = callback_id;
832
list_el->callback = callback;
833
list_el->data = data;
834
list_el->destroy = destroy;
835
list_el->next = callback_list;
837
list_el->next->prev = list_el;
839
callback_list = list_el;
842
return callback_id - 1;
846
xdg_mime_remove_callback (int callback_id)
848
XdgCallbackList *list;
850
for (list = callback_list; list; list = list->next)
852
if (list->callback_id == callback_id)
855
list->next = list->prev;
858
list->prev->next = list->next;
860
callback_list = list->next;
862
/* invoke the destroy handler */
863
(list->destroy) (list->data);