[file-roller: 55/123] re-added libmagic support



commit b7ebbb131afa0cae359ec6591187a93f4f64c5a6
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sun Jul 22 16:33:00 2012 +0200

    re-added libmagic support

 configure.ac     |    2 +-
 src/fr-archive.c |   33 +++++++++++++++++++++++++++++----
 2 files changed, 30 insertions(+), 5 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index b731a0c..82ed252 100644
--- a/configure.ac
+++ b/configure.ac
@@ -190,7 +190,7 @@ dnl ******************************
 
 AC_ARG_ENABLE([magic],
               AS_HELP_STRING([--enable-magic], [use libmagic to detect file type]),,
-              [enable_magic=no])
+              [enable_magic=yes])
 
 if test x"$enable_magic" = x"yes" ; then
 	save_LIBS="$LIBS"
diff --git a/src/fr-archive.c b/src/fr-archive.c
index 19f24f1..430b15b 100644
--- a/src/fr-archive.c
+++ b/src/fr-archive.c
@@ -28,6 +28,9 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gio/gio.h>
+#if ENABLE_MAGIC
+#  include <magic.h>
+#endif
 #include "glib-utils.h"
 #include "file-utils.h"
 #include "gio-utils.h"
@@ -40,10 +43,6 @@
 #include "fr-process.h"
 #include "fr-init.h"
 
-#if ENABLE_MAGIC
-#include <magic.h>
-#endif
-
 
 #define FILE_ARRAY_INITIAL_SIZE	256
 
@@ -620,6 +619,30 @@ static const char *
 get_mime_type_from_magic_numbers (char  *buffer,
 				  gsize  buffer_size)
 {
+#if ENABLE_MAGIC
+
+	static magic_t magic = NULL;
+
+	if (magic == NULL) {
+		magic = magic_open (MAGIC_MIME_TYPE);
+		if (magic != NULL)
+			magic_load (magic, NULL);
+		else
+			g_warning ("unable to open magic database");
+	}
+
+	if (magic != NULL) {
+		const char * mime_type;
+
+		mime_type = magic_buffer (magic, buffer, buffer_size);
+		if (mime_type)
+			return mime_type;
+
+		g_warning ("unable to detect filetype from magic: %s", magic_error (magic));
+	}
+
+#else
+
 	static const struct magic {
 		const unsigned int off;
 		const unsigned int len;
@@ -655,6 +678,8 @@ get_mime_type_from_magic_numbers (char  *buffer,
 			return magic->mime_type;
 	}
 
+#endif
+
 	return NULL;
 }
 



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