build: Optionally provide file checksums in package metadata
[openwrt/staging/chunkeey.git] / package / base-files / files / sbin / pkg_check
1 #!/bin/sh
2 #
3 # Package checksums checking script
4 # (C) 2018 CZ.NIC, z.s.p.o.
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 ERRFATAL="no"
21 QUIET="yes"
22 MISSING=""
23 SUMMARY=""
24 NL="
25 "
26
27 # Arguments parsing
28 while expr "x$1" : "x-" > /dev/null; do
29 if [ "x$1" = "x-s" ]; then
30 ERRFATAL="yes"
31 shift
32 elif [ "x$1" = "x-v" ]; then
33 QUIET=" no"
34 shift
35 else
36 echo "Usage: $(basename $0) [-s] [-v] [pkg1 pkg2 ...]"
37 echo
38 echo " -s Stop on first change"
39 echo " -v Verbose"
40 if [ "x$1" = "x-h" ]; then
41 exit 0
42 else
43 echo
44 echo "ERROR: Unknown option '$1'"
45 exit 1
46 fi
47 fi
48 done
49
50 # Check all packages by default
51 if [ -z "$1" ]; then
52 set $(cd /usr/lib/opkg/info/; for i in *.files-sha256sum; do basename $i .files-sha256sum; done)
53 fi
54
55 # Iterate over packages
56 while [ "$1" ]; do
57 if [ \! -f "/usr/lib/opkg/info/$1.files-sha256sum" ]; then
58 if [ "$ERRFATAL" = no ]; then
59 echo " * No checksums for $1 - skipping"
60 echo
61 else
62 echo " * No checksums for $1 - exiting"
63 exit 1
64 fi
65 if [ -z "$MISSING" ]; then
66 MISSING="$1"
67 else
68 MISSING="$MISSING, $1"
69 fi
70 shift
71 continue
72 fi
73 [ $QUIET = yes ] || echo " * Checking package $1:"
74 ERR=""
75 CHECK="`sha256sum -c /usr/lib/opkg/info/$1.files-sha256sum 2> /dev/null`"
76
77 # Are the changed files config files?
78 if [ $? -ne 0 ] && [ "`cat "/usr/lib/opkg/info/$1.files-sha256sum"`" ]; then
79 NEWCHECK="`echo "$CHECK" | grep '^.*: OK$'`"
80 for i in `echo "$CHECK" | sed -n 's|^\(.*\): FAILED$|\1|p'`; do
81 if [ "`grep "^$i\$" "/usr/lib/opkg/info/$1.conffiles" 2> /dev/null`" ] || \
82 [ "`echo "$i" | grep "^/etc/uci-defaults/"`" ]; then
83 NEWCHECK="${NEWCHECK}${NL}${i}: CONFIGURED"
84 else
85 NEWCHECK="${NEWCHECK}${NL}${i}: FAILED"
86 ERR="y"
87 fi
88 done
89 CHECK="$NEWCHECK"
90 fi
91
92 # Do we have changed files or not?
93 if [ -z "$ERR" ]; then
94 [ $QUIET = yes ] || [ -z "`cat "/usr/lib/opkg/info/$1.files-sha256sum"`" ] || echo "$CHECK" | sed 's|^| - |'
95 [ $QUIET = yes ] || echo " * Package $1 is ok"
96 [ $QUIET = yes ] || echo
97 else
98 if [ $QUIET = yes ]; then
99 echo " * Changes found in package $1:"
100 echo "$CHECK" | sed -n 's|^\(.*:[[:blank:]]*FAILED\)$| - \1|p'
101 else
102 echo "$CHECK" | sed 's|^| - |'
103 echo " * Changes found in package $1!"
104 fi
105 if [ "$ERRFATAL" = yes ]; then
106 echo
107 echo "Exiting on first change found!"
108 exit 1
109 fi
110 for i in `echo "$CHECK" | sed -n 's|^\(.*\): FAILED$|\1|p'`; do
111 SUMMARY="${SUMMARY}${NL} - $1: $i"
112 done
113 echo
114 fi
115 shift
116 done
117
118 # If there are changed files, report them
119 if [ "$SUMMARY" ]; then
120 echo "Some packages contain changed files!"
121 echo "Maybe something worth looking into?"
122 echo "Here is the list of packages and changed files:"
123 echo "$SUMMARY"
124 fi
125 if [ "$MISSING" ]; then
126 echo "Following packages are missing checksums: $MISSING"
127 fi
128 if [ "$MISSING" ] || [ "$SUMMARY" ]; then
129 exit 1
130 fi