forked from MiXXiM/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeb-chklist
193 lines (163 loc) · 3.58 KB
/
deb-chklist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
#cito M:755 O:0 G:0 T:/usr/local/bin/deb-chklist
#------------------------------------------------------------------------------
# Project Name - ShellProjects/source/deb-chklist
# Started On - Mon 26 Jun 22:53:12 BST 2023
# Last Change - Tue 27 Jun 11:28:55 BST 2023
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# For Debian- and Ubuntu-based distributions, scan '*.list' files for errors.
#
# Features:
#
# N/A
#
# Bugs:
#
# N/A
#
# Dependencies:
#
# bash (>= 4.1)
#------------------------------------------------------------------------------
CurVer='2023-06-27'
Progrm=${0##*/}
Usage() {
read -d '' <<-EOF
Usage: $Progrm [OPTS] [FILE [FILE] ...]
-h, --help - Display this help information.
-v, --version - Output the version datestamp.
-C, --nocolor - Disable ANSI color escape sequences.
The use of '--' to ignore proceeding options is supported.
EOF
printf '%s' "$REPLY"
}
Err() {
printf 'Err: %s\n' "$2" 1>&2
(( $1 > 0 )) && exit $1
}
NoColor=
BRed=$'\e[91m'
BYellow=$'\e[93m'
Reset=$'\e[0m'
Skip=
Args=()
while [[ -n $1 ]]; do
case $1 in
--)
Args+=(--)
Skip='True' ;;
-[^-]*)
if [[ $Skip == True ]]; then
Args+=("$1")
else
Str=${1#-}
Len=${#Str}
for (( Index = 0; Index < Len; Index++ )); {
Args+=(-"${Str:Index:1}")
}
fi ;;
*)
Args+=("$1") ;;
esac
shift
done
set -- "${Args[@]}"
while [[ -n $1 ]]; do
case $1 in
--)
break ;;
--help|-h)
Usage; exit 0 ;;
--version|-v)
printf '%s\n' "$CurVer"; exit 0 ;;
--nocolor|-C)
BRed=
BYellow=
Reset= ;;
-*)
Err 1 'Incorrect option(s) specified.' ;;
*)
break ;;
esac
shift
done
OneFile=
Space=' '
if (( $# > 0 )); then
Files=("$@")
if (( ${#Files[@]} == 1 )); then
OneFile='True'
Space=
fi
else
Files=(/var/lib/dpkg/info/*.list)
fi
FileErr() {
local FileArg=
if [[ $1 == -f ]]; then
FileArg='True'
shift
fi
if [[ $FileArg == True ]]; then
printf -v ErrorStr '%s%sFile%s %s.' "$Space"\
"$BRed" "$Reset" "${1,}" 1>&2
else
printf -v ErrorStr '%sLine %s%d%s %s.' "$Space"\
"$BRed" $LineNr "$Reset" "${1,}" 1>&2
fi
ErrorArr+=("$ErrorStr")
(( ErrCount++ ))
(( ErrCountThisFile++ ))
}
ErrCount=0
FileCount=0
for File in "${Files[@]}"; {
[[ -f $File ]] || continue
ErrorArr=()
ErrCountThisFile=0
(( FileCount++ ))
Stat=$(stat --printf='%a %u %g\n' "$File")
read Mode Owner Group <<< "$Stat"
if [[ $Mode != 644 ]]; then
FileErr -f "mode $Mode unexpected"
fi
(( Owner != 0 )) && FileErr -f "owner $Owner unexpected"
(( Group != 0 )) && FileErr -f "group $Group unexpected"
LineNr=0
while read; do
(( LineNr++ ))
if [[ -z $REPLY ]]; then
FileErr 'empty'
elif [[ $REPLY == +([[:space:]]) ]]; then
FileErr 'only spaces'
elif [[ $REPLY == *//* ]]; then
FileErr 'invalid path'
elif [[ $REPLY != /* ]]; then
FileErr 'relative path'
fi
done < "$File"
if [[ -n "${REPLY:${#REPLY} - 1}" ]]; then
FileErr -f 'missing final newline'
fi
if (( ErrCountThisFile > 0 )); then
if [[ $OneFile != True ]]; then
printf "* %s%s%s\n" "$BYellow" "${File##*/}" "$Reset"
fi
for Error in "${ErrorArr[@]}"; {
echo "$Error"
}
fi
}
(( ErrCount > 0 )) && printf '\n'
if (( FileCount > 1 )); then
printf 'Scanned %s%d%s file(s).\n'\
"$BYellow" $FileCount "$Reset"
fi
if (( ErrCount > 0 )); then
printf 'Found %s%d%s error(s).\n' "$BRed" $ErrCount "$Reset"
exit 1
else
printf 'No errors found.\n'
fi