Converted README to markdown
[rox-filer.git] / ROX-Filer / src / xdgmime.c
blob04122d957f7b0c7da1d0451e61744dfba1cf5627
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/
5 *
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.
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include "xdgmime.h"
33 #include "xdgmimeint.h"
34 #include "xdgmimeglob.h"
35 #include "xdgmimemagic.h"
36 #include "xdgmimealias.h"
37 #include "xdgmimeparent.h"
38 #include "xdgmimecache.h"
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45 #include <assert.h>
47 typedef struct XdgDirTimeList XdgDirTimeList;
48 typedef struct XdgCallbackList XdgCallbackList;
50 static int need_reread = TRUE;
51 static time_t last_stat_time = 0;
53 static XdgGlobHash *global_hash = NULL;
54 static XdgMimeMagic *global_magic = NULL;
55 static XdgAliasList *alias_list = NULL;
56 static XdgParentList *parent_list = NULL;
57 static XdgDirTimeList *dir_time_list = NULL;
58 static XdgCallbackList *callback_list = NULL;
60 XdgMimeCache **_caches = NULL;
61 static int n_caches = 0;
63 const char xdg_mime_type_unknown[] = "application/octet-stream";
64 const char xdg_mime_type_empty[] = "application/x-zerosize";
65 const char xdg_mime_type_textplain[] = "text/plain";
68 enum
70 XDG_CHECKED_UNCHECKED,
71 XDG_CHECKED_VALID,
72 XDG_CHECKED_INVALID
75 struct XdgDirTimeList
77 time_t mtime;
78 char *directory_name;
79 int checked;
80 XdgDirTimeList *next;
83 struct XdgCallbackList
85 XdgCallbackList *next;
86 XdgCallbackList *prev;
87 int callback_id;
88 XdgMimeCallback callback;
89 void *data;
90 XdgMimeDestroy destroy;
93 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
94 * directories aren't looked at */
95 typedef int (*XdgDirectoryFunc) (const char *directory,
96 void *user_data);
98 static void
99 xdg_dir_time_list_add (char *file_name,
100 time_t mtime)
102 XdgDirTimeList *list;
104 for (list = dir_time_list; list; list = list->next)
106 if (strcmp (list->directory_name, file_name) == 0)
108 free (file_name);
109 return;
113 list = calloc (1, sizeof (XdgDirTimeList));
114 list->checked = XDG_CHECKED_UNCHECKED;
115 list->directory_name = file_name;
116 list->mtime = mtime;
117 list->next = dir_time_list;
118 dir_time_list = list;
121 static void
122 xdg_dir_time_list_free (XdgDirTimeList *list)
124 XdgDirTimeList *next;
126 while (list)
128 next = list->next;
129 free (list->directory_name);
130 free (list);
131 list = next;
135 static int
136 xdg_mime_init_from_directory (const char *directory)
138 char *file_name;
139 struct stat st;
141 assert (directory != NULL);
143 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
144 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
145 if (stat (file_name, &st) == 0)
147 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
149 if (cache != NULL)
151 xdg_dir_time_list_add (file_name, st.st_mtime);
153 _caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
154 _caches[n_caches] = cache;
155 _caches[n_caches + 1] = NULL;
156 n_caches++;
158 return FALSE;
161 free (file_name);
163 file_name = malloc (strlen (directory) + strlen ("/mime/globs2") + 1);
164 strcpy (file_name, directory); strcat (file_name, "/mime/globs2");
165 if (stat (file_name, &st) == 0)
167 _xdg_mime_glob_read_from_file (global_hash, file_name, TRUE);
168 xdg_dir_time_list_add (file_name, st.st_mtime);
170 else
172 free (file_name);
173 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
174 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
175 if (stat (file_name, &st) == 0)
177 _xdg_mime_glob_read_from_file (global_hash, file_name, FALSE);
178 xdg_dir_time_list_add (file_name, st.st_mtime);
180 else
182 free (file_name);
186 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
187 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
188 if (stat (file_name, &st) == 0)
190 _xdg_mime_magic_read_from_file (global_magic, file_name);
191 xdg_dir_time_list_add (file_name, st.st_mtime);
193 else
195 free (file_name);
198 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
199 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
200 _xdg_mime_alias_read_from_file (alias_list, file_name);
201 free (file_name);
203 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
204 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
205 _xdg_mime_parent_read_from_file (parent_list, file_name);
206 free (file_name);
208 return FALSE; /* Keep processing */
211 /* Runs a command on all the directories in the search path */
212 static void
213 xdg_run_command_on_dirs (XdgDirectoryFunc func,
214 void *user_data)
216 const char *xdg_data_home;
217 const char *xdg_data_dirs;
218 const char *ptr;
220 xdg_data_home = getenv ("XDG_DATA_HOME");
221 if (xdg_data_home)
223 if ((func) (xdg_data_home, user_data))
224 return;
226 else
228 const char *home;
230 home = getenv ("HOME");
231 if (home != NULL)
233 char *guessed_xdg_home;
234 int stop_processing;
236 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
237 strcpy (guessed_xdg_home, home);
238 strcat (guessed_xdg_home, "/.local/share/");
239 stop_processing = (func) (guessed_xdg_home, user_data);
240 free (guessed_xdg_home);
242 if (stop_processing)
243 return;
247 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
248 if (xdg_data_dirs == NULL)
249 xdg_data_dirs = "/usr/local/share/:/usr/share/";
251 ptr = xdg_data_dirs;
253 while (*ptr != '\000')
255 const char *end_ptr;
256 char *dir;
257 int len;
258 int stop_processing;
260 end_ptr = ptr;
261 while (*end_ptr != ':' && *end_ptr != '\000')
262 end_ptr ++;
264 if (end_ptr == ptr)
266 ptr++;
267 continue;
270 if (*end_ptr == ':')
271 len = end_ptr - ptr;
272 else
273 len = end_ptr - ptr + 1;
274 dir = malloc (len + 1);
275 strncpy (dir, ptr, len);
276 dir[len] = '\0';
277 stop_processing = (func) (dir, user_data);
278 free (dir);
280 if (stop_processing)
281 return;
283 ptr = end_ptr;
287 /* Checks file_path to make sure it has the same mtime as last time it was
288 * checked. If it has a different mtime, or if the file doesn't exist, it
289 * returns FALSE.
291 * FIXME: This doesn't protect against permission changes.
293 static int
294 xdg_check_file (const char *file_path,
295 int *exists)
297 struct stat st;
299 /* If the file exists */
300 if (stat (file_path, &st) == 0)
302 XdgDirTimeList *list;
304 if (exists)
305 *exists = TRUE;
307 for (list = dir_time_list; list; list = list->next)
309 if (! strcmp (list->directory_name, file_path))
311 if (st.st_mtime == list->mtime)
312 list->checked = XDG_CHECKED_VALID;
313 else
314 list->checked = XDG_CHECKED_INVALID;
316 return (list->checked != XDG_CHECKED_VALID);
319 return TRUE;
322 if (exists)
323 *exists = FALSE;
325 return FALSE;
328 static int
329 xdg_check_dir (const char *directory,
330 int *invalid_dir_list)
332 int invalid, exists;
333 char *file_name;
335 assert (directory != NULL);
337 /* Check the mime.cache file */
338 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
339 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
340 invalid = xdg_check_file (file_name, &exists);
341 free (file_name);
342 if (invalid)
344 *invalid_dir_list = TRUE;
345 return TRUE;
347 else if (exists)
349 return FALSE;
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, NULL);
356 free (file_name);
357 if (invalid)
359 *invalid_dir_list = TRUE;
360 return 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, NULL);
367 free (file_name);
368 if (invalid)
370 *invalid_dir_list = TRUE;
371 return 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. */
379 static int
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,
389 &invalid_dir_list);
391 if (invalid_dir_list)
392 return TRUE;
394 for (list = dir_time_list; list; list = list->next)
396 if (list->checked != XDG_CHECKED_VALID)
397 return TRUE;
400 return FALSE;
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.
407 static int
408 xdg_check_time_and_dirs (void)
410 struct timeval tv;
411 time_t current_time;
412 int retval = FALSE;
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;
423 return retval;
426 /* Called in every public function. It reloads the hash function if need be.
428 static void
429 xdg_mime_init (void)
431 if (xdg_check_time_and_dirs ())
433 xdg_mime_shutdown ();
436 if (need_reread)
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,
444 NULL);
446 need_reread = FALSE;
450 const char *
451 xdg_mime_get_mime_type_for_data (const void *data,
452 size_t len,
453 int *result_prio)
455 const char *mime_type;
457 if (len == 0)
459 *result_prio = 100;
460 return XDG_MIME_TYPE_EMPTY;
463 xdg_mime_init ();
465 if (_caches)
466 mime_type = _xdg_mime_cache_get_mime_type_for_data (data, len, result_prio);
467 else
468 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, result_prio, NULL, 0);
470 if (mime_type)
471 return mime_type;
473 return _xdg_binary_or_text_fallback(data, len);
476 const char *
477 xdg_mime_get_mime_type_for_file (const char *file_name,
478 struct stat *statbuf)
480 const char *mime_type;
481 /* currently, only a few globs occur twice, and none
482 * more often, so 5 seems plenty.
484 const char *mime_types[5];
485 FILE *file;
486 unsigned char *data;
487 int max_extent;
488 int bytes_read;
489 struct stat buf;
490 const char *base_name;
491 int n;
493 if (file_name == NULL)
494 return NULL;
495 if (! _xdg_utf8_validate (file_name))
496 return NULL;
498 xdg_mime_init ();
500 if (_caches)
501 return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
503 base_name = _xdg_get_base_name (file_name);
504 n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
506 if (n == 1)
507 return mime_types[0];
509 if (!statbuf)
511 if (stat (file_name, &buf) != 0)
512 return XDG_MIME_TYPE_UNKNOWN;
514 statbuf = &buf;
517 if (statbuf->st_size == 0)
518 return XDG_MIME_TYPE_EMPTY;
520 if (!S_ISREG (statbuf->st_mode))
521 return XDG_MIME_TYPE_UNKNOWN;
523 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
524 * be large and need getting from a stream instead of just reading it all
525 * in. */
526 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
527 data = malloc (max_extent);
528 if (data == NULL)
529 return XDG_MIME_TYPE_UNKNOWN;
531 file = fopen (file_name, "r");
532 if (file == NULL)
534 free (data);
535 return XDG_MIME_TYPE_UNKNOWN;
538 bytes_read = fread (data, 1, max_extent, file);
539 if (ferror (file))
541 free (data);
542 fclose (file);
543 return XDG_MIME_TYPE_UNKNOWN;
546 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read, NULL,
547 mime_types, n);
549 fclose (file);
551 if (!mime_type)
552 mime_type = _xdg_binary_or_text_fallback(data, bytes_read);
554 free (data);
555 return mime_type;
558 const char *
559 xdg_mime_get_mime_type_from_file_name (const char *file_name)
561 const char *mime_type;
563 xdg_mime_init ();
565 if (_caches)
566 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
568 if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, &mime_type, 1))
569 return mime_type;
570 else
571 return XDG_MIME_TYPE_UNKNOWN;
575 xdg_mime_get_mime_types_from_file_name (const char *file_name,
576 const char *mime_types[],
577 int n_mime_types)
579 xdg_mime_init ();
581 if (_caches)
582 return _xdg_mime_cache_get_mime_types_from_file_name (file_name, mime_types, n_mime_types);
584 return _xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, n_mime_types);
588 xdg_mime_is_valid_mime_type (const char *mime_type)
590 /* FIXME: We should make this a better test
592 return _xdg_utf8_validate (mime_type);
595 void
596 xdg_mime_shutdown (void)
598 XdgCallbackList *list;
600 /* FIXME: Need to make this (and the whole library) thread safe */
601 if (dir_time_list)
603 xdg_dir_time_list_free (dir_time_list);
604 dir_time_list = NULL;
607 if (global_hash)
609 _xdg_glob_hash_free (global_hash);
610 global_hash = NULL;
612 if (global_magic)
614 _xdg_mime_magic_free (global_magic);
615 global_magic = NULL;
618 if (alias_list)
620 _xdg_mime_alias_list_free (alias_list);
621 alias_list = NULL;
624 if (parent_list)
626 _xdg_mime_parent_list_free (parent_list);
627 parent_list = NULL;
630 if (_caches)
632 int i;
634 for (i = 0; i < n_caches; i++)
635 _xdg_mime_cache_unref (_caches[i]);
636 free (_caches);
637 _caches = NULL;
638 n_caches = 0;
641 for (list = callback_list; list; list = list->next)
642 (list->callback) (list->data);
644 need_reread = TRUE;
648 xdg_mime_get_max_buffer_extents (void)
650 xdg_mime_init ();
652 if (_caches)
653 return _xdg_mime_cache_get_max_buffer_extents ();
655 return _xdg_mime_magic_get_buffer_extents (global_magic);
658 const char *
659 _xdg_mime_unalias_mime_type (const char *mime_type)
661 const char *lookup;
663 if (_caches)
664 return _xdg_mime_cache_unalias_mime_type (mime_type);
666 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
667 return lookup;
669 return mime_type;
672 const char *
673 xdg_mime_unalias_mime_type (const char *mime_type)
675 xdg_mime_init ();
677 return _xdg_mime_unalias_mime_type (mime_type);
681 _xdg_mime_mime_type_equal (const char *mime_a,
682 const char *mime_b)
684 const char *unalias_a, *unalias_b;
686 unalias_a = _xdg_mime_unalias_mime_type (mime_a);
687 unalias_b = _xdg_mime_unalias_mime_type (mime_b);
689 if (strcmp (unalias_a, unalias_b) == 0)
690 return 1;
692 return 0;
696 xdg_mime_mime_type_equal (const char *mime_a,
697 const char *mime_b)
699 xdg_mime_init ();
701 return _xdg_mime_mime_type_equal (mime_a, mime_b);
705 xdg_mime_media_type_equal (const char *mime_a,
706 const char *mime_b)
708 char *sep;
710 sep = strchr (mime_a, '/');
712 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
713 return 1;
715 return 0;
718 #if 1
719 static int
720 xdg_mime_is_super_type (const char *mime)
722 int length;
723 const char *type;
725 length = strlen (mime);
726 type = &(mime[length - 2]);
728 if (strcmp (type, "/*") == 0)
729 return 1;
731 return 0;
733 #endif
736 _xdg_mime_mime_type_subclass (const char *mime,
737 const char *base)
739 const char *umime, *ubase;
740 const char **parents;
742 if (_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)
749 return 1;
751 #if 1
752 /* Handle supertypes */
753 if (xdg_mime_is_super_type (ubase) &&
754 xdg_mime_media_type_equal (umime, ubase))
755 return 1;
756 #endif
758 /* Handle special cases text/plain and application/octet-stream */
759 if (strcmp (ubase, "text/plain") == 0 &&
760 strncmp (umime, "text/", 5) == 0)
761 return 1;
763 if (strcmp (ubase, "application/octet-stream") == 0)
764 return 1;
766 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
767 for (; parents && *parents; parents++)
769 if (_xdg_mime_mime_type_subclass (*parents, ubase))
770 return 1;
773 return 0;
777 xdg_mime_mime_type_subclass (const char *mime,
778 const char *base)
780 xdg_mime_init ();
782 return _xdg_mime_mime_type_subclass (mime, base);
785 char **
786 xdg_mime_list_mime_parents (const char *mime)
788 const char **parents;
789 char **result;
790 int i, n;
792 if (_caches)
793 return _xdg_mime_cache_list_mime_parents (mime);
795 parents = xdg_mime_get_mime_parents (mime);
797 if (!parents)
798 return NULL;
800 for (i = 0; parents[i]; i++) ;
802 n = (i + 1) * sizeof (char *);
803 result = (char **) malloc (n);
804 memcpy (result, parents, n);
806 return result;
809 const char **
810 xdg_mime_get_mime_parents (const char *mime)
812 const char *umime;
814 xdg_mime_init ();
816 umime = _xdg_mime_unalias_mime_type (mime);
818 return _xdg_mime_parent_list_lookup (parent_list, umime);
821 void
822 xdg_mime_dump (void)
824 xdg_mime_init();
826 printf ("*** ALIASES ***\n\n");
827 _xdg_mime_alias_list_dump (alias_list);
828 printf ("\n*** PARENTS ***\n\n");
829 _xdg_mime_parent_list_dump (parent_list);
830 printf ("\n*** CACHE ***\n\n");
831 _xdg_glob_hash_dump (global_hash);
832 printf ("\n*** GLOBS ***\n\n");
833 _xdg_glob_hash_dump (global_hash);
834 printf ("\n*** GLOBS REVERSE TREE ***\n\n");
835 _xdg_mime_cache_glob_dump ();
839 /* Registers a function to be called every time the mime database reloads its files
842 xdg_mime_register_reload_callback (XdgMimeCallback callback,
843 void *data,
844 XdgMimeDestroy destroy)
846 XdgCallbackList *list_el;
847 static int callback_id = 1;
849 /* Make a new list element */
850 list_el = calloc (1, sizeof (XdgCallbackList));
851 list_el->callback_id = callback_id;
852 list_el->callback = callback;
853 list_el->data = data;
854 list_el->destroy = destroy;
855 list_el->next = callback_list;
856 if (list_el->next)
857 list_el->next->prev = list_el;
859 callback_list = list_el;
860 callback_id ++;
862 return callback_id - 1;
865 void
866 xdg_mime_remove_callback (int callback_id)
868 XdgCallbackList *list;
870 for (list = callback_list; list; list = list->next)
872 if (list->callback_id == callback_id)
874 if (list->next)
875 list->next = list->prev;
877 if (list->prev)
878 list->prev->next = list->next;
879 else
880 callback_list = list->next;
882 /* invoke the destroy handler */
883 (list->destroy) (list->data);
884 free (list);
885 return;