[Pkg-pciutils-discuss] Re: Gzipped pci.ids file

Matthew Wilcox matthew at wil.cx
Tue Jun 27 14:44:55 UTC 2006


On Tue, Jun 27, 2006 at 03:33:09PM +0200, Martin Mares wrote:
> Of course that's a risk, but it would be also nice to run a new version
> of lspci on a machine with pci.ids from an older version installed.

OK.  Here's the third version of the patch, against -pre2

Index: Makefile
===================================================================
RCS file: /cvsroot/pkg-pciutils/pciutils/Makefile,v
retrieving revision 1.12
diff -u -p -r1.12 Makefile
--- Makefile	27 Jun 2006 14:21:13 -0000	1.12
+++ Makefile	27 Jun 2006 14:41:15 -0000
@@ -17,6 +17,14 @@ DIRINSTALL=install -d
 PCILIB=lib/libpci.a
 PCIINC=lib/config.h lib/header.h lib/pci.h lib/types.h lib/sysdep.h
 
+-include lib/config.mk
+ifdef PCI_USE_ZLIB
+LDFLAGS += -lz
+PCI_IDS=pci.ids.gz
+else
+PCI_IDS=pci.ids
+endif
+
 ifeq ($(shell uname),NetBSD)
 PCILIB=lib/libpciutils.a
 LDFLAGS+=-lpci
@@ -33,7 +41,7 @@ RELEASE=
 
 export
 
-all: $(PCILIB) lspci setpci lspci.8 setpci.8 update-pciids update-pciids.8 pci.ids
+all: $(PCILIB) lspci setpci lspci.8 setpci.8 update-pciids update-pciids.8 $(PCI_IDS)
 all: pcimodules pcimodules.8
 
 $(PCILIB): $(PCIINC) force
@@ -41,8 +49,8 @@ $(PCILIB): $(PCIINC) force
 
 force:
 
-lib/config.h:
-	cd lib && ./configure $(IDSDIR) $(VERSION) $(HOST) $(RELEASE)
+lib/config.h lib/config.mk:
+	cd lib && ./configure "$(IDSDIR)" "$(VERSION)" "$(HOST)" "$(RELEASE)" "$(ZLIB)"
 
 lspci: lspci.o common.o $(PCILIB)
 setpci: setpci.o common.o $(PCILIB)
