From 7c3fa7fd1a975b224188fa84c94bb32033606fed Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 8 Feb 2013 10:58:57 +0100 Subject: [PATCH] Add support to detect mimetypes with magic Thanks to Diego Joss for the patch. --- Makefile | 6 ++++++ config.mk | 9 +++++++++ document.c | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 99628a7..669d6a4 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,12 @@ else SOURCE = $(filter-out database-sqlite.c,$(OSOURCE)) endif +ifneq ($(WITH_MAGIC),0) +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..d6fd079 100644 --- a/config.mk +++ b/config.mk @@ -23,6 +23,10 @@ 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) +# mimetype detection +# To disable support for mimetype detction with libmagic set WITH_MAGIC to 0. +WITH_MAGIC ?= 1 + # paths PREFIX ?= /usr MANPREFIX ?= ${PREFIX}/share/man @@ -56,6 +60,11 @@ SQLITE_INC ?= $(shell pkg-config --cflags sqlite3) SQLITE_LIB ?= $(shell pkg-config --libs sqlite3) endif +ifneq (${WITH_MAGIC},0) +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..b06018a 100644 --- a/document.c +++ b/document.c @@ -2,7 +2,6 @@ #define _BSD_SOURCE #define _XOPEN_SOURCE 700 -// TODO: Implement realpath #include #include @@ -12,6 +11,9 @@ #include #include #include +#ifdef WITH_MAGIC +#include +#endif #include #include @@ -518,7 +520,35 @@ zathura_document_get_information(zathura_document_t* document, zathura_error_t* static const gchar* guess_type(const char* path) { - gboolean uncertain; +#ifdef WITH_MAGIC + const gchar *content_type = NULL; + const 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_debug("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; + } + const char* mime_type = magic_file(magic, path); + if (mime_type == NULL) { + girara_warning("failed guessing filetype: %s\n", magic_error(magic)); + goto cleanup; + } + content_type = g_strdup(mime_type); +cleanup: + magic_close(magic); + return content_type; +#else + gboolean uncertain = FALSE; const gchar* content_type = g_content_type_guess(path, NULL, 0, &uncertain); if (content_type == NULL) { return NULL; @@ -590,6 +620,7 @@ guess_type(const char* path) g_strdelimit(out, "\n\r", '\0'); return out; +#endif } zathura_plugin_t*