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

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