@@ -54,14 +62,14 @@ pcimodules.o: pcimodules.c pciutils.h $(
 common.o: common.c pciutils.h $(PCIINC)
 
 update-pciids: update-pciids.sh
-	sed <$< >$@ "s@^DEST=.*@DEST=$(IDSDIR)/pci.ids@"
+	sed <$< >$@ "s@^DEST=.*@DEST=$(IDSDIR)/$(PCI_IDS)@;s@^PCI_USE_ZLIB=.*@PCI_USE_ZLIB=$(PCI_USE_ZLIB)@"
 
 %.8: %.man
 	M=`echo $(DATE) | sed 's/-01-/-January-/;s/-02-/-February-/;s/-03-/-March-/;s/-04-/-April-/;s/-05-/-May-/;s/-06-/-June-/;s/-07-/-July-/;s/-08-/-August-/;s/-09-/-September-/;s/-10-/-October-/;s/-11-/-November-/;s/-12-/-December-/;s/\(.*\)-\(.*\)-\(.*\)/\3 \2 \1/'` ; sed <$< >$@ "s/@TODAY@/$$M/;s/@VERSION@/pciutils-$(VERSION)/;s#@IDSDIR@#$(IDSDIR)#"
 
 clean:
 	rm -f `find . -name "*~" -o -name "*.[oa]" -o -name "\#*\#" -o -name TAGS -o -name core`
-	rm -f update-pciids lspci setpci lib/config.* lib/example *.8 pci.ids.*
+	rm -f update-pciids lspci setpci lib/config.* lib/example *.8 pci.ids*.new* pci.ids*.old
 	rm -f pcimodules
 	rm -rf maint/dist
 
@@ -72,17 +80,20 @@ install: all
 	$(DIRINSTALL) -m 755 $(DESTDIR)/$(SBINDIR) $(DESTDIR)/$(IDSDIR) $(DESTDIR)/$(MANDIR)/man8
 	$(INSTALL) -c -m 755 -s lspci setpci $(DESTDIR)/$(SBINDIR)
 	$(INSTALL) -c -m 755 update-pciids $(DESTDIR)/$(SBINDIR)
-	$(INSTALL) -c -m 644 pci.ids $(DESTDIR)/$(IDSDIR)
+	$(INSTALL) -c -m 644 $(PCI_IDS) $(DESTDIR)/$(IDSDIR)
 	$(INSTALL) -c -m 644 lspci.8 setpci.8 update-pciids.8 $(DESTDIR)/$(MANDIR)/man8
 	$(INSTALL) -c -m 755 -s pcimodules $(DESTDIR)/$(SBINDIR)
 	$(INSTALL) -c -m 644 pcimodules.8 $(DESTDIR)/$(MANDIR)/man8
 
 uninstall: all
 	rm -f $(DESTDIR)/$(SBINDIR)/lspci $(DESTDIR)/$(SBINDIR)/setpci $(DESTDIR)/$(SBINDIR)/update-pciids
-	rm -f $(DESTDIR)/$(IDSDIR)/pci.ids
+	rm -f $(DESTDIR)/$(IDSDIR)/$(PCI_IDS)
 	rm -f $(DESTDIR)/$(MANDIR)/man8/lspci.8 $(DESTDIR)/$(MANDIR)/man8/setpci.8 $(DESTDIR)/$(MANDIR)/man8/update-pciids.8
 	rm -f $(DESTDIR)/$(SBINDIR)/pcimodules $(DESTDIR)/$(MANDIR)/man8/pcimodules.8
 
+pci.ids.gz: pci.ids
+	gzip -9 <$< >$@
+
 get-ids:
 	cp ~/tree/pciids/pci.ids pci.ids
 
Index: README
===================================================================
RCS file: /cvsroot/pkg-pciutils/pciutils/README,v
retrieving revision 1.1.1.4
diff -u -p -r1.1.1.4 README
--- README	27 Jun 2006 13:52:08 -0000	1.1.1.4
+++ README	27 Jun 2006 14:41:15 -0000
@@ -49,6 +49,10 @@ the ROOT and PREFIX variables specified 
 use "make PREFIX=/opt/pciutils ROOT=/opt/pciutils install" to create
 a separate installation not interfering with the rest of your system.
 
+Support for a compresses pci.ids file is optional.  The build system
+should automatically detect whether you have zlib installed, but you
+can explicitly tell it by using "make ZLIB=no" or "make ZLIB=yes".
+
 When you are bored of dumping PCI registers, just use "make uninstall".
 
 
Index: update-pciids.sh
===================================================================
RCS file: /cvsroot/pkg-pciutils/pciutils/update-pciids.sh,v
retrieving revision 1.5
diff -u -p -r1.5 update-pciids.sh
--- update-pciids.sh	13 Mar 2006 12:38:23 -0000	1.5
+++ update-pciids.sh	27 Jun 2006 14:41:17 -0000
@@ -3,8 +3,14 @@
 set -e
 SRC="http://pciids.sourceforge.net/v2.2/pci.ids"
 DEST=pci.ids
+PCI_USE_ZLIB=
+GREP=grep
 
-if which bzip2 >/dev/null ; then
+if [ -n "$PCI_USE_ZLIB" ] ; then
+	DECOMP="cat"
+	SRC="$SRC.gz"
+	GREP=zgrep
+elif which bzip2 >/dev/null ; then
 	DECOMP="bzip2 -d"
 	SRC="$SRC.bz2"
 elif which gzip >/dev/null ; then
@@ -36,7 +42,7 @@ if ! $DECOMP <$DEST.new >$DEST.neww ; th
 	exit 1
 fi
 
-if ! grep >/dev/null "^C " $DEST.neww ; then
+if ! $GREP >/dev/null "^C " $DEST.neww ; then
 	echo >&2 "update-pciids: missing class info, probably truncated file"
 	exit 1
 fi
Index: lib/configure
===================================================================
RCS file: /cvsroot/pkg-pciutils/pciutils/lib/configure,v
retrieving revision 1.6
diff -u -p -r1.6 configure
--- lib/configure	6 Jun 2006 11:57:57 -0000	1.6
+++ lib/configure	27 Jun 2006 14:41:17 -0000
@@ -33,6 +33,11 @@ sys=`echo $host | sed 's/^\([^-]*\)-\([^
 rel=${4:-$rel}
 echo " $host $rel"
 
+zlib=${5}
+if [ -z "$zlib" -a -f /usr/include/zlib.h ]; then
+	zlib=yes
+fi
+
 c=config.h
 echo >$c "#define PCI_ARCH_`echo $cpu | tr 'a-z' 'A-Z'`"
 echo >>$c "#define PCI_OS_`echo $sys | tr 'a-z' 'A-Z'`"
@@ -111,6 +116,13 @@ echo " dump"
 if [ -z "$ok" ] ; then
 	echo "WARNING: No real configuration access method is available."
 fi
-echo >>$c "#define PCI_PATH_IDS \"$idsdir/pci.ids\""
+if [ "$zlib" = "yes" ] ; then
+	echo "Including zlib support"
+	echo >>$c '#define PCI_USE_ZLIB'
+	echo >>$c "#define PCI_PATH_IDS \"$idsdir/pci.ids.gz\""
+else
+	echo "Not including zlib support"
+	echo >>$c "#define PCI_PATH_IDS \"$idsdir/pci.ids\""
+fi
 echo >>$c "#define PCILIB_VERSION \"$version\""
 sed '/^#define [^ ]*$/!d;s/^#define \(.*\)/\1=1/' <$c >config.mk
Index: lib/names.c
===================================================================
RCS file: /cvsroot/pkg-pciutils/pciutils/lib/names.c,v
retrieving revision 1.6
diff -u -p -r1.6 names.c
--- lib/names.c	6 Jun 2006 11:57:57 -0000	1.6
+++ lib/names.c	27 Jun 2006 14:41:17 -0000
@@ -14,6 +14,48 @@
 
 #include "internal.h"
 
+#ifdef PCI_USE_ZLIB
+#include <zlib.h>
+typedef gzFile pciFile;
+#define pci_gets(f, l, s)	gzgets(f, l, s)
+#define pci_eof(f)		gzeof(f)
+
+static pciFile pci_open(struct pci_access *a)
+{
+  pciFile result;
+  size_t len;
+  char *new_name;
+
+  result = gzopen(a->id_file_name, "r");
+  if (result)
+    return result;
+  len = strlen(a->id_file_name);
+  if (memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
+    return result;
+  new_name = malloc(len - 2);
+  memcpy(new_name, a->id_file_name, len - 3);
+  new_name[len - 3] = '\0';
+  a->id_file_name = new_name;
+  return gzopen(a->id_file_name, "r");
+}
+
+#define pci_close(f)		gzclose(f)
+#define pci_error(f, err)						\
+	if (!err) {							\
+		int errnum;						\
+		err = gzerror(f, &errnum);				\
+		if (errnum == Z_ERRNO)	err = "I/O error";		\
+		else if (errnum >= 0)	err = NULL;			\
+	}
+#else
+typedef FILE * pciFile;
+#define pci_gets(f, l, s)	fgets(l, s, f)
+#define pci_eof(f)		feof(f)
+#define pci_open(a)		fopen(a->id_file_name, "r")
+#define pci_close(f)		fclose(f)
+#define pci_error(f, err)	if (!err && ferror(f))	err = "I/O error";
+#endif
+
 struct id_entry {
   struct id_entry *next;
   u32 id12, id34;
@@ -141,7 +183,7 @@ static inline int id_white_p(int c)
   return (c == ' ') || (c == '\t');
 }
 
-static const char *id_parse_list(struct pci_access *a, FILE *f, int *lino)
+static const char *id_parse_list(struct pci_access *a, pciFile f, int *lino)
 {
   char line[MAX_LINE];
   char *p;
@@ -151,13 +193,13 @@ static const char *id_parse_list(struct 
   static const char parse_error[] = "Parse error";
 
   *lino = 0;
-  while (fgets(line, sizeof(line), f))
+  while (pci_gets(f, line, sizeof(line)))
     {
       (*lino)++;
       p = line;
       while (*p && *p != '\n' && *p != '\r')
 	p++;
-      if (!*p && !feof(f))
+      if (!*p && !pci_eof(f))
 	return "Line too long";
       *p = 0;
       if (p > line && (p[-1] == ' ' || p[-1] == '\t'))
@@ -275,20 +317,19 @@ static const char *id_parse_list(struct 
 int
 pci_load_name_list(struct pci_access *a)
 {
-  FILE *f;
+  pciFile f;
   int lino;
   const char *err;
 
   pci_free_name_list(a);
   a->hash_load_failed = 1;
-  if (!(f = fopen(a->id_file_name, "r")))
+  if (!(f = pci_open(a)))
     return 0;
   a->id_hash = pci_malloc(a, sizeof(struct id_entry *) * HASH_SIZE);
   bzero(a->id_hash, sizeof(struct id_entry *) * HASH_SIZE);
   err = id_parse_list(a, f, &lino);
-  if (!err && ferror(f))
-    err = "I/O error";
-  fclose(f);
+  pci_error(f, err);
+  pci_close(f);
   if (err)
     a->error("%s at %s, line %d\n", err, a->id_file_name, lino);
   a->hash_load_failed = 0;



More information about the Pkg-pciutils-discuss mailing list