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 "xdgmimeparent.h"
38 #include "xdgmimecache.h"
42 #include <sys/types.h>
51 typedef struct XdgDirTimeList XdgDirTimeList
;
52 typedef struct XdgCallbackList XdgCallbackList
;
54 static int need_reread
= TRUE
;
55 static time_t last_stat_time
= 0;
57 static XdgGlobHash
*global_hash
= NULL
;
58 static XdgMimeMagic
*global_magic
= NULL
;
59 static XdgAliasList
*alias_list
= NULL
;
60 static XdgParentList
*parent_list
= NULL
;
61 static XdgDirTimeList
*dir_time_list
= NULL
;
62 static XdgCallbackList
*callback_list
= NULL
;
64 XdgMimeCache
**_xdg_mime_caches
= NULL
;
65 static int n_caches
= 0;
67 const char xdg_mime_type_unknown
[] = "application/octet-stream";
72 XDG_CHECKED_UNCHECKED
,
86 struct XdgCallbackList
88 XdgCallbackList
*next
;
89 XdgCallbackList
*prev
;
91 XdgMimeCallback callback
;
93 XdgMimeDestroy destroy
;
96 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
97 * directories aren't looked at */
98 typedef int (*XdgDirectoryFunc
) (const char *directory
,
101 static XdgDirTimeList
*
102 xdg_dir_time_list_new (void)
104 XdgDirTimeList
*retval
;
106 retval
= calloc (1, sizeof (XdgDirTimeList
));
107 retval
->checked
= XDG_CHECKED_UNCHECKED
;
113 xdg_dir_time_list_free (XdgDirTimeList
*list
)
115 XdgDirTimeList
*next
;
120 free (list
->directory_name
);
127 xdg_mime_init_from_directory (const char *directory
)
131 XdgDirTimeList
*list
;
133 assert (directory
!= NULL
);
135 file_name
= malloc (strlen (directory
) + strlen ("/mime/mime.cache") + 1);
136 strcpy (file_name
, directory
); strcat (file_name
, "/mime/mime.cache");
137 if (stat (file_name
, &st
) == 0)
139 XdgMimeCache
*cache
= _xdg_mime_cache_new_from_file (file_name
);
143 list
= xdg_dir_time_list_new ();
144 list
->directory_name
= file_name
;
145 list
->mtime
= st
.st_mtime
;
146 list
->next
= dir_time_list
;
148 dir_time_list
= list
;
150 _xdg_mime_caches
= realloc (_xdg_mime_caches
, sizeof (XdgMimeCache
*) * (n_caches
+ 2));
151 _xdg_mime_caches
[n_caches
] = cache
;
152 _xdg_mime_caches
[n_caches
+ 1] = NULL
;
160 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs") + 1);
161 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs");
162 if (stat (file_name
, &st
) == 0)
164 _xdg_mime_glob_read_from_file (global_hash
, file_name
);
166 list
= xdg_dir_time_list_new ();
167 list
->directory_name
= file_name
;
168 list
->mtime
= st
.st_mtime
;
169 list
->next
= dir_time_list
;
170 dir_time_list
= list
;
177 file_name
= malloc (strlen (directory
) + strlen ("/mime/magic") + 1);
178 strcpy (file_name
, directory
); strcat (file_name
, "/mime/magic");
179 if (stat (file_name
, &st
) == 0)
181 _xdg_mime_magic_read_from_file (global_magic
, file_name
);
183 list
= xdg_dir_time_list_new ();
184 list
->directory_name
= file_name
;
185 list
->mtime
= st
.st_mtime
;
186 list
->next
= dir_time_list
;
187 dir_time_list
= list
;
194 file_name
= malloc (strlen (directory
) + strlen ("/mime/aliases") + 1);
195 strcpy (file_name
, directory
); strcat (file_name
, "/mime/aliases");
196 _xdg_mime_alias_read_from_file (alias_list
, file_name
);
199 file_name
= malloc (strlen (directory
) + strlen ("/mime/subclasses") + 1);
200 strcpy (file_name
, directory
); strcat (file_name
, "/mime/subclasses");
201 _xdg_mime_parent_read_from_file (parent_list
, file_name
);
204 return FALSE
; /* Keep processing */
207 /* Runs a command on all the directories in the search path */
209 xdg_run_command_on_dirs (XdgDirectoryFunc func
,
212 const char *xdg_data_home
;
213 const char *xdg_data_dirs
;
216 xdg_data_home
= getenv ("XDG_DATA_HOME");
219 if ((func
) (xdg_data_home
, user_data
))
226 home
= getenv ("HOME");
229 char *guessed_xdg_home
;
232 guessed_xdg_home
= malloc (strlen (home
) + strlen ("/.local/share/") + 1);
233 strcpy (guessed_xdg_home
, home
);
234 strcat (guessed_xdg_home
, "/.local/share/");
235 stop_processing
= (func
) (guessed_xdg_home
, user_data
);
236 free (guessed_xdg_home
);
243 xdg_data_dirs
= getenv ("XDG_DATA_DIRS");
244 if (xdg_data_dirs
== NULL
)
245 xdg_data_dirs
= "/usr/local/share/:/usr/share/";
249 while (*ptr
!= '\000')
257 while (*end_ptr
!= ':' && *end_ptr
!= '\000')
269 len
= end_ptr
- ptr
+ 1;
270 dir
= malloc (len
+ 1);
271 strncpy (dir
, ptr
, len
);
273 stop_processing
= (func
) (dir
, user_data
);
283 static XdgMimeCache
*
284 xdg_lookup_cache_for_file (const char *file_path
)
286 XdgDirTimeList
*list
;
288 for (list
= dir_time_list
; list
; list
= list
->next
)
289 if (! strcmp (list
->directory_name
, file_path
))
295 /* Checks file_path to make sure it has the same mtime as last time it was
296 * checked. If it has a different mtime, or if the file doesn't exist, it
299 * FIXME: This doesn't protect against permission changes.
302 xdg_check_file (const char *file_path
)
306 /* If the file exists */
307 if (stat (file_path
, &st
) == 0)
309 XdgDirTimeList
*list
;
311 for (list
= dir_time_list
; list
; list
= list
->next
)
313 if (! strcmp (list
->directory_name
, file_path
) &&
314 st
.st_mtime
== list
->mtime
)
316 if (list
->checked
== XDG_CHECKED_UNCHECKED
)
317 list
->checked
= XDG_CHECKED_VALID
;
318 else if (list
->checked
== XDG_CHECKED_VALID
)
319 list
->checked
= XDG_CHECKED_INVALID
;
321 return (list
->checked
!= XDG_CHECKED_VALID
);
331 xdg_check_dir (const char *directory
,
332 int *invalid_dir_list
)
334 int invalid
, has_cache
;
337 assert (directory
!= NULL
);
339 /* Check the mime.cache file */
340 file_name
= malloc (strlen (directory
) + strlen ("/mime/mime.cache") + 1);
341 strcpy (file_name
, directory
); strcat (file_name
, "/mime/mime.cache");
342 invalid
= xdg_check_file (file_name
);
343 has_cache
= xdg_lookup_cache_for_file (file_name
) != NULL
;
350 *invalid_dir_list
= TRUE
;
357 /* Check the globs file */
358 file_name
= malloc (strlen (directory
) + strlen ("/mime/globs") + 1);
359 strcpy (file_name
, directory
); strcat (file_name
, "/mime/globs");
360 invalid
= xdg_check_file (file_name
);
364 *invalid_dir_list
= TRUE
;
368 /* Check the magic file */
369 file_name
= malloc (strlen (directory
) + strlen ("/mime/magic") + 1);
370 strcpy (file_name
, directory
); strcat (file_name
, "/mime/magic");
371 invalid
= xdg_check_file (file_name
);
375 *invalid_dir_list
= TRUE
;
379 return FALSE
; /* Keep processing */
382 /* Walks through all the mime files stat()ing them to see if they've changed.
383 * Returns TRUE if they have. */
385 xdg_check_dirs (void)
387 XdgDirTimeList
*list
;
388 int invalid_dir_list
= FALSE
;
390 for (list
= dir_time_list
; list
; list
= list
->next
)
391 list
->checked
= XDG_CHECKED_UNCHECKED
;
393 xdg_run_command_on_dirs ((XdgDirectoryFunc
) xdg_check_dir
,
396 if (invalid_dir_list
)
399 for (list
= dir_time_list
; list
; list
= list
->next
)
401 if (list
->checked
!= XDG_CHECKED_VALID
)
408 /* We want to avoid stat()ing on every single mime call, so we only look for
409 * newer files every 5 seconds. This will return TRUE if we need to reread the
410 * mime data from disk.
413 xdg_check_time_and_dirs (void)
419 gettimeofday (&tv
, NULL
);
420 current_time
= tv
.tv_sec
;
422 if (current_time
>= last_stat_time
+ 5)
424 retval
= xdg_check_dirs ();
425 last_stat_time
= current_time
;
431 /* Called in every public function. It reloads the hash function if need be.
436 if (xdg_check_time_and_dirs ())
438 xdg_mime_shutdown ();
446 global_hash
= _xdg_glob_hash_new ();
447 global_magic
= _xdg_mime_magic_new ();
448 alias_list
= _xdg_mime_alias_list_new ();
449 parent_list
= _xdg_mime_parent_list_new ();
451 xdg_run_command_on_dirs ((XdgDirectoryFunc
) xdg_mime_init_from_directory
,
454 /* ROX: We want to support shared-mime-database < 0.16, where we can't
455 * do this with the rox.xml file.
457 mime_parents
= g_build_filename(app_dir
, "subclasses", NULL
);
458 _xdg_mime_parent_read_from_file(parent_list
, mime_parents
);
459 g_free(mime_parents
);
466 xdg_mime_get_mime_type_for_data (const void *data
,
469 const char *mime_type
;
473 if (_xdg_mime_caches
)
474 return _xdg_mime_cache_get_mime_type_for_data (data
, len
);
476 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, len
, NULL
, 0);
481 return XDG_MIME_TYPE_UNKNOWN
;
485 xdg_mime_get_mime_type_for_file (const char *file_name
,
486 struct stat
*statbuf
)
488 const char *mime_type
;
489 /* Used to detect whether multiple MIME types match file_name */
490 const char *mime_types
[2];
496 const char *base_name
;
499 if (file_name
== NULL
)
501 if (! _xdg_utf8_validate (file_name
))
506 if (_xdg_mime_caches
)
507 return _xdg_mime_cache_get_mime_type_for_file (file_name
, statbuf
);
509 base_name
= _xdg_get_base_name (file_name
);
510 n
= _xdg_glob_hash_lookup_file_name (global_hash
, base_name
, mime_types
, 2);
513 return mime_types
[0];
517 if (stat (file_name
, &buf
) != 0)
518 return XDG_MIME_TYPE_UNKNOWN
;
523 if (!S_ISREG (statbuf
->st_mode
))
524 return XDG_MIME_TYPE_UNKNOWN
;
526 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
527 * be large and need getting from a stream instead of just reading it all
529 max_extent
= _xdg_mime_magic_get_buffer_extents (global_magic
);
530 data
= malloc (max_extent
);
532 return XDG_MIME_TYPE_UNKNOWN
;
534 file
= fopen (file_name
, "r");
538 return XDG_MIME_TYPE_UNKNOWN
;
541 bytes_read
= fread (data
, 1, max_extent
, file
);
546 return XDG_MIME_TYPE_UNKNOWN
;
549 mime_type
= _xdg_mime_magic_lookup_data (global_magic
, data
, bytes_read
,
558 return XDG_MIME_TYPE_UNKNOWN
;
562 xdg_mime_get_mime_type_from_file_name (const char *file_name
)
564 const char *mime_types
[2];
568 if (_xdg_mime_caches
)
569 return _xdg_mime_cache_get_mime_type_from_file_name (file_name
);
571 if (_xdg_glob_hash_lookup_file_name (global_hash
, file_name
, mime_types
, 2) == 1)
572 return mime_types
[0];
574 return XDG_MIME_TYPE_UNKNOWN
;
578 xdg_mime_is_valid_mime_type (const char *mime_type
)
580 /* FIXME: We should make this a better test
582 return _xdg_utf8_validate (mime_type
);
586 xdg_mime_shutdown (void)
588 XdgCallbackList
*list
;
590 /* FIXME: Need to make this (and the whole library) thread safe */
593 xdg_dir_time_list_free (dir_time_list
);
594 dir_time_list
= NULL
;
599 _xdg_glob_hash_free (global_hash
);
604 _xdg_mime_magic_free (global_magic
);
610 _xdg_mime_alias_list_free (alias_list
);
616 _xdg_mime_parent_list_free (parent_list
);
620 if (_xdg_mime_caches
)
623 for (i
= 0; i
< n_caches
; i
++)
624 _xdg_mime_cache_unref (_xdg_mime_caches
[i
]);
625 free (_xdg_mime_caches
);
626 _xdg_mime_caches
= NULL
;
630 for (list
= callback_list
; list
; list
= list
->next
)
631 (list
->callback
) (list
->data
);
637 xdg_mime_get_max_buffer_extents (void)
641 if (_xdg_mime_caches
)
642 return _xdg_mime_cache_get_max_buffer_extents ();
644 return _xdg_mime_magic_get_buffer_extents (global_magic
);
648 _xdg_mime_unalias_mime_type (const char *mime_type
)
652 if (_xdg_mime_caches
)
653 return _xdg_mime_cache_unalias_mime_type (mime_type
);
655 if ((lookup
= _xdg_mime_alias_list_lookup (alias_list
, mime_type
)) != NULL
)
662 xdg_mime_unalias_mime_type (const char *mime_type
)
666 return _xdg_mime_unalias_mime_type (mime_type
);
670 _xdg_mime_mime_type_equal (const char *mime_a
,
673 const char *unalias_a
, *unalias_b
;
675 unalias_a
= _xdg_mime_unalias_mime_type (mime_a
);
676 unalias_b
= _xdg_mime_unalias_mime_type (mime_b
);
678 if (strcmp (unalias_a
, unalias_b
) == 0)
685 xdg_mime_mime_type_equal (const char *mime_a
,
690 return _xdg_mime_mime_type_equal (mime_a
, mime_b
);
694 _xdg_mime_media_type_equal (const char *mime_a
,
701 sep
= strchr (mime_a
, '/');
703 if (sep
&& strncmp (mime_a
, mime_b
, sep
- mime_a
+ 1) == 0)
710 xdg_mime_media_type_equal (const char *mime_a
,
715 return _xdg_mime_media_type_equal (mime_a
, mime_b
);
720 xdg_mime_is_super_type (const char *mime
)
725 length
= strlen (mime
);
726 type
= &(mime
[length
- 2]);
728 if (strcmp (type
, "/*") == 0)
736 _xdg_mime_mime_type_subclass (const char *mime
,
739 const char *umime
, *ubase
;
740 const char **parents
;
742 if (_xdg_mime_caches
)
743 return _xdg_mime_cache_mime_type_subclass (mime
, base
);
745 umime
= _xdg_mime_unalias_mime_type (mime
);
746 ubase
= _xdg_mime_unalias_mime_type (base
);
748 if (strcmp (umime
, ubase
) == 0)
752 /* Handle supertypes */
753 if (xdg_mime_is_super_type (ubase
) &&
754 _xdg_mime_media_type_equal (umime
, ubase
))
758 /* Handle special cases text/plain and application/octet-stream */
759 if (strcmp (ubase
, "text/plain") == 0 &&
760 strncmp (umime
, "text/", 5) == 0)
763 if (strcmp (ubase
, "application/octet-stream") == 0)
766 parents
= _xdg_mime_parent_list_lookup (parent_list
, umime
);
767 for (; parents
&& *parents
; parents
++)
769 if (_xdg_mime_mime_type_subclass (*parents
, ubase
))
777 xdg_mime_mime_type_subclass (const char *mime
,
782 return _xdg_mime_mime_type_subclass (mime
, base
);
786 xdg_mime_list_mime_parents (const char *mime
)
788 const char **parents
;
792 if (_xdg_mime_caches
)
793 return _xdg_mime_cache_list_mime_parents (mime
);
795 parents
= xdg_mime_get_mime_parents (mime
);
800 for (i
= 0; parents
[i
]; i
++) ;
802 n
= (i
+ 1) * sizeof (char *);
803 result
= (char **) malloc (n
);
804 memcpy (result
, parents
, n
);
810 xdg_mime_get_mime_parents (const char *mime
)
816 umime
= _xdg_mime_unalias_mime_type (mime
);
818 return _xdg_mime_parent_list_lookup (parent_list
, umime
);
824 printf ("*** ALIASES ***\n\n");
825 _xdg_mime_alias_list_dump (alias_list
);
826 printf ("\n*** PARENTS ***\n\n");
827 _xdg_mime_parent_list_dump (parent_list
);
828 printf ("\n*** CACHE ***\n\n");
829 _xdg_glob_hash_dump (global_hash
);
833 /* Registers a function to be called every time the mime database reloads its files
836 xdg_mime_register_reload_callback (XdgMimeCallback callback
,
838 XdgMimeDestroy destroy
)
840 XdgCallbackList
*list_el
;
841 static int callback_id
= 1;
843 /* Make a new list element */
844 list_el
= calloc (1, sizeof (XdgCallbackList
));
845 list_el
->callback_id
= callback_id
;
846 list_el
->callback
= callback
;
847 list_el
->data
= data
;
848 list_el
->destroy
= destroy
;
849 list_el
->next
= callback_list
;
851 list_el
->next
->prev
= list_el
;
853 callback_list
= list_el
;
856 return callback_id
- 1;
860 xdg_mime_remove_callback (int callback_id
)
862 XdgCallbackList
*list
;
864 for (list
= callback_list
; list
; list
= list
->next
)
866 if (list
->callback_id
== callback_id
)
869 list
->next
= list
->prev
;
872 list
->prev
->next
= list
->next
;
874 callback_list
= list
->next
;
876 /* invoke the destroy handler */
877 (list
->destroy
) (list
->data
);