r1213@localhost: muntyan | 2005-12-02 05:42:58 -0600

Synced with upstream
This commit is contained in:
Yevgen Muntyan 2005-12-02 17:51:44 +00:00
parent 8722b7c096
commit 2dabbddae1
10 changed files with 407 additions and 154 deletions

View File

@ -1,3 +1,54 @@
2005-12-01 Christian Neumair <chris@gnome-de.org>
* src/xdgmime.c: (xdg_mime_get_mime_type_from_file_name):
* src/xdgmimecache.c: (_xdg_mime_cache_get_mime_type_from_file_name):
Return XDG_MIME_TYPE_UNKNOWN if multiple MIME types match a simple
pattern.
2005-12-01 Matthias Clasen <mclasen@redhat.com>
* src/xdgmimecache.h:
* src/xdgmimecache.c (_xdg_mime_cache_get_mime_type_for_file):
Allow passing in a struct stat * to avoid re-stat()ing.
Forgotten commit from an earlier entry.
* src/xdgmimecache.c (cache_glob_lookup_literal):
(cache_glob_lookup_fnmatch):
(cache_glob_node_lookup_suffix): Change these functions to
allow returning more than one mime type if identical globs
match.
* src/xdgmimemagic.h:
* src/xdgmimeglob.h:
* src/xdgmimemagic.c (_xdg_mime_magic_lookup_data):
* src/xdgmimeglob.c (_xdg_glob_hash_lookup_file_name):
Change these functions to allow returning more than one
mime type if identical globs match.
* src/xdgmimecache.c (_xdg_mime_cache_get_mime_type_for_file):
* src/xdgmime.c (xdg_mime_get_mime_type_for_file):
If multiple identical globs match, use magic to disambiguate.
2005-11-04 Matthias Clasen <mclasen@redhat.com>
* xdgmime.c (xdg_mime_list_mime_parents): Prevent
a segfault.
2005-10-18 Matthias Clasen <mclasen@redhat.com>
* src/xdgmime.c:
* src/xdgmimecache.h:
* src/xdgmimecache.c: Make the array of caches NULL-terminated
and rename it to _caches. (#4011)
* src/xdgmime.h:
* src/xdgmime.c: Add a struct statbuf * argument to
xdg_mime_get_mime_type_for_file(). (#3529)
* src/test-mime.c: Adjust callers. Add License.
* src/xdgmimecache.c: Make magic comparisons work correctly.
Thu Jun 9 23:55:25 2005 Jonathan Blandford <jrb@redhat.com> Thu Jun 9 23:55:25 2005 Jonathan Blandford <jrb@redhat.com>
* src/xdgmime.c (xdg_mime_init_from_directory): patch from * src/xdgmime.c (xdg_mime_init_from_directory): patch from

View File

@ -1,3 +1,25 @@
/*
* Copyright (C) 2003,2004 Red Hat, Inc.
* Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
*
* Licensed under the Academic Free License version 2.0
* Or under the following terms:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "xdgmime.h" #include "xdgmime.h"
#include "xdgmimeglob.h" #include "xdgmimeglob.h"
#include <string.h> #include <string.h>
@ -108,7 +130,7 @@ main (int argc, char *argv[])
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
{ {
file_name = argv[i]; file_name = argv[i];
result = xdg_mime_get_mime_type_for_file (file_name); result = xdg_mime_get_mime_type_for_file (file_name, NULL);
printf ("File \"%s\" has a mime-type of %s\n", file_name, result); printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
} }

View File

@ -56,8 +56,9 @@ static XdgAliasList *alias_list = NULL;
static XdgParentList *parent_list = NULL; static XdgParentList *parent_list = NULL;
static XdgDirTimeList *dir_time_list = NULL; static XdgDirTimeList *dir_time_list = NULL;
static XdgCallbackList *callback_list = NULL; static XdgCallbackList *callback_list = NULL;
XdgMimeCache **caches = NULL;
int n_caches = 0; XdgMimeCache **_caches = NULL;
static int n_caches = 0;
const char *xdg_mime_type_unknown = "application/octet-stream"; const char *xdg_mime_type_unknown = "application/octet-stream";
@ -140,8 +141,9 @@ xdg_mime_init_from_directory (const char *directory)
list->next = dir_time_list; list->next = dir_time_list;
dir_time_list = list; dir_time_list = list;
caches = realloc (caches, sizeof (XdgMimeCache *) * (n_caches + 1)); _caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
caches[n_caches] = cache; _caches[n_caches] = cache;
_caches[n_caches + 1] = NULL;
n_caches++; n_caches++;
return FALSE; return FALSE;
@ -433,10 +435,10 @@ xdg_mime_get_mime_type_for_data (const void *data,
xdg_mime_init (); xdg_mime_init ();
if (caches) if (_caches)
return _xdg_mime_cache_get_mime_type_for_data (data, len); return _xdg_mime_cache_get_mime_type_for_data (data, len);
mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len); mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, NULL, 0);
if (mime_type) if (mime_type)
return mime_type; return mime_type;
@ -445,15 +447,21 @@ xdg_mime_get_mime_type_for_data (const void *data,
} }
const char * const char *
xdg_mime_get_mime_type_for_file (const char *file_name) xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf)
{ {
const char *mime_type; const char *mime_type;
/* currently, only a few globs occur twice, and none
* more often, so 5 seems plenty.
*/
const char *mime_types[5];
FILE *file; FILE *file;
unsigned char *data; unsigned char *data;
int max_extent; int max_extent;
int bytes_read; int bytes_read;
struct stat statbuf; struct stat buf;
const char *base_name; const char *base_name;
int n;
if (file_name == NULL) if (file_name == NULL)
return NULL; return NULL;
@ -462,19 +470,24 @@ xdg_mime_get_mime_type_for_file (const char *file_name)
xdg_mime_init (); xdg_mime_init ();
if (caches) if (_caches)
return _xdg_mime_cache_get_mime_type_for_file (file_name); return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
base_name = _xdg_get_base_name (file_name); base_name = _xdg_get_base_name (file_name);
mime_type = xdg_mime_get_mime_type_from_file_name (base_name); n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
if (mime_type != XDG_MIME_TYPE_UNKNOWN) if (n == 1)
return mime_type; return mime_types[0];
if (stat (file_name, &statbuf) != 0) if (!statbuf)
return XDG_MIME_TYPE_UNKNOWN; {
if (stat (file_name, &buf) != 0)
return XDG_MIME_TYPE_UNKNOWN;
if (!S_ISREG (statbuf.st_mode)) statbuf = &buf;
}
if (!S_ISREG (statbuf->st_mode))
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
/* FIXME: Need to make sure that max_extent isn't totally broken. This could /* FIXME: Need to make sure that max_extent isn't totally broken. This could
@ -485,7 +498,7 @@ xdg_mime_get_mime_type_for_file (const char *file_name)
if (data == NULL) if (data == NULL)
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
file = fopen (file_name, "r"); file = fopen (file_name, "r");
if (file == NULL) if (file == NULL)
{ {
free (data); free (data);
@ -500,7 +513,8 @@ xdg_mime_get_mime_type_for_file (const char *file_name)
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
} }
mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read); mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read,
mime_types, n);
free (data); free (data);
fclose (file); fclose (file);
@ -514,16 +528,15 @@ xdg_mime_get_mime_type_for_file (const char *file_name)
const char * const char *
xdg_mime_get_mime_type_from_file_name (const char *file_name) xdg_mime_get_mime_type_from_file_name (const char *file_name)
{ {
const char *mime_type; const char *mime_types[2];
xdg_mime_init (); xdg_mime_init ();
if (caches) if (_caches)
return _xdg_mime_cache_get_mime_type_from_file_name (file_name); return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
mime_type = _xdg_glob_hash_lookup_file_name (global_hash, file_name); if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, 2) == 1)
if (mime_type) return mime_types[0];
return mime_type;
else else
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
} }
@ -582,7 +595,7 @@ xdg_mime_get_max_buffer_extents (void)
{ {
xdg_mime_init (); xdg_mime_init ();
if (caches) if (_caches)
return _xdg_mime_cache_get_max_buffer_extents (); return _xdg_mime_cache_get_max_buffer_extents ();
return _xdg_mime_magic_get_buffer_extents (global_magic); return _xdg_mime_magic_get_buffer_extents (global_magic);
@ -595,7 +608,7 @@ xdg_mime_unalias_mime_type (const char *mime_type)
xdg_mime_init (); xdg_mime_init ();
if (caches) if (_caches)
return _xdg_mime_cache_unalias_mime_type (mime_type); return _xdg_mime_cache_unalias_mime_type (mime_type);
if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL) if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
@ -663,7 +676,7 @@ xdg_mime_mime_type_subclass (const char *mime,
xdg_mime_init (); xdg_mime_init ();
if (caches) if (_caches)
return _xdg_mime_cache_mime_type_subclass (mime, base); return _xdg_mime_cache_mime_type_subclass (mime, base);
umime = xdg_mime_unalias_mime_type (mime); umime = xdg_mime_unalias_mime_type (mime);
@ -704,7 +717,7 @@ xdg_mime_list_mime_parents (const char *mime)
char **result; char **result;
int i, n; int i, n;
if (caches) if (_caches)
return _xdg_mime_cache_list_mime_parents (mime); return _xdg_mime_cache_list_mime_parents (mime);
parents = xdg_mime_get_mime_parents (mime); parents = xdg_mime_get_mime_parents (mime);

View File

@ -30,6 +30,7 @@
#define __XDG_MIME_H__ #define __XDG_MIME_H__
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -68,7 +69,8 @@ extern const char *xdg_mime_type_unknown;
const char *xdg_mime_get_mime_type_for_data (const void *data, const char *xdg_mime_get_mime_type_for_data (const void *data,
size_t len); size_t len);
const char *xdg_mime_get_mime_type_for_file (const char *file_name); const char *xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf);
const char *xdg_mime_get_mime_type_from_file_name (const char *file_name); const char *xdg_mime_get_mime_type_from_file_name (const char *file_name);
int xdg_mime_is_valid_mime_type (const char *mime_type); int xdg_mime_is_valid_mime_type (const char *mime_type);
int xdg_mime_mime_type_equal (const char *mime_a, int xdg_mime_mime_type_equal (const char *mime_a,

View File

@ -65,12 +65,13 @@
#define _O_BINARY 0 #define _O_BINARY 0
#endif #endif
#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif
#define MAJOR_VERSION 1 #define MAJOR_VERSION 1
#define MINOR_VERSION 0 #define MINOR_VERSION 0
extern XdgMimeCache **caches;
extern int n_caches;
struct _XdgMimeCache struct _XdgMimeCache
{ {
int ref_count; int ref_count;
@ -79,8 +80,8 @@ struct _XdgMimeCache
char *buffer; char *buffer;
}; };
#define GET_UINT16(cache,offset) (ntohs(*(uint16_t*)((cache) + (offset)))) #define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset))))
#define GET_UINT32(cache,offset) (ntohl(*(uint32_t*)((cache) + (offset)))) #define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset))))
XdgMimeCache * XdgMimeCache *
_xdg_mime_cache_ref (XdgMimeCache *cache) _xdg_mime_cache_ref (XdgMimeCache *cache)
@ -175,8 +176,8 @@ cache_magic_matchlet_compare_to_data (XdgMimeCache *cache,
{ {
for (j = 0; j < data_length; j++) for (j = 0; j < data_length; j++)
{ {
if ((cache->buffer[data_offset + j] & cache->buffer[mask_offset + j]) != if ((((unsigned char *)cache->buffer)[data_offset + j] & ((unsigned char *)cache->buffer)[mask_offset + j]) !=
((((unsigned char *) data)[j + i]) & cache->buffer[mask_offset + j])) ((((unsigned char *) data)[j + i]) & ((unsigned char *)cache->buffer)[mask_offset + j]))
{ {
valid_matchlet = FALSE; valid_matchlet = FALSE;
break; break;
@ -187,7 +188,7 @@ cache_magic_matchlet_compare_to_data (XdgMimeCache *cache,
{ {
for (j = 0; j < data_length; j++) for (j = 0; j < data_length; j++)
{ {
if (cache->buffer[data_offset + j] != ((unsigned char *) data)[j + i]) if (((unsigned char *)cache->buffer)[data_offset + j] != ((unsigned char *) data)[j + i])
{ {
valid_matchlet = FALSE; valid_matchlet = FALSE;
break; break;
@ -261,13 +262,15 @@ static const char *
cache_magic_lookup_data (XdgMimeCache *cache, cache_magic_lookup_data (XdgMimeCache *cache,
const void *data, const void *data,
size_t len, size_t len,
int *prio) int *prio,
const char *mime_types[],
int n_mime_types)
{ {
xdg_uint32_t list_offset; xdg_uint32_t list_offset;
xdg_uint32_t n_entries; xdg_uint32_t n_entries;
xdg_uint32_t offset; xdg_uint32_t offset;
int j; int j, n;
*prio = 0; *prio = 0;
@ -277,10 +280,27 @@ cache_magic_lookup_data (XdgMimeCache *cache,
for (j = 0; j < n_entries; j++) for (j = 0; j < n_entries; j++)
{ {
const char *match = cache_magic_compare_to_data (cache, offset + 16 * j, const char *match;
data, len, prio);
match = cache_magic_compare_to_data (cache, offset + 16 * j,
data, len, prio);
if (match) if (match)
return match; return match;
else
{
xdg_uint32_t mimetype_offset;
const char *non_match;
mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * j + 4);
non_match = cache->buffer + mimetype_offset;
for (n = 0; n < n_mime_types; n++)
{
if (mime_types[n] &&
xdg_mime_mime_type_equal (mime_types[n], non_match))
mime_types[n] = NULL;
}
}
} }
return NULL; return NULL;
@ -292,10 +312,10 @@ cache_alias_lookup (const char *alias)
const char *ptr; const char *ptr;
int i, min, max, mid, cmp; int i, min, max, mid, cmp;
for (i = 0; i < n_caches; i++) for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4 ); xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4);
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
xdg_uint32_t offset; xdg_uint32_t offset;
@ -324,15 +344,17 @@ cache_alias_lookup (const char *alias)
return NULL; return NULL;
} }
static const char * static int
cache_glob_lookup_literal (const char *file_name) cache_glob_lookup_literal (const char *file_name,
const char *mime_types[],
int n_mime_types)
{ {
const char *ptr; const char *ptr;
int i, min, max, mid, cmp; int i, min, max, mid, cmp;
for (i = 0; i < n_caches; i++) for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12); xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12);
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
xdg_uint32_t offset; xdg_uint32_t offset;
@ -354,30 +376,35 @@ cache_glob_lookup_literal (const char *file_name)
else else
{ {
offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4); offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
return cache->buffer + offset; mime_types[0] = (const char *)(cache->buffer + offset);
return 1;
} }
} }
} }
return NULL; return 0;
} }
static const char * static int
cache_glob_lookup_fnmatch (const char *file_name) cache_glob_lookup_fnmatch (const char *file_name,
const char *mime_types[],
int n_mime_types)
{ {
const char *mime_type; const char *mime_type;
const char *ptr; const char *ptr;
int i, j; int i, j, n;
for (i = 0; i < n_caches; i++) n = 0;
for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20); xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20);
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
for (j = 0; j < n_entries; j++) for (j = 0; j < n_entries && n < n_mime_types; j++)
{ {
xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j); xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j);
xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4); xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
@ -386,19 +413,24 @@ cache_glob_lookup_fnmatch (const char *file_name)
/* FIXME: Not UTF-8 safe */ /* FIXME: Not UTF-8 safe */
if (fnmatch (ptr, file_name, 0) == 0) if (fnmatch (ptr, file_name, 0) == 0)
return mime_type; mime_types[n++] = mime_type;
} }
}
return NULL; if (n > 0)
return n;
}
return 0;
} }
static const char * static int
cache_glob_node_lookup_suffix (XdgMimeCache *cache, cache_glob_node_lookup_suffix (XdgMimeCache *cache,
xdg_uint32_t n_entries, xdg_uint32_t n_entries,
xdg_uint32_t offset, xdg_uint32_t offset,
const char *suffix, const char *suffix,
int ignore_case) int ignore_case,
const char *mime_types[],
int n_mime_types)
{ {
xdg_unichar_t character; xdg_unichar_t character;
xdg_unichar_t match_char; xdg_unichar_t match_char;
@ -406,7 +438,7 @@ cache_glob_node_lookup_suffix (XdgMimeCache *cache,
xdg_uint32_t n_children; xdg_uint32_t n_children;
xdg_uint32_t child_offset; xdg_uint32_t child_offset;
int min, max, mid; int min, max, mid, n, i;
character = _xdg_utf8_to_ucs4 (suffix); character = _xdg_utf8_to_ucs4 (suffix);
if (ignore_case) if (ignore_case)
@ -430,8 +462,24 @@ cache_glob_node_lookup_suffix (XdgMimeCache *cache,
if (*suffix == '\0') if (*suffix == '\0')
{ {
mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * mid + 4); mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * mid + 4);
n = 0;
mime_types[n++] = cache->buffer + mimetype_offset;
n_children = GET_UINT32 (cache->buffer, offset + 16 * mid + 8);
child_offset = GET_UINT32 (cache->buffer, offset + 16 * mid + 12);
i = 0;
while (n < n_mime_types && i < n_children)
{
match_char = GET_UINT32 (cache->buffer, child_offset + 16 * i);
mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * i + 4);
if (match_char != 0)
break;
return cache->buffer + mimetype_offset; mime_types[n++] = cache->buffer + mimetype_offset;
i++;
}
return n;
} }
else else
{ {
@ -440,38 +488,42 @@ cache_glob_node_lookup_suffix (XdgMimeCache *cache,
return cache_glob_node_lookup_suffix (cache, return cache_glob_node_lookup_suffix (cache,
n_children, child_offset, n_children, child_offset,
suffix, ignore_case); suffix, ignore_case,
mime_types,
n_mime_types);
} }
} }
} }
return NULL; return 0;
} }
static const char * static int
cache_glob_lookup_suffix (const char *suffix, cache_glob_lookup_suffix (const char *suffix,
int ignore_case) int ignore_case,
const char *mime_types[],
int n_mime_types)
{ {
const char *mime_type; int i, n;
int i; for (i = 0; _caches[i]; i++)
for (i = 0; i < n_caches; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16); xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4); xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
mime_type = cache_glob_node_lookup_suffix (cache, n = cache_glob_node_lookup_suffix (cache,
n_entries, offset, n_entries, offset,
suffix, ignore_case); suffix, ignore_case,
if (mime_type) mime_types,
return mime_type; n_mime_types);
if (n > 0)
return n;
} }
return NULL; return 0;
} }
static void static void
@ -480,9 +532,9 @@ find_stopchars (char *stopchars)
int i, j, k, l; int i, j, k, l;
k = 0; k = 0;
for (i = 0; i < n_caches; i++) for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16); xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
@ -511,19 +563,21 @@ find_stopchars (char *stopchars)
stopchars[k] = '\0'; stopchars[k] = '\0';
} }
static const char * static int
cache_glob_lookup_file_name (const char *file_name) cache_glob_lookup_file_name (const char *file_name,
const char *mime_types[],
int n_mime_types)
{ {
const char *mime_type;
const char *ptr; const char *ptr;
char stopchars[128]; char stopchars[128];
int n;
assert (file_name != NULL); assert (file_name != NULL);
/* First, check the literals */ /* First, check the literals */
mime_type = cache_glob_lookup_literal (file_name); n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types);
if (mime_type) if (n > 0)
return mime_type; return n;
find_stopchars (stopchars); find_stopchars (stopchars);
@ -531,19 +585,19 @@ cache_glob_lookup_file_name (const char *file_name)
ptr = strpbrk (file_name, stopchars); ptr = strpbrk (file_name, stopchars);
while (ptr) while (ptr)
{ {
mime_type = cache_glob_lookup_suffix (ptr, FALSE); n = cache_glob_lookup_suffix (ptr, FALSE, mime_types, n_mime_types);
if (mime_type != NULL) if (n > 0)
return mime_type; return n;
mime_type = cache_glob_lookup_suffix (ptr, TRUE); n = cache_glob_lookup_suffix (ptr, TRUE, mime_types, n_mime_types);
if (mime_type != NULL) if (n > 0)
return mime_type; return n;
ptr = strpbrk (ptr + 1, stopchars); ptr = strpbrk (ptr + 1, stopchars);
} }
/* Last, try fnmatch */ /* Last, try fnmatch */
return cache_glob_lookup_fnmatch (file_name); return cache_glob_lookup_fnmatch (file_name, mime_types, n_mime_types);
} }
int int
@ -554,9 +608,9 @@ _xdg_mime_cache_get_max_buffer_extents (void)
int i; int i;
max_extent = 0; max_extent = 0;
for (i = 0; i < n_caches; i++) for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
offset = GET_UINT32 (cache->buffer, 24); offset = GET_UINT32 (cache->buffer, 24);
max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4)); max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4));
@ -565,23 +619,26 @@ _xdg_mime_cache_get_max_buffer_extents (void)
return max_extent; return max_extent;
} }
const char * static const char *
_xdg_mime_cache_get_mime_type_for_data (const void *data, cache_get_mime_type_for_data (const void *data,
size_t len) size_t len,
const char *mime_types[],
int n_mime_types)
{ {
const char *mime_type; const char *mime_type;
int i, priority; int i, n, priority;
priority = 0; priority = 0;
mime_type = NULL; mime_type = NULL;
for (i = 0; i < n_caches; i++) for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
int prio; int prio;
const char *match; const char *match;
match = cache_magic_lookup_data (cache, data, len, &prio); match = cache_magic_lookup_data (cache, data, len, &prio,
mime_types, n_mime_types);
if (prio > priority) if (prio > priority)
{ {
priority = prio; priority = prio;
@ -592,19 +649,35 @@ _xdg_mime_cache_get_mime_type_for_data (const void *data,
if (priority > 0) if (priority > 0)
return mime_type; return mime_type;
for (n = 0; n < n_mime_types; n++)
{
if (mime_types[n])
return mime_types[n];
}
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
} }
const char * const char *
_xdg_mime_cache_get_mime_type_for_file (const char *file_name) _xdg_mime_cache_get_mime_type_for_data (const void *data,
size_t len)
{
return cache_get_mime_type_for_data (data, len, NULL, 0);
}
const char *
_xdg_mime_cache_get_mime_type_for_file (const char *file_name,
struct stat *statbuf)
{ {
const char *mime_type; const char *mime_type;
const char *mime_types[2];
FILE *file; FILE *file;
unsigned char *data; unsigned char *data;
int max_extent; int max_extent;
int bytes_read; int bytes_read;
struct stat statbuf; struct stat buf;
const char *base_name; const char *base_name;
int n;
if (file_name == NULL) if (file_name == NULL)
return NULL; return NULL;
@ -613,15 +686,20 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name)
return NULL; return NULL;
base_name = _xdg_get_base_name (file_name); base_name = _xdg_get_base_name (file_name);
mime_type = _xdg_mime_cache_get_mime_type_from_file_name (base_name); n = cache_glob_lookup_file_name (base_name, mime_types, 2);
if (mime_type != XDG_MIME_TYPE_UNKNOWN) if (n == 1)
return mime_type; return mime_types[0];
if (stat (file_name, &statbuf) != 0) if (!statbuf)
return XDG_MIME_TYPE_UNKNOWN; {
if (stat (file_name, &buf) != 0)
return XDG_MIME_TYPE_UNKNOWN;
if (!S_ISREG (statbuf.st_mode)) statbuf = &buf;
}
if (!S_ISREG (statbuf->st_mode))
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
/* FIXME: Need to make sure that max_extent isn't totally broken. This could /* FIXME: Need to make sure that max_extent isn't totally broken. This could
@ -647,7 +725,8 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name)
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
} }
mime_type = _xdg_mime_cache_get_mime_type_for_data (data, bytes_read); mime_type = cache_get_mime_type_for_data (data, bytes_read,
mime_types, n);
free (data); free (data);
fclose (file); fclose (file);
@ -658,12 +737,10 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name)
const char * const char *
_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name) _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
{ {
const char *mime_type; const char *mime_types[2];
mime_type = cache_glob_lookup_file_name (file_name); if (cache_glob_lookup_file_name (file_name, mime_types, 2) == 1)
return mime_types[0];
if (mime_type)
return mime_type;
else else
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
} }
@ -716,10 +793,10 @@ _xdg_mime_cache_mime_type_subclass (const char *mime,
if (strcmp (ubase, "application/octet-stream") == 0) if (strcmp (ubase, "application/octet-stream") == 0)
return 1; return 1;
for (i = 0; i < n_caches; i++) for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8); xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
@ -778,9 +855,9 @@ _xdg_mime_cache_list_mime_parents (const char *mime)
char **result; char **result;
p = 0; p = 0;
for (i = 0; i < n_caches; i++) for (i = 0; _caches[i]; i++)
{ {
XdgMimeCache *cache = caches[i]; XdgMimeCache *cache = _caches[i];
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8); xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);

View File

@ -37,6 +37,8 @@ typedef struct _XdgMimeCache XdgMimeCache;
#define _xdg_mime_cache_unref XDG_ENTRY(cache_unref) #define _xdg_mime_cache_unref XDG_ENTRY(cache_unref)
#endif #endif
extern XdgMimeCache **_caches;
XdgMimeCache *_xdg_mime_cache_new_from_file (const char *file_name); XdgMimeCache *_xdg_mime_cache_new_from_file (const char *file_name);
XdgMimeCache *_xdg_mime_cache_ref (XdgMimeCache *cache); XdgMimeCache *_xdg_mime_cache_ref (XdgMimeCache *cache);
void _xdg_mime_cache_unref (XdgMimeCache *cache); void _xdg_mime_cache_unref (XdgMimeCache *cache);
@ -44,7 +46,8 @@ void _xdg_mime_cache_unref (XdgMimeCache *cache);
const char *_xdg_mime_cache_get_mime_type_for_data (const void *data, const char *_xdg_mime_cache_get_mime_type_for_data (const void *data,
size_t len); size_t len);
const char *_xdg_mime_cache_get_mime_type_for_file (const char *file_name); const char *_xdg_mime_cache_get_mime_type_for_file (const char *file_name,
struct stat *statbuf);
const char *_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name); const char *_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name);
int _xdg_mime_cache_is_valid_mime_type (const char *mime_type); int _xdg_mime_cache_is_valid_mime_type (const char *mime_type);
int _xdg_mime_cache_mime_type_equal (const char *mime_a, int _xdg_mime_cache_mime_type_equal (const char *mime_a,

View File

@ -241,8 +241,39 @@ _xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
text = _xdg_utf8_next_char (text); text = _xdg_utf8_next_char (text);
if (*text == '\000') if (*text == '\000')
{ {
free ((void *) node->mime_type); if (node->mime_type)
node->mime_type = mime_type; {
if (strcmp (node->mime_type, mime_type))
{
XdgGlobHashNode *child;
int found_node = FALSE;
child = node->child;
while (child && child->character == '\0')
{
if (strcmp (child->mime_type, mime_type) == 0)
{
found_node = TRUE;
break;
}
child = child->next;
}
if (!found_node)
{
child = _xdg_glob_hash_node_new ();
child->character = '\000';
child->mime_type = mime_type;
child->child = NULL;
child->next = node->child;
node->child = child;
}
}
}
else
{
node->mime_type = mime_type;
}
} }
else else
{ {
@ -251,16 +282,19 @@ _xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
return glob_hash_node; return glob_hash_node;
} }
static const char * static int
_xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node, _xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node,
const char *file_name, const char *file_name,
int ignore_case) int ignore_case,
const char *mime_types[],
int n_mime_types)
{ {
int n;
XdgGlobHashNode *node; XdgGlobHashNode *node;
xdg_unichar_t character; xdg_unichar_t character;
if (glob_hash_node == NULL) if (glob_hash_node == NULL)
return NULL; return 0;
character = _xdg_utf8_to_ucs4 (file_name); character = _xdg_utf8_to_ucs4 (file_name);
if (ignore_case) if (ignore_case)
@ -272,34 +306,55 @@ _xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node,
{ {
file_name = _xdg_utf8_next_char (file_name); file_name = _xdg_utf8_next_char (file_name);
if (*file_name == '\000') if (*file_name == '\000')
return node->mime_type; {
n = 0;
mime_types[n++] = node->mime_type;
node = node->child;
while (n < n_mime_types && node && node->character == 0)
{
mime_types[n++] = node->mime_type;
node = node->next;
}
}
else else
return _xdg_glob_hash_node_lookup_file_name (node->child, {
file_name, n = _xdg_glob_hash_node_lookup_file_name (node->child,
ignore_case); file_name,
ignore_case,
mime_types,
n_mime_types);
}
return n;
} }
} }
return NULL;
return 0;
} }
const char * int
_xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash, _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
const char *file_name) const char *file_name,
const char *mime_types[],
int n_mime_types)
{ {
XdgGlobList *list; XdgGlobList *list;
const char *mime_type;
const char *ptr; const char *ptr;
char stopchars[128]; char stopchars[128];
int i; int i, n;
XdgGlobHashNode *node; XdgGlobHashNode *node;
/* First, check the literals */ /* First, check the literals */
assert (file_name != NULL); assert (file_name != NULL && n_mime_types > 0);
for (list = glob_hash->literal_list; list; list = list->next) for (list = glob_hash->literal_list; list; list = list->next)
if (strcmp ((const char *)list->data, file_name) == 0) {
return list->mime_type; if (strcmp ((const char *)list->data, file_name) == 0)
{
mime_types[0] = list->mime_type;
return 1;
}
}
i = 0; i = 0;
for (node = glob_hash->simple_node; node; node = node->next) for (node = glob_hash->simple_node; node; node = node->next)
@ -312,23 +367,28 @@ _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
ptr = strpbrk (file_name, stopchars); ptr = strpbrk (file_name, stopchars);
while (ptr) while (ptr)
{ {
mime_type = (_xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, FALSE)); n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, FALSE,
if (mime_type != NULL) mime_types, n_mime_types);
return mime_type; if (n > 0)
return n;
mime_type = (_xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, TRUE)); n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, TRUE,
if (mime_type != NULL) mime_types, n_mime_types);
return mime_type; if (n > 0)
return n;
ptr = strpbrk (ptr + 1, stopchars); ptr = strpbrk (ptr + 1, stopchars);
} }
/* FIXME: Not UTF-8 safe */ /* FIXME: Not UTF-8 safe */
for (list = glob_hash->full_list; list; list = list->next) n = 0;
if (fnmatch ((const char *)list->data, file_name, 0) == 0) for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next)
return list->mime_type; {
if (fnmatch ((const char *)list->data, file_name, 0) == 0)
mime_types[n++] = list->mime_type;
}
return NULL; return n;
} }

