Merge branch 'wip/pwithnall/2777-zerosize' into 'main'

gio: Change content type of zero-sized files to application/x-zerosize

Closes #2777

See merge request GNOME/glib!3053
This commit is contained in:
Marco Trevisan 2022-11-22 17:52:47 +00:00
commit 8fba287c81
3 changed files with 15 additions and 9 deletions

View File

@ -1361,12 +1361,8 @@ get_content_type (const char *basename,
{
/* 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");
return g_content_type_from_mime_type ("application/x-zerosize");
}
#endif
#ifdef S_ISSOCK

View File

@ -419,6 +419,12 @@ g_content_type_from_mime_type (const gchar *mime_type)
if (strcmp (mime_type, "text/plain") == 0)
return g_strdup ("public.text");
/* I dont know of an appropriate equivalent for application/x-zerosize, but
* historically GLib has returned public.text for zero-sized files, so lets
* continue doing that. */
if (strcmp (mime_type, "application/x-zerosize") == 0)
return g_strdup ("public.text");
/* Non standard type */
if (strcmp (mime_type, "application/x-executable") == 0)
return g_strdup ("public.executable");

View File

@ -3537,10 +3537,14 @@ test_query_zero_length_content_type (void)
GFileIOStream *iostream;
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=755795");
/* This is historic behaviour. See:
/* Historically, GLib used to explicitly consider zero-size files as text/plain,
* so they opened in a text editor. In 2.76, we changed that to application/x-zerosize,
* because thats what xdgmime uses:
* - https://gitlab.gnome.org/GNOME/glib/-/blob/2.74.0/gio/glocalfileinfo.c#L1360-1369
* - https://bugzilla.gnome.org/show_bug.cgi?id=755795 */
g_test_summary ("empty files should always be considered text/plain");
* - https://bugzilla.gnome.org/show_bug.cgi?id=755795
* - https://gitlab.gnome.org/GNOME/glib/-/issues/2777
*/
g_test_summary ("empty files should always be considered application/x-zerosize");
empty_file = g_file_new_tmp ("empty-file-XXXXXX", &iostream, &error);
g_assert_no_error (error);
@ -3557,7 +3561,7 @@ test_query_zero_length_content_type (void)
g_assert_no_error (error);
#ifndef __APPLE__
g_assert_cmpstr (g_file_info_get_content_type (file_info), ==, "text/plain");
g_assert_cmpstr (g_file_info_get_content_type (file_info), ==, "application/x-zerosize");
#else
g_assert_cmpstr (g_file_info_get_content_type (file_info), ==, "public.text");
#endif