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 "xdgmimeicon.h"
38 #include "xdgmimeparent.h"
39 #include "xdgmimecache.h"
43 #include <sys/types.h>
48 typedef struct XdgDirTimeList XdgDirTimeList
;
49 typedef struct XdgCallbackList XdgCallbackList
;
51 static int need_reread
= TRUE
;
52 static time_t last_stat_time
= 0;
54 static XdgGlobHash
*global_hash
= NULL
;
55 static XdgMimeMagic
*global_magic
= NULL
;
56 static XdgAliasList
*alias_list
= NULL
;
57 static XdgParentList
*parent_list
= NULL
;
58 static XdgDirTimeList
*dir_time_list
= NULL
;
59 static XdgCallbackList
*callback_list
= NULL
;
60 static XdgIconList
*icon_list
= NULL
;
61 static XdgIconList
*generic_icon_list
= NULL
;
63 XdgMimeCache
**_caches
= NULL
;
64 static int n_caches
= 0;
66 const char xdg_mime_type_unknown
[] = "application/octet-stream";
71 XDG_CHECKED_UNCHECKED
,
84 struct XdgCallbackList
86 XdgCallbackList
*next
;
87 XdgCallbackList
*prev
;
89 XdgMimeCallback callback
;
91 XdgMimeDestroy destroy
;
94 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
95 * directories aren't looked at */
96 typedef int (*XdgDirectoryFunc
) (const char *directory
,
100 xdg_dir_time_list_add (char *file_name
,
103 XdgDirTimeList
*list
;
105 for (list
= dir_time_list
; list
; list
= list
->next
)
107 if (strcmp (list
->directory_name
, file_name
) == 0)
114 list
= calloc (1, sizeof (XdgDirTimeList
));
115 list
->checked
= XDG_CHECKED_UNCHECKED
;
116 list
->directory_name
= file_name
;
118 list
->next
= dir_time_list
;
119 dir_time_list
= list
;
123 xdg_dir_time_list_free (XdgDirTimeList
*list
)
125 XdgDirTimeList
*next
;
130 free (list
->directory_name
);
137 xdg_mime_init_from_directory (const char *directory
)
142 assert (directory
!= NULL
);
144 file_name
= malloc (strlen (directory
) + strlen ("/mime/mime.cache") + 1);
145 strcpy (file_name
, directory
); strcat (file_name
, "/mime/mime.cache");
146 if (stat (file_name
, &st
) == 0)
148 XdgMimeCache
*cache
= _xdg_mime_cache_new_from_file (file_name
);
152 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
154 _caches
= realloc (_caches
, sizeof (XdgMimeCache
*) * (n_caches
+ 2));
155 _caches
[n_caches
] = cache
;
156 _caches
[n_caches
+ 1] = NULL
;
164 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs2") + 1);
165 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs2");
166 if (stat (file_name
, &st
) == 0)
168 _xdg_mime_glob_read_from_file (global_hash
, file_name
, TRUE
);
169 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
174 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs") + 1);
175 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs");
176 if (stat (file_name
, &st
) == 0)
178 _xdg_mime_glob_read_from_file (global_hash
, file_name
, FALSE
);
179 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
187 file_name
= malloc (strlen (directory
) + strlen ("/mime/magic") + 1);
188 strcpy (file_name
, directory
); strcat (file_name
, "/mime/magic");
189 if (stat (file_name
, &st
) == 0)
191 _xdg_mime_magic_read_from_file (global_magic
, file_name
);
192 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
199 file_name
= malloc (strlen (directory
) + strlen ("/mime/aliases") + 1);
200 strcpy (file_name
, directory
); strcat (file_name
, "/mime/aliases");
201 _xdg_mime_alias_read_from_file (alias_list
, file_name
);
204 file_name
= malloc (strlen (directory
) + strlen ("/mime/subclasses") + 1);
205 strcpy (file_name
, directory
); strcat (file_name
, "/mime/subclasses");
206 _xdg_mime_parent_read_from_file (parent_list
, file_name
);
209 file_name
= malloc (strlen (directory
) + strlen ("/mime/icons") + 1);
210 strcpy (file_name
, directory
); strcat (file_name
, "/mime/icons");
211 _xdg_mime_icon_read_from_file (icon_list
, file_name
);
214 file_name
= malloc (strlen (directory
) + strlen ("/mime/generic-icons") + 1);
215 strcpy (file_name
, directory
); strcat (file_name
, "/mime/generic-icons");
216 _xdg_mime_icon_read_from_file (generic_icon_list
, file_name
);
219 return FALSE
; /* Keep processing */
222 /* Runs a command on all the directories in the search path */
224 xdg_run_command_on_dirs (XdgDirectoryFunc func
,
227 const char *xdg_data_home
;
228 const char *xdg_data_dirs
;
231 xdg_data_home
= getenv ("XDG_DATA_HOME");
234 if ((func
) (xdg_data_home
, user_data
))
241 home
= getenv ("HOME");
244 char *guessed_xdg_home
;
247 guessed_xdg_home
= malloc (strlen (home
) + strlen ("/.local/share/") + 1);
248 strcpy (guessed_xdg_home
, home
);
249 strcat (guessed_xdg_home
, "/.local/share/");
250 stop_processing
= (func
) (guessed_xdg_home
, user_data
);
251 free (guessed_xdg_home
);
258 xdg_data_dirs
= getenv ("XDG_DATA_DIRS");
259 if (xdg_data_dirs
== NULL
)
260 xdg_data_dirs
= "/usr/local/share/:/usr/share/";
264 while (*ptr
!= '\000')
272 while (*end_ptr
!= ':' && *end_ptr
!= '\000')
284 len
= end_ptr
- ptr
+ 1;
285 dir
= malloc (len
+ 1);
286 strncpy (dir
, ptr
, len
);
288 stop_processing
= (func
) (dir
, user_data
);
298 /* Checks file_path to make sure it has the same mtime as last time it was
299 * checked. If it has a different mtime, or if the file doesn't exist, it
302 * FIXME: This doesn't protect against permission changes.
305 xdg_check_file (const char *file_path
,
310 /* If the file exists */
311 if (stat (file_path
, &st
) == 0)
313 XdgDirTimeList
*list
;
318 for (list
= dir_time_list
; list
; list
= list
->next
)
320 if (! strcmp (list
->directory_name
, file_path
))
322 if (st
.st_mtime
== list
->mtime
)
323 list
->checked
= XDG_CHECKED_VALID
;
325 list
->checked
= XDG_CHECKED_INVALID
;
327 return (list
->checked
!= XDG_CHECKED_VALID
);
340 xdg_check_dir (const char *directory
,
341 int *invalid_dir_list
)
346 assert (directory
!= NULL
);
348 /* Check the mime.cache file */
349 file_name
= malloc (strlen (directory
) + strlen ("/mime/mime.cache") + 1);
350 strcpy (file_name
, directory
); strcat (file_name
, "/mime/mime.cache");
351 invalid
= xdg_check_file (file_name
, &exists
);
355 *invalid_dir_list
= TRUE
;
363 /* Check the globs file */
364 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs") + 1);
365 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs");
366 invalid
= xdg_check_file (file_name
, NULL
);
370 *invalid_dir_list
= TRUE
;
374 /* Check the magic file */
375 file_name
= malloc (strlen (directory
) + strlen ("/mime/magic") + 1);
376 strcpy (file_name
, directory
); strcat (file_name
, "/mime/magic");
377 invalid
= xdg_check_file (file_name
, NULL
);
381 *invalid_dir_list
= TRUE
;
385 return FALSE
; /* Keep processing */
388 /* Walks through all the mime files stat()ing them to see if they've changed.
389 * Returns TRUE if they have. */
391 xdg_check_dirs (void)
393 XdgDirTimeList
*list
;
394 int invalid_dir_list
= FALSE
;
396 for (list
= dir_time_list
; list
; list
= list
->next
)
397 list
->checked
= XDG_CHECKED_UNCHECKED
;
399 xdg_run_command_on_dirs ((XdgDirectoryFunc
) xdg_check_dir
,
402 if (invalid_dir_list
)
405 for (list
= dir_time_list
; list
; list
= list
->next
)
407 if (list
->checked
!= XDG_CHECKED_VALID
)
414 /* We want to avoid stat()ing on every single mime call, so we only look for
415 * newer files every 5 seconds. This will return TRUE if we need to reread the
416 * mime data from disk.
419 xdg_check_time_and_dirs (void)
425 gettimeofday (&tv
, NULL
);
426 current_time
= tv
.tv_sec
;
428 if (current_time
>= last_stat_time
+ 5)
430 retval
= xdg_check_dirs ();
431 last_stat_time
= current_time
;
437 /* Called in every public function. It reloads the hash function if need be.
442 if (xdg_check_time_and_dirs ())
444 xdg_mime_shutdown ();
449 global_hash
= _xdg_glob_hash_new ();
450 global_magic
= _xdg_mime_magic_new ();
451 alias_list
= _xdg_mime_alias_list_new ();
452 parent_list
= _xdg_mime_parent_list_new ();
453 icon_list
= _xdg_mime_icon_list_new ();
454 generic_icon_list
= _xdg_mime_icon_list_new ();
456 xdg_run_command_on_dirs ((XdgDirectoryFunc
) xdg_mime_init_from_directory
,
464 xdg_mime_get_mime_type_for_data (const void *data
,
468 const char *mime_type
;
473 return _xdg_mime_cache_get_mime_type_for_data (data
, len
, result_prio
);
475 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, len
, result_prio
, NULL
, 0);
480 return XDG_MIME_TYPE_UNKNOWN
;
483 #ifdef NOT_USED_IN_GIO
486 xdg_mime_get_mime_type_for_file (const char *file_name
,
487 struct stat
*statbuf
)
489 const char *mime_type
;
490 /* currently, only a few globs occur twice, and none
491 * more often, so 5 seems plenty.
493 const char *mime_types
[5];
499 const char *base_name
;
502 if (file_name
== NULL
)
504 if (! _xdg_utf8_validate (file_name
))
510 return _xdg_mime_cache_get_mime_type_for_file (file_name
, statbuf
);
512 base_name
= _xdg_get_base_name (file_name
);
513 n
= _xdg_glob_hash_lookup_file_name (global_hash
, base_name
, mime_types
, 5);
516 return mime_types
[0];
520 if (stat (file_name
, &buf
) != 0)
521 return XDG_MIME_TYPE_UNKNOWN
;
526 if (!S_ISREG (statbuf
->st_mode
))
527 return XDG_MIME_TYPE_UNKNOWN
;
529 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
530 * be large and need getting from a stream instead of just reading it all
532 max_extent
= _xdg_mime_magic_get_buffer_extents (global_magic
);
533 data
= malloc (max_extent
);
535 return XDG_MIME_TYPE_UNKNOWN
;
537 file
= fopen (file_name
, "r");
541 return XDG_MIME_TYPE_UNKNOWN
;
544 bytes_read
= fread (data
, 1, max_extent
, file
);
549 return XDG_MIME_TYPE_UNKNOWN
;
552 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, bytes_read
, NULL
,
561 return XDG_MIME_TYPE_UNKNOWN
;
565 xdg_mime_get_mime_type_from_file_name (const char *file_name
)
567 const char *mime_type
;
572 return _xdg_mime_cache_get_mime_type_from_file_name (file_name
);
574 if (_xdg_glob_hash_lookup_file_name (global_hash
, file_name
, &mime_type
, 1))
577 return XDG_MIME_TYPE_UNKNOWN
;
583 xdg_mime_get_mime_types_from_file_name (const char *file_name
,
584 const char *mime_types
[],
590 return _xdg_mime_cache_get_mime_types_from_file_name (file_name
, mime_types
, n_mime_types
);
592 return _xdg_glob_hash_lookup_file_name (global_hash
, file_name
, mime_types
, n_mime_types
);
595 #ifdef NOT_USED_IN_GIO
598 xdg_mime_is_valid_mime_type (const char *mime_type
)
600 /* FIXME: We should make this a better test
602 return _xdg_utf8_validate (mime_type
);
608 xdg_mime_shutdown (void)
610 XdgCallbackList
*list
;
612 /* FIXME: Need to make this (and the whole library) thread safe */
615 xdg_dir_time_list_free (dir_time_list
);
616 dir_time_list
= NULL
;
621 _xdg_glob_hash_free (global_hash
);
626 _xdg_mime_magic_free (global_magic
);
632 _xdg_mime_alias_list_free (alias_list
);
638 _xdg_mime_parent_list_free (parent_list
);
644 _xdg_mime_icon_list_free (icon_list
);
648 if (generic_icon_list
)
650 _xdg_mime_icon_list_free (generic_icon_list
);
651 generic_icon_list
= NULL
;
658 for (i
= 0; i
< n_caches
; i
++)
659 _xdg_mime_cache_unref (_caches
[i
]);
665 for (list
= callback_list
; list
; list
= list
->next
)
666 (list
->callback
) (list
->data
);
672 xdg_mime_get_max_buffer_extents (void)
677 return _xdg_mime_cache_get_max_buffer_extents ();
679 return _xdg_mime_magic_get_buffer_extents (global_magic
);
683 _xdg_mime_unalias_mime_type (const char *mime_type
)
688 return _xdg_mime_cache_unalias_mime_type (mime_type
);
690 if ((lookup
= _xdg_mime_alias_list_lookup (alias_list
, mime_type
)) != NULL
)
697 xdg_mime_unalias_mime_type (const char *mime_type
)
701 return _xdg_mime_unalias_mime_type (mime_type
);
705 _xdg_mime_mime_type_equal (const char *mime_a
,
708 const char *unalias_a
, *unalias_b
;
710 unalias_a
= _xdg_mime_unalias_mime_type (mime_a
);
711 unalias_b
= _xdg_mime_unalias_mime_type (mime_b
);
713 if (strcmp (unalias_a
, unalias_b
) == 0)
720 xdg_mime_mime_type_equal (const char *mime_a
,
725 return _xdg_mime_mime_type_equal (mime_a
, mime_b
);
729 xdg_mime_media_type_equal (const char *mime_a
,
734 sep
= strchr (mime_a
, '/');
736 if (sep
&& strncmp (mime_a
, mime_b
, sep
- mime_a
+ 1) == 0)
744 ends_with (const char *str
,
750 length
= strlen (str
);
751 suffix_length
= strlen (suffix
);
752 if (length
< suffix_length
)
755 if (strcmp (str
+ length
- suffix_length
, suffix
) == 0)
762 xdg_mime_is_super_type (const char *mime
)
764 return ends_with (mime
, "/*");
769 _xdg_mime_mime_type_subclass (const char *mime
,
772 const char *umime
, *ubase
;
773 const char **parents
;
776 return _xdg_mime_cache_mime_type_subclass (mime
, base
);
778 umime
= _xdg_mime_unalias_mime_type (mime
);
779 ubase
= _xdg_mime_unalias_mime_type (base
);
781 if (strcmp (umime
, ubase
) == 0)
785 /* Handle supertypes */
786 if (xdg_mime_is_super_type (ubase
) &&
787 xdg_mime_media_type_equal (umime
, ubase
))
791 /* Handle special cases text/plain and application/octet-stream */
792 if (strcmp (ubase
, "text/plain") == 0 &&
793 strncmp (umime
, "text/", 5) == 0)
796 if (strcmp (ubase
, "application/octet-stream") == 0)
799 parents
= _xdg_mime_parent_list_lookup (parent_list
, umime
);
800 for (; parents
&& *parents
; parents
++)
802 if (_xdg_mime_mime_type_subclass (*parents
, ubase
))
810 xdg_mime_mime_type_subclass (const char *mime
,
815 return _xdg_mime_mime_type_subclass (mime
, base
);
819 xdg_mime_list_mime_parents (const char *mime
)
822 const char **parents
;
829 return _xdg_mime_cache_list_mime_parents (mime
);
831 umime
= _xdg_mime_unalias_mime_type (mime
);
833 parents
= _xdg_mime_parent_list_lookup (parent_list
, umime
);
838 for (i
= 0; parents
[i
]; i
++) ;
840 n
= (i
+ 1) * sizeof (char *);
841 result
= (char **) malloc (n
);
842 memcpy (result
, parents
, n
);
847 #ifdef NOT_USED_IN_GIO
850 xdg_mime_get_mime_parents (const char *mime
)
856 umime
= _xdg_mime_unalias_mime_type (mime
);
858 return _xdg_mime_parent_list_lookup (parent_list
, umime
);
866 printf ("*** ALIASES ***\n\n");
867 _xdg_mime_alias_list_dump (alias_list
);
868 printf ("\n*** PARENTS ***\n\n");
869 _xdg_mime_parent_list_dump (parent_list
);
870 printf ("\n*** CACHE ***\n\n");
871 _xdg_glob_hash_dump (global_hash
);
872 printf ("\n*** GLOBS ***\n\n");
873 _xdg_glob_hash_dump (global_hash
);
874 printf ("\n*** GLOBS REVERSE TREE ***\n\n");
875 _xdg_mime_cache_glob_dump ();
880 /* Registers a function to be called every time the mime database reloads its files
883 xdg_mime_register_reload_callback (XdgMimeCallback callback
,
885 XdgMimeDestroy destroy
)
887 XdgCallbackList
*list_el
;
888 static int callback_id
= 1;
890 /* Make a new list element */
891 list_el
= calloc (1, sizeof (XdgCallbackList
));
892 list_el
->callback_id
= callback_id
;
893 list_el
->callback
= callback
;
894 list_el
->data
= data
;
895 list_el
->destroy
= destroy
;
896 list_el
->next
= callback_list
;
898 list_el
->next
->prev
= list_el
;
900 callback_list
= list_el
;
903 return callback_id
- 1;
906 #ifdef NOT_USED_IN_GIO
909 xdg_mime_remove_callback (int callback_id
)
911 XdgCallbackList
*list
;
913 for (list
= callback_list
; list
; list
= list
->next
)
915 if (list
->callback_id
== callback_id
)
918 list
->next
= list
->prev
;
921 list
->prev
->next
= list
->next
;
923 callback_list
= list
->next
;
925 /* invoke the destroy handler */
926 (list
->destroy
) (list
->data
);
936 xdg_mime_get_icon (const char *mime
)
941 return _xdg_mime_cache_get_icon (mime
);
943 return _xdg_mime_icon_list_lookup (icon_list
, mime
);
947 xdg_mime_get_generic_icon (const char *mime
)
952 return _xdg_mime_cache_get_generic_icon (mime
);
954 return _xdg_mime_icon_list_lookup (generic_icon_list
, mime
);