20 |
20 |
# to determine the file type
|
21 |
21 |
function get_type
|
22 |
22 |
{
|
|
23 |
local command
|
|
24 |
|
23 |
25 |
if [ "$NO_DECOMPRESS" = "yes" ]; then
|
24 |
26 |
echo "application/octet-stream"
|
25 |
27 |
else
|
26 |
|
command=$1
|
|
28 |
command=("$@")
|
27 |
29 |
|
28 |
|
( $command | head -n 1024 | file -b --mime-type - ) 2>/dev/null
|
|
30 |
( "${command[@]}" | head -n 1024 | file -b --mime-type - ) 2>/dev/null
|
29 |
31 |
fi
|
30 |
32 |
}
|
31 |
33 |
|
32 |
34 |
# Gets the command needed to decompress an stream.
|
33 |
35 |
function get_decompressor
|
34 |
36 |
{
|
35 |
|
type=$1
|
|
37 |
local type
|
|
38 |
|
|
39 |
type="$1"
|
36 |
40 |
|
37 |
41 |
case "$type" in
|
38 |
42 |
"application/x-gzip"|"application/gzip")
|
... | ... | |
52 |
56 |
# - for stdout.
|
53 |
57 |
function decompress
|
54 |
58 |
{
|
|
59 |
local command to
|
|
60 |
|
55 |
61 |
command="$1"
|
56 |
62 |
to="$2"
|
57 |
63 |
|
... | ... | |
65 |
71 |
# Function called to hash a stream. First parameter is the algorithm name.
|
66 |
72 |
function hasher
|
67 |
73 |
{
|
|
74 |
local algorithm
|
|
75 |
algorithm="$1"
|
|
76 |
|
68 |
77 |
if [ -n "$1" ]; then
|
69 |
|
openssl dgst -$1 | awk '{print $NF}' > $HASH_FILE
|
|
78 |
openssl dgst "-${algorithm}" | awk '{print $NF}' > $HASH_FILE
|
70 |
79 |
else
|
71 |
80 |
# Needs something consuming stdin or the pipe will break
|
72 |
81 |
cat >/dev/null
|
... | ... | |
76 |
85 |
# Unarchives a tar or a zip a file to a directpry with the same name.
|
77 |
86 |
function unarchive
|
78 |
87 |
{
|
|
88 |
local TO file_type tmp IN OUT command
|
|
89 |
|
79 |
90 |
TO="$1"
|
80 |
91 |
|
81 |
|
file_type=$(get_type "cat $TO")
|
|
92 |
file_type=$(get_type cat "$TO")
|
82 |
93 |
|
83 |
94 |
tmp="$TO"
|
84 |
95 |
|
... | ... | |
92 |
103 |
|
93 |
104 |
case "$file_type" in
|
94 |
105 |
"application/x-tar")
|
95 |
|
command="tar -xf $IN -C $OUT"
|
|
106 |
command=(tar -xf "$IN" -C "$OUT")
|
96 |
107 |
;;
|
97 |
108 |
"application/zip")
|
98 |
|
command="unzip -d $OUT $IN"
|
|
109 |
command=(unzip -d "$OUT" "$IN")
|
99 |
110 |
;;
|
100 |
111 |
*)
|
101 |
|
command=""
|
|
112 |
command=()
|
102 |
113 |
;;
|
103 |
114 |
esac
|
104 |
115 |
|
105 |
|
if [ -n "$command" ]; then
|
|
116 |
if [ -n "${command[*]}" ]; then
|
106 |
117 |
mv "$OUT" "$IN"
|
107 |
118 |
mkdir "$OUT"
|
108 |
119 |
|
109 |
|
$command
|
|
120 |
"${command[@]}"
|
110 |
121 |
|
111 |
122 |
if [ "$?" != "0" ]; then
|
112 |
123 |
echo "Error uncompressing archive" >&2
|
... | ... | |
168 |
179 |
# -L to follow redirects
|
169 |
180 |
# -sS to hide output except on failure
|
170 |
181 |
# --limit_rate to limit the bw
|
171 |
|
curl_args="-sS -k -L $FROM"
|
|
182 |
curl_args=(-sS -k -L "$FROM")
|
172 |
183 |
|
173 |
184 |
if [ -n "$LIMIT_RATE" ]; then
|
174 |
|
curl_args="--limit-rate $LIMIT_RATE $curl_args"
|
|
185 |
curl_args=(--limit-rate "$LIMIT_RATE" "${curl_args[@]}")
|
175 |
186 |
fi
|
176 |
187 |
|
177 |
|
command="curl $curl_args"
|
|
188 |
command=(curl "${curl_args[@]}")
|
178 |
189 |
;;
|
179 |
190 |
*)
|
180 |
|
if [ ! -r $FROM ]; then
|
|
191 |
if [ ! -r "$FROM" ]; then
|
181 |
192 |
echo "Cannot read from $FROM" >&2
|
182 |
193 |
exit -1
|
183 |
194 |
fi
|
184 |
|
command="cat $FROM"
|
|
195 |
command=(cat "$FROM")
|
185 |
196 |
;;
|
186 |
197 |
esac
|
187 |
198 |
|
188 |
|
file_type=$(get_type "$command")
|
|
199 |
file_type=$(get_type "${command[@]}")
|
189 |
200 |
decompressor=$(get_decompressor "$file_type")
|
190 |
201 |
|
191 |
|
$command | tee >( hasher $HASH_TYPE) | decompress "$decompressor" "$TO"
|
|
202 |
"${command[@]}" | tee >( hasher "$HASH_TYPE") | decompress "$decompressor" "$TO"
|
192 |
203 |
|
193 |
204 |
if [ "$?" != "0" ]; then
|
194 |
205 |
echo "Error copying" >&2
|
... | ... | |
197 |
208 |
|
198 |
209 |
if [ -n "$HASH_TYPE" ]; then
|
199 |
210 |
HASH_RESULT=$( cat $HASH_FILE)
|
200 |
|
rm $HASH_FILE
|
|
211 |
rm "$HASH_FILE"
|
201 |
212 |
if [ "$HASH_RESULT" != "$HASH" ]; then
|
202 |
213 |
echo "Hash does not match" >&2
|
203 |
214 |
exit -1
|