/[packages]/cauldron/bzip2/current/SOURCES/bzme
ViewVC logotype

Contents of /cauldron/bzip2/current/SOURCES/bzme

Parent Directory Parent Directory | Revision Log Revision Log


Revision 241 - (show annotations) (download)
Fri Jan 7 12:50:50 2011 UTC (13 years, 3 months ago) by pterjan
File size: 5250 byte(s)
imported package bzip2
1 #!/bin/sh
2 # bzme re-compress gzip, zip, ... files into bzip2
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 # TODO:
64 # - retrieve my patch for solaris file utils
65 # - add trap for zip method (is it really useful?)
66 # - add a man page
67 # - move bzme in its own package that requires tar too
68
69
70 # Defaults
71 force=
72 keep=
73
74 # Corrupted source error message
75 src_err_msg ()
76 { if [ "$2" != 0 ]; then
77 echo "Corrupted source file ($1) !" 1>&2
78 rm -f "$TARGET"
79 STATUS=1
80 fi
81 }
82
83 gz_compr ()
84 { zcat "$1" | bzip2 -9 > "$TARGET"
85 # Keep PIPESTATUS
86 MY_STATUS=( ${PIPESTATUS[*]} )
87 src_err_msg "$1" ${MY_STATUS[0]}
88 if [[ "${MY_STATUS[1]}" != "0" ]]; then
89 echo "error while bziping !" 1>&2
90 STATUS=1
91 fi
92 }
93
94 zip_compr ()
95 {
96 [[ -z "$TMPDIR" ]] && TMPDIR=$TMP
97 MY_TMP=$(mktemp -d $TMPDIR/bzme.XXXXXX)
98 unzip -qd $MY_TMP "$1"
99 src_err_msg "$1" $?
100 tar cfj "$TARGET" -C $MY_TMP .
101 if [[ $? != 0 ]]; then
102 echo "error while taring !" 1>&2
103 STATUS=1
104 fi
105 # Removing temporary files
106 rm -fr $MY_TMP
107 }
108
109 compress ()
110 { echo -n "Compressing $1 ... "
111 if [[ ! -f "$1" ]]; then
112 echo "Source file doesn't exist" 1>&2
113 return
114 fi
115 STATUS=0
116 SIZE=$(du "$1"|cut -f 1 -d " ")
117 SIZE_o=$(du -b "$1"|cut -f 1 -d " ")
118 if [[ -f "$TARGET" ]]; then
119 if [[ -n $force ]];then
120 rm -f "$TARGET"
121 else
122 echo "$TARGET already exists !!" 1>&2
123 echo "Use -f to force it"
124 return
125 fi
126 fi
127 # Do the real compression job
128 $METHOD "$1"
129 # if there was an error
130 if [[ $STATUS = 1 ]]; then
131 [[ -z $keep ]] && rm -f "$TARGET"
132 return
133 fi
134 # Compare size in order to only keep the smallest file
135 SIZE2=$(du "$TARGET"|cut -f 1 -d " ")
136 SIZE2_o=$(du -b "$TARGET"|cut -f 1 -d " ")
137 if [[ $SIZE_o -lt $SIZE2_o && -z $force_compress ]]
138 then
139 echo "=> $TARGET is bigger than $1 ($SIZE"kb" => $SIZE2"kb") !!!"
140 echo "Use -F to force the recompression"
141 [[ -z $keep ]] && rm -f "$TARGET"
142 else
143 echo "=> $TARGET ($SIZE"kb" => $SIZE2"kb")"
144
145 [[ -z $keep ]] && rm -f "$1"
146 fi
147 }
148
149 while getopts Ffhk opt; do
150 case "$opt" in
151 F) force_compress="yes";;
152 f) force="yes";;
153 k) keep="yes";;
154 h)
155 echo "Usage: bzme [-Ffhk] file.*.({,t}gz|Z|zip)"
156 exit 1;;
157 *)
158 echo "See bzme -h for usage"
159 exit 1;;
160 esac
161 done
162 shift $((OPTIND - 1))
163
164 echo keeping: $keep
165
166 while [[ "$1" != "" ]]
167 do
168 #default method is gz,.Z,.z,..
169 METHOD=gz_compr
170 case "$1" in
171 *.bz2) echo "$1: already compressed!"; shift;continue;;
172 *.tgz) TARGET=${1%.tgz}.tar.bz2;;
173 *.Z) TARGET=${1%.Z}.bz2;;
174 *.gz) TARGET=${1%.gz}.bz2;;
175 *.zip)
176 METHOD=zip_compr
177 TARGET=${1%.zip}.tar.bz2
178 ;;
179 *) echo "$1: unknown file extension => ignored"; shift; continue;;
180 esac
181 compress "$1"
182 shift
183 done

  ViewVC Help
Powered by ViewVC 1.1.30