-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkcheck
executable file
·272 lines (243 loc) · 7.79 KB
/
mkcheck
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# Copyright (c) 2015, Mattia Penati <mattia.penati@gmail.com>
# Copyright (c) 2020-2024, Pasquale Claudio Africa <pasqualeclaudio.africa@polimi.it>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# the following environment variables can change the behaviour of the command
#
# mkUseColor = [auto|yes|no] (default: auto)
# If set to 'no' than the command does not use the colors for the output.
# mkDebug = [yes|no] (default: no)
# If set to 'yes' then bash print all the commands.
# checking if the environment is setup correctly (variable mkPrefix)
if test -z "$mkPrefix"; then
echo "The environment is not set properly, 'mkPrefix' variable is not set" >&2
exit 1
fi
if test ! -d $mkPrefix; then
echo "Variable 'mkPrefix' is not pointing to an existing directory" >&2
echo " mkPrefix='$mkPrefix'" >&2
exit 1
fi
if test ! -r $mkPrefix/etc/mk.config; then
echo "Unable to read the configuration file '$mkPrefix/etc/mk.config'" >&2
exit 1
fi
# Check the environment variables used by mkpkg command
[[ -z "$mkUseColor" ]] && mkUseColor=auto
[[ -z "$mkDebug" ]] && mkDebug=no
if [[ $mkUseColor = "auto" ]]; then
mkUseColor=$([[ $(tput colors 2> /dev/null) -ge 8 ]] && echo yes || echo no)
fi
if [[ ! "$mkUseColor" = "no" && ! "$mkUseColor" = "yes" ]]; then
echo "Invalid value '$mkUseColor' for variable 'mkUseColor'" >&2
exit 1
fi
if [[ ! "$mkDebug" = "no" && ! "$mkDebug" = "yes" ]]; then
echo "Invalid value '$mkDebug' for variable 'mkDebug'" >&2
exit 1
fi
# defined the variables and the function used for the output
# =============================================================================
# colors
unset RESET BOLD BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
if [[ "$mkUseColor" != "no" && $(tput colors 2> /dev/null) -ge 8 ]]; then
if tput setaf 0 &>/dev/null; then
RESET="$(tput sgr0)"
BOLD="$(tput bold)"
BLACK="$(tput setaf 0)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
WHITE="$(tput setaf 7)"
else
RESET="\e[0m"
BOLD="\e[1m"
BLACK="\e[30m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"
WHITE="\e[37m"
fi
fi
readonly RESET BOLD BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
# error <msg>
# Print an error message to the standard error stream
error() {
echo -e "${BOLD}${RED}>> Error${RESET}${BOLD} $1${RESET}" >&2
}
# warning <msg>
# Print a warning message to the standard error stream
warning() {
echo -e "${BOLD}${YELLOW}>> Warning${RESET}${BOLD} $1${RESET}" >&2
}
# message <msg>
# Print a message to the standard output stream
message() {
echo -e "${BOLD}>> $1${RESET}"
}
# plain <msg>
# Print a plain text to the standard output stream
plain() {
echo -e " ${RESET}$1${RESET}"
}
# parse the command line arguments
# =============================================================================
# usage
# Print the command help.
usage() {
# redirect to the correct file
echo "Check the installation of a package."
echo ""
echo "Usage:"
echo " mkcheck <dir>"
echo " mkcheck -h | --help"
echo ""
echo "Options:"
echo " -h | --help Show this help message"
echo ""
return 0
}
# default options
pkgPrefix=
# parse the command line arguments
while true; do
case "$1" in
-h|--help) usage; exit 0;;
-*) error "Unknown option '$1'"; exit 1;;
*) break;;
esac
shift
done
pkgPrefix="$1"; shift
# there are some unkown arguments
if [[ -n "$@" ]]; then
error "Unknown arguments '$@'"
exit 1
fi
# check the given directory
# =============================================================================
if [[ -z "$pkgPrefix" ]]; then
pkgPrefix=$(pwd)
fi
# Run through all files
check_rpath() {
rpath=$("$mkPrefix"/bin/patchelf --print-rpath "$1")
if [[ -n "$rpath" && ! "$rpath" = "\$ORIGIN" ]]; then
message "Not empty RPATH for '$1'"
plain "$rpath"
fi
}
check_interpreter() {
if [[ -n "$mkToolchain" ]]; then
interpreter=$("$mkPrefix"/bin/patchelf --print-interpreter "$1")
if [[ ! $interpreter = $mkToolchainPrefix/* ]]; then
message "Invalid dynamic loader for '$1'"
plain "$interpreter"
fi
fi
}
check_shebang() {
shebang=$(head -n1 "$1")
prefix=${shebang:0:2}
shebang=${shebang:2}
if [[ "$prefix" = "#!" ]]; then
shebang="$(echo -e "$shebang" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
if [[ ! $shebang = "$mkPrefix"/* && ! $shebang = "/bin/bash" && ! $shebang = "/bin/sh" ]]; then
message "Invalid interpreter for '$1'"
plain "$shebang"
fi
fi
}
find "$pkgPrefix" -type f -print0 | while IFS= read -r -d '' file; do
type=none
if [[ -n "$(file -b "$file" | grep "executable" | grep "ELF" | grep "dynamically")" ]]; then
type=executable
elif [[ -n "$(file -b "$file" | grep "shared object" | grep "ELF")" ]]; then
type=library
elif [[ -n "$(file -b "$file" | grep "text executable")" ]]; then
type=script
fi
case $type in
library) check_rpath $file;;
executable) check_rpath $file
check_interpreter $file;;
script) check_shebang $file;;
none)
;;
esac
done
# libraries
if [[ -d "$pkgPrefix"/lib64 ]]; then
message "Invalid libraries directory"
plain "$pkgPrefix/lib64"
fi
# checking the documentations location
manDir=$(find "$pkgPrefix" -type d -name man)
manDir=$(readlink -f "$manDir")
if [[ -n "$manDir" ]]; then
if [[ ! "$manDir" = $(readlink -f "$pkgPrefix/share/man") ]]; then
message "Man pages are not in the correct location"
plain "$manDir"
fi
for man in $(ls "$manDir"); do
for page in $(ls "$manDir"/$man); do
if [[ ! $page = *.gz ]]; then
message "Uncompressed man page"
plain "$manDir/$man/$page"
fi
done
done
fi
infoDir=$(find "$pkgPrefix" -type d -name info)
infoDir=$(readlink -f "$infoDir")
if [[ -n "$infoDir" ]]; then
if [[ ! "$infoDir" = $(readlink -f "$pkgPrefix/share/info") ]]; then
message "Info pages are not in the correct location"
plain "$infoDir"
fi
for page in $(ls "$infoDir"); do
if [[ ! $page = "dir" && ! $page = *.gz ]]; then
message "Uncompressed info page"
plain "$infoDir/$page"
fi
done
fi
docDir=$(find "$pkgPrefix" -type d -name doc)
if [[ -n "$docDir" ]]; then
message "Useless documentations directory"
plain "$docDir"
fi
# checking localization
localeDir=$(find "$pkgPrefix" -type d -name locale)
if [[ -n "$localeDir" ]]; then
message "Found a possible localization directory"
plain "$localeDir"
fi
exit 0