View File

@ -54,8 +54,10 @@ void _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,
const char *file_name); const char *file_name);
XdgGlobHash *_xdg_glob_hash_new (void); XdgGlobHash *_xdg_glob_hash_new (void);
void _xdg_glob_hash_free (XdgGlobHash *glob_hash); void _xdg_glob_hash_free (XdgGlobHash *glob_hash);
const char *_xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash, int _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
const char *text); const char *text,
const char *mime_types[],
int n_mime_types);
void _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash, void _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
const char *glob, const char *glob,
const char *mime_type); const char *mime_type);

View File

@ -653,10 +653,13 @@ _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
const char * const char *
_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic, _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
const void *data, const void *data,
size_t len) size_t len,
const char *mime_types[],
int n_mime_types)
{ {
XdgMimeMagicMatch *match; XdgMimeMagicMatch *match;
const char *mime_type; const char *mime_type;
int n;
mime_type = NULL; mime_type = NULL;
for (match = mime_magic->match_list; match; match = match->next) for (match = mime_magic->match_list; match; match = match->next)
@ -667,6 +670,24 @@ _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
mime_type = match->mime_type; mime_type = match->mime_type;
} }
} }
else
{
for (n = 0; n < n_mime_types; n++)
{
if (mime_types[n] &&
xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
mime_types[n] = NULL;
}
}
}
if (mime_type == NULL)
{
for (n = 0; n < n_mime_types; n++)
{
if (mime_types[n])
mime_type = mime_types[n];
}
} }
return mime_type; return mime_type;

View File

@ -49,6 +49,8 @@ void _xdg_mime_magic_free (XdgMimeMagic *mime_magic);
int _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic); int _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic);
const char *_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic, const char *_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
const void *data, const void *data,
size_t len); size_t len,
const char *mime_types[],
int n_mime_types);
#endif /* __XDG_MIME_MAGIC_H__ */ #endif /* __XDG_MIME_MAGIC_H__ */