xdgmime: Add better detection for text vs. binary and zero-sized files

This detects zero-sized files to return the special-case
"application/x-zerosize" mime-type, as well as trying to differentiate
unknown file types based on their first 128 bytes of data, so that text
editors can automatically handle unknown text files.

Based on:
https://cgit.freedesktop.org/xdg/xdgmime/commit/?id=5181175d5fdaa3832b0fd094cda0120b1fe92af6
https://cgit.freedesktop.org/xdg/xdgmime/commit/?id=9c5802b8da56187c5c6abaf70042d14b12d832a9

https://bugzilla.gnome.org/show_bug.cgi?id=795544
This commit is contained in:
David Faure
2018-05-02 15:46:25 +02:00
committed by Emmanuele Bassi
parent d0a48f26c7
commit 1c177ce0ab
5 changed files with 42 additions and 7 deletions

View File

@@ -760,12 +760,11 @@ cache_get_mime_type_for_data (const void *data,
for (n = 0; n < n_mime_types; n++)
{
if (mime_types[n])
return mime_types[n];
}
return XDG_MIME_TYPE_UNKNOWN;
return NULL;
}
const char *
@@ -812,6 +811,9 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
statbuf = &buf;
}
if (statbuf->st_size == 0)
return XDG_MIME_TYPE_EMPTY;
if (!S_ISREG (statbuf->st_mode))
return XDG_MIME_TYPE_UNKNOWN;
@@ -841,6 +843,9 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
mime_types, n);
if (!mime_type)
mime_type = _xdg_binary_or_text_fallback(data, bytes_read);
free (data);
fclose (file);