From 42c9e8218bd89ecbfd81f1f1e1a06ffebef16f1d Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 3 Nov 2022 09:39:53 +0000 Subject: [PATCH] gio: Change content type of zero-sized files to application/x-zerosize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Fixes: #2777 --- gio/glocalfileinfo.c | 6 +----- gio/gosxcontenttype.m | 6 ++++++ gio/tests/file.c | 12 ++++++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/gio/glocalfileinfo.c b/gio/glocalfileinfo.c index b63f7826c..4fb14e51b 100644 --- a/gio/glocalfileinfo.c +++ b/gio/glocalfileinfo.c @@ -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 diff --git a/gio/gosxcontenttype.m b/gio/gosxcontenttype.m index fad028038..4d7a650c7 100644 --- a/gio/gosxcontenttype.m +++ b/gio/gosxcontenttype.m @@ -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 don’t know of an appropriate equivalent for application/x-zerosize, but + * historically GLib has returned public.text for zero-sized files, so let’s + * 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"); diff --git a/gio/tests/file.c b/gio/tests/file.c index 4fec7aa09..19f5842b5 100644 --- a/gio/tests/file.c +++ b/gio/tests/file.c @@ -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 that’s 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