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 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
17 #include "xdgmimeint.h"
18 #include "xdgmimeglob.h"
19 #include "xdgmimemagic.h"
20 #include "xdgmimealias.h"
21 #include "xdgmimeicon.h"
22 #include "xdgmimeparent.h"
23 #include "xdgmimecache.h"
27 #include <sys/types.h>
33 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
36 typedef struct XdgDirTimeList XdgDirTimeList
;
37 typedef struct XdgCallbackList XdgCallbackList
;
39 static int need_reread
= TRUE
;
40 static time_t last_stat_time
= 0;
42 static XdgGlobHash
*global_hash
= NULL
;
43 static XdgMimeMagic
*global_magic
= NULL
;
44 static XdgAliasList
*alias_list
= NULL
;
45 static XdgParentList
*parent_list
= NULL
;
46 static XdgDirTimeList
*dir_time_list
= NULL
;
47 static XdgCallbackList
*callback_list
= NULL
;
48 static XdgIconList
*icon_list
= NULL
;
49 static XdgIconList
*generic_icon_list
= NULL
;
51 static char **xdg_dirs
= NULL
; /* NULL terminated */
53 XdgMimeCache
**_caches
= NULL
;
54 static int n_caches
= 0;
56 const char xdg_mime_type_unknown
[] = "application/octet-stream";
57 const char xdg_mime_type_empty
[] = "application/x-zerosize";
58 const char xdg_mime_type_textplain
[] = "text/plain";
63 XDG_CHECKED_UNCHECKED
,
76 struct XdgCallbackList
78 XdgCallbackList
*next
;
79 XdgCallbackList
*prev
;
81 XdgMimeCallback callback
;
83 XdgMimeDestroy destroy
;
86 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
87 * directories aren't looked at */
88 typedef int (*XdgDirectoryFunc
) (const char *directory
,
92 xdg_dir_time_list_add (char *file_name
,
97 for (list
= dir_time_list
; list
; list
= list
->next
)
99 if (strcmp (list
->directory_name
, file_name
) == 0)
106 list
= calloc (1, sizeof (XdgDirTimeList
));
107 list
->checked
= XDG_CHECKED_UNCHECKED
;
108 list
->directory_name
= file_name
;
110 list
->next
= dir_time_list
;
111 dir_time_list
= list
;
115 xdg_dir_time_list_free (XdgDirTimeList
*list
)
117 XdgDirTimeList
*next
;
122 free (list
->directory_name
);
129 xdg_mime_init_from_directory (const char *directory
,
135 assert (directory
!= NULL
);
137 file_name
= malloc (strlen (directory
) + strlen ("/mime.cache") + 1);
138 strcpy (file_name
, directory
); strcat (file_name
, "/mime.cache");
139 if (stat (file_name
, &st
) == 0)
141 XdgMimeCache
*cache
= _xdg_mime_cache_new_from_file (file_name
);
145 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
147 _caches
= realloc (_caches
, sizeof (XdgMimeCache
*) * (n_caches
+ 2));
148 _caches
[n_caches
] = cache
;
149 _caches
[n_caches
+ 1] = NULL
;
157 file_name
= malloc (strlen (directory
) + strlen ("/globs2") + 1);
158 strcpy (file_name
, directory
); strcat (file_name
, "/globs2");
159 if (stat (file_name
, &st
) == 0)
161 _xdg_mime_glob_read_from_file (global_hash
, file_name
, TRUE
);
162 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
167 file_name
= malloc (strlen (directory
) + strlen ("/globs") + 1);
168 strcpy (file_name
, directory
); strcat (file_name
, "/globs");
169 if (stat (file_name
, &st
) == 0)
171 _xdg_mime_glob_read_from_file (global_hash
, file_name
, FALSE
);
172 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
180 file_name
= malloc (strlen (directory
) + strlen ("/magic") + 1);
181 strcpy (file_name
, directory
); strcat (file_name
, "/magic");
182 if (stat (file_name
, &st
) == 0)
184 _xdg_mime_magic_read_from_file (global_magic
, file_name
);
185 xdg_dir_time_list_add (file_name
, st
.st_mtime
);
192 file_name
= malloc (strlen (directory
) + strlen ("/aliases") + 1);
193 strcpy (file_name
, directory
); strcat (file_name
, "/aliases");
194 _xdg_mime_alias_read_from_file (alias_list
, file_name
);
197 file_name
= malloc (strlen (directory
) + strlen ("/subclasses") + 1);
198 strcpy (file_name
, directory
); strcat (file_name
, "/subclasses");
199 _xdg_mime_parent_read_from_file (parent_list
, file_name
);
202 file_name
= malloc (strlen (directory
) + strlen ("/icons") + 1);
203 strcpy (file_name
, directory
); strcat (file_name
, "/icons");
204 _xdg_mime_icon_read_from_file (icon_list
, file_name
);
207 file_name
= malloc (strlen (directory
) + strlen ("/generic-icons") + 1);
208 strcpy (file_name
, directory
); strcat (file_name
, "/generic-icons");
209 _xdg_mime_icon_read_from_file (generic_icon_list
, file_name
);
212 return FALSE
; /* Keep processing */
215 /* Set @xdg_dirs from the environment. It must not have been set already. */
219 const char *xdg_data_home
, *home
, *xdg_data_dirs
;
222 size_t i
, current_dir
;
224 assert (xdg_dirs
== NULL
);
226 xdg_data_home
= getenv ("XDG_DATA_HOME");
227 home
= getenv ("HOME");
228 xdg_data_dirs
= getenv ("XDG_DATA_DIRS");
230 if (xdg_data_dirs
== NULL
)
231 xdg_data_dirs
= "/usr/local/share/:/usr/share/";
233 /* Work out how many dirs we’re dealing with. */
234 if (xdg_data_home
!= NULL
|| home
!= NULL
)
236 n_dirs
++; /* initial entry in @xdg_data_dirs */
237 for (i
= 0; xdg_data_dirs
[i
] != '\0'; i
++)
238 if (xdg_data_dirs
[i
] == ':')
241 xdg_dirs
= calloc (n_dirs
+ 1 /* NULL terminator */, sizeof (char *));
245 if (xdg_data_home
!= NULL
)
249 mime_subdir
= malloc (strlen (xdg_data_home
) + strlen ("/mime/") + 1);
250 strcpy (mime_subdir
, xdg_data_home
);
251 strcat (mime_subdir
, "/mime/");
253 xdg_dirs
[current_dir
++] = mime_subdir
;
255 else if (home
!= NULL
)
257 char *guessed_xdg_home
;
259 guessed_xdg_home
= malloc (strlen (home
) + strlen ("/.local/share/mime/") + 1);
260 strcpy (guessed_xdg_home
, home
);
261 strcat (guessed_xdg_home
, "/.local/share/mime/");
263 xdg_dirs
[current_dir
++] = guessed_xdg_home
;
269 while (*ptr
!= '\000')
276 while (*end_ptr
!= ':' && *end_ptr
!= '\000')
288 len
= end_ptr
- ptr
+ 1;
289 dir
= malloc (len
+ strlen ("/mime/") + 1);
290 strncpy (dir
, ptr
, len
);
292 strcat (dir
, "/mime/");
294 xdg_dirs
[current_dir
++] = dir
;
299 /* NULL terminator */
300 xdg_dirs
[current_dir
] = NULL
;
305 /* Runs a command on all the directories in the search path (@xdg_dirs). */
307 xdg_run_command_on_dirs (XdgDirectoryFunc func
,
312 if (xdg_dirs
== NULL
)
315 for (i
= 0; xdg_dirs
[i
] != NULL
; i
++)
317 if ((func
) (xdg_dirs
[i
], user_data
))
322 /* Allows the calling code to override the directories used by xdgmime, without
323 * having to change environment variables in a running process (which is not
324 * thread safe). This is intended to be used by tests. The changes will be
325 * picked up by xdg_mime_init() next time public API is called.
327 * This will set @xdg_dirs. Directories in @dirs must be complete, including
328 * the conventional `/mime` subdirectory. This is to allow tests to override
329 * them without the need to create a subdirectory. */
331 xdg_mime_set_dirs (const char * const *dirs
)
335 for (i
= 0; xdg_dirs
!= NULL
&& xdg_dirs
[i
] != NULL
; i
++)
342 for (i
= 0; dirs
[i
] != NULL
; i
++);
343 xdg_dirs
= calloc (i
+ 1 /* NULL terminator */, sizeof (char*));
344 for (i
= 0; dirs
[i
] != NULL
; i
++)
345 xdg_dirs
[i
] = strdup (dirs
[i
]);
352 /* Checks file_path to make sure it has the same mtime as last time it was
353 * checked. If it has a different mtime, or if the file doesn't exist, it
356 * FIXME: This doesn't protect against permission changes.
359 xdg_check_file (const char *file_path
,
364 /* If the file exists */
365 if (stat (file_path
, &st
) == 0)
367 XdgDirTimeList
*list
;
372 for (list
= dir_time_list
; list
; list
= list
->next
)
374 if (! strcmp (list
->directory_name
, file_path
))
376 if (st
.st_mtime
== list
->mtime
)
377 list
->checked
= XDG_CHECKED_VALID
;
379 list
->checked
= XDG_CHECKED_INVALID
;
381 return (list
->checked
!= XDG_CHECKED_VALID
);
394 xdg_check_dir (const char *directory
,
399 int* invalid_dir_list
= user_data
;
401 assert (directory
!= NULL
);
403 /* Check the mime.cache file */
404 file_name
= malloc (strlen (directory
) + strlen ("/mime.cache") + 1);
405 strcpy (file_name
, directory
); strcat (file_name
, "/mime.cache");
406 invalid
= xdg_check_file (file_name
, &exists
);
410 *invalid_dir_list
= TRUE
;
418 /* Check the globs file */
419 file_name
= malloc (strlen (directory
) + strlen ("/globs") + 1);
420 strcpy (file_name
, directory
); strcat (file_name
, "/globs");
421 invalid
= xdg_check_file (file_name
, NULL
);
425 *invalid_dir_list
= TRUE
;
429 /* Check the magic file */
430 file_name
= malloc (strlen (directory
) + strlen ("/magic") + 1);
431 strcpy (file_name
, directory
); strcat (file_name
, "/magic");
432 invalid
= xdg_check_file (file_name
, NULL
);
436 *invalid_dir_list
= TRUE
;
440 return FALSE
; /* Keep processing */
443 /* Walks through all the mime files stat()ing them to see if they've changed.
444 * Returns TRUE if they have. */
446 xdg_check_dirs (void)
448 XdgDirTimeList
*list
;
449 int invalid_dir_list
= FALSE
;
451 for (list
= dir_time_list
; list
; list
= list
->next
)
452 list
->checked
= XDG_CHECKED_UNCHECKED
;
454 xdg_run_command_on_dirs (xdg_check_dir
, &invalid_dir_list
);
456 if (invalid_dir_list
)
459 for (list
= dir_time_list
; list
; list
= list
->next
)
461 if (list
->checked
!= XDG_CHECKED_VALID
)
468 /* We want to avoid stat()ing on every single mime call, so we only look for
469 * newer files every 5 seconds. This will return TRUE if we need to reread the
470 * mime data from disk.
473 xdg_check_time_and_dirs (void)
479 gettimeofday (&tv
, NULL
);
480 current_time
= tv
.tv_sec
;
482 if (current_time
>= last_stat_time
+ 5)
484 retval
= xdg_check_dirs ();
485 last_stat_time
= current_time
;
491 /* Called in every public function. It reloads the hash function if need be.
496 if (xdg_check_time_and_dirs ())
498 xdg_mime_shutdown ();
503 global_hash
= _xdg_glob_hash_new ();
504 global_magic
= _xdg_mime_magic_new ();
505 alias_list
= _xdg_mime_alias_list_new ();
506 parent_list
= _xdg_mime_parent_list_new ();
507 icon_list
= _xdg_mime_icon_list_new ();
508 generic_icon_list
= _xdg_mime_icon_list_new ();
510 xdg_run_command_on_dirs (xdg_mime_init_from_directory
, NULL
);
517 xdg_mime_get_mime_type_for_data (const void *data
,
521 const char *mime_type
;
525 if (result_prio
!= NULL
)
527 return XDG_MIME_TYPE_EMPTY
;
533 mime_type
= _xdg_mime_cache_get_mime_type_for_data (data
, len
, result_prio
);
535 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, len
, result_prio
, NULL
, 0);
540 return _xdg_binary_or_text_fallback(data
, len
);
544 xdg_mime_get_mime_type_for_file (const char *file_name
,
545 struct stat
*statbuf
)
547 const char *mime_type
;
548 /* currently, only a few globs occur twice, and none
549 * more often, so 5 seems plenty.
551 const char *mime_types
[5];
557 const char *base_name
;
560 if (file_name
== NULL
)
562 if (! _xdg_utf8_validate (file_name
))
568 return _xdg_mime_cache_get_mime_type_for_file (file_name
, statbuf
);
570 base_name
= _xdg_get_base_name (file_name
);
571 n
= _xdg_glob_hash_lookup_file_name (global_hash
, base_name
, mime_types
, 5);
574 return mime_types
[0];
578 if (stat (file_name
, &buf
) != 0)
579 return XDG_MIME_TYPE_UNKNOWN
;
584 if (!S_ISREG (statbuf
->st_mode
))
585 return XDG_MIME_TYPE_UNKNOWN
;
587 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
588 * be large and need getting from a stream instead of just reading it all
590 max_extent
= _xdg_mime_magic_get_buffer_extents (global_magic
);
591 data
= malloc (max_extent
);
593 return XDG_MIME_TYPE_UNKNOWN
;
595 file
= fopen (file_name
, "r");
599 return XDG_MIME_TYPE_UNKNOWN
;
602 bytes_read
= fread (data
, 1, max_extent
, file
);
607 return XDG_MIME_TYPE_UNKNOWN
;
610 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, bytes_read
, NULL
,
614 mime_type
= _xdg_binary_or_text_fallback (data
, bytes_read
);
623 xdg_mime_get_mime_type_from_file_name (const char *file_name
)
625 const char *mime_type
;
630 return _xdg_mime_cache_get_mime_type_from_file_name (file_name
);
632 if (_xdg_glob_hash_lookup_file_name (global_hash
, file_name
, &mime_type
, 1))
635 return XDG_MIME_TYPE_UNKNOWN
;
639 xdg_mime_get_mime_types_from_file_name (const char *file_name
,
640 const char *mime_types
[],
646 return _xdg_mime_cache_get_mime_types_from_file_name (file_name
, mime_types
, n_mime_types
);
648 return _xdg_glob_hash_lookup_file_name (global_hash
, file_name
, mime_types
, n_mime_types
);
652 xdg_mime_is_valid_mime_type (const char *mime_type
)
654 /* FIXME: We should make this a better test
656 return _xdg_utf8_validate (mime_type
);
660 xdg_mime_shutdown (void)
662 XdgCallbackList
*list
;
664 /* FIXME: Need to make this (and the whole library) thread safe */
667 xdg_dir_time_list_free (dir_time_list
);
668 dir_time_list
= NULL
;
673 _xdg_glob_hash_free (global_hash
);
678 _xdg_mime_magic_free (global_magic
);
684 _xdg_mime_alias_list_free (alias_list
);
690 _xdg_mime_parent_list_free (parent_list
);
696 _xdg_mime_icon_list_free (icon_list
);
700 if (generic_icon_list
)
702 _xdg_mime_icon_list_free (generic_icon_list
);
703 generic_icon_list
= NULL
;
710 for (i
= 0; i
< n_caches
; i
++)
711 _xdg_mime_cache_unref (_caches
[i
]);
717 for (list
= callback_list
; list
; list
= list
->next
)
718 (list
->callback
) (list
->data
);
724 xdg_mime_get_max_buffer_extents (void)
729 return _xdg_mime_cache_get_max_buffer_extents ();
731 return _xdg_mime_magic_get_buffer_extents (global_magic
);
735 _xdg_mime_unalias_mime_type (const char *mime_type
)
740 return _xdg_mime_cache_unalias_mime_type (mime_type
);
742 if ((lookup
= _xdg_mime_alias_list_lookup (alias_list
, mime_type
)) != NULL
)
749 xdg_mime_unalias_mime_type (const char *mime_type
)
753 return _xdg_mime_unalias_mime_type (mime_type
);
757 _xdg_mime_mime_type_equal (const char *mime_a
,
760 const char *unalias_a
, *unalias_b
;
762 unalias_a
= _xdg_mime_unalias_mime_type (mime_a
);
763 unalias_b
= _xdg_mime_unalias_mime_type (mime_b
);
765 if (strcmp (unalias_a
, unalias_b
) == 0)
772 xdg_mime_mime_type_equal (const char *mime_a
,
777 return _xdg_mime_mime_type_equal (mime_a
, mime_b
);
781 xdg_mime_media_type_equal (const char *mime_a
,
786 sep
= strchr (mime_a
, '/');
788 if (sep
&& strncmp (mime_a
, mime_b
, sep
- mime_a
+ 1) == 0)
796 ends_with (const char *str
,
802 length
= strlen (str
);
803 suffix_length
= strlen (suffix
);
804 if (length
< suffix_length
)
807 if (strcmp (str
+ length
- suffix_length
, suffix
) == 0)
814 xdg_mime_is_super_type (const char *mime
)
816 return ends_with (mime
, "/*");
821 _xdg_mime_mime_type_subclass (const char *mime
,
824 const char *umime
, *ubase
;
825 const char **parents
;
828 return _xdg_mime_cache_mime_type_subclass (mime
, base
);
830 umime
= _xdg_mime_unalias_mime_type (mime
);
831 ubase
= _xdg_mime_unalias_mime_type (base
);
833 if (strcmp (umime
, ubase
) == 0)
837 /* Handle supertypes */
838 if (xdg_mime_is_super_type (ubase
) &&
839 xdg_mime_media_type_equal (umime
, ubase
))
843 /* Handle special cases text/plain and application/octet-stream */
844 if (strcmp (ubase
, "text/plain") == 0 &&
845 strncmp (umime
, "text/", 5) == 0)
848 if (strcmp (ubase
, "application/octet-stream") == 0 &&
849 strncmp (umime
, "inode/", 6) != 0)
852 parents
= _xdg_mime_parent_list_lookup (parent_list
, umime
);
853 for (; parents
&& *parents
; parents
++)
855 if (_xdg_mime_mime_type_subclass (*parents
, ubase
))
863 xdg_mime_mime_type_subclass (const char *mime
,
868 return _xdg_mime_mime_type_subclass (mime
, base
);
872 xdg_mime_list_mime_parents (const char *mime
)
874 const char **parents
;
881 return _xdg_mime_cache_list_mime_parents (mime
);
883 parents
= xdg_mime_get_mime_parents (mime
);
888 for (i
= 0; parents
[i
]; i
++) ;
890 n
= (i
+ 1) * sizeof (char *);
891 result
= (char **) malloc (n
);
892 memcpy (result
, parents
, n
);
898 xdg_mime_get_mime_parents (const char *mime
)
904 umime
= _xdg_mime_unalias_mime_type (mime
);
906 return _xdg_mime_parent_list_lookup (parent_list
, umime
);
914 printf ("*** ALIASES ***\n\n");
915 _xdg_mime_alias_list_dump (alias_list
);
916 printf ("\n*** PARENTS ***\n\n");
917 _xdg_mime_parent_list_dump (parent_list
);
918 printf ("\n*** CACHE ***\n\n");
919 _xdg_glob_hash_dump (global_hash
);
920 printf ("\n*** GLOBS ***\n\n");
921 _xdg_glob_hash_dump (global_hash
);
922 printf ("\n*** GLOBS REVERSE TREE ***\n\n");
923 _xdg_mime_cache_glob_dump ();
927 /* Registers a function to be called every time the mime database reloads its files
930 xdg_mime_register_reload_callback (XdgMimeCallback callback
,
932 XdgMimeDestroy destroy
)
934 XdgCallbackList
*list_el
;
935 static int callback_id
= 1;
937 /* Make a new list element */
938 list_el
= calloc (1, sizeof (XdgCallbackList
));
939 list_el
->callback_id
= callback_id
;
940 list_el
->callback
= callback
;
941 list_el
->data
= data
;
942 list_el
->destroy
= destroy
;
943 list_el
->next
= callback_list
;
945 list_el
->next
->prev
= list_el
;
947 callback_list
= list_el
;
950 return callback_id
- 1;
954 xdg_mime_remove_callback (int callback_id
)
956 XdgCallbackList
*list
;
958 for (list
= callback_list
; list
; list
= list
->next
)
960 if (list
->callback_id
== callback_id
)
963 list
->next
= list
->prev
;
966 list
->prev
->next
= list
->next
;
968 callback_list
= list
->next
;
970 /* invoke the destroy handler */
971 (list
->destroy
) (list
->data
);
979 xdg_mime_get_icon (const char *mime
)
984 return _xdg_mime_cache_get_icon (mime
);
986 return _xdg_mime_icon_list_lookup (icon_list
, mime
);
990 xdg_mime_get_generic_icon (const char *mime
)
995 return _xdg_mime_cache_get_generic_icon (mime
);
997 return _xdg_mime_icon_list_lookup (generic_icon_list
, mime
);