xref: /aosp_15_r20/external/pciutils/update-pciids.sh (revision c2e0c6b56a71da9abe8df5c8348fb3eb5c2c9251)
1#!/bin/sh
2
3set -e
4
5SRC="https://pci-ids.ucw.cz/v2.2/pci.ids"
6DEST=pci.ids
7PCI_COMPRESSED_IDS=
8GREP=grep
9VERSION=unknown
10USER_AGENT=update-pciids/$VERSION
11QUIET=
12
13[ "$1" = "-q" ] && quiet=true || quiet=false
14
15# if pci.ids is read-only (because the filesystem is read-only),
16# then just skip this whole process.
17if ! touch ${DEST} >/dev/null 2>&1 ; then
18	${quiet} || echo "${DEST} is read-only, exiting." 1>&2
19	exit 1
20fi
21
22if command -v xz >/dev/null 2>&1 ; then
23	DECOMP="xz -d"
24	SRC="$SRC.xz"
25elif command -v bzip2 >/dev/null 2>&1 ; then
26	DECOMP="bzip2 -d"
27	SRC="$SRC.bz2"
28elif command -v gzip >/dev/null 2>&1 ; then
29	DECOMP="gzip -d"
30	SRC="$SRC.gz"
31else
32	DECOMP="cat"
33fi
34
35if command -v curl >/dev/null 2>&1 ; then
36	${quiet} && QUIET="-s -S"
37	dl ()
38	{
39		curl -o $DEST.new --user-agent "$USER_AGENT curl" $QUIET $SRC
40	}
41elif command -v wget >/dev/null 2>&1 ; then
42	${quiet} && QUIET="-q"
43	dl ()
44	{
45		wget --no-timestamping -O $DEST.new --user-agent "$USER_AGENT wget" $QUIET $SRC
46	}
47elif command -v lynx >/dev/null 2>&1 ; then
48	dl ()
49	{
50		lynx -source -useragent="$USER_AGENT lynx" $SRC >$DEST.new
51	}
52else
53	echo >&2 "update-pciids: cannot find curl, wget or lynx"
54	exit 1
55fi
56
57if ! dl ; then
58	echo >&2 "update-pciids: download failed"
59	rm -f $DEST.new
60	exit 1
61fi
62
63if ! $DECOMP <$DEST.new >$DEST.new.plain ; then
64	echo >&2 "update-pciids: decompression failed, probably truncated file"
65	exit 1
66fi
67
68if ! $GREP >/dev/null "^C " $DEST.new.plain ; then
69	echo >&2 "update-pciids: missing class info, probably truncated file"
70	exit 1
71fi
72
73if [ -f $DEST ] ; then
74	ln -f $DEST $DEST.old
75	# --reference is supported only by chmod from GNU file, so let's ignore any errors
76	chmod -f --reference=$DEST.old $DEST.new $DEST.new.plain 2>/dev/null || true
77fi
78
79if [ "$PCI_COMPRESSED_IDS" = 1 ] ; then
80	if [ "${SRC%.gz}" != .gz ] ; then
81		# Recompress to gzip
82		gzip <$DEST.new.plain >$DEST.new
83	fi
84	mv $DEST.new $DEST
85	rm -f $DEST.new.plain
86else
87	mv $DEST.new.plain $DEST
88	rm -f $DEST.new
89fi
90
91# Older versions did not compress the ids file, so let's make sure we
92# clean that up.
93if [ ${DEST%.gz} != ${DEST} ] ; then
94	rm -f ${DEST%.gz} ${DEST%.gz}.old
95fi
96
97${quiet} || echo "Done."
98