From f304622af8954aeb6ec594aa16613383d6832c30 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 14 May 2014 10:29:55 +0000 Subject: [PATCH] Opening the magic database only when actually going to use it, and closing it afterward. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4884 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 2 ++ src/color.c | 48 +++++++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index 748a61d4..b3b77078 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,8 @@ * src/nano.c (precalc_multicolorinfo): Improve debugging messages, and remove superfluous assignment (fileptr already equals endptr). * src/color.c (color_update): Move magic check to after headerline. + * src/color.c (color_update): Open the magic database only when + actually going to use it, and close it afterward. 2014-05-13 Benno Schulenberg * src/text.c (do_linter): Make an error message somewhat clearer. diff --git a/src/color.c b/src/color.c index af4261d2..ee831210 100644 --- a/src/color.c +++ b/src/color.c @@ -158,7 +158,7 @@ void color_update(void) /* Var magicstring will stay NULL if we fail to get a magic result. */ #ifdef HAVE_LIBMAGIC const char *magicstring = NULL; - magic_t cookie; + magic_t cookie = NULL; struct stat fileinfo; #endif @@ -186,28 +186,6 @@ void color_update(void) } } -#ifdef HAVE_LIBMAGIC - if (stat(openfile->filename, &fileinfo) == 0) { - cookie = magic_open(MAGIC_SYMLINK | -#ifdef DEBUG - MAGIC_DEBUG | MAGIC_CHECK | -#endif - MAGIC_ERROR); - if (cookie == NULL || magic_load(cookie, NULL) < 0) - statusbar(_("magic_load() failed: %s"), strerror(errno)); - else { - magicstring = magic_file(cookie, openfile->filename); - if (magicstring == NULL) { - statusbar(_("magic_file(%s) failed: %s"), - openfile->filename, magic_error(cookie)); - } -#ifdef DEBUG - fprintf(stderr, "Returned magic string is: %s\n", magicstring); -#endif - } - } -#endif /* HAVE_LIBMAGIC */ - /* If we didn't specify a syntax override string, or if we did and * there was no syntax by that name, get the syntax based on the * file extension, then try magic, and then look in the header. */ @@ -287,6 +265,28 @@ void color_update(void) #ifdef DEBUG fprintf(stderr, "No result from headerline either, trying libmagic...\n"); #endif + if (stat(openfile->filename, &fileinfo) == 0) { + /* Open the magic database and get a diagnosis of the file. */ + cookie = magic_open(MAGIC_SYMLINK | +#ifdef DEBUG + MAGIC_DEBUG | MAGIC_CHECK | +#endif + MAGIC_ERROR); + if (cookie == NULL || magic_load(cookie, NULL) < 0) + statusbar(_("magic_load() failed: %s"), strerror(errno)); + else { + magicstring = magic_file(cookie, openfile->filename); + if (magicstring == NULL) { + statusbar(_("magic_file(%s) failed: %s"), + openfile->filename, magic_error(cookie)); + } +#ifdef DEBUG + fprintf(stderr, "Returned magic string is: %s\n", magicstring); +#endif + } + } + + /* Now try and find a syntax that matches the magicstring. */ for (tmpsyntax = syntaxes; tmpsyntax != NULL; tmpsyntax = tmpsyntax->next) { @@ -311,6 +311,8 @@ void color_update(void) nfreeregex(&e->ext); } } + if (stat(openfile->filename, &fileinfo) == 0) + magic_close(cookie); } #endif /* HAVE_LIBMAGIC */ } -- 2.39.5