replace g_content_type_guess with libmagic

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Diego Joss 2013-02-11 14:11:15 +01:00 committed by Sebastian Ramacher
parent 74330c6459
commit 1d23f466a2
3 changed files with 51 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -12,6 +12,9 @@
#include <errno.h>
#include <glib.h>
#include <glib/gi18n.h>
#ifdef WITH_MAGIC
# include <magic.h>
#endif /*WITH_MAGIC*/
#include <girara/datastructures.h>
#include <girara/utils.h>
@ -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) {