[glib] GLocalFile: return text/plain for empty files



commit 202a9c3497e0c0b5789e533509dd8671617ae20c
Author: Ryan Lortie <desrt desrt ca>
Date:   Tue Sep 29 11:18:54 2015 -0400

    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 b6fc1df022a0326e7c36122b1416061bf796c98f
    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

 gio/glocalfileinfo.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
---
diff --git a/gio/glocalfileinfo.c b/gio/glocalfileinfo.c
index c952d75..75bcef4 100644
--- a/gio/glocalfileinfo.c
+++ b/gio/glocalfileinfo.c
@@ -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;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]