Debian Codegraph @

scripts/wzpaq.in

raw
1 #!@BASHREAL@
2
3 # Wrap zpaq as a more generic compression utility
4 #
5 # Copyright 2017 Robert Krawitz ([email protected])
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 2 of the License, or (at your option)
10 # any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 declare -r ZPAQ=@ZPAQ@
21 declare METHOD=5
22 declare VERBOSE=
23 declare ZPCAT=
24 declare DECOMPRESS=
25 declare FORCE=
26 declare KEEP=
27 declare SAVE_META=
28 declare RECURSE=
29 declare STATUS=0
30 declare -r PROGNAME=$0
31 declare -r BASEDIR=$(pwd)
32 declare TMP_FILE=
33 declare TMP_DIR=
34 declare TMP_ARCHIVE=
35
36 usage() {
37 declare -l USTATUS=${1:-0}
38 echo "Usage: $PROGNAME [OPTIONS] [FILE]..."
39 echo "Compress or decompress FILEs (by default, compress FILEs in place)"
40 echo
41 echo " -c Write to standard output, keep original files unchanged"
42 echo " -d Decompress"
43 echo " -f Force overwrite of output file"
44 echo " -h Print this help"
45 echo " -k Keep (don't delete) input files"
46 echo " -n Do not preserve original filename and timestamp"
47 echo " -N Preserve original filename and timestamp"
48 echo " -q Suppress all messages"
49 echo " -r Operate recursively on directories"
50 echo " -v Print verbose messages"
51 echo " -1 Use minimum compression"
52 echo " -5 Use maximum compression"
53 echo
54 echo "With no FILE, or when only FILE is -, read standard input"
55 exit "$USTATUS"
56 }
57
58 while getopts "qvcdhfknNrz0123456789" opt ; do
59 case "$opt" in
60 q) VERBOSE= ;;
61 v) VERBOSE=1 ;;
62 c) ZPCAT=1 ;;
63 d) DECOMPRESS=1 ;;
64 f) FORCE=1 ;;
65 h) usage ;;
66 k) KEEP=1 ;;
67 n) SAVE_META=-noattributes ;;
68 N) SAVE_META= ;;
69 r) RECURSE=1 ;;
70 z) DECOMPRESS= ;;
71 [0-9]) METHOD=$opt ;;
72 *) usage 1 ;;
73 esac
74 done
75 shift $((OPTIND-1))
76
77 if [[ -n $RECURSE && -n $ZPCAT ]] ; then
78 echo "May not combine recursive and output to stdout"
79 usage "$0" 1
80 fi
81
82 if [[ -n $RECURSE && -z $* ]] ; then
83 echo "May not use recursive with no inputs"
84 usage "$0" 1
85 fi
86
87 declare -a FILES
88
89 g() {
90 if [[ $1 == /* ]] ; then
91 echo "$1"
92 else
93 echo "$BASEDIR/$1"
94 fi
95 }
96
97 build_file_list() {
98 for f in "$@" ; do
99 if [[ -d $f ]] ; then
100 if [[ -n $RECURSE ]] ; then
101 OIFS=$IFS
102 IFS=
103 if [[ -n $DECOMPRESS ]] ; then
104 while read -r FILE ; do
105 FILES+=("$(g "$f1")")
106 done <<< "$(find "$f" -type f -name '*.zpaq' -print)"
107 else
108 while read -r FILE ; do
109 FILES+=("$(g "$f1")")
110 done <<< "$(find "$f" -type f \! -name '*.zpaq' -print)"
111 fi
112 IFS=$OIFS
113 else
114 echo "$f: is a directory, skipping" 1>&2
115 STATUS=1
116 fi
117 elif [[ -f $f ]] ; then
118 if [[ -n $DECOMPRESS && $f != *.zpaq ]] ; then
119 echo "$f: not a zpaq archive, skipping" 1>&2
120 STATUS=1
121 elif [[ -z $DECOMPRESS && $f == *.zpaq ]] ; then
122 echo "$f: is a zpaq archive, skipping" 1>&2
123 STATUS=1
124 else
125 FILES+=("$(g "$f")")
126 fi
127 else
128 echo "$f: not a plain file, skipping" 1>&2
129 STATUS=1
130 fi
131 done
132 }
133
134 if [[ -z $* || $* == '-' ]] ; then
135 ZPCAT=1
136 else
137 build_file_list "$@"
138 fi
139
140 if [[ -n $ZPCAT && ${#FILES[@]} -gt 1 ]] ; then
141 echo "May not compress/decompress more than one file to stdout" 1>&2
142 usage 1
143 fi
144
145 # Wrappers to simplify debugging
146
147 run_zpaq() {
148 if [[ -n $VERBOSE ]] ; then
149 "$ZPAQ" "$@"
150 else
151 "$ZPAQ" "$@" >/dev/null 2>&1
152 fi
153 }
154
155 RM() {
156 for f in "$@" ; do
157 if [[ $1 != "$TMP_FILE" && $1 != "$TMP_DIR" && $1 != "$TMP_ARCHIVE" ]] ; then
158 @RM@ -rf "$f"
159 fi
160 done
161 }
162
163 do_decompress() {
164 FILE="$1"
165 DEST="$2"
166 # shellcheck disable=SC2164
167 [[ -n $3 && -d $3 ]] && cd "$3"
168 run_zpaq extract "$FILE" $SAVE_META || STATUS=1
169 if [[ $(wc -l <<< "$(find . -type f -print)") -gt 1 ]] ; then
170 echo "Multiple file archive $TMP_ARCHIVE, skipping" 1>&2
171 exit 1
172 fi
173 if [[ -z $DEST ]] ; then
174 cat "$(find . -type f -print)"
175 else
176 mv "$(find . -type f -print)" "$DEST"
177 fi
178 }
179
180 decompress_file() {
181 FILE="$1"
182 DEST="$2"
183 if [[ -d $TMP_DIR ]] ; then
184 # Decompress into a temporary directory. We don't actually
185 # know what's in the archive and don't want to dump arbitrary
186 # contents into the user's directory. We do this in a subshell
187 # so that we don't have to try to cd back to the current directory
188 # (which may be problematic if the directory is remote and we
189 # spend a lot of time in the temp directory).
190 (do_decompress "$FILE" "$DEST" "$TMP_DIR")
191 # shellcheck disable=2181
192 if (( $? > 0 )) ; then
193 STATUS=1
194 elif [[ -z $ZPCAT && -z $KEEP ]] ; then
195 RM "$FILE"
196 fi
197 fi
198 }
199
200 decompress() {
201 # Decompress: decompress all .zpaq files.
202 if [[ -z ${FILES[*]} ]] ; then
203 # Decompress stdin implies output to stdout
204 ZPCAT=1
205 cat > "$TMP_ARCHIVE" && decompress_file "$TMP_ARCHIVE"
206 else
207 for f in "${FILES[@]}" ; do
208 declare DEST=
209 [[ -z $ZPCAT ]] && DEST="${f%.zpaq}"
210 if [[ -n $DEST && -e $DEST && -z $FORCE ]] ; then
211 echo "${DEST#${BASEDIR}/} exists, skipping" 1>&2
212 STATUS=1
213 else
214 decompress_file "$f" "$DEST"
215 fi
216 done
217 fi
218 }
219
220 do_compress() {
221 FILE="$1"
222 DEST=$(g "$2")
223 cd "${FILE%/*}" || return 1
224 run_zpaq add "$DEST" "${FILE##*/}" $SAVE_META -method "$METHOD"
225 }
226
227 compress_file() {
228 FILE="$1"
229 DEST=${2:-${FILE}.zpaq}
230 # Make sure that DEST really is empty.
231 RM "$DEST"
232 (do_compress "$FILE" "$DEST") || return 1
233 if [[ -n "$ZPCAT" ]] ; then
234 # zpaq won't send anything to stdout; we have to
235 cat "$DEST"
236 RM "$DEST"
237 elif [[ -z $KEEP ]] ; then
238 RM "$FILE"
239 fi
240 return 0
241 }
242
243 compress() {
244 if [[ -z ${FILES[*]} ]] ; then
245 # Compress stdin implies output to stdout
246 ZPCAT=1
247 cat > "$TMP_FILE" && {
248 compress_file "$TMP_FILE" "$TMP_ARCHIVE" || STATUS=1
249 }
250 else
251 for f in "${FILES[@]}" ; do
252 declare DEST="${f}.zpaq"
253 [[ -n $ZPCAT ]] && DEST="$TMP_ARCHIVE"
254 if [[ -z $ZPCAT && -e $DEST && -z $FORCE ]] ; then
255 echo "${DEST#${BASEDIR}/} exists, skipping" 1>&2
256 STATUS=1
257 else
258 compress_file "$f" "$DEST" || STATUS=1
259 fi
260 done
261 fi
262 }
263
264 # If all of the command line files were disqualified because they
265 # are compressed files, don't try to compress stdin.
266 [[ -n $* && -z ${FILES[*]} ]] && exit 1
267
268 cleanup() {
269 [[ -n $TMP_FILE && -f $TMP_FILE ]] && rm -f "$TMP_FILE"
270 [[ -n $TMP_ARCHIVE && -f $TMP_ARCHIVE ]] && rm -f "$TMP_ARCHIVE"
271 [[ -n $TMP_DIR && -d $TMP_DIR ]] && rm -rf "$TMP_DIR"
272 }
273
274 trap cleanup EXIT
275
276 declare TMP_FILE=$(mktemp /tmp/wzpaqinXXXXXXXXX)
277 declare TMP_DIR=$(mktemp -d /tmp/wzpaq_outXXXXXXXXX)
278 declare TMP_ARCHIVE=$(mktemp /tmp/wzpaqXXXXXXXXX.zpaq)
279
280 if [[ -n $DECOMPRESS ]] ; then
281 decompress
282 else
283 compress
284 fi
285
286 exit $STATUS