GLocalFile: return text/plain for empty files

Previously, GLib returned text/plain for empty files.

This is important because people may want to open empty (eg:
just-created) text files with the text editor.

An unintended side-effect of b6fc1df022
caused GLib to start returning application/octet-stream instead of
text/plain for these files.

This commit is essentially a revert of that commit, with a different
solution: we move the special-case up a bit in the function and
hard-code it to text/plain.

This change does not exactly maintain the old behaviour: previously, a
"fast" lookup would have returned application/octet-stream on an empty
file and now it will return text/plain.  I consider this to be an
improvement (since we're returning better data) and don't expect it to
cause problems.

https://bugzilla.gnome.org/show_bug.cgi?id=755795
This commit is contained in:
Ryan Lortie 2015-09-29 11:18:54 -04:00
parent 56b164a195
commit 202a9c3497

View File

@ -1240,6 +1240,17 @@ get_content_type (const char *basename,
return g_content_type_from_mime_type ("inode/blockdevice");
else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
return g_content_type_from_mime_type ("inode/fifo");
else if (statbuf != NULL && S_ISREG(statbuf->st_mode) && statbuf->st_size == 0)
{
/* Don't sniff zero-length files in order to avoid reading files
* that appear normal but are not (eg: files in /proc and /sys)
*
* Note that we need to return text/plain here so that
* newly-created text files are opened by the text editor.
* See https://bugzilla.gnome.org/show_bug.cgi?id=755795
*/
return g_content_type_from_mime_type ("text/plain");
}
#endif
#ifdef S_ISSOCK
else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
@ -1249,14 +1260,11 @@ get_content_type (const char *basename,
{
char *content_type;
gboolean result_uncertain;
content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
#ifndef G_OS_WIN32
/* Don't sniff zero-length files in order to avoid reading files
* that appear normal but are not (eg: files in /proc and /sys)
*/
if (!fast && result_uncertain && path != NULL && statbuf && statbuf->st_size != 0)
if (!fast && result_uncertain && path != NULL)
{
guchar sniff_buffer[4096];
gsize sniff_length;