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>
49 #define API __attribute__ ((visibility("default")))
52 typedef struct XdgDirTimeList XdgDirTimeList;
53 typedef struct XdgCallbackList XdgCallbackList;
55 static int need_reread = TRUE;
56 static time_t last_stat_time = 0;
58 static XdgGlobHash *global_hash = NULL;
59 static XdgMimeMagic *global_magic = NULL;
60 static XdgAliasList *alias_list = NULL;
61 static XdgParentList *parent_list = NULL;
62 static XdgDirTimeList *dir_time_list = NULL;
63 static XdgCallbackList *callback_list = NULL;
64 static XdgIconList *icon_list = NULL;
65 static XdgIconList *generic_icon_list = NULL;
67 XdgMimeCache **_caches = NULL;
68 static int n_caches = 0;
70 const char xdg_mime_type_unknown[] = "application/octet-stream";
75 XDG_CHECKED_UNCHECKED,
88 struct XdgCallbackList
90 XdgCallbackList *next;
91 XdgCallbackList *prev;
93 XdgMimeCallback callback;
95 XdgMimeDestroy destroy;
98 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
99 * directories aren't looked at */
100 typedef int (*XdgDirectoryFunc) (const char *directory,
104 xdg_dir_time_list_add (char *file_name,
107 XdgDirTimeList *list;
109 for (list = dir_time_list; list; list = list->next)
111 if (strcmp (list->directory_name, file_name) == 0)
118 list = calloc (1, sizeof (XdgDirTimeList));
119 list->checked = XDG_CHECKED_UNCHECKED;
120 list->directory_name = file_name;
122 list->next = dir_time_list;
123 dir_time_list = list;
127 xdg_dir_time_list_free (XdgDirTimeList *list)
129 XdgDirTimeList *next;
134 free (list->directory_name);
141 xdg_mime_init_from_directory (const char *directory)
146 assert (directory != NULL);
148 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
149 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
150 if (stat (file_name, &st) == 0)
152 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
156 xdg_dir_time_list_add (file_name, st.st_mtime);
158 _caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
159 _caches[n_caches] = cache;
160 _caches[n_caches + 1] = NULL;
168 file_name = malloc (strlen (directory) + strlen ("/mime/globs2") + 1);
169 strcpy (file_name, directory); strcat (file_name, "/mime/globs2");
170 if (stat (file_name, &st) == 0)
172 _xdg_mime_glob_read_from_file (global_hash, file_name, TRUE);
173 xdg_dir_time_list_add (file_name, st.st_mtime);
178 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
179 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
180 if (stat (file_name, &st) == 0)
182 _xdg_mime_glob_read_from_file (global_hash, file_name, FALSE);
183 xdg_dir_time_list_add (file_name, st.st_mtime);
191 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
192 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
193 if (stat (file_name, &st) == 0)
195 _xdg_mime_magic_read_from_file (global_magic, file_name);
196 xdg_dir_time_list_add (file_name, st.st_mtime);
203 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
204 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
205 _xdg_mime_alias_read_from_file (alias_list, file_name);
208 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
209 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
210 _xdg_mime_parent_read_from_file (parent_list, file_name);
213 file_name = malloc (strlen (directory) + strlen ("/mime/icons") + 1);
214 strcpy (file_name, directory); strcat (file_name, "/mime/icons");
215 _xdg_mime_icon_read_from_file (icon_list, file_name);
218 file_name = malloc (strlen (directory) + strlen ("/mime/generic-icons") + 1);
219 strcpy (file_name, directory); strcat (file_name, "/mime/generic-icons");
220 _xdg_mime_icon_read_from_file (generic_icon_list, file_name);
223 return FALSE; /* Keep processing */
226 /* Runs a command on all the directories in the search path */
228 xdg_run_command_on_dirs (XdgDirectoryFunc func,
231 const char *xdg_data_home;
232 const char *xdg_data_dirs;
235 xdg_data_home = getenv ("XDG_DATA_HOME");
238 if ((func) (xdg_data_home, user_data))
245 home = getenv ("HOME");
248 char *guessed_xdg_home;
251 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
252 strcpy (guessed_xdg_home, home);
253 strcat (guessed_xdg_home, "/.local/share/");
254 stop_processing = (func) (guessed_xdg_home, user_data);
255 free (guessed_xdg_home);
262 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
263 if (xdg_data_dirs == NULL)
264 xdg_data_dirs = "/usr/local/share/:/usr/share/";
268 while (*ptr != '\000')
276 while (*end_ptr != ':' && *end_ptr != '\000')
288 len = end_ptr - ptr + 1;
289 dir = malloc (len + 1);
290 strncpy (dir, ptr, len);
292 stop_processing = (func) (dir, user_data);
302 /* Checks file_path to make sure it has the same mtime as last time it was
303 * checked. If it has a different mtime, or if the file doesn't exist, it
306 * FIXME: This doesn't protect against permission changes.
309 xdg_check_file (const char *file_path,
314 /* If the file exists */
315 if (stat (file_path, &st) == 0)
317 XdgDirTimeList *list;
322 for (list = dir_time_list; list; list = list->next)
324 if (! strcmp (list->directory_name, file_path))
326 if (st.st_mtime == list->mtime)
327 list->checked = XDG_CHECKED_VALID;
329 list->checked = XDG_CHECKED_INVALID;
331 return (list->checked != XDG_CHECKED_VALID);
344 xdg_check_dir (const char *directory,
345 int *invalid_dir_list)
350 assert (directory != NULL);
352 /* Check the mime.cache file */
353 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
354 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
355 invalid = xdg_check_file (file_name, &exists);
359 *invalid_dir_list = TRUE;
367 /* Check the globs file */
368 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
369 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
370 invalid = xdg_check_file (file_name, NULL);
374 *invalid_dir_list = TRUE;
378 /* Check the magic file */
379 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
380 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
381 invalid = xdg_check_file (file_name, NULL);
385 *invalid_dir_list = TRUE;
389 return FALSE; /* Keep processing */
392 /* Walks through all the mime files stat()ing them to see if they've changed.
393 * Returns TRUE if they have. */
395 xdg_check_dirs (void)
397 XdgDirTimeList *list;
398 int invalid_dir_list = FALSE;
400 for (list = dir_time_list; list; list = list->next)
401 list->checked = XDG_CHECKED_UNCHECKED;
403 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
406 if (invalid_dir_list)
409 for (list = dir_time_list; list; list = list->next)
411 if (list->checked != XDG_CHECKED_VALID)
418 /* We want to avoid stat()ing on every single mime call, so we only look for
419 * newer files every 5 seconds. This will return TRUE if we need to reread the
420 * mime data from disk.
423 xdg_check_time_and_dirs (void)
429 gettimeofday (&tv, NULL);
430 current_time = tv.tv_sec;
432 if (current_time >= last_stat_time + 5)
434 retval = xdg_check_dirs ();
435 last_stat_time = current_time;
441 /* Called in every public function. It reloads the hash function if need be.
446 if (xdg_check_time_and_dirs ())
448 xdg_mime_shutdown ();
453 global_hash = _xdg_glob_hash_new ();
454 global_magic = _xdg_mime_magic_new ();
455 alias_list = _xdg_mime_alias_list_new ();
456 parent_list = _xdg_mime_parent_list_new ();
457 icon_list = _xdg_mime_icon_list_new ();
458 generic_icon_list = _xdg_mime_icon_list_new ();
460 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
468 xdg_mime_get_mime_type_for_data (const void *data,
472 const char *mime_type;
477 return _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_MIME_TYPE_UNKNOWN;
488 xdg_mime_get_mime_type_for_file (const char *file_name,
489 struct stat *statbuf)
491 const char *mime_type;
492 /* currently, only a few globs occur twice, and none
493 * more often, so 5 seems plenty.
495 const char *mime_types[5];
501 const char *base_name;
504 if (file_name == NULL)
506 if (! _xdg_utf8_validate (file_name))
512 return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
514 base_name = _xdg_get_base_name (file_name);
515 n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
518 return mime_types[0];
522 if (stat (file_name, &buf) != 0)
523 return XDG_MIME_TYPE_UNKNOWN;
528 if (!S_ISREG (statbuf->st_mode))
529 return XDG_MIME_TYPE_UNKNOWN;
531 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
532 * be large and need getting from a stream instead of just reading it all
534 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
535 data = malloc (max_extent);
537 return XDG_MIME_TYPE_UNKNOWN;
539 file = fopen (file_name, "r");
543 return XDG_MIME_TYPE_UNKNOWN;
546 bytes_read = fread (data, 1, max_extent, file);
551 return XDG_MIME_TYPE_UNKNOWN;
554 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read, NULL,
563 return XDG_MIME_TYPE_UNKNOWN;
567 xdg_mime_get_mime_type_from_file_name (const char *file_name)
569 const char *mime_type;
574 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
576 if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, &mime_type, 1))
579 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);
596 xdg_mime_is_valid_mime_type (const char *mime_type)
598 /* FIXME: We should make this a better test
600 return _xdg_utf8_validate (mime_type);
604 xdg_mime_shutdown (void)
606 XdgCallbackList *list;
608 /* FIXME: Need to make this (and the whole library) thread safe */
611 xdg_dir_time_list_free (dir_time_list);
612 dir_time_list = NULL;
617 _xdg_glob_hash_free (global_hash);
622 _xdg_mime_magic_free (global_magic);
628 _xdg_mime_alias_list_free (alias_list);
634 _xdg_mime_parent_list_free (parent_list);
640 _xdg_mime_icon_list_free (icon_list);
644 if (generic_icon_list)
646 _xdg_mime_icon_list_free (generic_icon_list);
647 generic_icon_list = NULL;
654 for (i = 0; i < n_caches; i++)
655 _xdg_mime_cache_unref (_caches[i]);
661 for (list = callback_list; list; list = list->next)
662 (list->callback) (list->data);
668 xdg_mime_get_max_buffer_extents (void)
673 return _xdg_mime_cache_get_max_buffer_extents ();
675 return _xdg_mime_magic_get_buffer_extents (global_magic);
679 _xdg_mime_unalias_mime_type (const char *mime_type)
684 return _xdg_mime_cache_unalias_mime_type (mime_type);
686 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
693 xdg_mime_unalias_mime_type (const char *mime_type)
697 return _xdg_mime_unalias_mime_type (mime_type);
701 _xdg_mime_mime_type_equal (const char *mime_a,
704 const char *unalias_a, *unalias_b;
706 unalias_a = _xdg_mime_unalias_mime_type (mime_a);
707 unalias_b = _xdg_mime_unalias_mime_type (mime_b);
709 if (strcmp (unalias_a, unalias_b) == 0)
716 xdg_mime_mime_type_equal (const char *mime_a,
721 return _xdg_mime_mime_type_equal (mime_a, mime_b);
725 xdg_mime_media_type_equal (const char *mime_a,
730 sep = strchr (mime_a, '/');
732 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
740 xdg_mime_is_super_type (const char *mime)
745 length = strlen (mime);
746 type = &(mime[length - 2]);
748 if (strcmp (type, "/*") == 0)
756 _xdg_mime_mime_type_subclass (const char *mime,
759 const char *umime, *ubase;
760 const char **parents;
763 return _xdg_mime_cache_mime_type_subclass (mime, base);
765 umime = _xdg_mime_unalias_mime_type (mime);
766 ubase = _xdg_mime_unalias_mime_type (base);
768 if (strcmp (umime, ubase) == 0)
772 /* Handle supertypes */
773 if (xdg_mime_is_super_type (ubase) &&
774 xdg_mime_media_type_equal (umime, ubase))
778 /* Handle special cases text/plain and application/octet-stream */
779 if (strcmp (ubase, "text/plain") == 0 &&
780 strncmp (umime, "text/", 5) == 0)
783 if (strcmp (ubase, "application/octet-stream") == 0)
786 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
787 for (; parents && *parents; parents++)
789 if (_xdg_mime_mime_type_subclass (*parents, ubase))
797 xdg_mime_mime_type_subclass (const char *mime,
802 return _xdg_mime_mime_type_subclass (mime, base);
806 xdg_mime_list_mime_parents (const char *mime)
808 const char **parents;
813 return _xdg_mime_cache_list_mime_parents (mime);
815 parents = xdg_mime_get_mime_parents (mime);
820 for (i = 0; parents[i]; i++) ;
822 n = (i + 1) * sizeof (char *);
823 result = (char **) malloc (n);
824 memcpy (result, parents, n);
830 xdg_mime_get_mime_parents (const char *mime)
836 umime = _xdg_mime_unalias_mime_type (mime);
838 return _xdg_mime_parent_list_lookup (parent_list, umime);
846 printf ("*** ALIASES ***\n\n");
847 _xdg_mime_alias_list_dump (alias_list);
848 printf ("\n*** PARENTS ***\n\n");
849 _xdg_mime_parent_list_dump (parent_list);
850 printf ("\n*** CACHE ***\n\n");
851 _xdg_glob_hash_dump (global_hash);
852 printf ("\n*** GLOBS ***\n\n");
853 _xdg_glob_hash_dump (global_hash);
854 printf ("\n*** GLOBS REVERSE TREE ***\n\n");
855 _xdg_mime_cache_glob_dump ();
859 /* Registers a function to be called every time the mime database reloads its files
862 xdg_mime_register_reload_callback (XdgMimeCallback callback,
864 XdgMimeDestroy destroy)
866 XdgCallbackList *list_el;
867 static int callback_id = 1;
869 /* Make a new list element */
870 list_el = calloc (1, sizeof (XdgCallbackList));
871 list_el->callback_id = callback_id;
872 list_el->callback = callback;
873 list_el->data = data;
874 list_el->destroy = destroy;
875 list_el->next = callback_list;
877 list_el->next->prev = list_el;
879 callback_list = list_el;
882 return callback_id - 1;
886 xdg_mime_remove_callback (int callback_id)
888 XdgCallbackList *list;
890 for (list = callback_list; list; list = list->next)
892 if (list->callback_id == callback_id)
895 list->next = list->prev;
898 list->prev->next = list->next;
900 callback_list = list->next;
902 /* invoke the destroy handler */
903 (list->destroy) (list->data);
911 xdg_mime_get_icon (const char *mime)
916 return _xdg_mime_cache_get_icon (mime);
918 return _xdg_mime_icon_list_lookup (icon_list, mime);
922 xdg_mime_get_generic_icon (const char *mime)
927 return _xdg_mime_cache_get_generic_icon (mime);
929 return _xdg_mime_icon_list_lookup (generic_icon_list, mime);