This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: FYI: load libmagic dynamically
- From: Tom Tromey <tromey at redhat dot com>
- To: GCJ-patches <java-patches at gcc dot gnu dot org>
- Date: 25 Jan 2007 10:38:48 -0700
- Subject: Patch: FYI: load libmagic dynamically
- Reply-to: tromey at redhat dot com
I'm checking this in.
Andrew wrote it, I just redid the configury and wrote the ChangeLog
entry.
This changes our use of libmagic to be really dynamic. We ran into a
couple problems linking against it: static linking didn't work
properly, and some systems (I'm looking at you suse) only ship a
static libmagic.a. Also on some systems it apparently interacted
poorly with multi-arch.
Tom
Index: ChangeLog
from Andrew Haley <aph@redhat.com>
* configure, Makefile.in, include/config.h.in: Rebuilt.
* Makefile.am (libgcj_la_LIBADD): Removed $(LIBMAGIC).
* configure.ac: Don't check for libmagic.
* java/net/natVMURLConnection.cc (p_magic_open, p_magic_load,
p_magic_close, p_magic_buffer): New globals.
(init): Look up 'magic' functions.
(guessContentTypeFromBuffer): Updated.
Index: configure.ac
===================================================================
--- configure.ac (revision 121138)
+++ configure.ac (working copy)
@@ -1209,12 +1209,6 @@
AC_CHECK_LIB(z, deflate, ZLIBSPEC=-lz, ZLIBSPEC=)
fi
- LIBMAGIC=
- AC_CHECK_LIB(magic, magic_open, [
- AC_DEFINE([HAVE_MAGIC_OPEN], 1, [Define if you have magic_open().])
- LIBMAGIC="-lmagic"])
- AC_SUBST(LIBMAGIC)
-
# Test for Gtk stuff, if asked for.
if test "$use_gtk_awt" = yes; then
PKG_CHECK_MODULES(GTK, gtk+-2.0 >= 2.4)
Index: java/net/natVMURLConnection.cc
===================================================================
--- java/net/natVMURLConnection.cc (revision 121138)
+++ java/net/natVMURLConnection.cc (working copy)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2006 Free Software Foundation
+/* Copyright (C) 2006, 2007 Free Software Foundation
This file is part of libgcj.
@@ -11,46 +11,71 @@
#include <java/net/VMURLConnection.h>
#include <gcj/cni.h>
#include <java/lang/UnsupportedOperationException.h>
+#include <stdio.h>
-#if defined (HAVE_MAGIC_H) && defined (HAVE_MAGIC_OPEN)
+#if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
#include <magic.h>
+#include <ltdl.h>
static magic_t cookie;
-#endif /* HAVE_MAGIC_H && HAVE_MAGIC_OPEN */
+static magic_t (*p_magic_open)(int flags);
+static int (*p_magic_load)(magic_t cookie, const char *filename);
+static void (*p_magic_close)(magic_t cookie);
+static const char * (*p_magic_buffer) (magic_t cookie, const void *buffer,
+ size_t length);
+#endif /* HAVE_MAGIC_H && defined (USE_LTDL) */
+
void
java::net::VMURLConnection::init ()
{
-#if defined (HAVE_MAGIC_H) && defined (HAVE_MAGIC_OPEN)
- cookie = magic_open (MAGIC_MIME);
+#if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
+ lt_dlhandle handle = lt_dlopenext ("libmagic.so");
+ if (!handle)
+ return;
+
+ p_magic_open = (typeof (p_magic_open))lt_dlsym(handle, "magic_open");
+ if (p_magic_open == NULL)
+ return;
+ p_magic_buffer = (typeof (p_magic_buffer))lt_dlsym(handle, "magic_buffer");
+ if (p_magic_buffer == NULL)
+ return;
+ p_magic_close = (typeof (p_magic_close))lt_dlsym(handle, "magic_close");
+ if (p_magic_close == NULL)
+ return;
+ p_magic_load = (typeof (p_magic_load))lt_dlsym(handle, "magic_load");
+ if (p_magic_load == NULL)
+ return;
+
+ cookie = p_magic_open (MAGIC_MIME);
if (cookie == (magic_t) NULL)
return;
- if (magic_load (cookie, NULL) == -1)
+ if (p_magic_load (cookie, NULL) == -1)
{
- magic_close (cookie);
+ p_magic_close (cookie);
cookie = (magic_t) NULL;
}
-#endif /* HAVE_MAGIC_H && HAVE_MAGIC_OPEN */
+#endif /* HAVE_MAGIC_H && defined (USE_LTDL) */
}
::java::lang::String *
java::net::VMURLConnection::guessContentTypeFromBuffer (jbyteArray bytes,
jint valid)
{
-#if defined (HAVE_MAGIC_H) && defined (HAVE_MAGIC_OPEN)
+#if defined (HAVE_MAGIC_H) && defined (USE_LTDL)
const char *result;
if (cookie == (magic_t) NULL)
return NULL;
- result = magic_buffer (cookie, elements(bytes), valid);
+ result = p_magic_buffer (cookie, elements(bytes), valid);
if (result == NULL)
return NULL;
return _Jv_NewStringUTF (result);
#else
return NULL;
-#endif /* HAVE_MAGIC_H && HAVE_MAGIC_OPEN */
+#endif /* HAVE_MAGIC_H && defined (USE_LTDL) */
}
Index: Makefile.am
===================================================================
--- Makefile.am (revision 121138)
+++ Makefile.am (working copy)
@@ -240,7 +240,7 @@
$(all_packages_source_files:.list=.lo) \
$(bc_objects) \
$(propertyo_files) \
- $(LIBMAGIC) $(LIBFFI) $(ZLIBS) $(GCLIBS)
+ $(LIBFFI) $(ZLIBS) $(GCLIBS)
libgcj_la_DEPENDENCIES = libgcj-$(gcc_version).jar \
java/lang/Object.lo \
java/lang/Class.lo \