Add support to detect mimetypes with magic

Thanks to Diego Joss <djego.joss@gmail.com> for the patch.
This commit is contained in:
Sebastian Ramacher 2013-02-08 10:58:57 +01:00
parent 74330c6459
commit 7c3fa7fd1a
3 changed files with 48 additions and 2 deletions

View File

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

View File

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

View File

@ -2,7 +2,6 @@
#define _BSD_SOURCE
#define _XOPEN_SOURCE 700
// TODO: Implement realpath
#include <sys/wait.h>
#include <stdlib.h>
@ -12,6 +11,9 @@
#include <errno.h>
#include <glib.h>
#include <glib/gi18n.h>
#ifdef WITH_MAGIC
#include <magic.h>
#endif
#include <girara/datastructures.h>
#include <girara/utils.h>
@ -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*