summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Hesse <mail@eworm.de>2022-01-12 09:53:15 +0100
committerChristian Hesse <mail@eworm.de>2022-02-08 10:54:40 +0100
commitfacb67f8438aa8ef18ffacdccfd0a2d1c2730e5c (patch)
tree1a3588038565354151a92a616f3c3f3311fab62f /src
parent4affafe91579799efd83f4c8e05c291eeb684c9c (diff)
global initialization for libmagic
Add a global `magic_t magic` and initialize it just once. Also `feh_is_image()` now calls itself to check compressed files, saving some duplicate code.
Diffstat (limited to 'src')
-rw-r--r--src/feh.h4
-rw-r--r--src/imlib.c94
-rw-r--r--src/main.c8
3 files changed, 66 insertions, 40 deletions
diff --git a/src/feh.h b/src/feh.h
index 007c7c5..9afe238 100644
--- a/src/feh.h
+++ b/src/feh.h
@@ -145,6 +145,10 @@ void init_slideshow_mode(void);
void init_list_mode(void);
void init_loadables_mode(void);
void init_unloadables_mode(void);
+#ifdef HAVE_LIBMAGIC
+void uninit_magic(void);
+void init_magic(void);
+#endif
void feh_clean_exit(void);
int feh_should_ignore_image(Imlib_Image * im);
int feh_load_image(Imlib_Image * im, feh_file * file);
diff --git a/src/imlib.c b/src/imlib.c
index 70d459f..0accbdc 100644
--- a/src/imlib.c
+++ b/src/imlib.c
@@ -46,6 +46,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifdef HAVE_LIBMAGIC
#include <magic.h>
+
+magic_t magic = NULL;
#endif
Display *disp = NULL;
@@ -239,6 +241,33 @@ void feh_print_load_error(char *file, winwidget w, Imlib_Load_Error err, enum fe
}
}
+#ifdef HAVE_LIBMAGIC
+void uninit_magic(void)
+{
+ if (!magic) {
+ return;
+ }
+
+ magic_close(magic);
+ magic = NULL;
+}
+void init_magic(void)
+{
+ if (getenv("FEH_SKIP_MAGIC")) {
+ return;
+ }
+
+ if (!(magic = magic_open(MAGIC_NONE))) {
+ weprintf("unable to initialize magic library\n");
+ return;
+ }
+
+ if (magic_load(magic, NULL) != 0) {
+ weprintf("cannot load magic database: %s\n", magic_error(magic));
+ uninit_magic();
+ }
+}
+
/*
* This is a workaround for an Imlib2 regression, causing unloadable image
* detection to be excessively slow (and, thus, causing feh to hang for a while
@@ -247,62 +276,47 @@ void feh_print_load_error(char *file, winwidget w, Imlib_Load_Error err, enum fe
* <https://phab.enlightenment.org/T8739> and
* <https://github.com/derf/feh/issues/505>.
*/
-int feh_is_image(feh_file * file)
+int feh_is_image(feh_file * file, int magic_flags)
{
-#ifdef HAVE_LIBMAGIC
- magic_t magic;
- const char * mime_type;
- int is_image = 0;
+ const char * mime_type = NULL;
- if (getenv("FEH_SKIP_MAGIC")) {
+ if (!magic) {
return 1;
}
- if (!(magic = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK))) {
- weprintf("unable to initialize magic library\n");
- return 0;
- }
+ magic_setflags(magic, MAGIC_MIME_TYPE | MAGIC_SYMLINK | magic_flags);
+ mime_type = magic_file(magic, file->filename);
- if (magic_load(magic, NULL) != 0) {
- weprintf("cannot load magic database: %s\n", magic_error(magic));
- magic_close(magic);
+ if (!mime_type) {
return 0;
}
- mime_type = magic_file(magic, file->filename);
-
- if (mime_type) {
- D(("file %s has mime type: %s\n", file->filename, mime_type));
-
- if (strncmp(mime_type, "image/", 6) == 0) {
- is_image = 1;
- }
-
- /* imlib2 supports loading compressed images, let's have a look inside */
- if (strcmp(mime_type, "application/gzip") == 0 ||
- strcmp(mime_type, "application/x-bzip2") == 0 ||
- strcmp(mime_type, "application/x-xz") == 0) {
- magic_setflags(magic, magic_getflags(magic) | MAGIC_COMPRESS);
- mime_type = magic_file(magic, file->filename);
+ D(("file %s has mime type: %s\n", file->filename, mime_type));
- if (mime_type) {
- D(("uncompressed file %s has mime type: %s\n", file->filename, mime_type));
+ if (strncmp(mime_type, "image/", 6) == 0) {
+ return 1;
+ }
- if (strncmp(mime_type, "image/", 6) == 0) {
- is_image = 1;
- }
- }
- }
+ /* no infinite loop on compressed content, please */
+ if (magic_flags) {
+ return 0;
}
- magic_close(magic);
+ /* imlib2 supports loading compressed images, let's have a look inside */
+ if (strcmp(mime_type, "application/gzip") == 0 ||
+ strcmp(mime_type, "application/x-bzip2") == 0 ||
+ strcmp(mime_type, "application/x-xz") == 0) {
+ return feh_is_image(file, MAGIC_COMPRESS);
+ }
- return is_image;
+ return 0;
+}
#else
- (void)file;
+int feh_is_image(__attribute__((unused)) feh_file * file, __attribute__((unused)) int magic_flags)
+{
return 1;
-#endif
}
+#endif
int feh_load_image(Imlib_Image * im, feh_file * file)
{
@@ -326,7 +340,7 @@ int feh_load_image(Imlib_Image * im, feh_file * file)
}
}
else {
- if (feh_is_image(file)) {
+ if (feh_is_image(file, 0)) {
*im = imlib_load_image_with_error_return(file->filename, &err);
} else {
feh_err = LOAD_ERROR_MAGICBYTES;
diff --git a/src/main.c b/src/main.c
index 3d124fd..34b667a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -69,6 +69,10 @@ int main(int argc, char **argv)
#endif
}
+#ifdef HAVE_LIBMAGIC
+ init_magic();
+#endif
+
feh_event_init();
if (opt.index)
@@ -262,6 +266,10 @@ void feh_clean_exit(void)
if(disp)
XCloseDisplay(disp);
+#ifdef HAVE_LIBMAGIC
+ uninit_magic();
+#endif
+
/*
* Only restore the old terminal settings if
* - we changed them in the first place