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

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