Line 0
Link Here
|
|
|
1 |
#!/bin/bash |
2 |
# Copyright 1999-2007 Gentoo Foundation |
3 |
# Distributed under the terms of the GNU General Public License v2 |
4 |
|
5 |
source "${PORTAGE_BIN_PATH:-/usr/lib/portage/bin}"/isolated-functions.sh |
6 |
|
7 |
if [[ -z $1 ]] ; then |
8 |
die "${0##*/}: at least one argument needed" |
9 |
exit 1 |
10 |
fi |
11 |
|
12 |
# setup compression stuff |
13 |
PORTAGE_COMPRESS=${PORTAGE_COMPRESS-bzip2} |
14 |
[[ -z ${PORTAGE_COMPRESS} ]] && exit 0 |
15 |
|
16 |
if [[ ${PORTAGE_COMPRESS_FLAGS+set} != "set" ]] ; then |
17 |
case ${PORTAGE_COMPRESS} in |
18 |
bzip2|gzip) PORTAGE_COMPRESS_FLAGS="-9";; |
19 |
esac |
20 |
fi |
21 |
|
22 |
# decompress_args(suffix, binary) |
23 |
# - suffix: the compression suffix to work with |
24 |
# - binary: the program to execute that'll compress/decompress |
25 |
# new_args: global array used to return revised arguments |
26 |
decompress_args() { |
27 |
local suffix=$1 binary=$2 |
28 |
shift 2 |
29 |
|
30 |
# Initialize the global new_args array. |
31 |
new_args=() |
32 |
declare -a decompress_args=() |
33 |
local x i=0 decompress_count=0 |
34 |
for x in "$@" ; do |
35 |
if [[ ${x%$suffix} = $x ]] ; then |
36 |
new_args[$i]=$x |
37 |
else |
38 |
new_args[$i]=${x%$suffix} |
39 |
decompress_args[$decompress_count]=$x |
40 |
((decompress_count++)) |
41 |
fi |
42 |
((i++)) |
43 |
done |
44 |
|
45 |
if [ $decompress_count -gt 0 ] ; then |
46 |
${binary} "${decompress_args[@]}" |
47 |
if [ $? -ne 0 ] ; then |
48 |
# Apparently decompression failed for one or more files, so |
49 |
# drop those since we don't want to compress them twice. |
50 |
new_args=() |
51 |
local x i=0 |
52 |
for x in "$@" ; do |
53 |
if [[ ${x%$suffix} = $x ]] ; then |
54 |
new_args[$i]=$x |
55 |
((i++)) |
56 |
elif [[ -f ${x%$suffix} ]] ; then |
57 |
new_args[$i]=${x%$suffix} |
58 |
((i++)) |
59 |
else |
60 |
# Apparently decompression failed for this one, so drop |
61 |
# it since we don't want to compress it twice. |
62 |
true |
63 |
fi |
64 |
done |
65 |
fi |
66 |
fi |
67 |
} |
68 |
|
69 |
case $1 in |
70 |
--suffix) |
71 |
[[ -n $2 ]] && vecho "${0##*/}: --suffix takes no additional arguments" 1>&2 |
72 |
|
73 |
if [[ ! -e ${T}/.ecompress.suffix ]] ; then |
74 |
set -e |
75 |
tmpdir="${T}"/.ecompress$$.${RANDOM} |
76 |
mkdir "${tmpdir}" |
77 |
cd "${tmpdir}" |
78 |
# we have to fill the file enough so that there is something |
79 |
# to compress as some programs will refuse to do compression |
80 |
# if it cannot actually compress the file |
81 |
echo {0..1000} > compressme |
82 |
${PORTAGE_COMPRESS} ${PORTAGE_COMPRESS_FLAGS} compressme > /dev/null |
83 |
suffix=$(ls compressme*) |
84 |
suffix=${suffix#compressme} |
85 |
cd / |
86 |
rm -rf "${tmpdir}" |
87 |
echo "${suffix}" > "${T}/.ecompress.suffix" |
88 |
fi |
89 |
cat "${T}/.ecompress.suffix" |
90 |
;; |
91 |
--bin) |
92 |
[[ -n $2 ]] && vecho "${0##*/}: --bin takes no additional arguments" 1>&2 |
93 |
|
94 |
echo "${PORTAGE_COMPRESS} ${PORTAGE_COMPRESS_FLAGS}" |
95 |
;; |
96 |
--queue) |
97 |
shift |
98 |
touch "${@/%/.ecompress.file}" |
99 |
ret=$? |
100 |
[[ $ret -ne 0 ]] && die "$0 failed" |
101 |
exit $ret |
102 |
;; |
103 |
--dequeue) |
104 |
[[ -n $2 ]] && vecho "${0##*/}: --dequeue takes no additional arguments" 1>&2 |
105 |
find "${D}" -name '*.ecompress.file' -print0 \ |
106 |
| sed -e 's:\.ecompress\.file::g' \ |
107 |
| ${XARGS} -0 ecompress |
108 |
find "${D}" -name '*.ecompress.file' -print0 | ${XARGS} -0 rm -f |
109 |
;; |
110 |
--*) |
111 |
die "${0##*/}: unknown arguments '$*'" |
112 |
exit 1 |
113 |
;; |
114 |
*) |
115 |
# Since dodoc calls ecompress on files that are already compressed, |
116 |
# perform decompression here (similar to ecompressdir behavior). |
117 |
decompress_args ".Z" "gunzip -f" "$@" |
118 |
set -- "${new_args[@]}" |
119 |
decompress_args ".gz" "gunzip -f" "$@" |
120 |
set -- "${new_args[@]}" |
121 |
decompress_args ".bz2" "bunzip2 -f" "$@" |
122 |
set -- "${new_args[@]}" |
123 |
|
124 |
mask_ext_re="" |
125 |
set -f |
126 |
for x in $PORTAGE_COMPRESS_EXCLUDE_SUFFIXES ; do |
127 |
mask_ext_re+="|$x" |
128 |
done |
129 |
set +f |
130 |
mask_ext_re="^(${mask_ext_re:1})\$" |
131 |
declare -a filtered_args=() |
132 |
i=0 |
133 |
for x in "$@" ; do |
134 |
[[ ${x##*.} =~ $mask_ext_re ]] && continue |
135 |
filtered_args[$i]=$x |
136 |
((i++)) |
137 |
done |
138 |
[ $i -eq 0 ] && exit 0 |
139 |
set -- "${filtered_args[@]}" |
140 |
|
141 |
# If a compressed version of the file already exists, simply |
142 |
# delete it so that the compressor doesn't whine (bzip2 will |
143 |
# complain and skip, gzip will prompt for input) |
144 |
suffix=$(ecompress --suffix) |
145 |
[[ -n ${suffix} ]] && echo -n "${@/%/${suffix}$'\001'}" | \ |
146 |
tr '\001' '\000' | ${XARGS} -0 rm -f |
147 |
# Finally, let's actually do some real work |
148 |
"${PORTAGE_COMPRESS}" ${PORTAGE_COMPRESS_FLAGS} "$@" |
149 |
ret=$? |
150 |
[[ $ret -ne 0 ]] && die "$0 failed" |
151 |
exit $ret |
152 |
;; |
153 |
esac |