Commit 1d23f466 authored by Diego Joss's avatar Diego Joss Committed by Sebastian Ramacher
Browse files

replace g_content_type_guess with libmagic



Signed-off-by: Sebastian Ramacher's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent 74330c64
......@@ -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
......
......@@ -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
......
......@@ -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) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment