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 if (file_name == NULL)
152 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
153 if (stat (file_name, &st) == 0)
155 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
159 xdg_dir_time_list_add (file_name, st.st_mtime);
161 XdgMimeCache **tmp_caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
162 if (tmp_caches != NULL)
164 _caches = tmp_caches;
165 _caches[n_caches] = cache;
166 _caches[n_caches + 1] = NULL;
180 file_name = malloc (strlen (directory) + strlen ("/mime/globs2") + 1);
181 if (file_name == NULL)
184 strcpy (file_name, directory); strcat (file_name, "/mime/globs2");
185 if (stat (file_name, &st) == 0)
187 _xdg_mime_glob_read_from_file (global_hash, file_name, TRUE);
188 xdg_dir_time_list_add (file_name, st.st_mtime);
193 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
194 if (file_name == NULL)
197 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
198 if (stat (file_name, &st) == 0)
200 _xdg_mime_glob_read_from_file (global_hash, file_name, FALSE);
201 xdg_dir_time_list_add (file_name, st.st_mtime);
209 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
210 if (file_name == NULL)
213 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
214 if (stat (file_name, &st) == 0)
216 _xdg_mime_magic_read_from_file (global_magic, file_name);
217 xdg_dir_time_list_add (file_name, st.st_mtime);
224 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
225 if (file_name == NULL)
228 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
229 _xdg_mime_alias_read_from_file (alias_list, file_name);
232 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
233 if (file_name == NULL)
236 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
237 _xdg_mime_parent_read_from_file (parent_list, file_name);
240 file_name = malloc (strlen (directory) + strlen ("/mime/icons") + 1);
241 if (file_name == NULL)
244 strcpy (file_name, directory); strcat (file_name, "/mime/icons");
245 _xdg_mime_icon_read_from_file (icon_list, file_name);
248 file_name = malloc (strlen (directory) + strlen ("/mime/generic-icons") + 1);
249 if (file_name == NULL)
252 strcpy (file_name, directory); strcat (file_name, "/mime/generic-icons");
253 _xdg_mime_icon_read_from_file (generic_icon_list, file_name);
256 return FALSE; /* Keep processing */
259 /* Runs a command on all the directories in the search path */
261 xdg_run_command_on_dirs (XdgDirectoryFunc func,
264 const char *xdg_data_home;
265 const char *xdg_data_dirs;
268 xdg_data_home = getenv ("XDG_DATA_HOME");
271 if ((func) (xdg_data_home, user_data))
278 home = getenv ("HOME");
281 char *guessed_xdg_home;
284 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
285 strcpy (guessed_xdg_home, home);
286 strcat (guessed_xdg_home, "/.local/share/");
287 stop_processing = (func) (guessed_xdg_home, user_data);
288 free (guessed_xdg_home);
295 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
296 if (xdg_data_dirs == NULL)
297 xdg_data_dirs = "/usr/local/share/:/usr/share/";
301 while (*ptr != '\000')
309 while (*end_ptr != ':' && *end_ptr != '\000')
321 len = end_ptr - ptr + 1;
322 dir = malloc (len + 1);
323 strncpy (dir, ptr, len);
325 stop_processing = (func) (dir, user_data);
335 /* Checks file_path to make sure it has the same mtime as last time it was
336 * checked. If it has a different mtime, or if the file doesn't exist, it
339 * FIXME: This doesn't protect against permission changes.
342 xdg_check_file (const char *file_path,
347 /* If the file exists */
348 if (stat (file_path, &st) == 0)
350 XdgDirTimeList *list;
355 for (list = dir_time_list; list; list = list->next)
357 if (! strcmp (list->directory_name, file_path))
359 if (st.st_mtime == list->mtime)
360 list->checked = XDG_CHECKED_VALID;
362 list->checked = XDG_CHECKED_INVALID;
364 return (list->checked != XDG_CHECKED_VALID);
377 xdg_check_dir (const char *directory,
378 int *invalid_dir_list)
383 assert (directory != NULL);
385 /* Check the mime.cache file */
386 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
387 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
388 invalid = xdg_check_file (file_name, &exists);
392 *invalid_dir_list = TRUE;
400 /* Check the globs file */
401 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
402 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
403 invalid = xdg_check_file (file_name, NULL);
407 *invalid_dir_list = TRUE;
411 /* Check the magic file */
412 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
413 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
414 invalid = xdg_check_file (file_name, NULL);
418 *invalid_dir_list = TRUE;
422 return FALSE; /* Keep processing */
425 /* Walks through all the mime files stat()ing them to see if they've changed.
426 * Returns TRUE if they have. */
428 xdg_check_dirs (void)
430 XdgDirTimeList *list;
431 int invalid_dir_list = FALSE;
433 for (list = dir_time_list; list; list = list->next)
434 list->checked = XDG_CHECKED_UNCHECKED;
436 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
439 if (invalid_dir_list)
442 for (list = dir_time_list; list; list = list->next)
444 if (list->checked != XDG_CHECKED_VALID)
451 /* We want to avoid stat()ing on every single mime call, so we only look for
452 * newer files every 5 seconds. This will return TRUE if we need to reread the
453 * mime data from disk.
456 xdg_check_time_and_dirs (void)
462 gettimeofday (&tv, NULL);
463 current_time = tv.tv_sec;
465 if (current_time >= last_stat_time + 5)
467 retval = xdg_check_dirs ();
468 last_stat_time = current_time;
474 /* Called in every public function. It reloads the hash function if need be.
479 if (xdg_check_time_and_dirs ())
481 xdg_mime_shutdown ();
486 global_hash = _xdg_glob_hash_new ();
487 global_magic = _xdg_mime_magic_new ();
488 alias_list = _xdg_mime_alias_list_new ();
489 parent_list = _xdg_mime_parent_list_new ();
490 icon_list = _xdg_mime_icon_list_new ();
491 generic_icon_list = _xdg_mime_icon_list_new ();
493 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
501 xdg_mime_get_mime_type_for_data (const void *data,
505 const char *mime_type;
510 return _xdg_mime_cache_get_mime_type_for_data (data, len, result_prio);
512 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, result_prio, NULL, 0);
517 return XDG_MIME_TYPE_UNKNOWN;
521 xdg_mime_get_mime_type_for_file (const char *file_name,
522 struct stat *statbuf)
524 const char *mime_type;
525 /* currently, only a few globs occur twice, and none
526 * more often, so 5 seems plenty.
528 const char *mime_types[5];
534 const char *base_name;
537 if (file_name == NULL)
539 if (! _xdg_utf8_validate (file_name))
545 return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
547 base_name = _xdg_get_base_name (file_name);
548 n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
551 return mime_types[0];
555 if (stat (file_name, &buf) != 0)
556 return XDG_MIME_TYPE_UNKNOWN;
561 if (!S_ISREG (statbuf->st_mode))
562 return XDG_MIME_TYPE_UNKNOWN;
564 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
565 * be large and need getting from a stream instead of just reading it all
567 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
568 data = malloc (max_extent);
570 return XDG_MIME_TYPE_UNKNOWN;
572 file = fopen (file_name, "r");
576 return XDG_MIME_TYPE_UNKNOWN;
579 bytes_read = fread (data, 1, max_extent, file);
584 return XDG_MIME_TYPE_UNKNOWN;
587 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read, NULL,
596 return XDG_MIME_TYPE_UNKNOWN;
600 xdg_mime_get_mime_type_from_file_name (const char *file_name)
602 const char *mime_type;
607 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
609 if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, &mime_type, 1))
612 return XDG_MIME_TYPE_UNKNOWN;
616 xdg_mime_get_mime_types_from_file_name (const char *file_name,
617 const char *mime_types[],
623 return _xdg_mime_cache_get_mime_types_from_file_name (file_name, mime_types, n_mime_types);
625 return _xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, n_mime_types);
629 xdg_mime_is_valid_mime_type (const char *mime_type)
631 /* FIXME: We should make this a better test
633 return _xdg_utf8_validate (mime_type);
637 xdg_mime_shutdown (void)
639 XdgCallbackList *list;
641 /* FIXME: Need to make this (and the whole library) thread safe */
644 xdg_dir_time_list_free (dir_time_list);
645 dir_time_list = NULL;
650 _xdg_glob_hash_free (global_hash);
655 _xdg_mime_magic_free (global_magic);
661 _xdg_mime_alias_list_free (alias_list);
667 _xdg_mime_parent_list_free (parent_list);
673 _xdg_mime_icon_list_free (icon_list);
677 if (generic_icon_list)
679 _xdg_mime_icon_list_free (generic_icon_list);
680 generic_icon_list = NULL;
687 for (i = 0; i < n_caches; i++)
688 _xdg_mime_cache_unref (_caches[i]);
694 for (list = callback_list; list; list = list->next)
695 (list->callback) (list->data);
701 xdg_mime_get_max_buffer_extents (void)
706 return _xdg_mime_cache_get_max_buffer_extents ();
708 return _xdg_mime_magic_get_buffer_extents (global_magic);
712 _xdg_mime_unalias_mime_type (const char *mime_type)
717 return _xdg_mime_cache_unalias_mime_type (mime_type);
719 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
726 xdg_mime_unalias_mime_type (const char *mime_type)
730 return _xdg_mime_unalias_mime_type (mime_type);
734 _xdg_mime_mime_type_equal (const char *mime_a,
737 const char *unalias_a, *unalias_b;
739 unalias_a = _xdg_mime_unalias_mime_type (mime_a);
740 unalias_b = _xdg_mime_unalias_mime_type (mime_b);
742 if (strcmp (unalias_a, unalias_b) == 0)
749 xdg_mime_mime_type_equal (const char *mime_a,
754 return _xdg_mime_mime_type_equal (mime_a, mime_b);
758 xdg_mime_media_type_equal (const char *mime_a,
763 sep = strchr (mime_a, '/');
765 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
773 xdg_mime_is_super_type (const char *mime)
778 length = strlen (mime);
779 type = &(mime[length - 2]);
781 if (strcmp (type, "/*") == 0)
789 _xdg_mime_mime_type_subclass (const char *mime,
792 const char *umime, *ubase;
793 const char **parents;
796 return _xdg_mime_cache_mime_type_subclass (mime, base);
798 umime = _xdg_mime_unalias_mime_type (mime);
799 ubase = _xdg_mime_unalias_mime_type (base);
801 if (strcmp (umime, ubase) == 0)
805 /* Handle supertypes */
806 if (xdg_mime_is_super_type (ubase) &&
807 xdg_mime_media_type_equal (umime, ubase))
811 /* Handle special cases text/plain and application/octet-stream */
812 if (strcmp (ubase, "text/plain") == 0 &&
813 strncmp (umime, "text/", 5) == 0)
816 if (strcmp (ubase, "application/octet-stream") == 0)
819 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
820 for (; parents && *parents; parents++)
822 if (_xdg_mime_mime_type_subclass (*parents, ubase))
830 xdg_mime_mime_type_subclass (const char *mime,
835 return _xdg_mime_mime_type_subclass (mime, base);
839 xdg_mime_list_mime_parents (const char *mime)
841 const char **parents;
846 return _xdg_mime_cache_list_mime_parents (mime);
848 parents = xdg_mime_get_mime_parents (mime);
853 for (i = 0; parents[i]; i++) ;
855 n = (i + 1) * sizeof (char *);
856 result = (char **) malloc (n);
857 memcpy (result, parents, n);
863 xdg_mime_get_mime_parents (const char *mime)
869 umime = _xdg_mime_unalias_mime_type (mime);
871 return _xdg_mime_parent_list_lookup (parent_list, umime);
879 printf ("*** ALIASES ***\n\n");
880 _xdg_mime_alias_list_dump (alias_list);
881 printf ("\n*** PARENTS ***\n\n");
882 _xdg_mime_parent_list_dump (parent_list);
883 printf ("\n*** CACHE ***\n\n");
884 _xdg_glob_hash_dump (global_hash);
885 printf ("\n*** GLOBS ***\n\n");
886 _xdg_glob_hash_dump (global_hash);
887 printf ("\n*** GLOBS REVERSE TREE ***\n\n");
888 _xdg_mime_cache_glob_dump ();
892 /* Registers a function to be called every time the mime database reloads its files
895 xdg_mime_register_reload_callback (XdgMimeCallback callback,
897 XdgMimeDestroy destroy)
899 XdgCallbackList *list_el;
900 static int callback_id = 1;
902 /* Make a new list element */
903 list_el = calloc (1, sizeof (XdgCallbackList));
904 list_el->callback_id = callback_id;
905 list_el->callback = callback;
906 list_el->data = data;
907 list_el->destroy = destroy;
908 list_el->next = callback_list;
910 list_el->next->prev = list_el;
912 callback_list = list_el;
915 return callback_id - 1;
919 xdg_mime_remove_callback (int callback_id)
921 XdgCallbackList *list;
923 for (list = callback_list; list; list = list->next)
925 if (list->callback_id == callback_id)
928 list->next = list->prev;
931 list->prev->next = list->next;
933 callback_list = list->next;
935 /* invoke the destroy handler */
936 (list->destroy) (list->data);
944 xdg_mime_get_icon (const char *mime)
949 return _xdg_mime_cache_get_icon (mime);
951 return _xdg_mime_icon_list_lookup (icon_list, mime);
955 xdg_mime_get_generic_icon (const char *mime)
960 return _xdg_mime_cache_get_generic_icon (mime);
962 return _xdg_mime_icon_list_lookup (generic_icon_list, mime);