22108
|
1 |
#!/bin/bash
|
|
2 |
|
|
3 |
# This script runs after the %install scriptlet of the Desktop spec files,
|
|
4 |
# The purpose of the script is post-processing files before packaging.
|
|
5 |
#
|
|
6 |
# Currently it does two things:
|
|
7 |
#
|
|
8 |
# 1) delete CDDL header from text files, if found
|
|
9 |
# 2) validate smf manifests
|
|
10 |
#
|
|
11 |
# command line arguments:
|
|
12 |
#
|
|
13 |
# spec-post-install [DIR|FILE]...
|
|
14 |
#
|
|
15 |
# Processes a single file or all files in a directory, recursively
|
|
16 |
#
|
|
17 |
# returns 0 on success or 1 on failure (breaks the build)
|
|
18 |
#
|
|
19 |
# requires file/gnu-coreutils
|
|
20 |
|
|
21 |
SED=/usr/xpg4/bin/sed
|
|
22 |
TR=/usr/xpg4/bin/tr
|
|
23 |
NL=/usr/xpg4/bin/nl
|
|
24 |
STAT=/usr/bin/stat
|
|
25 |
|
|
26 |
# deletes the CDDL header from a text file
|
|
27 |
# returns 0 on success, 1 on error
|
|
28 |
delete_cddl() {
|
|
29 |
mode=$(${STAT} -c '%a' "$1")
|
|
30 |
chmod +w "$1"
|
|
31 |
tmpfile=$(mktemp)
|
|
32 |
${SED} -e '/CDDL HEADER START/,/CDDL HEADER END/d' < "$1" > $tmpfile \
|
|
33 |
|| return 1
|
|
34 |
mv $tmpfile "$1" || return 1
|
|
35 |
chmod $mode "$1"
|
|
36 |
return 0
|
|
37 |
}
|
|
38 |
|
|
39 |
# return 0 is arg is an integer number
|
|
40 |
is_int() {
|
|
41 |
echo "$1" | grep '^[1-9][0-9]*$' > /dev/null
|
|
42 |
}
|
|
43 |
|
|
44 |
# verifies if a given file needs the CDDL header removed
|
|
45 |
has_cddl() {
|
22190
|
46 |
start_line=$(${NL} -ba "$1" | ${TR} -cd 'a-z0-9A-Z \n' | \
|
|
47 |
grep '^ *[0-9]*[ ]*CDDL HEADER START *$' | awk '{print $1}')
|
|
48 |
end_line=$(${NL} -ba "$1" | ${TR} -cd 'a-z0-9A-Z \n' | \
|
|
49 |
grep '^ *[0-9]*[ ]*CDDL HEADER END *$' | awk '{print $1}')
|
22108
|
50 |
is_int "$start_line" || return 1
|
|
51 |
is_int "$end_line" || return 1
|
|
52 |
if [ $start_line -gt $end_line ]; then
|
|
53 |
return 1
|
|
54 |
fi
|
22190
|
55 |
if [ $start_line -gt 15 ]; then
|
|
56 |
echo "WARNING: CDDL-like header starts after the 15th line"
|
22108
|
57 |
return 1
|
|
58 |
fi
|
|
59 |
diff=$(($end_line - $start_line))
|
22190
|
60 |
if [ $diff != 17 -a $diff != 18 -a $diff != 11 -a $diff != 12 ]; then
|
|
61 |
echo "WARNING: $1: unrecognised CDDL-like header, $diff lines long"
|
22108
|
62 |
return 1
|
|
63 |
fi
|
|
64 |
}
|
|
65 |
|
|
66 |
# runs svccfg validate on arg
|
|
67 |
validate_manifest() {
|
|
68 |
svccfg validate "$1" || return 1
|
|
69 |
|
|
70 |
return 0
|
|
71 |
}
|
|
72 |
|
|
73 |
# processes a single file
|
|
74 |
process_file() {
|
|
75 |
grep 'CDDL HEADER' "$1" > /dev/null && has_cddl "$1" && {
|
|
76 |
echo "Deleting CDDL header from $1"
|
|
77 |
delete_cddl "$1" || return 1
|
|
78 |
}
|
|
79 |
|
|
80 |
echo "$1" | egrep '/(var|lib)/svc/manifest/' > /dev/null && {
|
|
81 |
echo "Validating SMF manifest $1"
|
|
82 |
validate_manifest "$1" || return 1
|
|
83 |
}
|
|
84 |
|
|
85 |
return 0
|
|
86 |
}
|
|
87 |
|
|
88 |
# processes all files in a directory, recursively
|
|
89 |
process_dir() {
|
|
90 |
files=$(find "$1" -type f -print)
|
|
91 |
for file in $files; do
|
|
92 |
process_file "$file" || return 1
|
|
93 |
done
|
|
94 |
|
|
95 |
return 0
|
|
96 |
}
|
|
97 |
|
|
98 |
for arg in "${@}"; do
|
|
99 |
echo "Post-processing $arg"
|
|
100 |
if [ -d "$arg" ]; then
|
|
101 |
process_dir "$arg"
|
|
102 |
elif [ -f "$arg" ]; then
|
|
103 |
process_file "$arg"
|
|
104 |
else
|
|
105 |
echo "$0: $arg not found" 1>&2
|
|
106 |
exit 1
|
|
107 |
fi
|
|
108 |
done
|