wok-stable view xarchive/stuff/slitaz-wrap.sh @ rev 5068
move static binaries in /usr/share/boot to avoid tazwok strip
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Mar 12 17:24:19 2010 +0100 (2010-03-12) |
parents | ac195e0fe6bf |
children | ea802196d3e8 |
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 # main: option switches
239 case "$opt" in
240 -i) # info: output supported extentions for progs that exist
241 while read exe exts; do
242 [ "$(which $exe)" -o -f /lib/modules/$(uname -r)/kernel/$exe.ko ] &&
243 echo -n "$exts " | sed 's/ /;/g'
244 done <<EOT
245 tar $TAR_EXTS $IPK_EXTS
246 cpio $CPIO_EXTS $TAZPKG_EXTS
247 unzip $ZIP_EXTS
248 dpkg-deb $DEB_EXTS
249 rpm2cpio $RPM_EXTS
250 mount $ISO_EXTS $FS_EXTS
251 xz $XZ_EXTS $CPIOXZ_EXTS
252 lrzip $LRZIP_EXTS $CPIOLRZIP_EXTS
253 rar $RAR_EXTS
254 unace ace
255 arj $ARJ_EXTS
256 7zr $_7Z_EXTS
257 lha $LHA_EXTS
258 lzop $LZO_EXTS
259 cabextract cab
260 cromfs-driver $CROMFS_EXTS
261 fs/squashfs/squashfs $SQUASHFS_EXTS
262 drivers/block/cloop $CLOOP_EXTS
263 EOT
264 echo ""
265 exit
266 ;;
267 -o) # open: mangle output for xarchive
268 AWK_MISC='
269 BEGIN {
270 attr="-"
271 link="-"
272 uid=uuid
273 gid=uuid
274 }
275 function getlink(arg)
276 {
277 split(arg, x, " -> ")
278 name=x[1]
279 link=x[2]
280 if (!link) link="-"
281 }
283 function getname(arg)
284 {
285 # works with filenames that start with a space (evil!)
286 split($0,x, arg)
287 getlink(x[2])
288 }
290 function getid(arg)
291 {
292 if (uuid != "") return
293 split(arg,x,"/")
294 uid=x[1]
295 gid=x[2]
296 }
298 function getattr(arg)
299 {
300 attr=arg
301 if (index(attr, "D") != 0) attr="drwxr-xr-x"
302 else if (index(attr, ".") != 0 || attr !~ /^[rwx-]/) attr="-rw-r--r--"
303 }
305 function display()
306 {
307 if (name != "") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
308 }
310 function show(arg)
311 {
312 getid($2)
313 getname(arg)
314 if (uid != "blocks") display()
315 }'
316 AWKU="$AWK_PROG -v uuid=$(id -u -n) $AWK_MISC"
317 AWK="$AWK_PROG $AWK_MISC"
318 while read filter exts; do
319 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
320 case " $exts " in *\ $lc_ext\ *)
321 $DECOMPRESS "$archive" | $filter | $AWK'
322 {
323 attr=$1
324 size=$3
325 date=$4
326 time=$5
327 show($5 " ")
328 }'
329 esac
330 done <<EOT
331 cpio\ -tv $CPIO_EXTS $CPIOXZ_EXTS $CPIOLRZIP_EXTS $RPM_EXTS $TAZPKG_EXTS
332 tar\ -tvf\ - $TAR_EXTS $XZ_EXTS $LRZIP_EXTS $IPK_EXTS
333 dpkg_c $DEB_EXTS
334 EOT
335 loop_fs $opt
336 case " $ZIP_EXTS " in *\ $lc_ext\ *)
337 if [ "$(which zipinfo)" ]; then
338 # -rw-r--r-- 2.3 unx 11512 tx defN YYYYMMDD.HHMMSS file
339 zipinfo -T -s-h-t "$archive" | $AWKU'
340 {
341 attr=$1
342 size=$4
343 date=substr($7,1,4) "-" substr($7,5,2) "-" substr($7,7,2)
344 time=substr($7,10,2) ":" substr($7,12,2) ":" substr($7,14,2)
345 show($7 " ")
346 }'
347 else
348 # 6622 2005-04-22 12:29:14 file
349 unzip -l "$archive" | $AWKU'
350 /-[0-9]+-/ {
351 size=$1
352 date=$2
353 time=$3
354 show($3 " ")
355 }'
356 fi
357 esac
358 # -----------+---------------------+-------
359 # 6622 | 22.04.2005 12:29:14 | file
360 [ "$lc_ext" == "cab" ] && cabextract -q -l "$archive" | $AWKU'
361 /[0-9]+ |/ {
362 size=$1
363 date=$3
364 time=$4
365 show($4 " | ")
366 }'
367 case " $RAR_EXTS " in *\ $lc_ext\ *)
368 #-------------------------------------
369 # bookmarks/mozilla_bookmarks.html
370 # 11512 5231 45% 28-02-05 16:19 -rw-r--r-- F3F3477F m3b 2.9
371 # (or 11512 5231 45% 28-02-05 16:19 .D.... 00000000 m3b 2.9)
372 rar v -c- "$archive" | $AWKU'
373 /-[0-9]+-/ {
374 getattr($6)
375 size=$1
376 date=$4
377 time=$5
378 display()
379 }
380 {
381 name=substr($0,2)
382 }'
383 esac
384 case "$lc_ext" in
385 ace)
386 # Date ³Time ³Packed ³Size ³Ratio³File
387 # 17.09.02³00:32³ 394116³ 414817³ 95%³ OggDS0993.exe
388 unace v -c- "$archive" | $AWKU'
389 /^[0-9][0-9]\./ {
390 # strip the funky little 3 off the end of size
391 size=substr($3,1,(length($3)-1))
392 date=substr($1,1,8)
393 time=substr($1,10,5)
394 show($4 " ")
395 }'
396 ;;
397 7z|bcj|bcj2)
398 # ------------------- ----- ------------ ------------ ------------
399 # 1992-04-12 11:39:46 ....A 356 ANKETA.FRG
400 7zr l "$archive" | $AWKU'
401 /^[0-9]+-/ {
402 date=$1
403 time=$2
404 getattr($3)
405 size=$4
406 $0=substr($0, 54)
407 name=$1
408 if (NF > 1) name=$0
409 gsub(/\\/, "/", name)
410 display()
411 }'
412 esac
413 case " $ARJ_EXTS " in *\ $lc_ext\ *)
414 # 001) ANKETA.FRG
415 # 3 MS-DOS 356 121 0.340 92-04-12 11:39:46 1
416 arj v "$archive" | $AWKU'
417 BEGIN { name="" }
418 /^[0-9]+\) / {
419 display()
420 getname($1 " ")
421 }
422 /^[\t ]+[0-9]+/ {
423 size=$3
424 date=$6
425 time=$7
426 getattr($8)
427 }
428 /^[\t ]+Owner: UID [0-9]+\, GID [0-9]+/ { uid=$3; gid=$5 }
429 /^[\t ]+SymLink -> / { link=$3; attr="l"substr(attr, 2) }
430 /^---/ { display() }
431 '
432 esac
433 case " $LHA_EXTS " in *\ $lc_ext\ *)
434 # Desktop/up -> ..
435 # lrwxrwxrwx 0/0 0 0 ****** -lhd- 0000 2009-05-03 16:59:03 [2]
436 lha v -q -v "$archive" | $AWK'
437 {
438 if ($4 == "") { getlink($0); next }
439 attr=$1
440 getid($2)
441 size=$4
442 date=$8
443 time=$9
444 display()
445 }'
446 esac
447 case " $LZO_EXTS " in *\ $lc_ext\ *)
448 # ------ ------ ------ ----- ---- ---- ----
449 # LZO1X-1 10057 5675 56.4% 2005-07-25 16:26 path/file
450 lzop -Plv "$archive" | $AWKU'
451 /-[0-9]+-/ {
452 size=$2
453 date=$5
454 time=$6
455 show($6 " ")
456 }'
457 esac
458 exit 0
459 ;;
460 -a|-n) # add to archive / new: create new archive
461 update $opt "$@"
462 while read exe args exts; do
463 [ "$(which $exe)" ] || continue
464 [ "$opt$exe" = "-nzip" ] && args="-r"
465 case " $exts " in *\ $lc_ext\ *)
466 # only add the file's basename, not the full path
467 while [ "$1" ]; do
468 cd "$(dirname "$1")"
469 $exe $args "$archive" "$(basename "$1")"
470 status=$?
471 shift
472 done
473 exit $status
474 esac
475 done <<EOT
476 zip -g\ -r $ZIP_EXTS
477 rar a $RAR_EXTS
478 arj a $ARJ_EXTS
479 7zr a\ -ms=off $_7Z_EXTS
480 lha a $LHA_EXTS
481 EOT
482 ;;
483 -r) # remove from archive passed files
484 update $opt "$@"
485 while read exe args exts; do
486 [ "$(which $exe)" ] || continue
487 case " $exts " in *\ $lc_ext\ *)
488 $exe $args "$archive" "$@" | grep -q E_NOTIMPL && {
489 echo -e "7z cannot delete files from solid archive." >&2
490 exit $UNSUPPORTED
491 }
492 exit 0
493 esac
494 done <<EOT
495 zip -d $ZIP_EXTS
496 rar d $RAR_EXTS
497 arj d $ARJ_EXTS
498 lha d $LHA_EXTS
499 7zr d $_7Z_EXTS
500 EOT
501 ;;
502 -e) # extract from archive passed files
503 while read arch exts; do
504 case " $exts " in *\ $lc_ext\ *)
505 $DECOMPRESS "$archive" | $arch "$@"
506 exit $?
507 esac
508 done <<EOT
509 tar\ -xf\ - $TAR_EXTS $IPK_EXTS $XZ_EXTS $LRZIP_EXTS
510 cpio\ -idm $CPIO_EXTS $CPIOXZ_EXTS $CPIOLRZIP_EXTS $RPM_EXTS $TAZPKG_EXTS
511 EOT
512 while read exe x p exts; do
513 [ "$(which $exe)" ] || continue
514 case " $exts " in *\ $lc_ext\ *)
515 [ "$x" != "N/A" ] && $exe $x "$archive" "$@"
516 status=$?
517 if [ "$p" != "N/A" -a $status -ne 0 ]; then
518 echo Password protected, opening an x-term...
519 $XTERM_PROG -e $exe $p "$archive" "$@"
520 exit $?
521 fi
522 exit $status
523 esac
524 done <<EOT
525 unzip -n N/A $ZIP_EXTS
526 dpkg-deb -X N/A $DEB_EXTS
527 lha x N/A $LHA_EXTS
528 lzop -x N/A $LZO_EXTS
529 rar x\ -o-\ -p- x\ -o- $RAR_EXTS
530 arj x\ -y x\ -y\ -g? $ARJ_EXTS
531 7zr x\ -y\ -p- x\ -y $_7Z_EXTS
532 unace N/A x\ -o\ -y ace
533 cabextract -q N/A cab
534 EOT
535 loop_fs $opt "$@"
536 ;;
537 *) cat <<EOT
538 error, option $opt not supported
539 use one of these:
540 -i #info
541 -o archive #open
542 -a archive files #add
543 -n archive file #new
544 -r archive files #remove
545 -e archive files #extract
546 EOT
547 esac
548 exit $UNSUPPORTED