File xznew of Package xz
xxxxxxxxxx
1
#!/bin/sh
2
3
# Copyright (C) 2015 Hans-Peter Jansen
4
# Copyright (C) 1998, 2002, 2004 Free Software Foundation
5
# Copyright (C) 1993 Jean-loup Gailly
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 of the License, or
10
# (at your option) 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 along
18
# with this program; if not, write to the Free Software Foundation, Inc.,
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
22
PATH="/usr/bin:$PATH"; export PATH
23
check=0
24
pipe=0
25
opt=
26
files=
27
keep=0
28
res=0
29
old=0
30
new=0
31
block=1024
32
# block is the disk block size (best guess, need not be exact)
33
34
warn="(does not preserve modes and timestamp)"
35
tmp=`mktemp -d ${TMPDIR-/tmp}/zfoo.XXXXXX` || {
36
echo 'cannot create temporary directory' >&2
37
exit 1
38
}
39
trap "rm -rf $tmp/" 0 1 2 3 6 13 15
40
set -C
41
echo hi > $tmp/1
42
echo hi > $tmp/2
43
if test -z "`(${CPMOD-cpmod} $tmp/1 $tmp/2) 2>&1`"; then
44
cpmod=${CPMOD-cpmod}
45
warn=""
46
fi
47
48
if test -z "$cpmod" && ${TOUCH-touch} -r $tmp/1 $tmp/2 2>/dev/null; then
49
cpmod="${TOUCH-touch}"
50
cpmodarg="-r"
51
warn="(does not preserve file modes)"
52
fi
53
54
# check about xz extension
55
xz $tmp/1 &> /dev/null
56
ext=`echo $tmp/1* | sed "s|$tmp/1||"`
57
rm -rf $tmp/
58
trap - 0 1 2 3 6 13 15
59
if test -z "$ext"; then
60
echo xznew: error determining xz extension
61
exit 1
62
fi
63
if test "$ext" = ".gz"; then
64
echo xznew: cannot use .gz as xz extension.
65
exit 1
66
fi
67
68
for arg
69
do
70
case "$arg" in
71
-*) opt="$opt $arg"; shift;;
72
*) break;;
73
esac
74
done
75
76
if test $# -eq 0; then
77
echo "recompress .gz or .tgz files into $ext (xz) files"
78
echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.gz...
79
echo " -t tests the new files before deleting originals"
80
echo " -v be verbose"
81
echo " -9 use the slowest compression method (optimal compression)"
82
echo " -K keep a .gz file when it is smaller than the $ext file"
83
echo " -P use pipes for the conversion $warn"
84
exit 1
85
fi
86
87
opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
88
case "$opt" in
89
*t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
90
esac
91
case "$opt" in
92
*K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
93
esac
94
case "$opt" in
95
*P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
96
esac
97
if test -n "$opt"; then
98
opt="-$opt"
99
fi
100
101
for i do
102
m=`echo $i | sed 's/\.tgz$//'`
103
if test -f "$m.tgz" ; then
104
echo "Renaming file *.tgz to *.tar.gz"
105
mv "$i" "$m.tar.gz"
106
n=`echo $i | sed 's/\.tgz$/\.tar/'`
107
else
108
n=`echo $i | sed 's/\.gz$//'`
109
fi
110
if test ! -f "$n.gz" ; then
111
echo $n.gz not found
112
res=1; continue
113
fi
114
test $keep -eq 1 && old=`wc -c < "$n.gz"`
115
if test $pipe -eq 1; then
116
if gzip -d < "$n.gz" | xz $opt > "$n$ext"; then
117
# Copy file attributes from old file to new one, if possible.
118
test -n "$cpmod" && $cpmod $cpmodarg "$n.gz" "$n$ext" 2> /dev/null
119
else
120
echo error while recompressing $n.gz
121
res=1; continue
122
fi
123
else
124
if test $check -eq 1; then
125
if cp -p "$n.gz" "$n.$$" 2> /dev/null || cp "$n.gz" "$n.$$"; then
126
:
127
else
128
echo cannot backup "$n.gz"
129
res=1; continue
130
fi
131
fi
132
if gzip -d "$n.gz"; then
133
:
134
else
135
test $check -eq 1 && mv "$n.$$" "$n.gz"
136
echo error while uncompressing $n.gz
137
res=1; continue
138
fi
139
if xz $opt "$n"; then
140
:
141
else
142
if test $check -eq 1; then
143
mv "$n.$$" "$n.gz" && rm -f "$n"
144
echo error while recompressing $n
145
else
146
# gzip $n (might be dangerous if disk full)
147
echo error while recompressing $n, left uncompressed
148
fi
149
res=1; continue
150
fi
151
fi
152
test $keep -eq 1 && new=`wc -c < "$n$ext"`
153
if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \
154
`expr \( $new + $block - 1 \) / $block`; then
155
if test $pipe -eq 1; then
156
rm -f "$n$ext"
157
elif test $check -eq 1; then
158
mv "$n.$$" "$n.gz" && rm -f "$n$ext"
159
else
160
xz -d "$n$ext" && gzip "$n" && rm -f "$n$ext"
161
fi
162
echo "$n.gz smaller than $n$ext -- unchanged"
163
164
elif test $check -eq 1; then
165
if xz -t "$n$ext" ; then
166
rm -f "$n.$$" "$n.gz"
167
else
168
test $pipe -eq 0 && mv "$n.$$" "$n.gz"
169
rm -f "$n$ext"
170
echo error while testing $n$ext, $n.gz unchanged
171
res=1; continue
172
fi
173
elif test $pipe -eq 1; then
174
rm -f "$n.gz"
175
fi
176
done
177
exit $res
178