Commit 7499e33c authored by Matthias Clasen's avatar Matthias Clasen

Merge upstream changes to handle duplicate glob patterns.

parent 5e133537
2005-12-01 Matthias Clasen <mclasen@redhat.com>
* Merge upstream changes to handle duplicate glob
patterns.
2005-11-04 Matthias Clasen <mclasen@redhat.com>
* xdgmime.c (xdg_mime_list_mime_parents): Prevent
......
/*
* 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 "xdgmimeglob.h"
#include <string.h>
......
......@@ -438,7 +438,7 @@ xdg_mime_get_mime_type_for_data (const void *data,
if (_caches)
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)
return mime_type;
......@@ -451,12 +451,17 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf)
{
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;
unsigned char *data;
int max_extent;
int bytes_read;
struct stat buf;
const char *base_name;
int n;
if (file_name == NULL)
return NULL;
......@@ -466,13 +471,13 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
xdg_mime_init ();
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);
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)
return mime_type;
if (n == 1)
return mime_types[0];
if (!statbuf)
{
......@@ -508,7 +513,8 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
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);
fclose (file);
......@@ -529,8 +535,7 @@ xdg_mime_get_mime_type_from_file_name (const char *file_name)
if (_caches)
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 (mime_type)
if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, &mime_type, 1))
return mime_type;
else
return XDG_MIME_TYPE_UNKNOWN;
......@@ -680,9 +685,6 @@ xdg_mime_mime_type_subclass (const char *mime,
if (strcmp (umime, ubase) == 0)
return 1;
/* We really want to handle text/ * in GtkFileFilter, so we just
* turn on the supertype matching
*/
#if 1
/* Handle supertypes */
if (xdg_mime_is_super_type (ubase) &&
......
......@@ -62,7 +62,6 @@ typedef void (*XdgMimeDestroy) (void *user_data);
#define xdg_mime_register_reload_callback XDG_ENTRY(register_reload_callback)
#define xdg_mime_remove_callback XDG_ENTRY(remove_callback)
#define xdg_mime_type_unknown XDG_ENTRY(type_unknown)
#define xdg_mime_dump XDG_ENTRY(dump)
#endif
extern const char *xdg_mime_type_unknown;
......
......@@ -262,13 +262,15 @@ static const char *
cache_magic_lookup_data (XdgMimeCache *cache,
const void *data,
size_t len,
int *prio)
int *prio,
const char *mime_types[],
int n_mime_types)
{
xdg_uint32_t list_offset;
xdg_uint32_t n_entries;
xdg_uint32_t offset;
int j;
int j, n;
*prio = 0;
......@@ -278,10 +280,27 @@ cache_magic_lookup_data (XdgMimeCache *cache,
for (j = 0; j < n_entries; j++)
{
const char *match = cache_magic_compare_to_data (cache, offset + 16 * j,
data, len, prio);
const char *match;
match = cache_magic_compare_to_data (cache, offset + 16 * j,
data, len, prio);
if (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;
......@@ -325,8 +344,10 @@ cache_alias_lookup (const char *alias)
return NULL;
}
static const char *
cache_glob_lookup_literal (const char *file_name)
static int
cache_glob_lookup_literal (const char *file_name,
const char *mime_types[],
int n_mime_types)
{
const char *ptr;
int i, min, max, mid, cmp;
......@@ -355,22 +376,27 @@ cache_glob_lookup_literal (const char *file_name)
else
{
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 *
cache_glob_lookup_fnmatch (const char *file_name)
static int
cache_glob_lookup_fnmatch (const char *file_name,
const char *mime_types[],
int n_mime_types)
{
const char *mime_type;
const char *ptr;
int i, j;
int i, j, n;
n = 0;
for (i = 0; _caches[i]; i++)
{
XdgMimeCache *cache = _caches[i];
......@@ -378,7 +404,7 @@ cache_glob_lookup_fnmatch (const char *file_name)
xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20);
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 mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
......@@ -387,19 +413,24 @@ cache_glob_lookup_fnmatch (const char *file_name)
/* FIXME: Not UTF-8 safe */
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,
xdg_uint32_t n_entries,
xdg_uint32_t offset,
const char *suffix,
int ignore_case)
int ignore_case,
const char *mime_types[],
int n_mime_types)
{
xdg_unichar_t character;
xdg_unichar_t match_char;
......@@ -407,7 +438,7 @@ cache_glob_node_lookup_suffix (XdgMimeCache *cache,
xdg_uint32_t n_children;
xdg_uint32_t child_offset;
int min, max, mid;
int min, max, mid, n, i;
character = _xdg_utf8_to_ucs4 (suffix);
if (ignore_case)
......@@ -431,8 +462,24 @@ cache_glob_node_lookup_suffix (XdgMimeCache *cache,
if (*suffix == '\0')
{
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
{
......@@ -441,21 +488,23 @@ cache_glob_node_lookup_suffix (XdgMimeCache *cache,
return cache_glob_node_lookup_suffix (cache,
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,
int ignore_case)
int ignore_case,
const char *mime_types[],
int n_mime_types)
{
const char *mime_type;
int i;
int i, n;
for (i = 0; _caches[i]; i++)
{
......@@ -465,14 +514,16 @@ cache_glob_lookup_suffix (const char *suffix,
xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
mime_type = cache_glob_node_lookup_suffix (cache,
n_entries, offset,
suffix, ignore_case);
if (mime_type)
return mime_type;
n = cache_glob_node_lookup_suffix (cache,
n_entries, offset,
suffix, ignore_case,
mime_types,
n_mime_types);
if (n > 0)
return n;
}
return NULL;
return 0;
}
static void
......@@ -512,19 +563,21 @@ find_stopchars (char *stopchars)
stopchars[k] = '\0';
}
static const char *
cache_glob_lookup_file_name (const char *file_name)
static int
cache_glob_lookup_file_name (const char *file_name,
const char *mime_types[],
int n_mime_types)
{
const char *mime_type;
const char *ptr;
char stopchars[128];
int n;
assert (file_name != NULL);
/* First, check the literals */
mime_type = cache_glob_lookup_literal (file_name);
if (mime_type)
return mime_type;
n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types);
if (n > 0)
return n;
find_stopchars (stopchars);
......@@ -532,19 +585,19 @@ cache_glob_lookup_file_name (const char *file_name)
ptr = strpbrk (file_name, stopchars);
while (ptr)
{
mime_type = cache_glob_lookup_suffix (ptr, FALSE);
if (mime_type != NULL)
return mime_type;
n = cache_glob_lookup_suffix (ptr, FALSE, mime_types, n_mime_types);
if (n > 0)
return n;
mime_type = cache_glob_lookup_suffix (ptr, TRUE);
if (mime_type != NULL)
return mime_type;
n = cache_glob_lookup_suffix (ptr, TRUE, mime_types, n_mime_types);
if (n > 0)
return n;
ptr = strpbrk (ptr + 1, stopchars);
}
/* Last, try fnmatch */
return cache_glob_lookup_fnmatch (file_name);
return cache_glob_lookup_fnmatch (file_name, mime_types, n_mime_types);
}
int
......@@ -566,12 +619,14 @@ _xdg_mime_cache_get_max_buffer_extents (void)
return max_extent;
}
const char *
_xdg_mime_cache_get_mime_type_for_data (const void *data,
size_t len)
static const char *
cache_get_mime_type_for_data (const void *data,
size_t len,
const char *mime_types[],
int n_mime_types)
{
const char *mime_type;
int i, priority;
int i, n, priority;
priority = 0;
mime_type = NULL;
......@@ -582,7 +637,8 @@ _xdg_mime_cache_get_mime_type_for_data (const void *data,
int prio;
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)
{
priority = prio;
......@@ -593,19 +649,35 @@ _xdg_mime_cache_get_mime_type_for_data (const void *data,
if (priority > 0)
return mime_type;
for (n = 0; n < n_mime_types; n++)
{
if (mime_types[n])
return mime_types[n];
}
return XDG_MIME_TYPE_UNKNOWN;
}
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_types[2];
FILE *file;
unsigned char *data;
int max_extent;
int bytes_read;
struct stat statbuf;
struct stat buf;
const char *base_name;
int n;
if (file_name == NULL)
return NULL;
......@@ -614,15 +686,20 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name)
return NULL;
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)
return mime_type;
if (n == 1)
return mime_types[0];
if (stat (file_name, &statbuf) != 0)
return XDG_MIME_TYPE_UNKNOWN;
if (!statbuf)
{
if (stat (file_name, &buf) != 0)
return XDG_MIME_TYPE_UNKNOWN;
statbuf = &buf;
}
if (!S_ISREG (statbuf.st_mode))
if (!S_ISREG (statbuf->st_mode))
return XDG_MIME_TYPE_UNKNOWN;
/* FIXME: Need to make sure that max_extent isn't totally broken. This could
......@@ -648,7 +725,8 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name)
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);
fclose (file);
......@@ -661,9 +739,7 @@ _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
{
const char *mime_type;
mime_type = cache_glob_lookup_file_name (file_name);
if (mime_type)
if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
return mime_type;
else
return XDG_MIME_TYPE_UNKNOWN;
......
......@@ -46,7 +46,8 @@ void _xdg_mime_cache_unref (XdgMimeCache *cache);
const char *_xdg_mime_cache_get_mime_type_for_data (const void *data,
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);
int _xdg_mime_cache_is_valid_mime_type (const char *mime_type);
int _xdg_mime_cache_mime_type_equal (const char *mime_a,
......
......@@ -241,8 +241,39 @@ _xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
text = _xdg_utf8_next_char (text);
if (*text == '\000')
{
free ((void *) node->mime_type);
node->mime_type = mime_type;
if (node->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
{
......@@ -251,16 +282,19 @@ _xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node,
return glob_hash_node;
}
static const char *
static int
_xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node,
const char *file_name,
int ignore_case)
int ignore_case,
const char *mime_types[],
int n_mime_types)
{
int n;
XdgGlobHashNode *node;
xdg_unichar_t character;
if (glob_hash_node == NULL)
return NULL;
return 0;
character = _xdg_utf8_to_ucs4 (file_name);
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);
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
return _xdg_glob_hash_node_lookup_file_name (node->child,
file_name,
ignore_case);
{
n = _xdg_glob_hash_node_lookup_file_name (node->child,
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,
const char *file_name)
const char *file_name,
const char *mime_types[],
int n_mime_types)
{
XdgGlobList *list;
const char *mime_type;
const char *ptr;
char stopchars[128];
int i;
int i, n;
XdgGlobHashNode *node;
/* 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)
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;
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);
while (ptr)
{
mime_type = (_xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, FALSE));
if (mime_type != NULL)
return mime_type;
n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, FALSE,