From mboxrd@z Thu Jan 1 00:00:00 1970 From: Piter PUNK Date: Tue, 22 Sep 2009 02:42:33 +0000 Subject: [PATCH] Add compressed ID database support to (usb|pci)-db Message-Id: <4AB83999.1060004@unitednerds.org> MIME-Version: 1 Content-Type: multipart/mixed; boundary="------------000509040402090509030703" List-Id: To: linux-hotplug@vger.kernel.org This is a multi-part message in MIME format. --------------000509040402090509030703 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi usbutils and pciutils have support to gzip'ed ID databases. But, if I use usb.ids.gz or pci.ids.gz, usb-db and pci-db can't show anything, giving only an error of file not found. Looking in the code, they didn't have any support to compressed ID files. This patch add support to gzip'ed database files to usb-db.c and add the option --disable-zlib to configure.ac. Most of code here come from usbutils names.c file with little modifications to work with udev. Bye, Piter PUNK --------------000509040402090509030703 Content-Type: text/plain; name="udev-zlib-support.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="udev-zlib-support.patch" diff --git a/configure.ac b/configure.ac index 3afb94a..5a273e6 100644 --- a/configure.ac +++ b/configure.ac @@ -6,7 +6,7 @@ AC_DISABLE_STATIC AC_USE_SYSTEM_EXTENSIONS AC_SYS_LARGEFILE AC_CONFIG_MACRO_DIR([m4]) -LT_INIT +m4_ifdef([LT_INIT],[LT_INIT],[AC_PROG_LIBTOOL]) AC_PROG_AWK GTK_DOC_CHECK(1.10) AC_PREFIX_DEFAULT([/usr]) @@ -69,6 +69,22 @@ if test "x$enable_extras" = xyes; then PKG_CHECK_MODULES(USBUTILS, usbutils >= 0.82) AC_SUBST([USB_DATABASE], [$($PKG_CONFIG --variable=usbids usbutils)]) + USE_ZLIB=yes + AC_ARG_ENABLE(zlib, + AS_HELP_STRING(--disable-zlib,disable support for zlib), + [ + if eval "test x$enable_zlib = xno"; then + USE_ZLIB= + fi + ]) + if test "$USE_ZLIB" = "yes" ; then + AC_CHECK_LIB(z, inflateEnd) + if test "${ac_cv_lib_z_inflateEnd}" = "yes" ; then + HAVE_ZLIB="yes" + fi + fi + AM_CONDITIONAL(HAVE_ZLIB, test x$HAVE_ZLIB = xyes) + AC_CHECK_FILES([/usr/share/pci.ids], [pciids=/usr/share/pci.ids]) AC_CHECK_FILES([/usr/share/hwdata/pci.ids], [pciids=/usr/share/hwdata/pci.ids]) AC_CHECK_FILES([/usr/share/misc/pci.ids], [pciids=/usr/share/misc/pci.ids]) diff --git a/extras/usb-db/usb-db.c b/extras/usb-db/usb-db.c index db1f843..1381c3c 100644 --- a/extras/usb-db/usb-db.c +++ b/extras/usb-db/usb-db.c @@ -39,6 +39,19 @@ # error "Are you havin' a laugh?" #endif +#ifdef HAVE_LIBZ +#include +#define data_file gzFile +#define data_fopen(path, mode) gzopen(path, mode) +#define data_fgets(s, size, stream) gzgets(stream, s, size) +#define data_close(f) gzclose(f) +#else +#define data_file FILE* +#define data_fopen(path, mode) fopen(path, mode) +#define data_fgets(s, size, stream) fgets(s, size, stream) +#define data_close(f) fclose(f) +#endif + static int get_id_attr( struct udev_device *parent, const char *name, @@ -98,29 +111,36 @@ static int lookup_vid_pid( char **vendor, char **product) { - FILE *f; + data_file f; int ret = -1; int found_vendor = 0; - char *line = NULL; + char buf[512], *line; + char *filename = DATABASE ; *vendor = *product = NULL; - if (!(f = fopen(DATABASE, "r"))) { +#ifdef HAVE_LIBZ + filename = DATABASE".gz"; + if (!(f = data_fopen(filename, "r"))) { + filename = DATABASE; + if (!(f = data_fopen(filename, "r"))) { + fprintf(stderr, "Failed to open database file "DATABASE": %s\n", strerror(errno)); + return -1; + } + } +#else + if (!(f = data_fopen(filename, "r"))) { fprintf(stderr, "Failed to open database file "DATABASE": %s\n", strerror(errno)); return -1; } +#endif for (;;) { size_t n; - if (line) { - free(line); - line = NULL; - } - - if (getline(&line, &n, f) < 0) + if (data_fgets(buf, sizeof(buf), f) < 0) break; - + line = buf; rstrip(line); if (line[0] == '#' || line[0] == 0) @@ -171,8 +191,7 @@ static int lookup_vid_pid( ret = 0; finish: - free(line); - fclose(f); + data_close(f); if (ret < 0) { free(*product); --------------000509040402090509030703--