[file-roller] add support for magic based identification of files



commit 7b23961d200e3545af3815a8eb40d6d31757024f
Author: Saleem Abdulrasool <compnerd compnerd org>
Date:   Sun Jan 2 13:53:39 2011 -0800

    add support for magic based identification of files

 configure.ac     |   32 ++++++++++++++++++++++++++++++++
 src/Makefile.am  |    4 ++++
 src/fr-archive.c |   32 ++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 80da1af..05c9d80 100644
--- a/configure.ac
+++ b/configure.ac
@@ -106,6 +106,38 @@ fi
 
 dnl ******************************
 
+dnl ******************************
+
+AC_ARG_ENABLE([magic],
+              AS_HELP_STRING([--enable-magic], [use libmagic to detect file type]),,
+              [enable_magic=no])
+
+if test x"$enable_magic" = x"yes" ; then
+	save_LIBS="$LIBS"
+	LIBS="$LIBS -lmagic"
+	AC_MSG_CHECKING([whether libmagic works])
+	AC_LINK_IFELSE([AC_LANG_SOURCE([
+#include <magic.h>
+int main () { magic_t m = magic_open(MAGIC_NONE); }
+])],
+		       [AC_MSG_RESULT([yes])],
+		       [AC_MSG_RESULT([no])
+		        AC_MSG_ERROR([libmagic is needed for magic])])
+	LIBS="$save_LIBS"
+
+	MAGIC_CFLAGS=
+	MAGIC_LIBS=-lmagic
+
+	AC_SUBST(MAGIC_CFLAGS)
+	AC_SUBST(MAGIC_LIBS)
+
+	AC_DEFINE(ENABLE_MAGIC, 1, [define to enable magic])
+fi
+
+AM_CONDITIONAL(ENABLE_MAGIC, test x"$enable_magic" != x"no")
+
+dnl ******************************
+
 IT_PROG_INTLTOOL([0.40.0])
 
 GETTEXT_PACKAGE=file-roller
diff --git a/src/Makefile.am b/src/Makefile.am
index fa88925..424d2b7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -187,6 +187,10 @@ file_roller_LDADD =					\
 	$(top_builddir)/copy-n-paste/libeggsmclient.la	\
 	$(FR_LIBS)
 
+if ENABLE_MAGIC
+file_roller_LDADD += $(MAGIC_LIBS)
+endif
+
 file_roller_server_SOURCES =		\
 	$(COMMON_SOURCES)		\
 	server.c
diff --git a/src/fr-archive.c b/src/fr-archive.c
index 01597e4..70a1f93 100644
--- a/src/fr-archive.c
+++ b/src/fr-archive.c
@@ -40,6 +40,10 @@
 #include "fr-process.h"
 #include "fr-init.h"
 
+#if ENABLE_MAGIC
+#include <magic.h>
+#endif
+
 #ifndef NCARGS
 #define NCARGS _POSIX_ARG_MAX
 #endif
@@ -524,6 +528,33 @@ get_mime_type_from_content (GFile *file)
 static const char *
 get_mime_type_from_magic_numbers (GFile *file)
 {
+#if ENABLE_MAGIC
+	static magic_t magic = NULL;
+
+	if (! magic) {
+		magic = magic_open (MAGIC_MIME);
+		if (magic)
+			magic_load (magic, NULL);
+		else
+			g_warning ("unable to open magic database");
+	}
+
+	if (magic) {
+		const char * mime_type = NULL;
+		char * filepath = g_file_get_path (file);
+
+		if (filepath) {
+			mime_type = magic_file (magic, filepath);
+			g_free (filepath);
+		}
+
+		if (mime_type)
+			return mime_type;
+
+		g_warning ("unable to detect filetype from magic: %s",
+			   magic_error (magic));
+	}
+#else
 	static struct {
 		const char *mime_type;
 		const char *first_bytes;
@@ -561,6 +592,7 @@ get_mime_type_from_magic_numbers (GFile *file)
 		{
 			return sniffer_data[i].mime_type;
 		}
+#endif
 
 	return NULL;
 }



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