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.1 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, see <http://www.gnu.org/licenses/>.
29 #include "xdgmimeint.h"
30 #include "xdgmimeglob.h"
31 #include "xdgmimemagic.h"
32 #include "xdgmimealias.h"
33 #include "xdgmimeicon.h"
34 #include "xdgmimeparent.h"
35 #include "xdgmimecache.h"
39 #include <sys/types.h>
44 typedef struct XdgDirTimeList XdgDirTimeList
;
45 typedef struct XdgCallbackList XdgCallbackList
;
47 static int need_reread
= TRUE
;
48 static time_t last_stat_time
= 0;
50 static XdgGlobHash
*global_hash
= NULL
;
51 static XdgMimeMagic
*global_magic
= NULL
;
52 static XdgAliasList
*alias_list
= NULL
;
53 static XdgParentList
*parent_list
= NULL
;
54 static XdgDirTimeList
*dir_time_list
= NULL
;
55 static XdgCallbackList
*callback_list
= NULL
;
56 static XdgIconList
*icon_list
= NULL
;
57 static XdgIconList
*generic_icon_list
= NULL
;
59 XdgMimeCache
**_caches
= NULL
;
60 static int n_caches
= 0;
62 const char xdg_mime_type_unknown
[] = "application/octet-stream";
63 const char xdg_mime_type_empty
[] = "application/x-zerosize";
64 const char xdg_mime_type_textplain
[] = "text/plain";
69 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
,
98 xdg_dir_time_list_add (char *file_name
,
101 XdgDirTimeList
*list
;
103 for (list
= dir_time_list
; list
; list
= list
->next
)
105 if (strcmp (list
->directory_name
, file_name
) == 0)
112 list
= calloc (1, sizeof (XdgDirTimeList
));
113 list
->checked
= XDG_CHECKED_UNCHECKED
;
114 list
->directory_name
= file_name
;
116 list
->next
= dir_time_list
;
117 dir_time_list
= list
;
121 xdg_dir_time_list_free (XdgDirTimeList
*list
)
123 XdgDirTimeList
*next
;
128 free (list
->directory_name
);
135 xdg_mime_init_from_directory (const char *directory
)
140 assert (directory
!= NULL
);
142 file_name
= malloc (strlen (directory
) + strlen ("/mime/mime.cache") + 1);
143 strcpy (file_name
, directory
); strcat (file_name
, "/mime/mime.cache");
144 if (stat (file_name
, &st
) == 0)
146 XdgMimeCache
*cache
= _xdg_mime_cache_new_from_file (file_name
);
150 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
152 _caches
= realloc (_caches
, sizeof (XdgMimeCache
*) * (n_caches
+ 2));
153 _caches
[n_caches
] = cache
;
154 _caches
[n_caches
+ 1] = NULL
;
162 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs2") + 1);
163 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs2");
164 if (stat (file_name
, &st
) == 0)
166 _xdg_mime_glob_read_from_file (global_hash
, file_name
, TRUE
);
167 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
172 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs") + 1);
173 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs");
174 if (stat (file_name
, &st
) == 0)
176 _xdg_mime_glob_read_from_file (global_hash
, file_name
, FALSE
);
177 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
185 file_name
= malloc (strlen (directory
) + strlen ("/mime/magic") + 1);
186 strcpy (file_name
, directory
); strcat (file_name
, "/mime/magic");
187 if (stat (file_name
, &st
) == 0)
189 _xdg_mime_magic_read_from_file (global_magic
, file_name
);
190 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
197 file_name
= malloc (strlen (directory
) + strlen ("/mime/aliases") + 1);
198 strcpy (file_name
, directory
); strcat (file_name
, "/mime/aliases");
199 _xdg_mime_alias_read_from_file (alias_list
, file_name
);
202 file_name
= malloc (strlen (directory
) + strlen ("/mime/subclasses") + 1);
203 strcpy (file_name
, directory
); strcat (file_name
, "/mime/subclasses");
204 _xdg_mime_parent_read_from_file (parent_list
, file_name
);
207 file_name
= malloc (strlen (directory
) + strlen ("/mime/icons") + 1);
208 strcpy (file_name
, directory
); strcat (file_name
, "/mime/icons");
209 _xdg_mime_icon_read_from_file (icon_list
, file_name
);
212 file_name
= malloc (strlen (directory
) + strlen ("/mime/generic-icons") + 1);
213 strcpy (file_name
, directory
); strcat (file_name
, "/mime/generic-icons");
214 _xdg_mime_icon_read_from_file (generic_icon_list
, file_name
);
217 return FALSE
; /* Keep processing */
220 /* Runs a command on all the directories in the search path */
222 xdg_run_command_on_dirs (XdgDirectoryFunc func
,
225 const char *xdg_data_home
;
226 const char *xdg_data_dirs
;
229 xdg_data_home
= getenv ("XDG_DATA_HOME");
232 if ((func
) (xdg_data_home
, user_data
))
239 home
= getenv ("HOME");
242 char *guessed_xdg_home
;
245 guessed_xdg_home
= malloc (strlen (home
) + strlen ("/.local/share/") + 1);
246 strcpy (guessed_xdg_home
, home
);
247 strcat (guessed_xdg_home
, "/.local/share/");
248 stop_processing
= (func
) (guessed_xdg_home
, user_data
);
249 free (guessed_xdg_home
);
256 xdg_data_dirs
= getenv ("XDG_DATA_DIRS");
257 if (xdg_data_dirs
== NULL
)
258 xdg_data_dirs
= "/usr/local/share/:/usr/share/";
262 while (*ptr
!= '\000')
270 while (*end_ptr
!= ':' && *end_ptr
!= '\000')
282 len
= end_ptr
- ptr
+ 1;
283 dir
= malloc (len
+ 1);
284 strncpy (dir
, ptr
, len
);
286 stop_processing
= (func
) (dir
, user_data
);
296 /* Checks file_path to make sure it has the same mtime as last time it was
297 * checked. If it has a different mtime, or if the file doesn't exist, it
300 * FIXME: This doesn't protect against permission changes.
303 xdg_check_file (const char *file_path
,
308 /* If the file exists */
309 if (stat (file_path
, &st
) == 0)
311 XdgDirTimeList
*list
;
316 for (list
= dir_time_list
; list
; list
= list
->next
)
318 if (! strcmp (list
->directory_name
, file_path
))
320 if (st
.st_mtime
== list
->mtime
)
321 list
->checked
= XDG_CHECKED_VALID
;
323 list
->checked
= XDG_CHECKED_INVALID
;
325 return (list
->checked
!= XDG_CHECKED_VALID
);
338 xdg_check_dir (const char *directory
,
339 int *invalid_dir_list
)
344 assert (directory
!= NULL
);
346 /* Check the mime.cache file */
347 file_name
= malloc (strlen (directory
) + strlen ("/mime/mime.cache") + 1);
348 strcpy (file_name
, directory
); strcat (file_name
, "/mime/mime.cache");
349 invalid
= xdg_check_file (file_name
, &exists
);
353 *invalid_dir_list
= TRUE
;
361 /* Check the globs file */
362 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs") + 1);
363 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs");
364 invalid
= xdg_check_file (file_name
, NULL
);
368 *invalid_dir_list
= TRUE
;
372 /* Check the magic file */
373 file_name
= malloc (strlen (directory
) + strlen ("/mime/magic") + 1);
374 strcpy (file_name
, directory
); strcat (file_name
, "/mime/magic");
375 invalid
= xdg_check_file (file_name
, NULL
);
379 *invalid_dir_list
= TRUE
;
383 return FALSE
; /* Keep processing */
386 /* Walks through all the mime files stat()ing them to see if they've changed.
387 * Returns TRUE if they have. */
389 xdg_check_dirs (void)
391 XdgDirTimeList
*list
;
392 int invalid_dir_list
= FALSE
;
394 for (list
= dir_time_list
; list
; list
= list
->next
)
395 list
->checked
= XDG_CHECKED_UNCHECKED
;
397 xdg_run_command_on_dirs ((XdgDirectoryFunc
) xdg_check_dir
,
400 if (invalid_dir_list
)
403 for (list
= dir_time_list
; list
; list
= list
->next
)
405 if (list
->checked
!= XDG_CHECKED_VALID
)
412 /* We want to avoid stat()ing on every single mime call, so we only look for
413 * newer files every 5 seconds. This will return TRUE if we need to reread the
414 * mime data from disk.
417 xdg_check_time_and_dirs (void)
423 gettimeofday (&tv
, NULL
);
424 current_time
= tv
.tv_sec
;
426 if (current_time
>= last_stat_time
+ 5)
428 retval
= xdg_check_dirs ();
429 last_stat_time
= current_time
;
435 /* Called in every public function. It reloads the hash function if need be.
440 if (xdg_check_time_and_dirs ())
442 xdg_mime_shutdown ();
447 global_hash
= _xdg_glob_hash_new ();
448 global_magic
= _xdg_mime_magic_new ();
449 alias_list
= _xdg_mime_alias_list_new ();
450 parent_list
= _xdg_mime_parent_list_new ();
451 icon_list
= _xdg_mime_icon_list_new ();
452 generic_icon_list
= _xdg_mime_icon_list_new ();
454 xdg_run_command_on_dirs ((XdgDirectoryFunc
) xdg_mime_init_from_directory
,
462 xdg_mime_get_mime_type_for_data (const void *data
,
466 const char *mime_type
;
471 return XDG_MIME_TYPE_EMPTY
;
477 mime_type
= _xdg_mime_cache_get_mime_type_for_data (data
, len
, result_prio
);
479 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, len
, result_prio
, NULL
, 0);
484 return _xdg_binary_or_text_fallback(data
, len
);
487 #ifdef NOT_USED_IN_GIO
490 xdg_mime_get_mime_type_for_file (const char *file_name
,
491 struct stat
*statbuf
)
493 const char *mime_type
;
494 /* currently, only a few globs occur twice, and none
495 * more often, so 5 seems plenty.
497 const char *mime_types
[5];
503 const char *base_name
;
506 if (file_name
== NULL
)
508 if (! _xdg_utf8_validate (file_name
))
514 return _xdg_mime_cache_get_mime_type_for_file (file_name
, statbuf
);
516 base_name
= _xdg_get_base_name (file_name
);
517 n
= _xdg_glob_hash_lookup_file_name (global_hash
, base_name
, mime_types
, 5);
520 return mime_types
[0];
524 if (stat (file_name
, &buf
) != 0)
525 return XDG_MIME_TYPE_UNKNOWN
;
530 if (!S_ISREG (statbuf
->st_mode
))
531 return XDG_MIME_TYPE_UNKNOWN
;
533 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
534 * be large and need getting from a stream instead of just reading it all
536 max_extent
= _xdg_mime_magic_get_buffer_extents (global_magic
);
537 data
= malloc (max_extent
);
539 return XDG_MIME_TYPE_UNKNOWN
;
541 file
= fopen (file_name
, "r");
545 return XDG_MIME_TYPE_UNKNOWN
;
548 bytes_read
= fread (data
, 1, max_extent
, file
);
553 return XDG_MIME_TYPE_UNKNOWN
;
556 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, bytes_read
, NULL
,
565 return _xdg_binary_or_text_fallback(data
, bytes_read
);
569 xdg_mime_get_mime_type_from_file_name (const char *file_name
)
571 const char *mime_type
;
576 return _xdg_mime_cache_get_mime_type_from_file_name (file_name
);
578 if (_xdg_glob_hash_lookup_file_name (global_hash
, file_name
, &mime_type
, 1))
581 return XDG_MIME_TYPE_UNKNOWN
;
587 xdg_mime_get_mime_types_from_file_name (const char *file_name
,
588 const char *mime_types
[],
594 return _xdg_mime_cache_get_mime_types_from_file_name (file_name
, mime_types
, n_mime_types
);
596 return _xdg_glob_hash_lookup_file_name (global_hash
, file_name
, mime_types
, n_mime_types
);
599 #ifdef NOT_USED_IN_GIO
602 xdg_mime_is_valid_mime_type (const char *mime_type
)
604 /* FIXME: We should make this a better test
606 return _xdg_utf8_validate (mime_type
);
612 xdg_mime_shutdown (void)
614 XdgCallbackList
*list
;
616 /* FIXME: Need to make this (and the whole library) thread safe */
619 xdg_dir_time_list_free (dir_time_list
);
620 dir_time_list
= NULL
;
625 _xdg_glob_hash_free (global_hash
);
630 _xdg_mime_magic_free (global_magic
);
636 _xdg_mime_alias_list_free (alias_list
);
642 _xdg_mime_parent_list_free (parent_list
);
648 _xdg_mime_icon_list_free (icon_list
);
652 if (generic_icon_list
)
654 _xdg_mime_icon_list_free (generic_icon_list
);
655 generic_icon_list
= NULL
;
662 for (i
= 0; i
< n_caches
; i
++)
663 _xdg_mime_cache_unref (_caches
[i
]);
669 for (list
= callback_list
; list
; list
= list
->next
)
670 (list
->callback
) (list
->data
);
676 xdg_mime_get_max_buffer_extents (void)
681 return _xdg_mime_cache_get_max_buffer_extents ();
683 return _xdg_mime_magic_get_buffer_extents (global_magic
);
687 _xdg_mime_unalias_mime_type (const char *mime_type
)
692 return _xdg_mime_cache_unalias_mime_type (mime_type
);
694 if ((lookup
= _xdg_mime_alias_list_lookup (alias_list
, mime_type
)) != NULL
)
701 xdg_mime_unalias_mime_type (const char *mime_type
)
705 return _xdg_mime_unalias_mime_type (mime_type
);
709 _xdg_mime_mime_type_equal (const char *mime_a
,
712 const char *unalias_a
, *unalias_b
;
714 unalias_a
= _xdg_mime_unalias_mime_type (mime_a
);
715 unalias_b
= _xdg_mime_unalias_mime_type (mime_b
);
717 if (strcmp (unalias_a
, unalias_b
) == 0)
724 xdg_mime_mime_type_equal (const char *mime_a
,
729 return _xdg_mime_mime_type_equal (mime_a
, mime_b
);
733 xdg_mime_media_type_equal (const char *mime_a
,
738 sep
= strchr (mime_a
, '/');
740 if (sep
&& strncmp (mime_a
, mime_b
, sep
- mime_a
+ 1) == 0)
748 ends_with (const char *str
,
754 length
= strlen (str
);
755 suffix_length
= strlen (suffix
);
756 if (length
< suffix_length
)
759 if (strcmp (str
+ length
- suffix_length
, suffix
) == 0)
766 xdg_mime_is_super_type (const char *mime
)
768 return ends_with (mime
, "/*");
773 _xdg_mime_mime_type_subclass (const char *mime
,
776 const char *umime
, *ubase
;
777 const char **parents
;
780 return _xdg_mime_cache_mime_type_subclass (mime
, base
);
782 umime
= _xdg_mime_unalias_mime_type (mime
);
783 ubase
= _xdg_mime_unalias_mime_type (base
);
785 if (strcmp (umime
, ubase
) == 0)
789 /* Handle supertypes */
790 if (xdg_mime_is_super_type (ubase
) &&
791 xdg_mime_media_type_equal (umime
, ubase
))
795 /* Handle special cases text/plain and application/octet-stream */
796 if (strcmp (ubase
, "text/plain") == 0 &&
797 strncmp (umime
, "text/", 5) == 0)
800 if (strcmp (ubase
, "application/octet-stream") == 0 &&
801 strncmp (umime
, "inode/", 6) != 0)
804 parents
= _xdg_mime_parent_list_lookup (parent_list
, umime
);
805 for (; parents
&& *parents
; parents
++)
807 if (_xdg_mime_mime_type_subclass (*parents
, ubase
))
815 xdg_mime_mime_type_subclass (const char *mime
,
820 return _xdg_mime_mime_type_subclass (mime
, base
);
824 xdg_mime_list_mime_parents (const char *mime
)
827 const char **parents
;
834 return _xdg_mime_cache_list_mime_parents (mime
);
836 umime
= _xdg_mime_unalias_mime_type (mime
);
838 parents
= _xdg_mime_parent_list_lookup (parent_list
, umime
);
843 for (i
= 0; parents
[i
]; i
++) ;
845 n
= (i
+ 1) * sizeof (char *);
846 result
= (char **) malloc (n
);
847 memcpy (result
, parents
, n
);
852 #ifdef NOT_USED_IN_GIO
855 xdg_mime_get_mime_parents (const char *mime
)
861 umime
= _xdg_mime_unalias_mime_type (mime
);
863 return _xdg_mime_parent_list_lookup (parent_list
, umime
);
871 printf ("*** ALIASES ***\n\n");
872 _xdg_mime_alias_list_dump (alias_list
);
873 printf ("\n*** PARENTS ***\n\n");
874 _xdg_mime_parent_list_dump (parent_list
);
875 printf ("\n*** CACHE ***\n\n");
876 _xdg_glob_hash_dump (global_hash
);
877 printf ("\n*** GLOBS ***\n\n");
878 _xdg_glob_hash_dump (global_hash
);
879 printf ("\n*** GLOBS REVERSE TREE ***\n\n");
880 _xdg_mime_cache_glob_dump ();
885 /* Registers a function to be called every time the mime database reloads its files
888 xdg_mime_register_reload_callback (XdgMimeCallback callback
,
890 XdgMimeDestroy destroy
)
892 XdgCallbackList
*list_el
;
893 static int callback_id
= 1;
895 /* Make a new list element */
896 list_el
= calloc (1, sizeof (XdgCallbackList
));
897 list_el
->callback_id
= callback_id
;
898 list_el
->callback
= callback
;
899 list_el
->data
= data
;
900 list_el
->destroy
= destroy
;
901 list_el
->next
= callback_list
;
903 list_el
->next
->prev
= list_el
;
905 callback_list
= list_el
;
908 return callback_id
- 1;
911 #ifdef NOT_USED_IN_GIO
914 xdg_mime_remove_callback (int callback_id
)
916 XdgCallbackList
*list
;
918 for (list
= callback_list
; list
; list
= list
->next
)
920 if (list
->callback_id
== callback_id
)
923 list
->next
= list
->prev
;
926 list
->prev
->next
= list
->next
;
928 callback_list
= list
->next
;
930 /* invoke the destroy handler */
931 (list
->destroy
) (list
->data
);
941 xdg_mime_get_icon (const char *mime
)
946 return _xdg_mime_cache_get_icon (mime
);
948 return _xdg_mime_icon_list_lookup (icon_list
, mime
);
952 xdg_mime_get_generic_icon (const char *mime
)
957 return _xdg_mime_cache_get_generic_icon (mime
);
959 return _xdg_mime_icon_list_lookup (generic_icon_list
, mime
);