Split guess_type methods into separate functions

Also don't try to run file if we have libmagic available.

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2014-01-05 23:40:37 +01:00
parent 250c575f44
commit ecf090b9b1

View file

@ -665,11 +665,9 @@ zathura_document_get_information(zathura_document_t* document, zathura_error_t*
return result; return result;
} }
static const gchar*
guess_type(const char* path)
{
const gchar* content_type = NULL;
#ifdef WITH_MAGIC #ifdef WITH_MAGIC
static const char*
guess_type_magic(const char* path) {
const char* mime_type = NULL; const char* mime_type = NULL;
/* creat magic cookie */ /* creat magic cookie */
@ -698,22 +696,30 @@ guess_type(const char* path)
girara_debug("failed guessing filetype: %s", magic_error(magic)); girara_debug("failed guessing filetype: %s", magic_error(magic));
goto cleanup; goto cleanup;
} }
/* dup so we own the memory */
mime_type = g_strdup(mime_type);
girara_debug("magic detected filetype: %s", mime_type); girara_debug("magic detected filetype: %s", mime_type);
content_type = g_strdup(mime_type);
cleanup: cleanup:
if (magic != NULL) { if (magic != NULL) {
magic_close(magic); magic_close(magic);
} }
if (content_type != NULL) { return mime_type;
return content_type; }
} #else
/* else fallback to g_content_type_guess method */ static const char*
#endif /*WITH_MAGIC*/ guess_type_magic(const char* UNUSED(path)) {
return NULL;
}
#endif
static const char*
guess_type_glib(const char* path)
{
gboolean uncertain = FALSE; gboolean uncertain = FALSE;
content_type = g_content_type_guess(path, NULL, 0, &uncertain); const char* content_type = g_content_type_guess(path, NULL, 0, &uncertain);
if (content_type == NULL) { if (content_type == NULL) {
girara_debug("g_content_type failed\n"); girara_debug("g_content_type failed\n");
} else { } else {
@ -755,8 +761,26 @@ cleanup:
} }
g_free((void*)content_type); g_free((void*)content_type);
content_type = NULL; return NULL;
}
static const char*
guess_type(const char* path)
{
/* try libmagic first */
const char* content_type = guess_type_magic(path);
if (content_type != NULL) {
return content_type;
}
/* else fallback to g_content_type_guess method */
content_type = guess_type_glib(path);
if (content_type != NULL) {
return content_type;
}
#ifdef WITH_MAGIC
return NULL;
#else
/* and if libmagic is not available, try file as last resort */
girara_debug("falling back to file"); girara_debug("falling back to file");
GString* command = g_string_new("file -b --mime-type "); GString* command = g_string_new("file -b --mime-type ");
@ -784,6 +808,7 @@ cleanup:
g_strdelimit(out, "\n\r", '\0'); g_strdelimit(out, "\n\r", '\0');
return out; return out;
#endif
} }
zathura_plugin_t* zathura_plugin_t*