1 /* xdgmime.c: XDG Mime Spec mime resolver. Based on version 0.11 of the spec.
3 * More info can be found at http://www.freedesktop.org/standards/
5 * Copyright (C) 2003,2004 Red Hat, Inc.
6 * Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
8 * Licensed under the Academic Free License version 2.0
9 * Or under the following terms:
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
32 #include "xdgmimeint.h"
33 #include "xdgmimeglob.h"
34 #include "xdgmimemagic.h"
35 #include "xdgmimealias.h"
36 #include "xdgmimeparent.h"
37 #include "xdgmimecache.h"
41 #include <sys/types.h>
46 typedef struct XdgDirTimeList XdgDirTimeList;
47 typedef struct XdgCallbackList XdgCallbackList;
49 static int need_reread = TRUE;
50 static time_t last_stat_time = 0;
52 static XdgGlobHash *global_hash = NULL;
53 static XdgMimeMagic *global_magic = NULL;
54 static XdgAliasList *alias_list = NULL;
55 static XdgParentList *parent_list = NULL;
56 static XdgDirTimeList *dir_time_list = NULL;
57 static XdgCallbackList *callback_list = NULL;
59 XdgMimeCache **_xdg_mime_caches = NULL;
60 static int n_caches = 0;
62 const char xdg_mime_type_unknown[] = "application/octet-stream";
67 XDG_CHECKED_UNCHECKED,
81 struct XdgCallbackList
83 XdgCallbackList *next;
84 XdgCallbackList *prev;
86 XdgMimeCallback callback;
88 XdgMimeDestroy destroy;
91 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
92 * directories aren't looked at */
93 typedef int (*XdgDirectoryFunc) (const char *directory,
96 static XdgDirTimeList *
97 xdg_dir_time_list_new (void)
99 XdgDirTimeList *retval;
101 retval = calloc (1, sizeof (XdgDirTimeList));
102 retval->checked = XDG_CHECKED_UNCHECKED;
108 xdg_dir_time_list_free (XdgDirTimeList *list)
110 XdgDirTimeList *next;
115 free (list->directory_name);
122 xdg_mime_init_from_directory (const char *directory)
126 XdgDirTimeList *list;
128 assert (directory != NULL);
130 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
131 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
132 if (stat (file_name, &st) == 0)
134 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
138 list = xdg_dir_time_list_new ();
139 list->directory_name = file_name;
140 list->mtime = st.st_mtime;
141 list->next = dir_time_list;
143 dir_time_list = list;
145 _xdg_mime_caches = realloc (_xdg_mime_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
146 _xdg_mime_caches[n_caches] = cache;
147 _xdg_mime_caches[n_caches + 1] = NULL;
155 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
156 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
157 if (stat (file_name, &st) == 0)
159 _xdg_mime_glob_read_from_file (global_hash, file_name);
161 list = xdg_dir_time_list_new ();
162 list->directory_name = file_name;
163 list->mtime = st.st_mtime;
164 list->next = dir_time_list;
165 dir_time_list = list;
172 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
173 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
174 if (stat (file_name, &st) == 0)
176 _xdg_mime_magic_read_from_file (global_magic, file_name);
178 list = xdg_dir_time_list_new ();
179 list->directory_name = file_name;
180 list->mtime = st.st_mtime;
181 list->next = dir_time_list;
182 dir_time_list = list;
189 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
190 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
191 _xdg_mime_alias_read_from_file (alias_list, file_name);
194 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
195 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
196 _xdg_mime_parent_read_from_file (parent_list, file_name);
199 return FALSE; /* Keep processing */
202 /* Runs a command on all the directories in the search path */
204 xdg_run_command_on_dirs (XdgDirectoryFunc func,
207 const char *xdg_data_home;
208 const char *xdg_data_dirs;
211 xdg_data_home = getenv ("XDG_DATA_HOME");
214 if ((func) (xdg_data_home, user_data))
221 home = getenv ("HOME");
224 char *guessed_xdg_home;
227 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
228 strcpy (guessed_xdg_home, home);
229 strcat (guessed_xdg_home, "/.local/share/");
230 stop_processing = (func) (guessed_xdg_home, user_data);
231 free (guessed_xdg_home);
238 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
239 if (xdg_data_dirs == NULL)
240 xdg_data_dirs = "/usr/local/share/:/usr/share/";
244 while (*ptr != '\000')
252 while (*end_ptr != ':' && *end_ptr != '\000')
264 len = end_ptr - ptr + 1;
265 dir = malloc (len + 1);
266 strncpy (dir, ptr, len);
268 stop_processing = (func) (dir, user_data);
278 static XdgMimeCache *
279 xdg_lookup_cache_for_file (const char *file_path)
281 XdgDirTimeList *list;
283 for (list = dir_time_list; list; list = list->next)
284 if (! strcmp (list->directory_name, file_path))
290 /* Checks file_path to make sure it has the same mtime as last time it was
291 * checked. If it has a different mtime, or if the file doesn't exist, it
294 * FIXME: This doesn't protect against permission changes.
297 xdg_check_file (const char *file_path)
301 /* If the file exists */
302 if (stat (file_path, &st) == 0)
304 XdgDirTimeList *list;
306 for (list = dir_time_list; list; list = list->next)
308 if (! strcmp (list->directory_name, file_path) &&
309 st.st_mtime == list->mtime)
311 if (list->checked == XDG_CHECKED_UNCHECKED)
312 list->checked = XDG_CHECKED_VALID;
313 else if (list->checked == XDG_CHECKED_VALID)
314 list->checked = XDG_CHECKED_INVALID;
316 return (list->checked != XDG_CHECKED_VALID);
326 xdg_check_dir (const char *directory,
327 int *invalid_dir_list)
329 int invalid, has_cache;
332 assert (directory != NULL);
334 /* Check the mime.cache file */
335 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
336 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
337 invalid = xdg_check_file (file_name);
338 has_cache = xdg_lookup_cache_for_file (file_name) != NULL;
345 *invalid_dir_list = TRUE;
352 /* Check the globs file */
353 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
354 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
355 invalid = xdg_check_file (file_name);
359 *invalid_dir_list = TRUE;
363 /* Check the magic file */
364 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
365 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
366 invalid = xdg_check_file (file_name);
370 *invalid_dir_list = TRUE;
374 return FALSE; /* Keep processing */
377 /* Walks through all the mime files stat()ing them to see if they've changed.
378 * Returns TRUE if they have. */
380 xdg_check_dirs (void)
382 XdgDirTimeList *list;
383 int invalid_dir_list = FALSE;
385 for (list = dir_time_list; list; list = list->next)
386 list->checked = XDG_CHECKED_UNCHECKED;
388 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
391 if (invalid_dir_list)
394 for (list = dir_time_list; list; list = list->next)
396 if (list->checked != XDG_CHECKED_VALID)
403 /* We want to avoid stat()ing on every single mime call, so we only look for
404 * newer files every 5 seconds. This will return TRUE if we need to reread the
405 * mime data from disk.
408 xdg_check_time_and_dirs (void)
414 gettimeofday (&tv, NULL);
415 current_time = tv.tv_sec;
417 if (current_time >= last_stat_time + 5)
419 retval = xdg_check_dirs ();
420 last_stat_time = current_time;
426 /* Called in every public function. It reloads the hash function if need be.
431 if (xdg_check_time_and_dirs ())
433 xdg_mime_shutdown ();
438 global_hash = _xdg_glob_hash_new ();
439 global_magic = _xdg_mime_magic_new ();
440 alias_list = _xdg_mime_alias_list_new ();
441 parent_list = _xdg_mime_parent_list_new ();
443 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
451 xdg_mime_get_mime_type_for_data (const void *data,
454 const char *mime_type;
458 if (_xdg_mime_caches)
459 return _xdg_mime_cache_get_mime_type_for_data (data, len);
461 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, NULL, 0);
466 return XDG_MIME_TYPE_UNKNOWN;
470 xdg_mime_get_mime_type_for_file (const char *file_name,
471 struct stat *statbuf)
473 const char *mime_type;
474 /* Used to detect whether multiple MIME types match file_name */
475 const char *mime_types[2];
481 const char *base_name;
484 if (file_name == NULL)
486 if (! _xdg_utf8_validate (file_name))
491 if (_xdg_mime_caches)
492 return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
494 base_name = _xdg_get_base_name (file_name);
495 n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 2);
498 return mime_types[0];
502 if (stat (file_name, &buf) != 0)
503 return XDG_MIME_TYPE_UNKNOWN;
508 if (!S_ISREG (statbuf->st_mode))
509 return XDG_MIME_TYPE_UNKNOWN;
511 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
512 * be large and need getting from a stream instead of just reading it all
514 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
515 data = malloc (max_extent);
517 return XDG_MIME_TYPE_UNKNOWN;
519 file = fopen (file_name, "r");
523 return XDG_MIME_TYPE_UNKNOWN;
526 bytes_read = fread (data, 1, max_extent, file);
531 return XDG_MIME_TYPE_UNKNOWN;
534 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read,
543 return XDG_MIME_TYPE_UNKNOWN;
547 xdg_mime_get_mime_type_from_file_name (const char *file_name)
549 const char *mime_types[2];
553 if (_xdg_mime_caches)
554 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
556 if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, 2) == 1)
557 return mime_types[0];
559 return XDG_MIME_TYPE_UNKNOWN;
563 xdg_mime_is_valid_mime_type (const char *mime_type)
565 /* FIXME: We should make this a better test
567 return _xdg_utf8_validate (mime_type);
571 xdg_mime_shutdown (void)
573 XdgCallbackList *list;
575 /* FIXME: Need to make this (and the whole library) thread safe */
578 xdg_dir_time_list_free (dir_time_list);
579 dir_time_list = NULL;
584 _xdg_glob_hash_free (global_hash);
589 _xdg_mime_magic_free (global_magic);
595 _xdg_mime_alias_list_free (alias_list);
601 _xdg_mime_parent_list_free (parent_list);
605 if (_xdg_mime_caches)
608 for (i = 0; i < n_caches; i++)
609 _xdg_mime_cache_unref (_xdg_mime_caches[i]);
610 free (_xdg_mime_caches);
611 _xdg_mime_caches = NULL;
615 for (list = callback_list; list; list = list->next)
616 (list->callback) (list->data);
622 xdg_mime_get_max_buffer_extents (void)
626 if (_xdg_mime_caches)
627 return _xdg_mime_cache_get_max_buffer_extents ();
629 return _xdg_mime_magic_get_buffer_extents (global_magic);
633 _xdg_mime_unalias_mime_type (const char *mime_type)
637 if (_xdg_mime_caches)
638 return _xdg_mime_cache_unalias_mime_type (mime_type);
640 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
647 xdg_mime_unalias_mime_type (const char *mime_type)
651 return _xdg_mime_unalias_mime_type (mime_type);
655 _xdg_mime_mime_type_equal (const char *mime_a,
658 const char *unalias_a, *unalias_b;
660 unalias_a = _xdg_mime_unalias_mime_type (mime_a);
661 unalias_b = _xdg_mime_unalias_mime_type (mime_b);
663 if (strcmp (unalias_a, unalias_b) == 0)
670 xdg_mime_mime_type_equal (const char *mime_a,
675 return _xdg_mime_mime_type_equal (mime_a, mime_b);
679 _xdg_mime_media_type_equal (const char *mime_a,
686 sep = strchr (mime_a, '/');
688 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
695 xdg_mime_media_type_equal (const char *mime_a,
700 return _xdg_mime_media_type_equal (mime_a, mime_b);
705 xdg_mime_is_super_type (const char *mime)
710 length = strlen (mime);
711 type = &(mime[length - 2]);
713 if (strcmp (type, "/*") == 0)
721 _xdg_mime_mime_type_subclass (const char *mime,
724 const char *umime, *ubase;
725 const char **parents;
727 if (_xdg_mime_caches)
728 return _xdg_mime_cache_mime_type_subclass (mime, base);
730 umime = _xdg_mime_unalias_mime_type (mime);
731 ubase = _xdg_mime_unalias_mime_type (base);
733 if (strcmp (umime, ubase) == 0)
737 /* Handle supertypes */
738 if (xdg_mime_is_super_type (ubase) &&
739 _xdg_mime_media_type_equal (umime, ubase))
743 /* Handle special cases text/plain and application/octet-stream */
744 if (strcmp (ubase, "text/plain") == 0 &&
745 strncmp (umime, "text/", 5) == 0)
748 if (strcmp (ubase, "application/octet-stream") == 0)
751 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
752 for (; parents && *parents; parents++)
754 if (_xdg_mime_mime_type_subclass (*parents, ubase))
762 xdg_mime_mime_type_subclass (const char *mime,
767 return _xdg_mime_mime_type_subclass (mime, base);
771 xdg_mime_list_mime_parents (const char *mime)
773 const char **parents;
777 if (_xdg_mime_caches)
778 return _xdg_mime_cache_list_mime_parents (mime);
780 parents = xdg_mime_get_mime_parents (mime);
785 for (i = 0; parents[i]; i++) ;
787 n = (i + 1) * sizeof (char *);
788 result = (char **) malloc (n);
789 memcpy (result, parents, n);
795 xdg_mime_get_mime_parents (const char *mime)
801 umime = _xdg_mime_unalias_mime_type (mime);
803 return _xdg_mime_parent_list_lookup (parent_list, umime);
809 printf ("*** ALIASES ***\n\n");
810 _xdg_mime_alias_list_dump (alias_list);
811 printf ("\n*** PARENTS ***\n\n");
812 _xdg_mime_parent_list_dump (parent_list);
813 printf ("\n*** CACHE ***\n\n");
814 _xdg_glob_hash_dump (global_hash);
818 /* Registers a function to be called every time the mime database reloads its files
821 xdg_mime_register_reload_callback (XdgMimeCallback callback,
823 XdgMimeDestroy destroy)
825 XdgCallbackList *list_el;
826 static int callback_id = 1;
828 /* Make a new list element */
829 list_el = calloc (1, sizeof (XdgCallbackList));
830 list_el->callback_id = callback_id;
831 list_el->callback = callback;
832 list_el->data = data;
833 list_el->destroy = destroy;
834 list_el->next = callback_list;
836 list_el->next->prev = list_el;
838 callback_list = list_el;
841 return callback_id - 1;
845 xdg_mime_remove_callback (int callback_id)
847 XdgCallbackList *list;
849 for (list = callback_list; list; list = list->next)
851 if (list->callback_id == callback_id)
854 list->next = list->prev;
857 list->prev->next = list->next;
859 callback_list = list->next;
861 /* invoke the destroy handler */
862 (list->destroy) (list->data);