1 |
blino |
576 |
#!/bin/bash |
2 |
|
|
# lzme re-compress gzip, zip, bzip2 ... files into xz |
3 |
|
|
#============================================================================== |
4 |
|
|
# Copyright (C) 1999-2002 MandrakeSoft (tvignaud@mandrakesoft.com) |
5 |
|
|
# By Thierry Vignaud <tvignaud@mandrakesoft.com> |
6 |
|
|
# |
7 |
|
|
# This program is free software; you can redistribute it and/or modify |
8 |
|
|
# it under the terms of the GNU General Public License as published by |
9 |
|
|
# the Free Software Foundation; either version 2, or (at your option) |
10 |
|
|
# any later version. |
11 |
|
|
# |
12 |
|
|
# This program is distributed in the hope that it will be useful, |
13 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
# GNU General Public License for more details. |
16 |
|
|
# |
17 |
|
|
# You should have received a copy of the GNU General Public License |
18 |
|
|
# along with this program; if not, write to the Free Software |
19 |
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
20 |
|
|
# |
21 |
|
|
# The GNU General Public License can be read at |
22 |
|
|
# http://www.fsf.org/copyleft/gpl.html |
23 |
|
|
#============================================================================== |
24 |
|
|
# |
25 |
|
|
# Know bugs: |
26 |
|
|
# ---------- |
27 |
|
|
# - bash getopt isn't gnu style aware, ie cmd opt1 file1 file2 opt2 |
28 |
|
|
# will result in ignoring opt2 option |
29 |
|
|
# |
30 |
|
|
#============================================================================== |
31 |
|
|
# |
32 |
|
|
# Changelog: |
33 |
|
|
# ---------- |
34 |
|
|
# v1.0: original release |
35 |
|
|
# v1.1: fix space usage (use pipe rather than temp file) |
36 |
|
|
# v1.2: keep source file, not bz2 one if eof while decompressing |
37 |
|
|
# v1.3: reduce used cpu time (decompressing only one time; |
38 |
|
|
# source crc error 're detected through PIPESTATUS) |
39 |
|
|
# v1.4: add zip support on popular^h^h^h^h^hGwenole request |
40 |
|
|
# v1.5: |
41 |
|
|
# - make zip method acting as z one (remove original file, |
42 |
|
|
# keeping only smallest file, displaying size gain, ...) |
43 |
|
|
# thus giving occasion to factorize some common code |
44 |
|
|
# - check that the source file does exists |
45 |
|
|
# - handle corrupted zip source file |
46 |
|
|
# - comment the script and verbos-ize() some old changes |
47 |
|
|
# - use cheaper shell tests |
48 |
|
|
# - add GPL reference |
49 |
|
|
# - update online help to reflect optional options and newer |
50 |
|
|
# supported formats |
51 |
|
|
# - remove dependency on sed by using ${1%old}new_extension |
52 |
|
|
# v1.6: |
53 |
|
|
# - print error message on stderr rather than on stdin |
54 |
|
|
# - factorize/simplify zip method (fix erase temp files on bzip2ing |
55 |
|
|
# error) |
56 |
|
|
# - typo fixes |
57 |
|
|
# - simplify for_each(file) loop |
58 |
|
|
# - add "Know bugs" and TODO sections |
59 |
|
|
# - add -h and -k options |
60 |
|
|
# - if -k (keep) option is used, keep all files |
61 |
|
|
# v1.7: handle file names with spaces |
62 |
|
|
# |
63 |
|
|
# v1.7a: added support for lzmash (Giuseppe Ghibò, <ghibo@mandriva.com>) |
64 |
|
|
# |
65 |
|
|
# v1.7b: updated to use new lzma command line tool which replaces old lzmash wrapper |
66 |
|
|
# (Per Øyvind Karlsen <pkarlsen@mandriva.com>) |
67 |
|
|
# |
68 |
|
|
# v1.7c: use default compression level as -9 will take too much time and use too much |
69 |
|
|
# memory for decompression to make it worthwhile (Per Øyvind Karlsen <pkarlsen@mandriva.com> |
70 |
|
|
# |
71 |
|
|
# v1.7d: us new xz format rather than old lzma_alone format (Per Øyvind Karlsen <peroyvind@mandriva.org> |
72 |
|
|
# |
73 |
|
|
# TODO: |
74 |
|
|
# - retrieve my patch for solaris file utils |
75 |
|
|
# - add trap for zip method (is it really useful?) |
76 |
|
|
# - add a man page |
77 |
|
|
# - move bzme in its own package that requires tar too |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
# Defaults |
81 |
|
|
force= |
82 |
|
|
keep= |
83 |
|
|
|
84 |
|
|
# Corrupted source error message |
85 |
|
|
src_err_msg () |
86 |
|
|
{ if [ "$2" != 0 ]; then |
87 |
|
|
echo "Corrupted source file ($1) !" 1>&2 |
88 |
|
|
rm -f "$TARGET" |
89 |
|
|
STATUS=1 |
90 |
|
|
fi |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
gz_compr () |
94 |
|
|
{ zcat "$1" | xz > "$TARGET" |
95 |
|
|
# Keep PIPESTATUS |
96 |
|
|
MY_STATUS=( ${PIPESTATUS[*]} ) |
97 |
|
|
src_err_msg "$1" ${MY_STATUS[0]} |
98 |
|
|
if [[ "${MY_STATUS[1]}" != "0" ]]; then |
99 |
|
|
echo "error while xz'ing !" 1>&2 |
100 |
|
|
STATUS=1 |
101 |
|
|
fi |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
bzip2_compr () |
105 |
|
|
{ bzcat "$1" | xz > "$TARGET" |
106 |
|
|
# Keep PIPESTATUS |
107 |
|
|
MY_STATUS=( ${PIPESTATUS[*]} ) |
108 |
|
|
src_err_msg "$1" ${MY_STATUS[0]} |
109 |
|
|
if [[ "${MY_STATUS[1]}" != "0" ]]; then |
110 |
|
|
echo "error while xz'ing !" 1>&2 |
111 |
|
|
STATUS=1 |
112 |
|
|
fi |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
lzma_compr () |
116 |
|
|
{ xzcat "$1" | xz > "$TARGET" |
117 |
|
|
# Keep PIPESTATUS |
118 |
|
|
MY_STATUS=( ${PIPESTATUS[*]} ) |
119 |
|
|
src_err_msg "$1" ${MY_STATUS[0]} |
120 |
|
|
if [[ "${MY_STATUS[1]}" != "0" ]]; then |
121 |
|
|
echo "error while xz'ing !" 1>&2 |
122 |
|
|
STATUS=1 |
123 |
|
|
fi |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
zip_compr () |
127 |
|
|
{ |
128 |
|
|
[[ -z "$TMPDIR" ]] && TMPDIR=$TMP |
129 |
|
|
MY_TMP=$(mktemp -d $TMPDIR/xzme.XXXXXX) |
130 |
|
|
unzip -qd $MY_TMP "$1" |
131 |
|
|
src_err_msg "$1" $? |
132 |
|
|
tar cfj "$TARGET" -C $MY_TMP . |
133 |
|
|
if [[ $? != 0 ]]; then |
134 |
|
|
echo "error while taring !" 1>&2 |
135 |
|
|
STATUS=1 |
136 |
|
|
fi |
137 |
|
|
# Removing temporary files |
138 |
|
|
rm -fr $MY_TMP |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
compress () |
142 |
|
|
{ echo -n "Compressing $1 ... " |
143 |
|
|
if [[ ! -f "$1" ]]; then |
144 |
|
|
echo "Source file doesn't exist" 1>&2 |
145 |
|
|
return |
146 |
|
|
fi |
147 |
|
|
STATUS=0 |
148 |
|
|
SIZE=$(du -k "$1"|cut -f 1 -d " ") |
149 |
|
|
if [[ -f "$TARGET" ]]; then |
150 |
|
|
if [[ -n $force ]];then |
151 |
|
|
rm -f "$TARGET" |
152 |
|
|
else |
153 |
|
|
echo "$TARGET already exists !!" 1>&2 |
154 |
|
|
echo "Use -f to force it" |
155 |
|
|
return |
156 |
|
|
fi |
157 |
|
|
fi |
158 |
|
|
# Do the real compression job |
159 |
|
|
$METHOD "$1" |
160 |
|
|
# if there was an error |
161 |
|
|
if [[ $STATUS = 1 ]]; then |
162 |
|
|
[[ -z $keep ]] && rm -f "$TARGET" |
163 |
|
|
return |
164 |
|
|
fi |
165 |
|
|
# Compare size in order to only keep the smallest file |
166 |
|
|
SIZE2=$(du -k "$TARGET"|cut -f 1 -d " ") |
167 |
|
|
if [[ $SIZE -lt $SIZE2 && -z $force_compress ]] |
168 |
|
|
then |
169 |
|
|
echo "=> $TARGET is bigger than $1 ($SIZE"kb" => $SIZE2"kb") !!!" |
170 |
|
|
echo "Use -F to force the recompression" |
171 |
|
|
[[ -z $keep ]] && rm -f "$TARGET" |
172 |
|
|
else |
173 |
|
|
echo "=> $TARGET ($SIZE"kb" => $SIZE2"kb")" |
174 |
|
|
|
175 |
|
|
[[ -z $keep ]] && rm -f "$1" |
176 |
|
|
fi |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
while getopts Ffhk opt; do |
180 |
|
|
case "$opt" in |
181 |
|
|
F) force_compress="yes";; |
182 |
|
|
f) force="yes";; |
183 |
|
|
k) keep="yes";; |
184 |
|
|
h) |
185 |
|
|
echo "Usage: xzme [-Ffhk] file.*.({,t}gz|bz2|lzma|Z|zip)" |
186 |
|
|
exit 1;; |
187 |
|
|
*) |
188 |
|
|
echo "See xzme -h for usage" |
189 |
|
|
exit 1;; |
190 |
|
|
esac |
191 |
|
|
done |
192 |
|
|
shift $((OPTIND - 1)) |
193 |
|
|
|
194 |
|
|
echo keeping: $keep |
195 |
|
|
|
196 |
|
|
while [[ "$1" != "" ]] |
197 |
|
|
do |
198 |
|
|
#default method is .lzma,.bz2,.gz,.Z,.z,.. |
199 |
|
|
METHOD=gz_compr |
200 |
|
|
case "$1" in |
201 |
|
|
*.xz) echo "$1: already compressed!"; shift;continue;; |
202 |
|
|
*.lzma) METHOD=lzma_compr |
203 |
|
|
TARGET=${1%.lzma}.xz;; |
204 |
|
|
*.bz2) METHOD=bzip2_compr |
205 |
|
|
TARGET=${1%.tar.bz2}.tar.xz |
206 |
|
|
;; |
207 |
|
|
*.tgz) TARGET=${1%.tgz}.txz;; |
208 |
|
|
*.Z) TARGET=${1%.Z}.xz;; |
209 |
|
|
*.gz) TARGET=${1%.gz}.xz;; |
210 |
|
|
*.zip) |
211 |
|
|
METHOD=zip_compr |
212 |
|
|
TARGET=${1%.zip}.tar.xz |
213 |
|
|
;; |
214 |
|
|
*) echo "$1: unknown file extension => ignored"; shift; continue;; |
215 |
|
|
esac |
216 |
|
|
compress "$1" |
217 |
|
|
shift |
218 |
|
|
done |