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

That’s what xdgmime uses for zero-sized files (see `XDG_MIME_TYPE_EMPTY`).

Historically, GLib explicitly used `text/plain` for empty files, to
ensure they would open in a text editor. But `text/plain` is not really
correct for an empty file: the content isn’t text because there is no
content. The file could eventually become something else when written
to.

Text editors which want to be opened for new, empty files should add
`application/x-zerosize` to their list of supported content types.

Users who want to set a handler for `application/x-zerosize` on their
desktop should use
```sh
gio mime application/x-zerosize  # to see the current handler
gio mime application/x-zerosize org.gnome.gedit.desktop  # to set it
```

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2777
This commit is contained in:
Philip Withnall 2022-11-03 09:39:53 +00:00
parent 4b561a908f
commit 42c9e8218b
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