wok-stable view xarchive/stuff/slitaz-wrap.sh @ rev 941

xarchive: enhance tar and cpio support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jun 24 13:47:32 2008 +0000 (2008-06-24)
parents 7c31f0d8f00c
children 19e156dfc1af
line source
1 #!/bin/sh
2 # slitaz-wrap.sh - sh slitaz core wrapper for xarchive frontend
3 # Copyright (C) 2005 Lee Bigelow <ligelowbee@yahoo.com>
4 # Copyright (C) 2008 Pascal Bellard <pascal.bellard@slitaz.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # set up exit status variables
21 E_UNSUPPORTED=65
23 # Supported file extentions for tar
24 TAR_EXTS="tar"
25 GZIP_EXTS="tar.gz tgz"
26 LZMA_EXTS="tar.lz tar.lzma tlz"
27 BZIP2_EXTS="tar.bz tbz tar.bz2 tbz2"
28 COMPRESS_EXTS="tar.z tar.Z"
29 CPIO_EXTS="cpio"
30 CPIOGZ_EXTS="cpio.gz"
31 ZIP_EXTS="zip cbz jar"
32 RPM_EXTS="rpm"
33 DEB_EXTS="deb"
34 TAZPKG_EXTS="tazpkg"
36 # Setup awk program
37 AWK_PROGS="mawk gawk awk"
38 AWK_PROG=""
39 for awkprog in $AWK_PROGS; do
40 if [ "$(which $awkprog)" ]; then
41 AWK_PROG="$awkprog"
42 break
43 fi
44 done
46 # setup variables opt and archive.
47 # the shifting will leave the files passed as
48 # all the remaining args "$@"
49 opt="$1"
50 test -z $1 || shift 1
51 archive="$1"
52 test -z $1 || shift 1
54 # set up compression variables for our compression functions.
55 # translate archive name to lower case for pattern matching.
56 # use compressor -c option to output to stdout and direct it where we want
57 lc_archive="$(echo $archive|tr [:upper:] [:lower:])"
58 DECOMPRESS="cat"
59 COMPRESS="cat"
60 TAR_COMPRESS_OPT=""
61 for ext in $GZIP_EXTS $CPIOGZ_EXTS; do
62 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
63 DECOMPRESS="gzip -dc"
64 COMPRESS="gzip -c"
65 TAR_COMPRESS_OPT="z"
66 fi
67 done
68 for ext in $BZIP2_EXTS; do
69 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
70 DECOMPRESS="bzip2 -dc"
71 COMPRESS="bzip2 -c"
72 TAR_COMPRESS_OPT="j"
73 fi
74 done
75 for ext in $LZMA_EXTS; do
76 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
77 DECOMPRESS="unlzma -c"
78 COMPRESS="lzma e -si -so"
79 TAR_COMPRESS_OPT="a"
80 fi
81 done
82 for ext in $COMPRESS_EXTS; do
83 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
84 DECOMPRESS="uncompress -dc"
85 COMPRESS="compress -c"
86 TAR_COMPRESS_OPT="--use-compress-prog=compress"
87 fi
88 done
90 # Compression functions
91 decompress_func()
92 {
93 if [ "$DECOMPRESS" ]; then
94 tmpname="$(mktemp -t tartmp.XXXXXX)"
95 if [ -f "$archive" ]; then
96 $DECOMPRESS "$archive" > "$tmpname"
97 fi
98 # store old name for when we recompress
99 oldarch="$archive"
100 # change working file to decompressed tmp
101 archive="$tmpname"
102 fi
103 }
105 compress_func()
106 {
107 if [ "$COMPRESS" ] && [ "$oldarch" ]; then
108 [ -f "$oldarch" ] && rm "$oldarch"
109 if $COMPRESS < "$archive" > "$oldarch"; then
110 rm "$archive"
111 fi
112 fi
113 }
115 not_busybox()
116 {
117 case "$(readlink $(which $1))" in
118 *busybox*) return 1;;
119 esac
120 return 0
121 }
123 add_file()
124 {
125 ( cd $2 ; tar -cf - $1 ) | tar -xf -
126 }
128 remove_file()
129 {
130 rm -rf ./$1
131 }
133 update_tar_cpio()
134 {
135 action=$1
136 shift
137 tardir="$(dirname "$archive")"
138 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
139 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
140 if [ "$action" = "new_archive" ]; then
141 decompress_func
142 cd "$tardir"
143 tar -cf "$archive" "${1#$tardir/}"
144 status=$?
145 compress_func
146 exit $status
147 fi
148 if not_busybox tar; then
149 decompress_func
150 case "$action" in
151 remove_file)
152 tar --delete -f "$archive" "$@";;
153 add_file)
154 cd "$tardir"
155 tar -rf "$archive" "${1#$tardir/}";;
156 *) false;;
157 esac
158 status=$?
159 compress_func
160 exit $status
161 fi
162 tmptar="$(mktemp -d -t tartmp.XXXXXX)"
163 here="$(pwd)"
164 cd $tmptar
165 $DECOMPRESS < "$archive" | tar -xf -
166 status=$?
167 if [ $status -eq 0 -a -n "$1" ]; then
168 while [ "$1" ]; do
169 $action "${1#$tardir/}" $here
170 shift
171 done
172 tar -cf - $(ls -a | grep -v ^\.$ | grep -v ^\.\.$) | \
173 $COMPRESS > "$archive"
174 status=$?
175 fi
176 cd $here
177 rm -rf $tmptar
178 exit $status
179 fi
180 done
181 for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
182 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
183 if [ "$action" = "new_archive" ]; then
184 decompress_func
185 cd "$tardir"
186 echo "${1#$tardir/}" | cpio -o -H newc > "$archive"
187 status=$?
188 compress_func
189 exit $status
190 fi
191 tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
192 here="$(pwd)"
193 cd $tmpcpio
194 $DECOMPRESS "$archive" | cpio -id > /dev/null
195 status=$?
196 if [ $status -eq 0 -a -n "$1" ]; then
197 while [ "$1" ]; do
198 $action "${1#$tardir/}" $here
199 shift
200 done
201 find . | cpio -o -H newc | $COMPRESS > "$archive"
202 status=$?
203 fi
204 cd $here
205 rm -rf $tmpcpio
206 exit $status
207 fi
208 done
209 }
211 # the option switches
212 case "$opt" in
213 -i) # info: output supported extentions for progs that exist
214 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS \
215 $CPIO_EXTS $CPIOGZ_EXTS $ZIP_EXTS $DEB_EXTS $RPM_EXTS \
216 $TAZPKG_EXTS ; do
217 printf "%s;" $ext
218 if [ "$ext" = "zip" -a ! "$(which zip)" ]; then
219 echo warning: zip not found, extract only >/dev/stderr
220 fi
221 done
222 printf "\n"
223 exit
224 ;;
226 -o) # open: mangle output of tar cmd for xarchive
227 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
228 # format of tar output:
229 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
230 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
231 # 1 2 3 4 5 6
232 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
233 tar -tv${TAR_COMPRESS_OPT}f "$archive" | $AWK_PROG '
234 {
235 attr=$1
236 split($2,ids,"/") #split up the 2nd field to get uid/gid
237 uid=ids[1]
238 gid=ids[2]
239 size=$3
240 date=$4
241 time=$5
243 #this method works with filenames that start with a space (evil!)
244 #split line a time and a space
245 split($0,linesplit, $5 " ")
246 #then split the second item (name&link) at the space arrow space
247 split(linesplit[2], nlsplit, " -> ")
249 name=nlsplit[1]
250 link=nlsplit[2]
252 if (! link) {link="-"} #if there was no link set it to a dash
254 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
255 }'
256 exit
257 fi
258 done
260 for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
261 # format of cpio output:
262 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
263 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
264 # 1 2 3 4 5 6
265 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
266 $DECOMPRESS "$archive" | cpio -tv | $AWK_PROG '
267 {
268 attr=$1
269 split($2,ids,"/") #split up the 2nd field to get uid/gid
270 uid=ids[1]
271 gid=ids[2]
272 size=$3
273 date=$4
274 time=$5
276 #this method works with filenames that start with a space (evil!)
277 #split line a time and a space
278 split($0,linesplit, $5 " ")
279 #then split the second item (name&link) at the space arrow space
280 split(linesplit[2], nlsplit, " -> ")
282 name=nlsplit[1]
283 link=nlsplit[2]
285 if (! link) {link="-"} #if there was no link set it to a dash
287 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
288 }'
289 exit
290 fi
291 done
293 for ext in $ZIP_EXTS; do
294 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
295 if which zipinfo; then
296 # format of zipinfo -T -s-h- output:
297 # -rw-r--r-- 2.3 unx 11512 tx defN YYYYMMDD.HHMMSS file
298 # 1 2 3 4 5 6 7 8
299 zipinfo -T -s-h-t "$archive" | $AWK_PROG -v uuid=$(id -u -n) '
300 {
301 attr=$1; size=$4
303 year=substr($7,1,4)
304 month=substr($7,5,2)
305 day=substr($7,7,2)
306 date=year "-" month "-" day
308 hours=substr($7,10,2)
309 mins=substr($7,12,2)
310 secs=substr($7,14,2)
311 time=hours ":" mins ":" secs
313 uid=uuid; gid=uuid; link="-"
314 #split line at the time and a space, second item is our name
315 split($0, linesplit, ($7 " "))
316 name=linesplit[2]
317 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
318 }'
319 exit
320 else
321 # format of unzip -l output:
322 # 6622 2005-04-22 12:29:14 file
323 # 1 2 3 4
324 unzip -l "$archive" | $AWK_PROG -v uuid=$(id -u -n) '
325 BEGIN { n = 0}
326 {
327 n=n+1
328 attr=""
329 uid=uuid; gid=uuid
330 size=$1
331 date=$2
332 time=$3
334 #this method works with filenames that start with a space (evil!)
335 #split line a time and a space
336 split($0,linesplit, $3 " ")
337 #then split the second item (name&link) at the space arrow space
338 split(linesplit[2], nlsplit, " -> ")
340 name=nlsplit[1]
341 link=nlsplit[2]
343 if (! link) {link="-"} #if there was no link set it to a dash
345 if (name != "" && n > 3) printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
346 }'
347 exit
348 fi
349 fi
350 done
352 for ext in $RPM_EXTS; do
353 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
354 rpm2cpio "$archive" | cpio -tv | $AWK_PROG '
355 {
356 attr=$1
357 split($2,ids,"/") #split up the 2nd field to get uid/gid
358 uid=ids[1]
359 gid=ids[2]
360 size=$3
361 date=$4
362 time=$5
364 #this method works with filenames that start with a space (evil!)
365 #split line a time and a space
366 split($0,linesplit, $5 " ")
367 #then split the second item (name&link) at the space arrow space
368 split(linesplit[2], nlsplit, " -> ")
370 name=substr(nlsplit[1],2)
371 link=nlsplit[2]
373 if (! link) {link="-"} #if there was no link set it to a dash
375 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
376 }'
377 exit
378 fi
379 done
381 for ext in $DEB_EXTS; do
382 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
383 dpkg-deb -c "$archive" | $AWK_PROG '
384 {
385 attr=$1
386 split($2,ids,"/") #split up the 2nd field to get uid/gid
387 uid=ids[1]
388 gid=ids[2]
389 size=$3
390 date=$4
391 time=$5
393 #this method works with filenames that start with a space (evil!)
394 #split line a time and a space
395 split($0,linesplit, $5 " ")
396 #then split the second item (name&link) at the space arrow space
397 split(linesplit[2], nlsplit, " -> ")
399 name=substr(nlsplit[1],2)
400 link=nlsplit[2]
402 if (! link) {link="-"} #if there was no link set it to a dash
404 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
405 }'
406 exit
407 fi
408 done
410 for ext in $TAZPKG_EXTS; do
411 # format of cpio output:
412 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
413 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
414 # 1 2 3 4 5 6
415 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
416 tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
417 here="$(pwd)"
418 cd $tmpcpio
419 cpio -i fs.cpio.gz > /dev/null < "$archive"
420 zcat fs.cpio.gz | cpio -tv | $AWK_PROG '
421 {
422 attr=$1
423 split($2,ids,"/") #split up the 2nd field to get uid/gid
424 uid=ids[1]
425 gid=ids[2]
426 size=$3
427 date=$4
428 time=$5
430 #this method works with filenames that start with a space (evil!)
431 #split line a time and a space
432 split($0,linesplit, $5 " ")
433 #then split the second item (name&link) at the space arrow space
434 split(linesplit[2], nlsplit, " -> ")
436 name=substr(nlsplit[1],3)
437 link=nlsplit[2]
439 if (! link) {link="-"} #if there was no link set it to a dash
441 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
442 }'
443 cd $here
444 rm -rf $tmpcpio
445 exit
446 fi
447 done
448 exit $E_UNSUPPORTED
449 ;;
451 -a) # add to archive passed files
452 update_tar_cpio add_file "$@"
453 which zip >/dev/null && for ext in $ZIP_EXTS; do
454 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
455 # we only want to add the file's basename, not
456 # the full path so...
457 while [ "$1" ]; do
458 cd "$(dirname "$1")"
459 zip -g -r "$archive" "$(basename "$1")"
460 wrapper_status=$?
461 shift 1
462 done
463 exit $wrapper_status
464 fi
465 done
466 exit $E_UNSUPPORTED
467 ;;
469 -n) # new: create new archive with passed files
470 update_tar_cpio new_archive "$@"
471 which zip >/dev/null && for ext in $ZIP_EXTS; do
472 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
473 # create will only be passed the first file, the
474 # rest will be "added" to the new archive
475 cd "$(dirname "$1")"
476 zip -r "$archive" "$(basename "$1")"
477 fi
478 done
479 exit $E_UNSUPPORTED
480 ;;
482 -r) # remove: from archive passed files
483 update_tar_cpio remove_file "$@"
484 which zip >/dev/null && for ext in $ZIP_EXTS; do
485 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
486 zip -d "$archive" "$@"
487 fi
488 done
489 exit $E_UNSUPPORTED
490 ;;
492 -e) # extract: from archive passed files
493 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
494 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
495 # xarchive will put is the right extract dir
496 # so we just have to extract.
497 $DECOMPRESS < "$archive" | tar -xf - "$@"
498 exit $?
499 fi
500 done
501 for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
502 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
503 if [ -n "$1" ]; then
504 while [ "$1" ]; do
505 $DECOMPRESS "$archive" | cpio -idv "$1"
506 shift 1
507 done
508 else
509 $DECOMPRESS "$archive" | cpio -idv
510 fi
511 exit $?
512 fi
513 done
514 for ext in $ZIP_EXTS; do
515 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
516 # xarchive will put is the right extract dir
517 # so we just have to extract.
518 unzip -n "$archive" "$@"
519 exit $?
520 fi
521 done
522 for ext in $RPM_EXTS; do
523 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
524 if [ -n "$1" ]; then
525 while [ "$1" ]; do
526 rpm2cpio "$archive" | cpio -idv "$1"
527 shift 1
528 done
529 else
530 rpm2cpio "$archive" | cpio -idv
531 fi
532 exit $?
533 fi
534 done
536 for ext in $DEB_EXTS; do
537 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
538 dpkg-deb -X "$archive" "$@"
539 exit $?
540 fi
541 done
542 for ext in $TAZPKG_EXTS; do
543 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
544 tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
545 here="$(pwd)"
546 cd $tmpcpio
547 cpio -i < "$archive" > /dev/null
548 zcat fs.cpio.gz | cpio -id > /dev/null
549 status=$?
550 if [ -n "$1" ]; then
551 while [ "$1" ]; do
552 dir=$(dirname "$here/$1")
553 mkdir -p "$dir" 2> /dev/null
554 mv "fs/$1" "$dir" 2> /dev/null
555 done
556 else
557 mv fs/* fs/.* $here 2> /dev/null
558 fi
559 cd $here
560 rm -rf $tmpcpio
561 exit $status
562 fi
563 done
564 exit $E_UNSUPPORTED
565 ;;
567 *) echo "error, option $opt not supported"
568 echo "use one of these:"
569 echo "-i #info"
570 echo "-o archive #open"
571 echo "-a archive files #add"
572 echo "-n archive file #new"
573 echo "-r archive files #remove"
574 echo "-e archive files #extract"
575 exit
576 esac