From 1d23f466a28be3ffc998fc3c1742df4119461f7f Mon Sep 17 00:00:00 2001 From: Diego Joss Date: Mon, 11 Feb 2013 14:11:15 +0100 Subject: [PATCH] replace g_content_type_guess with libmagic Signed-off-by: Sebastian Ramacher --- Makefile | 6 ++++++ config.mk | 8 ++++++++ document.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/Makefile b/Makefile index 99628a7..a72be8d 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,12 @@ else SOURCE = $(filter-out database-sqlite.c,$(OSOURCE)) endif +ifneq (${WITH_MAGIC},) +INCS += $(MAGIC_INC) +LIBS += $(MAGIC_LIB) +CPPFLAGS += -DWITH_MAGIC +endif + ifneq ($(wildcard ${VALGRIND_SUPPRESSION_FILE}),) VALGRIND_ARGUMENTS += --suppressions=${VALGRIND_SUPPRESSION_FILE} endif diff --git a/config.mk b/config.mk index bd20747..e1b13a9 100644 --- a/config.mk +++ b/config.mk @@ -23,6 +23,9 @@ GIRARA_VERSION_CHECK ?= $(shell pkg-config --atleast-version=$(GIRARA_MIN_VERSIO # To disable support for the sqlite backend set WITH_SQLITE to 0. WITH_SQLITE ?= $(shell (pkg-config --atleast-version=3.5.9 sqlite3 && echo 1) || echo 0) +# to disable use of libmagic for filetype detection comment the following line +WITH_MAGIC ?= 1 + # paths PREFIX ?= /usr MANPREFIX ?= ${PREFIX}/share/man @@ -56,6 +59,11 @@ SQLITE_INC ?= $(shell pkg-config --cflags sqlite3) SQLITE_LIB ?= $(shell pkg-config --libs sqlite3) endif +ifneq (${WITH_MAGIC},) +MAGIC_INC ?= +MAGIC_LIB ?= -lmagic +endif + INCS = ${GIRARA_INC} ${GTK_INC} ${GTHREAD_INC} ${GMODULE_INC} LIBS = ${GIRARA_LIB} ${GTK_LIB} ${GTHREAD_LIB} ${GMODULE_LIB} -lpthread -lm diff --git a/document.c b/document.c index ae85aa9..a8699e4 100644 --- a/document.c +++ b/document.c @@ -12,6 +12,9 @@ #include #include #include +#ifdef WITH_MAGIC +# include +#endif /*WITH_MAGIC*/ #include #include @@ -518,6 +521,40 @@ zathura_document_get_information(zathura_document_t* document, zathura_error_t* static const gchar* guess_type(const char* path) { +#ifdef WITH_MAGIC + { + const char *mime_type = NULL; + const gchar *content_type = NULL; + int flags = + MAGIC_MIME_TYPE | + MAGIC_SYMLINK | + MAGIC_NO_CHECK_APPTYPE | + MAGIC_NO_CHECK_CDF | + MAGIC_NO_CHECK_ELF | + MAGIC_NO_CHECK_ENCODING; + magic_t magic = magic_open(flags); + if (magic == NULL) { + girara_warning("failed creating the magic cookie\n"); + return NULL; + } + if (magic_load(magic, NULL) < 0) { + girara_warning("failed loading the magic database: %s\n", magic_error(magic)); + goto cleanup; + } + mime_type = (const gchar *)magic_file(magic, path); + if (mime_type == NULL) { + girara_warning("failed guessing filetype: %s\n", magic_error(magic)); + goto cleanup; + } + content_type = g_strdup((const gchar *)mime_type); +cleanup: + magic_close(magic); + if (content_type != NULL) { + return content_type; + } + /* else fallback to g_content_type_guess method */ + } +#endif /*WITH_MAGIC*/ gboolean uncertain; const gchar* content_type = g_content_type_guess(path, NULL, 0, &uncertain); if (content_type == NULL) {