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

slitaz-doc: fix version for 3.0
author Christophe Lincoln <pankso@slitaz.org>
date Sun Mar 28 17:16:33 2010 +0200 (2010-03-28)
parents a93b9fa277ad
children 30da93cc3cd5
line source
1 #!/bin/sh
2 #
3 # slitaz-wrap.sh - slitaz core wrapper for xarchive frontend
4 # Copyright (C) 2005 Lee Bigelow <ligelowbee@yahoo.com>
5 # Copyright (C) 2010 Pascal Bellard <pascal.bellard@slitaz.org>
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
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
21 UNSUPPORTED=65
23 GZIP_EXTS="tar.gz tgz cpio.gz"
24 LZMA_EXTS="tar.lz tar.lzma tlz"
25 BZIP2_EXTS="tar.bz tbz tar.bz2 tbz2"
26 COMPRESS_EXTS="tar.z tar.Z"
27 TAR_EXTS="tar tar.gz tgz $LZMA_EXTS $BZIP2_EXTS $COMPRESS_EXTS"
28 XZ_EXTS="tar.xz txz"
29 LRZIP_EXTS="tar.lrz tlrz"
30 IPK_EXTS="ipk"
31 CPIO_EXTS="cpio cpio.gz"
32 CPIOXZ_EXTS="cpio.xz"
33 CPIOLRZIP_EXTS="cpio.lrz"
34 ZIP_EXTS="zip cbz jar"
35 RPM_EXTS="rpm"
36 DEB_EXTS="deb udeb"
37 TAZPKG_EXTS="tazpkg spkg"
38 ISO_EXTS="iso"
39 SQUASHFS_EXTS="sfs sqfs squashfs"
40 CROMFS_EXTS="cfs cromfs"
41 FS_EXTS="ext2 ext3 dos fat vfat xfs fd fs loop"
42 CLOOP_EXTS="cloop"
43 RAR_EXTS="rar cbr"
44 LHA_EXTS="lha lzh lzs"
45 LZO_EXTS="lzo"
46 ARJ_EXTS="arj pak arc j uc2 zoo"
47 _7Z_EXTS="7z bcj bcj2"
49 while read var progs; do
50 eval $var=""
51 for i in $progs; do
52 [ "$(which $i)" ] || continue
53 eval $var="$i"
54 break
55 done
56 done <<EOT
57 AWK_PROG mawk gawk awk
58 XTERM_PROG xterm rxvt xvt wterm aterm Eterm false
59 EOT
61 # the shifting will leave the files passed as all the remaining args "$@"
62 opt="$1"
63 archive="$2"
64 lc_ext="$(echo ${2##*.} |tr [:upper:] [:lower:])"
65 shift 2
67 tazpkg2cpio()
68 {
69 tmp="$(mktemp -d -t tmpcpio.XXXXXX)"
70 case "$(cd $tmp; cpio -iv < "$1")" in
71 *lzma*) unlzma -c $tmp/fs.cpio.lzma;;
72 *gz*) zcat $tmp/fs.cpio.gz
73 esac
74 rm -rf $tmp
75 }
77 decompress_ipk()
78 {
79 tar xOzf "$1" ./data.tar.gz | gzip -dc
80 }
82 # variables for our compression functions.
83 DECOMPRESS="cat"
84 COMPRESS="cat"
85 while read d c exts; do
86 case " $exts " in *\ $lc_ext\ *)
87 [ "$d" == "${d% *}" -o "$(which ${d% *})" ] || exit $UNSUPPORTED
88 DECOMPRESS="$d"
89 COMPRESS="$c"
90 [ "$(which ${c% *})" ] || COMPRESS="false"
91 break
92 esac
93 done <<EOT
94 unlzma\ -c lzma\ e\ -si\ -so $LZMA_EXTS
95 bunzip2\ -c bzip2\ -c $BZIP2_EXTS
96 gzip\ -dc gzip\ -c $GZIP_EXTS
97 xz\ -dc xz\ -c $XZ_EXTS $CPIOXZ_EXTS
98 lrzip\ -d lrzip $LRZIP_EXTS $CPIOLRZIP_EXTS
99 uncompress\ -dc compress\ -c $COMPRESS_EXTS
100 rpm2cpio false $RPM_EXTS
101 tazpkg2cpio false $TAZPKG_EXTS
102 decompress_ipk false $IPK_EXTS
103 EOT
105 decompress_func()
106 {
107 [ "$DECOMPRESS" = "cat" ] && return
108 tmp="$(mktemp -t tartmp.XXXXXX)"
109 [ -f "$archive" ] && $DECOMPRESS "$archive" > "$tmp"
110 # store old name for compress_func
111 oldarch="$archive"
112 # change working file
113 archive="$tmp"
114 }
116 compress_func()
117 {
118 status=$?
119 if [ "$COMPRESS" != "cat" -a -n "$oldarch" ]; then
120 [ -f "$oldarch" ] && rm "$oldarch"
121 $COMPRESS < "$archive" > "$oldarch" && rm "$archive"
122 fi
123 exit $status
124 }
126 not_busybox()
127 {
128 [ "$(basename $(readlink $(which $1)))" != "busybox" ]
129 }
131 addcpio()
132 {
133 find $@ | cpio -o -H newc
134 }
136 a_file()
137 {
138 ( cd $2 ; tar -cf - $1 ) | tar -xf -
139 }
141 r_file()
142 {
143 rm -rf ./$1
144 }
146 dpkg_c()
147 {
148 dpkg-deb -c "$archive"
149 }
151 loop_fs()
152 {
153 [ "$1" = "-n" ] && retrun
154 cmd=$1
155 shift
156 tmp="$(mktemp -d -t fstmp.XXXXXX)"
157 while read command umnt exts; do
158 case " $exts " in *\ $lc_ext\ *)
159 $command "$archive" $tmp
160 break
161 esac
162 done <<EOT
163 cromfs-driver fusermount\ -u $CROMFS_EXTS
164 mount\ -o\ loop=/dev/cloop,ro umount\ -d $CLOOP_EXTS
165 mount\ -o\ loop,rw umount\ -d $FS_EXTS
166 mount\ -o\ loop,ro umount\ -d $ISO_EXTS $SQUASHFS_EXTS
167 EOT
168 rmdir $tmp 2> /dev/null && return
169 case "$cmd" in
170 -o) find $tmp | while read f; do
171 [ "$f" = "$tmp" ] && continue
172 link="-"
173 [ -L "$f" ] && link=$(readlink "$f")
174 date=$(stat -c "%y" $f | $AWK_PROG \
175 '{ printf "%s;%s\n",$1,substr($2,0,8)}')
176 echo "${f#$tmp/};$(stat -c "%s;%A;%U;%G" $f);$date;$link"
177 done;;
178 -e) ( cd $tmp ; tar cf - "$@" ) | tar xf - ;;
179 -a) tar cf - "$@" | ( cd $tmp ; tar xf - );;
180 -r) ( cd $tmp ; rm -rf "$@" )
181 esac
182 $umnt $tmp
183 rmdir $tmp
184 exit
185 }
187 update()
188 {
189 loop_fs "$@"
190 [ "$COMPRESS" = "false" ] && return
191 action=$1
192 shift
193 tardir="$(dirname "$archive")"
194 if not_busybox tar && [ "$action" != "-n" ]; then
195 case " $TAR_EXTS $XZ_EXTS $LRZIP_EXTS " in *\ $lc_ext\ *)
196 decompress_func
197 case "$action" in
198 -r) tar --delete -f "$archive" "$@";;
199 -a) cd "$tardir"
200 while [ -n "$1" ]; do
201 tar -rf "$archive" "${1#$tardir/}"
202 done
203 esac
204 compress_func
205 esac
206 fi
207 while read add extract exts; do
208 case " $exts " in *\ $lc_ext\ *)
209 if [ "$action" = "-n" ]; then
210 decompress_func
211 cd "$tardir"
212 $add "${1#$tardir/}" > "$archive"
213 compress_func
214 fi
215 tmp="$(mktemp -d -t tartmp.XXXXXX)"
216 cd $tmp
217 $DECOMPRESS "$archive" | $extract
218 status=$?
219 if [ $status -eq 0 -a -n "$1" ]; then
220 while [ "$1" ]; do
221 ${action#-}_file "${1#$tardir/}" $here
222 shift
223 done
224 $add $(ls -a | grep -v ^\.$ | grep -v ^\.\.$) | \
225 $COMPRESS > "$archive"
226 status=$?
227 fi
228 cd - >/dev/null
229 rm -rf $tmp
230 exit $status
231 esac
232 done <<EOT
233 tar\ -cf\ - tar\ -xf\ - $TAR_EXTS $XZ_EXTS $LRZIP_EXTS
234 addcpio cpio\ -id\ >\ /dev/null $CPIO_EXTS $CPIOXZ_EXTS $CPIOLRZIP_EXTS
235 EOT
236 }
238 AWK_MISC='
239 BEGIN {
240 attr="-"
241 link="-"
242 uid=uuid
243 gid=uuid
244 }
245 function getlink(arg)
246 {
247 split(arg, x, " -> ")
248 name=x[1]
249 link=x[2]
250 if (!link) link="-"
251 }
253 function getname(arg)
254 {
255 # works with filenames that start with a space (evil!)
256 split($0,x, arg)
257 getlink(x[2])
258 }
260 function getid(arg)
261 {
262 if (uuid != "") return
263 split(arg,x,"/")
264 uid=x[1]
265 gid=x[2]
266 }
268 function getattr(arg)
269 {
270 attr=arg
271 if (index(attr, "D") != 0) attr="drwxr-xr-x"
272 else if (index(attr, ".") != 0 || attr !~ /^[rwx-]/) attr="-rw-r--r--"
273 }
275 function display()
276 {
277 if (name != "") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
278 }
280 function show(arg)
281 {
282 getid($2)
283 getname(arg)
284 if (uid != "blocks") display()
285 }'
287 awk0()
288 {
289 $AWK_PROG "$AWK_MISC $1"
290 }
292 awku()
293 {
294 $AWK_PROG -v uuid=$(id -u -n) "$AWK_MISC $1"
295 }
298 # main: option switches
299 case "$opt" in
300 -i) # info: output supported extentions for progs that exist
301 while read exe exts; do
302 [ "$(which $exe)" -o -f /lib/modules/$(uname -r)/kernel/$exe.ko ] &&
303 echo -n "$exts " | sed 's/ /;/g'
304 done <<EOT
305 tar $TAR_EXTS $IPK_EXTS
306 cpio $CPIO_EXTS $TAZPKG_EXTS
307 unzip $ZIP_EXTS
308 dpkg-deb $DEB_EXTS
309 rpm2cpio $RPM_EXTS
310 mount $ISO_EXTS $FS_EXTS
311 xz $XZ_EXTS $CPIOXZ_EXTS
312 lrzip $LRZIP_EXTS $CPIOLRZIP_EXTS
313 rar $RAR_EXTS
314 unace ace
315 arj $ARJ_EXTS
316 7zr $_7Z_EXTS
317 lha $LHA_EXTS
318 lzop $LZO_EXTS
319 cabextract cab
320 cromfs-driver $CROMFS_EXTS
321 fs/squashfs/squashfs $SQUASHFS_EXTS
322 drivers/block/cloop $CLOOP_EXTS
323 EOT
324 echo ""
325 exit
326 ;;
327 -o) # open: mangle output for xarchive
328 while read filter exts; do
329 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
330 case " $exts " in *\ $lc_ext\ *)
331 $DECOMPRESS "$archive" | $filter | awk0 '
332 {
333 attr=$1
334 size=$3
335 date=$4
336 time=$5
337 show($5 " ")
338 }'
339 esac
340 done <<EOT
341 cpio\ -tv $CPIO_EXTS $CPIOXZ_EXTS $CPIOLRZIP_EXTS $RPM_EXTS $TAZPKG_EXTS
342 tar\ -tvf\ - $TAR_EXTS $XZ_EXTS $LRZIP_EXTS $IPK_EXTS
343 dpkg_c $DEB_EXTS
344 EOT
345 loop_fs $opt
346 case " $ZIP_EXTS " in *\ $lc_ext\ *)
347 if [ "$(which zipinfo)" ]; then
348 # -rw-r--r-- 2.3 unx 11512 tx defN YYYYMMDD.HHMMSS file
349 zipinfo -T -s-h-t "$archive" | awku '
350 {
351 attr=$1
352 size=$4
353 date=substr($7,1,4) "-" substr($7,5,2) "-" substr($7,7,2)
354 time=substr($7,10,2) ":" substr($7,12,2) ":" substr($7,14,2)
355 show($7 " ")
356 }'
357 else
358 # 6622 2005-04-22 12:29:14 file
359 unzip -l "$archive" | awku '
360 /-[0-9]+-/ {
361 size=$1
362 date=$2
363 time=$3
364 show($3 " ")
365 }'
366 fi
367 esac
368 # -----------+---------------------+-------
369 # 6622 | 22.04.2005 12:29:14 | file
370 [ "$lc_ext" == "cab" ] && cabextract -q -l "$archive" | awku '
371 /[0-9]+ |/ {
372 size=$1
373 date=$3
374 time=$4
375 show($4 " | ")
376 }'
377 case " $RAR_EXTS " in *\ $lc_ext\ *)
378 #-------------------------------------
379 # bookmarks/mozilla_bookmarks.html
380 # 11512 5231 45% 28-02-05 16:19 -rw-r--r-- F3F3477F m3b 2.9
381 # (or 11512 5231 45% 28-02-05 16:19 .D.... 00000000 m3b 2.9)
382 rar v -c- "$archive" | awku '
383 /-[0-9]+-/ {
384 getattr($6)
385 size=$1
386 date=$4
387 time=$5
388 display()
389 }
390 {
391 name=substr($0,2)
392 }'
393 esac
394 case "$lc_ext" in
395 ace)
396 # Date ³Time ³Packed ³Size ³Ratio³File
397 # 17.09.02³00:32³ 394116³ 414817³ 95%³ OggDS0993.exe
398 unace v -c- "$archive" | awku '
399 /^[0-9][0-9]\./ {
400 # strip the funky little 3 off the end of size
401 size=substr($3,1,(length($3)-1))
402 date=substr($1,1,8)
403 time=substr($1,10,5)
404 show($4 " ")
405 }'
406 ;;
407 7z|bcj|bcj2)
408 # ------------------- ----- ------------ ------------ ------------
409 # 1992-04-12 11:39:46 ....A 356 ANKETA.FRG
410 7zr l "$archive" | awku '
411 /^[0-9]+-/ {
412 date=$1
413 time=$2
414 getattr($3)
415 size=$4
416 $0=substr($0, 54)
417 name=$1
418 if (NF > 1) name=$0
419 gsub(/\\/, "/", name)
420 display()
421 }'
422 esac
423 case " $ARJ_EXTS " in *\ $lc_ext\ *)
424 # 001) ANKETA.FRG
425 # 3 MS-DOS 356 121 0.340 92-04-12 11:39:46 1
426 arj v "$archive" | awku '
427 BEGIN { name="" }
428 /^[0-9]+\) / {
429 display()
430 getname($1 " ")
431 }
432 /^[\t ]+[0-9]+/ {
433 size=$3
434 date=$6
435 time=$7
436 getattr($8)
437 }
438 /^[\t ]+Owner: UID [0-9]+\, GID [0-9]+/ { uid=$3; gid=$5 }
439 /^[\t ]+SymLink -> / { link=$3; attr="l"substr(attr, 2) }
440 /^---/ { display() }
441 '
442 esac
443 case " $LHA_EXTS " in *\ $lc_ext\ *)
444 # Desktop/up -> ..
445 # lrwxrwxrwx 0/0 0 0 ****** -lhd- 0000 2009-05-03 16:59:03 [2]
446 lha v -q -v "$archive" | awk0 '
447 {
448 if ($4 == "") { getlink($0); next }
449 attr=$1
450 getid($2)
451 size=$4
452 date=$8
453 time=$9
454 display()
455 }'
456 esac
457 case " $LZO_EXTS " in *\ $lc_ext\ *)
458 # ------ ------ ------ ----- ---- ---- ----
459 # LZO1X-1 10057 5675 56.4% 2005-07-25 16:26 path/file
460 lzop -Plv "$archive" | awku '
461 /-[0-9]+-/ {
462 size=$2
463 date=$5
464 time=$6
465 show($6 " ")
466 }'
467 esac
468 exit 0
469 ;;
470 -a|-n) # add to archive / new: create new archive
471 update $opt "$@"
472 while read exe args exts; do
473 [ "$(which $exe)" ] || continue
474 [ "$opt$exe" = "-nzip" ] && args="-r"
475 case " $exts " in *\ $lc_ext\ *)
476 # only add the file's basename, not the full path
477 while [ "$1" ]; do
478 cd "$(dirname "$1")"
479 $exe $args "$archive" "$(basename "$1")"
480 status=$?
481 shift
482 done
483 exit $status
484 esac
485 done <<EOT
486 zip -g\ -r $ZIP_EXTS
487 rar a $RAR_EXTS
488 arj a $ARJ_EXTS
489 7zr a\ -ms=off $_7Z_EXTS
490 lha a $LHA_EXTS
491 EOT
492 ;;
493 -r) # remove from archive passed files
494 update $opt "$@"
495 while read exe args exts; do
496 [ "$(which $exe)" ] || continue
497 case " $exts " in *\ $lc_ext\ *)
498 $exe $args "$archive" "$@" | grep -q E_NOTIMPL && {
499 echo -e "7z cannot delete files from solid archive." >&2
500 exit $UNSUPPORTED
501 }
502 exit 0
503 esac
504 done <<EOT
505 zip -d $ZIP_EXTS
506 rar d $RAR_EXTS
507 arj d $ARJ_EXTS
508 lha d $LHA_EXTS
509 7zr d $_7Z_EXTS
510 EOT
511 ;;
512 -e) # extract from archive passed files
513 while read arch exts; do
514 case " $exts " in *\ $lc_ext\ *)
515 $DECOMPRESS "$archive" | $arch "$@"
516 exit $?
517 esac
518 done <<EOT
519 tar\ -xf\ - $TAR_EXTS $IPK_EXTS $XZ_EXTS $LRZIP_EXTS
520 cpio\ -idm $CPIO_EXTS $CPIOXZ_EXTS $CPIOLRZIP_EXTS $RPM_EXTS $TAZPKG_EXTS
521 EOT
522 while read exe x p exts; do
523 [ "$(which $exe)" ] || continue
524 case " $exts " in *\ $lc_ext\ *)
525 [ "$x" != "N/A" ] && $exe $x "$archive" "$@"
526 status=$?
527 if [ "$p" != "N/A" -a $status -ne 0 ]; then
528 echo Password protected, opening an x-term...
529 $XTERM_PROG -e $exe $p "$archive" "$@"
530 exit $?
531 fi
532 exit $status
533 esac
534 done <<EOT
535 unzip -n N/A $ZIP_EXTS
536 dpkg-deb -X N/A $DEB_EXTS
537 lha x N/A $LHA_EXTS
538 lzop -x N/A $LZO_EXTS
539 rar x\ -o-\ -p- x\ -o- $RAR_EXTS
540 arj x\ -y x\ -y\ -g? $ARJ_EXTS
541 7zr x\ -y\ -p- x\ -y $_7Z_EXTS
542 unace N/A x\ -o\ -y ace
543 cabextract -q N/A cab
544 EOT
545 loop_fs $opt "$@"
546 ;;
547 *) cat <<EOT
548 error, option $opt not supported
549 use one of these:
550 -i #info
551 -o archive #open
552 -a archive files #add
553 -n archive file #new
554 -r archive files #remove
555 -e archive files #extract
556 EOT
557 esac
558 exit $UNSUPPORTED