]>
Commit | Line | Data |
---|---|---|
7f0798ef GI |
1 | #! /bin/sh |
2 | # | |
3 | # smartmontools drive database update script | |
4 | # | |
17d0b8d9 | 5 | <<<<<<< HEAD |
a86ec89e | 6 | # Copyright (C) 2010-16 Christian Franke |
17d0b8d9 | 7 | ======= |
3d8ad6fa | 8 | # Copyright (C) 2010-14 Christian Franke <smartmontools-support@lists.sourceforge.net> |
17d0b8d9 | 9 | >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74 |
7f0798ef GI |
10 | # |
11 | # This program is free software; you can redistribute it and/or modify | |
12 | # it under the terms of the GNU General Public License as published by | |
13 | # the Free Software Foundation; either version 2, or (at your option) | |
14 | # any later version. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public License | |
17 | # (for example COPYING); If not, see <http://www.gnu.org/licenses/>. | |
18 | # | |
17d0b8d9 | 19 | <<<<<<< HEAD |
a86ec89e | 20 | # $Id: update-smart-drivedb.in 4224 2016-02-26 20:29:24Z chrfranke $ |
17d0b8d9 | 21 | ======= |
3d8ad6fa | 22 | # $Id: update-smart-drivedb.in 4019 2014-12-06 20:12:50Z chrfranke $ |
17d0b8d9 | 23 | >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74 |
7f0798ef GI |
24 | # |
25 | ||
26 | set -e | |
27 | ||
28 | # Set by config.status | |
29 | PACKAGE="@PACKAGE@" | |
30 | VERSION="@VERSION@" | |
31 | prefix="@prefix@" | |
32 | exec_prefix="@exec_prefix@" | |
33 | sbindir="@sbindir@" | |
34 | datarootdir="@datarootdir@" | |
35 | datadir="@datadir@" | |
36 | drivedbdir="@drivedbdir@" | |
37 | ||
cfbba5b9 GI |
38 | # Download tools |
39 | os_dltools="@os_dltools@" | |
40 | ||
41 | # drivedb.h update branch | |
42 | BRANCH="@DRIVEDB_BRANCH@" | |
43 | ||
7f0798ef | 44 | # Default drivedb location |
a86ec89e | 45 | DRIVEDB="$drivedbdir/drivedb.h" |
7f0798ef GI |
46 | |
47 | # Smartctl used for syntax check | |
48 | SMARTCTL="$sbindir/smartctl" | |
49 | ||
a86ec89e | 50 | myname=$0 |
7f0798ef | 51 | |
a86ec89e GI |
52 | usage() |
53 | { | |
54 | cat <<EOF | |
7f0798ef GI |
55 | smartmontools $VERSION drive database update script |
56 | ||
a86ec89e | 57 | Usage: $myname [OPTIONS] [DESTFILE] |
7f0798ef | 58 | |
a86ec89e GI |
59 | -s SMARTCTL Use SMARTCTL for syntax check ('-s -' to disable) |
60 | [default: $SMARTCTL] | |
61 | -t TOOL Use TOOL for download: $os_dltools | |
62 | [default: first one found in PATH] | |
63 | -u LOCATION Use URL of LOCATION for download: | |
64 | sf (Sourceforge code browser via HTTPS) | |
65 | svn (SVN repository via HTTPS) [default] | |
66 | svni (SVN repository via HTTP) | |
67 | trac (Trac code browser via HTTPS) | |
68 | --cacert FILE Use CA certificates from FILE to verify the peer | |
69 | --capath DIR Use CA certificate files from DIR to verify the peer | |
70 | --insecure Don't abort download if certificate verification fails | |
71 | --dryrun Print download commands only | |
72 | -v Verbose output | |
7f0798ef | 73 | |
a86ec89e | 74 | Updates $DRIVEDB |
7f0798ef GI |
75 | or DESTFILE from smartmontools SVN repository. |
76 | Tries to download first from branch $BRANCH | |
77 | and then from trunk. | |
78 | EOF | |
a86ec89e GI |
79 | exit 1 |
80 | } | |
7f0798ef | 81 | |
a86ec89e GI |
82 | error() |
83 | { | |
84 | echo "$myname: $*" >&2 | |
85 | exit 1 | |
86 | } | |
7f0798ef | 87 | |
a86ec89e GI |
88 | warning() |
89 | { | |
90 | echo "$myname: (Warning) $*" >&2 | |
91 | } | |
92 | ||
93 | selecturl() | |
94 | { | |
95 | case $1 in | |
96 | sf) url='https://sourceforge.net/p/smartmontools/code/HEAD/tree/trunk/smartmontools/drivedb.h?format=raw' ;; | |
97 | svn) url='https://svn.code.sf.net/p/smartmontools/code/trunk/smartmontools/drivedb.h' ;; | |
98 | svni) url='http://svn.code.sf.net/p/smartmontools/code/trunk/smartmontools/drivedb.h' ;; | |
99 | trac) url='https://www.smartmontools.org/export/HEAD/trunk/smartmontools/drivedb.h' ;; | |
100 | *) usage ;; | |
101 | esac | |
102 | } | |
103 | ||
104 | inpath() | |
105 | { | |
106 | local d rc save | |
107 | rc=1 | |
108 | save=$IFS | |
109 | IFS=':' | |
110 | for d in $PATH; do | |
111 | test -f "$d/$1" || continue | |
112 | test -x "$d/$1" || continue | |
113 | rc=0 | |
cfbba5b9 | 114 | break |
a86ec89e GI |
115 | done |
116 | IFS=$save | |
117 | return $rc | |
118 | } | |
119 | ||
120 | vecho() | |
121 | { | |
122 | test -n "$q" || echo "$*" | |
123 | } | |
124 | ||
125 | # vrun COMMAND ARGS... | |
126 | vrun() | |
127 | { | |
128 | if [ -n "$dryrun" ]; then | |
129 | echo "$*" | |
130 | elif [ -n "$q" ]; then | |
131 | "$@" 2>/dev/null | |
132 | else | |
133 | echo "$*" | |
134 | "$@" | |
cfbba5b9 | 135 | fi |
a86ec89e GI |
136 | } |
137 | ||
138 | # vrun2 OUTFILE COMMAND ARGS... | |
139 | vrun2() | |
140 | { | |
141 | local f err rc | |
142 | f=$1; shift | |
143 | rc=0 | |
144 | if [ -n "$dryrun" ]; then | |
145 | echo "$* > $f" | |
146 | else | |
147 | vecho "$* > $f" | |
148 | err=`"$@" 2>&1 > $f` || rc=$? | |
149 | if [ -n "$err" ]; then | |
150 | vecho "$err" >&2 | |
151 | test $rc != 0 || rc=42 | |
152 | fi | |
153 | fi | |
154 | return $rc | |
155 | } | |
156 | ||
157 | # download URL FILE | |
158 | download() | |
159 | { | |
160 | local f u se rc | |
161 | u=$1; f=$2 | |
162 | rc=0 | |
163 | ||
164 | case $tool in | |
165 | curl) | |
166 | vrun curl ${q:+-s} -f --max-redirs 0 \ | |
167 | ${cacert:+--cacert "$cacert"} \ | |
168 | ${capath:+--capath "$capath"} \ | |
169 | ${insecure:+--insecure} \ | |
170 | -o "$f" "$u" || rc=$? | |
171 | ;; | |
172 | ||
173 | wget) | |
174 | vrun wget $q --max-redirect=0 \ | |
175 | ${cacert:+--ca-certificate="$cacert"} \ | |
176 | ${capath:+--ca-directory="$capath"} \ | |
177 | ${insecure:+--no-check-certificate} \ | |
178 | -O "$f" "$u" || rc=$? | |
179 | ;; | |
180 | ||
181 | lynx) | |
182 | test -z "$cacert" || vrun export SSL_CERT_FILE="$cacert" | |
183 | test -z "$capath" || vrun export SSL_CERT_DIR="$capath" | |
184 | # Check also stderr as lynx does not return != 0 on HTTP error | |
185 | vrun2 "$f" lynx -stderr -noredir -source "$u" || rc=$? | |
186 | ;; | |
187 | ||
188 | svn) | |
189 | vrun svn $q export \ | |
190 | --non-interactive --no-auth-cache \ | |
191 | ${cacert:+--config-option "servers:global:ssl-trust-default-ca=no"} \ | |
192 | ${cacert:+--config-option "servers:global:ssl-authority-files=$cacert"} \ | |
193 | ${insecure:+--trust-server-cert} \ | |
194 | "$u" "$f" || rc=$? | |
195 | ;; | |
196 | ||
197 | fetch) # FreeBSD | |
198 | vrun fetch $q --no-redirect \ | |
199 | ${cacert:+--ca-cert "$cacert"} \ | |
200 | ${capath:+--ca-path "$capath"} \ | |
201 | ${insecure:+--no-verify-hostname} \ | |
202 | -o "$f" "$u" || rc=$? | |
203 | ;; | |
204 | ||
205 | ftp) # OpenBSD | |
206 | vrun ftp \ | |
207 | ${cacert:+-S cafile="$cacert"} \ | |
208 | ${capath:+-S capath="$capath"} \ | |
209 | ${insecure:+-S dont} \ | |
210 | -o "$f" "$u" || rc=$? | |
211 | ;; | |
212 | ||
213 | *) error "$tool: unknown (internal error)" ;; | |
214 | esac | |
215 | return $rc | |
216 | } | |
217 | ||
218 | # Parse options | |
219 | smtctl=$SMARTCTL | |
220 | tool= | |
221 | url= | |
222 | q="-q" | |
223 | dryrun= | |
224 | cacert= | |
225 | capath= | |
226 | insecure= | |
227 | ||
228 | while true; do case $1 in | |
229 | -s) | |
230 | shift; test -n "$1" || usage | |
231 | smtctl=$1 ;; | |
232 | ||
233 | -t) | |
234 | shift | |
235 | case $1 in *\ *) usage ;; esac | |
236 | case " $os_dltools " in *\ $1\ *) ;; *) usage ;; esac | |
237 | tool=$1 ;; | |
238 | ||
239 | -u) | |
240 | shift; selecturl "$1" ;; | |
241 | ||
242 | -v) | |
243 | q= ;; | |
244 | ||
245 | --dryrun) | |
246 | dryrun=t ;; | |
247 | ||
248 | --cacert) | |
249 | shift; test -n "$1" || usage | |
250 | cacert=$1 ;; | |
251 | ||
252 | --capath) | |
253 | shift; test -n "$1" || usage | |
254 | capath=$1 ;; | |
255 | ||
256 | --insecure) | |
257 | insecure=t ;; | |
258 | ||
259 | -*) | |
260 | usage ;; | |
261 | ||
262 | *) | |
263 | break ;; | |
264 | esac; shift; done | |
265 | ||
266 | case $# in | |
267 | 0) DEST=$DRIVEDB ;; | |
268 | 1) DEST=$1 ;; | |
269 | *) usage ;; | |
270 | esac | |
271 | ||
272 | if [ -z "$tool" ]; then | |
273 | # Find download tool in PATH | |
274 | for t in $os_dltools; do | |
275 | if inpath "$t"; then | |
276 | tool=$t | |
277 | break | |
278 | fi | |
279 | done | |
280 | test -n "$tool" || error "found none of: $os_dltools" | |
7f0798ef GI |
281 | fi |
282 | ||
a86ec89e GI |
283 | test -n "$url" || selecturl "svn" |
284 | ||
285 | # Check option compatibility | |
286 | case "$tool:$url" in | |
287 | svn:http*://svn.code.sf.net*) ;; | |
288 | svn:*) error "'-t svn' requires '-u svn' or '-u svni'" ;; | |
289 | esac | |
290 | case "$tool:${capath:+set}" in | |
291 | svn:set) warning "'--capath' is ignored if '-t svn' is used" ;; | |
292 | esac | |
293 | case "${insecure:-f}:$url" in | |
294 | t:http:*) insecure= ;; | |
295 | ?:https:*) ;; | |
296 | *) error "'-u svni' requires '--insecure'" ;; | |
297 | esac | |
298 | case "$tool:$insecure" in | |
299 | lynx:t) warning "'--insecure' is ignored if '-t lynx' is used" ;; | |
300 | esac | |
301 | ||
7f0798ef | 302 | # Try possible branch first, then trunk |
a86ec89e GI |
303 | errmsg= |
304 | errmsg2= | |
7f0798ef | 305 | for location in "branches/$BRANCH" "trunk"; do |
a86ec89e GI |
306 | test -z "$errmsg" || errmsg2=$errmsg |
307 | vecho "Download from $location with $tool" | |
7f0798ef | 308 | |
a86ec89e GI |
309 | # Adjust URL |
310 | case $location in | |
311 | trunk) src=$url ;; | |
312 | *) src=`echo "$url" | sed "s,/trunk/,/$location/,"` ;; | |
313 | esac | |
314 | ||
315 | # Download | |
316 | test -n "$dryrun" || rm -f "$DEST.new" || exit 1 | |
317 | rc=0 | |
318 | download "$src" "$DEST.new" || rc=$? | |
319 | test -z "$dryrun" || continue | |
7f0798ef | 320 | |
a86ec89e GI |
321 | errmsg= |
322 | if [ $rc != 0 ]; then | |
323 | errmsg="download from $location failed ($tool: exit $rc)" | |
7f0798ef GI |
324 | continue |
325 | fi | |
a86ec89e GI |
326 | |
327 | # Check file contents | |
328 | case `sed 1q "$DEST.new"` in | |
329 | /*) ;; | |
330 | \<*) | |
331 | errmsg="download from $location failed (HTML error message)" | |
332 | continue ;; | |
333 | *) | |
334 | errmsg="download from $location failed (Unknown file contents)" | |
335 | continue ;; | |
336 | esac | |
337 | ||
338 | # Check file size | |
339 | size=`wc -c < "$DEST.new"` | |
340 | if [ "$size" -lt 10000 ]; then | |
341 | errmsg="download from $location failed (too small file size $size bytes)" | |
7f0798ef GI |
342 | continue |
343 | fi | |
a86ec89e GI |
344 | if [ "$size" -gt 1000000 ]; then |
345 | errmsg="download from $location failed (too large file size $size bytes)" | |
346 | break | |
347 | fi | |
7f0798ef GI |
348 | |
349 | break | |
350 | done | |
351 | ||
a86ec89e GI |
352 | test -z "$dryrun" || exit 0 |
353 | ||
7f0798ef GI |
354 | if [ -n "$errmsg" ]; then |
355 | rm -f "$DEST.new" | |
a86ec89e GI |
356 | test -z "$errmsg2" || echo "$myname: $errmsg2" >&2 |
357 | error "$errmsg" | |
7f0798ef GI |
358 | fi |
359 | ||
360 | # Adjust timestamp and permissions | |
361 | touch "$DEST.new" | |
362 | chmod 0644 "$DEST.new" | |
363 | ||
17d0b8d9 | 364 | <<<<<<< HEAD |
a86ec89e GI |
365 | if [ "$smtctl" != "-" ]; then |
366 | # Check syntax | |
367 | rm -f "$DEST.error" | |
368 | if "$smtctl" -B "$DEST.new" -P showall >/dev/null; then | |
369 | test -n "$q" || echo "$smtctl: syntax OK" | |
370 | else | |
371 | mv "$DEST.new" "$DEST.error" | |
372 | echo "$DEST.error: rejected by $smtctl, probably no longer compatible" >&2 | |
373 | exit 1 | |
374 | fi | |
17d0b8d9 | 375 | ======= |
7f0798ef GI |
376 | # Check syntax |
377 | rm -f "$DEST.error" | |
3d8ad6fa | 378 | if "$SMARTCTL" -B "$DEST.new" -P showall >/dev/null; then :; else |
7f0798ef GI |
379 | mv "$DEST.new" "$DEST.error" |
380 | echo "$DEST.error: rejected by $SMARTCTL, probably no longer compatible" >&2 | |
381 | exit 1 | |
17d0b8d9 | 382 | >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74 |
7f0798ef GI |
383 | fi |
384 | ||
a86ec89e | 385 | # Keep old file if identical, ignore missing Id keyword expansion in new file |
7f0798ef GI |
386 | rm -f "$DEST.lastcheck" |
387 | if [ -f "$DEST" ]; then | |
a86ec89e GI |
388 | if cmp "$DEST" "$DEST.new" >/dev/null 2>/dev/null \ |
389 | || cat "$DEST" | sed 's|\$''Id''[^$]*\$|$''Id''$|' \ | |
390 | | cmp - "$DEST.new" >/dev/null 2>/dev/null; then | |
7f0798ef GI |
391 | rm -f "$DEST.new" |
392 | touch "$DEST.lastcheck" | |
393 | echo "$DEST is already up to date" | |
394 | exit 0 | |
395 | fi | |
396 | mv "$DEST" "$DEST.old" | |
397 | fi | |
398 | ||
399 | mv "$DEST.new" "$DEST" | |
400 | ||
401 | echo "$DEST updated from $location" | |
402 |