File libfm-1.2.3-Fix-SF925-and-SF938.patch of Package libfm-extra

From 019ef925edcd6f0c0cbd4ed41e67b301bc866837 Mon Sep 17 00:00:00 2001
From: Andriy Grytsenko <andrej@rep.kiev.ua>
Date: Sat, 9 May 2015 13:52:34 +0300
Subject: [PATCH] [SF#925,938] Improve file type detection, it fails in some
 cases such as DOCX.

For file types that have multiple entries in MIME database the function
g_content_type_guess() always returns "uncertain true" attribute, even if
both content and file name are passed to it. And if only content was passed
then it always returns "uncertain false" so previous logic fails for such
files.

The commit changes that logic to not rely on uncertain attribute but test
if adding content test changes type returned, and if does then return to
previous test type (content only), but if it doesn't then trust that value
ignoring uncertain attribute.

My testing shows good acceptable results on previously wrong types while
still giving right results on files with incorrect suffixes.
---
 NEWS                    |  2 ++
 src/base/fm-mime-type.c | 16 +++++++++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index 0402db7..9866ee9 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,8 @@
 
 * Corrected missing newline at end of generated shortcut desktop entry.
 
+* Improved file type detection, it failed in some cases such as DOCX.
+
 
 Changes on 1.2.3 since 1.2.2:
 
diff --git a/src/base/fm-mime-type.c b/src/base/fm-mime-type.c
index 1b28e48..f69a15f 100644
--- a/src/base/fm-mime-type.c
+++ b/src/base/fm-mime-type.c
@@ -3,7 +3,7 @@
  *
  *      Copyright 2009 - 2012 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
  *      Copyright 2009 Juergen Hoetzel <juergen@archlinux.org>
- *      Copyright 2012-2014 Andriy Grytsenko (LStranger) <andrej@rep.kiev.ua>
+ *      Copyright 2012-2015 Andriy Grytsenko (LStranger) <andrej@rep.kiev.ua>
  *
  *      This file is a part of the Libfm library.
  *
@@ -191,14 +191,20 @@ FmMimeType* fm_mime_type_from_native_file(const char* file_path,
                 char buf[4096];
                 len = read(fd, buf, MIN(pstat->st_size, 4096));
                 const char *tmp;
-                g_free(type);
+                char *qtype = type; /* questionable type */
                 close(fd);
-                type = g_content_type_guess(NULL, (guchar*)buf, len, &uncertain);
-                if (uncertain)
+                type = g_content_type_guess(base_name, (guchar*)buf, len, &uncertain);
+                /* we need more complicated guessing here: file may have some
+                   wrong suffix or no suffix at all, and g_content_type_guess()
+                   very probably will guess it wrong so let believe it only
+                   if it insists on guessed type after testing its content,
+                   otherwise discard name completely and analyze just content */
+                if (g_strcmp0(qtype, type) != 0)
                 {
                     g_free(type);
-                    type = g_content_type_guess(base_name, (guchar*)buf, len, &uncertain);
+                    type = g_content_type_guess(NULL, (guchar*)buf, len, &uncertain);
                 }
+                g_free(qtype);
                 /* bug: improperly named desktop entries are detected as text/plain */
                 if (uncertain && len > 40 && (tmp = memchr(buf, '[', 40)) != NULL &&
                     strncmp(tmp, "[Desktop Entry]\n", 16) == 0)
-- 
2.1.4

openSUSE Build Service is sponsored by