wok-stable view xarchive/stuff/slitaz-wrap.sh @ rev 1703
busybox: / is tmpfs in livecd/frugal
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Nov 13 23:45:08 2008 +0000 (2008-11-13) |
parents | 19e156dfc1af |
children | 77a4483b1fa9 |
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"
35 ISO_EXTS="iso"
36 SQUASHFS_EXTS="sqfs squashfs"
37 CROMFS_EXTS="cromfs"
38 FS_EXTS="ext2 ext3 dos fat vfat fd fs loop"
39 CLOOP_EXTS="cloop"
41 # Setup awk program
42 AWK_PROGS="mawk gawk awk"
43 AWK_PROG=""
44 for awkprog in $AWK_PROGS; do
45 if [ "$(which $awkprog)" ]; then
46 AWK_PROG="$awkprog"
47 break
48 fi
49 done
51 # setup variables opt and archive.
52 # the shifting will leave the files passed as
53 # all the remaining args "$@"
54 opt="$1"
55 test -z $1 || shift 1
56 archive="$1"
57 test -z $1 || shift 1
59 # set up compression variables for our compression functions.
60 # translate archive name to lower case for pattern matching.
61 # use compressor -c option to output to stdout and direct it where we want
62 lc_archive="$(echo $archive|tr [:upper:] [:lower:])"
63 DECOMPRESS="cat"
64 COMPRESS="cat"
65 for ext in $GZIP_EXTS $CPIOGZ_EXTS; do
66 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
67 DECOMPRESS="gzip -dc"
68 COMPRESS="gzip -c"
69 fi
70 done
71 for ext in $BZIP2_EXTS; do
72 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
73 DECOMPRESS="bzip2 -dc"
74 COMPRESS="bzip2 -c"
75 fi
76 done
77 for ext in $LZMA_EXTS; do
78 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
79 DECOMPRESS="unlzma -c"
80 COMPRESS="lzma e -si -so"
81 fi
82 done
83 for ext in $COMPRESS_EXTS; do
84 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
85 DECOMPRESS="uncompress -dc"
86 COMPRESS="compress -c"
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 loop_fs()
212 {
213 tmpfs="$(mktemp -d -t fstmp.XXXXXX)"
214 umnt="umount -d"
215 case " $CLOOP_EXTS " in
216 \ $1\ ) mount -o loop=/dev/cloop,ro "$archive" $tmpfs;;
217 esac
218 case " $FS_EXTS " in
219 \ $1\ ) mount -o loop,rw "$archive" $tmpfs;;
220 esac
221 case " $ISO_EXTS " in
222 \ $1\ ) mount -o loop,ro -t iso9660 "$archive" $tmpfs;;
223 esac
224 case " $SQUASHFS_EXTS " in
225 \ $1\ ) mount -o loop,ro -t squashfs "$archive" $tmpfs;;
226 esac
227 case " $CROMFS_EXTS " in
228 \ $1\ ) cromfs-driver "$archive" $tmpfs; umnt="fusermount -u";;
229 esac
230 case "$2" in
231 stat) find $tmpfs | while read f; do
232 [ "$f" = "$tmpfs" ] && continue
233 link="-"
234 [ -L "$f" ] && link=$(readlink "$f")
235 date=$(stat -c "%y" $f | awk \
236 '{ printf "%s;%s\n",$1,substr($2,0,8)}')
237 echo "${f#$tmpfs/};$(stat -c "%s;%A;%U;%G" $f);$date;$link"
238 done;;
239 copy) if [ -z "$3" ]; then
240 ( cd $tmpfs ; tar cf - . ) | tar xf -
241 else
242 while [ -n "$3" ]; do
243 ( cd $tmpfs ; tar cf - "$3" ) | tar xf -
244 shift;
245 done
246 fi;;
247 add) tar cf - "$@" | ( cd $tmpfs ; tar xf - );;
248 remove) while [ -n "$3" ]; do
249 rm -rf $tmpfs/$3
250 done;;
251 esac
252 $umnt $tmpfs
253 rmdir $tmpfs
254 }
256 # the option switches
257 case "$opt" in
258 -i) # info: output supported extentions for progs that exist
259 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS \
260 $CPIO_EXTS $CPIOGZ_EXTS $ZIP_EXTS $DEB_EXTS $RPM_EXTS \
261 $TAZPKG_EXTS $ISO_EXTS $FS_EXTS; do
262 printf "%s;" $ext
263 if [ "$ext" = "zip" -a ! "$(which zip)" ]; then
264 echo warning: zip not found, extract only >/dev/stderr
265 fi
266 done
267 [ -d /lib/modules/$(uname -r)/kernel/fs/squashfs/squashfs.ko ] && \
268 for ext in $SQUASHFS_EXTS; do
269 printf "%s;" $ext
270 done
271 [ -x /bin/cromfs-driver ] && for ext in $CROMFS_EXTS; do
272 printf "%s;" $ext
273 done
274 [ -d /lib/modules/$(uname -r)/kernel/drivers/block/cloop.ko ] && \
275 for ext in $CLOOP_EXTS; do
276 printf "%s;" $ext
277 done
278 printf "\n"
279 exit
280 ;;
282 -o) # open: mangle output of tar cmd for xarchive
283 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
284 # format of tar output:
285 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
286 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
287 # 1 2 3 4 5 6
288 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
289 $DECOMPRESS "$archive" | tar -tvf - | $AWK_PROG '
290 {
291 attr=$1
292 split($2,ids,"/") #split up the 2nd field to get uid/gid
293 uid=ids[1]
294 gid=ids[2]
295 size=$3
296 date=$4
297 time=$5
299 #this method works with filenames that start with a space (evil!)
300 #split line a time and a space
301 split($0,linesplit, $5 " ")
302 #then split the second item (name&link) at the space arrow space
303 split(linesplit[2], nlsplit, " -> ")
305 name=nlsplit[1]
306 link=nlsplit[2]
308 if (! link) {link="-"} #if there was no link set it to a dash
310 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
311 }'
312 exit
313 fi
314 done
316 for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
317 # format of cpio output:
318 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
319 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
320 # 1 2 3 4 5 6
321 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
322 $DECOMPRESS "$archive" | cpio -tv | $AWK_PROG '
323 {
324 attr=$1
325 split($2,ids,"/") #split up the 2nd field to get uid/gid
326 uid=ids[1]
327 gid=ids[2]
328 size=$3
329 date=$4
330 time=$5
332 #this method works with filenames that start with a space (evil!)
333 #split line a time and a space
334 split($0,linesplit, $5 " ")
335 #then split the second item (name&link) at the space arrow space
336 split(linesplit[2], nlsplit, " -> ")
338 name=nlsplit[1]
339 link=nlsplit[2]
341 if (! link) {link="-"} #if there was no link set it to a dash
343 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
344 }'
345 exit
346 fi
347 done
349 for ext in $ZIP_EXTS; do
350 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
351 if which zipinfo; then
352 # format of zipinfo -T -s-h- output:
353 # -rw-r--r-- 2.3 unx 11512 tx defN YYYYMMDD.HHMMSS file
354 # 1 2 3 4 5 6 7 8
355 zipinfo -T -s-h-t "$archive" | $AWK_PROG -v uuid=$(id -u -n) '
356 {
357 attr=$1; size=$4
359 year=substr($7,1,4)
360 month=substr($7,5,2)
361 day=substr($7,7,2)
362 date=year "-" month "-" day
364 hours=substr($7,10,2)
365 mins=substr($7,12,2)
366 secs=substr($7,14,2)
367 time=hours ":" mins ":" secs
369 uid=uuid; gid=uuid; link="-"
370 #split line at the time and a space, second item is our name
371 split($0, linesplit, ($7 " "))
372 name=linesplit[2]
373 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
374 }'
375 exit
376 else
377 # format of unzip -l output:
378 # 6622 2005-04-22 12:29:14 file
379 # 1 2 3 4
380 unzip -l "$archive" | $AWK_PROG -v uuid=$(id -u -n) '
381 BEGIN { n = 0}
382 {
383 n=n+1
384 attr=""
385 uid=uuid; gid=uuid
386 size=$1
387 date=$2
388 time=$3
390 #this method works with filenames that start with a space (evil!)
391 #split line a time and a space
392 split($0,linesplit, $3 " ")
393 #then split the second item (name&link) at the space arrow space
394 split(linesplit[2], nlsplit, " -> ")
396 name=nlsplit[1]
397 link=nlsplit[2]
399 if (! link) {link="-"} #if there was no link set it to a dash
401 if (name != "" && n > 3) printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
402 }'
403 exit
404 fi
405 fi
406 done
408 for ext in $RPM_EXTS; do
409 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
410 rpm2cpio "$archive" | cpio -tv | $AWK_PROG '
411 {
412 attr=$1
413 split($2,ids,"/") #split up the 2nd field to get uid/gid
414 uid=ids[1]
415 gid=ids[2]
416 size=$3
417 date=$4
418 time=$5
420 #this method works with filenames that start with a space (evil!)
421 #split line a time and a space
422 split($0,linesplit, $5 " ")
423 #then split the second item (name&link) at the space arrow space
424 split(linesplit[2], nlsplit, " -> ")
426 name=substr(nlsplit[1],2)
427 link=nlsplit[2]
429 if (! link) {link="-"} #if there was no link set it to a dash
431 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
432 }'
433 exit
434 fi
435 done
437 for ext in $DEB_EXTS; do
438 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
439 dpkg-deb -c "$archive" | $AWK_PROG '
440 {
441 attr=$1
442 split($2,ids,"/") #split up the 2nd field to get uid/gid
443 uid=ids[1]
444 gid=ids[2]
445 size=$3
446 date=$4
447 time=$5
449 #this method works with filenames that start with a space (evil!)
450 #split line a time and a space
451 split($0,linesplit, $5 " ")
452 #then split the second item (name&link) at the space arrow space
453 split(linesplit[2], nlsplit, " -> ")
455 name=substr(nlsplit[1],2)
456 link=nlsplit[2]
458 if (! link) {link="-"} #if there was no link set it to a dash
460 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
461 }'
462 exit
463 fi
464 done
466 for ext in $TAZPKG_EXTS; do
467 # format of cpio output:
468 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
469 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
470 # 1 2 3 4 5 6
471 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
472 tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
473 here="$(pwd)"
474 cd $tmpcpio
475 cpio -i fs.cpio.gz > /dev/null < "$archive"
476 zcat fs.cpio.gz | cpio -tv | $AWK_PROG '
477 {
478 attr=$1
479 split($2,ids,"/") #split up the 2nd field to get uid/gid
480 uid=ids[1]
481 gid=ids[2]
482 size=$3
483 date=$4
484 time=$5
486 #this method works with filenames that start with a space (evil!)
487 #split line a time and a space
488 split($0,linesplit, $5 " ")
489 #then split the second item (name&link) at the space arrow space
490 split(linesplit[2], nlsplit, " -> ")
492 name=substr(nlsplit[1],3)
493 link=nlsplit[2]
495 if (! link) {link="-"} #if there was no link set it to a dash
497 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
498 }'
499 cd $here
500 rm -rf $tmpcpio
501 exit
502 fi
503 done
505 for ext in $ISO_EXTS $SQUASHFS_EXTS $CROMFS_EXTS $CLOOP_EXTS $FS_EXTS; do
506 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
507 loop_fs $ext stat
508 exit
509 fi
510 done
511 exit $E_UNSUPPORTED
512 ;;
514 -a) # add to archive passed files
515 update_tar_cpio add_file "$@"
516 for ext in $FS_EXTS; do
517 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
518 loop_fs $ext add "$@"
519 exit 0
520 fi
521 done
522 which zip >/dev/null && for ext in $ZIP_EXTS; do
523 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
524 # we only want to add the file's basename, not
525 # the full path so...
526 while [ "$1" ]; do
527 cd "$(dirname "$1")"
528 zip -g -r "$archive" "$(basename "$1")"
529 wrapper_status=$?
530 shift 1
531 done
532 exit $wrapper_status
533 fi
534 done
535 exit $E_UNSUPPORTED
536 ;;
538 -n) # new: create new archive with passed files
539 update_tar_cpio new_archive "$@"
540 which zip >/dev/null && for ext in $ZIP_EXTS; do
541 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
542 # create will only be passed the first file, the
543 # rest will be "added" to the new archive
544 cd "$(dirname "$1")"
545 zip -r "$archive" "$(basename "$1")"
546 fi
547 done
548 exit $E_UNSUPPORTED
549 ;;
551 -r) # remove: from archive passed files
552 update_tar_cpio remove_file "$@"
553 for ext in $FS_EXTS; do
554 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
555 loop_fs $ext remove "$@"
556 exit 0
557 fi
558 done
559 which zip >/dev/null && for ext in $ZIP_EXTS; do
560 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
561 zip -d "$archive" "$@"
562 fi
563 done
564 exit $E_UNSUPPORTED
565 ;;
567 -e) # extract: from archive passed files
568 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
569 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
570 # xarchive will put is the right extract dir
571 # so we just have to extract.
572 $DECOMPRESS "$archive" | tar -xf - "$@"
573 exit $?
574 fi
575 done
576 for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
577 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
578 if [ -n "$1" ]; then
579 while [ "$1" ]; do
580 $DECOMPRESS "$archive" | cpio -idv "$1"
581 shift 1
582 done
583 else
584 $DECOMPRESS "$archive" | cpio -idv
585 fi
586 exit $?
587 fi
588 done
589 for ext in $ZIP_EXTS; do
590 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
591 # xarchive will put is the right extract dir
592 # so we just have to extract.
593 unzip -n "$archive" "$@"
594 exit $?
595 fi
596 done
597 for ext in $RPM_EXTS; do
598 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
599 if [ -n "$1" ]; then
600 while [ "$1" ]; do
601 rpm2cpio "$archive" | cpio -idv "$1"
602 shift 1
603 done
604 else
605 rpm2cpio "$archive" | cpio -idv
606 fi
607 exit $?
608 fi
609 done
611 for ext in $DEB_EXTS; do
612 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
613 dpkg-deb -X "$archive" "$@"
614 exit $?
615 fi
616 done
617 for ext in $TAZPKG_EXTS; do
618 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
619 tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
620 here="$(pwd)"
621 cd $tmpcpio
622 cpio -i < "$archive" > /dev/null
623 zcat fs.cpio.gz | cpio -id > /dev/null
624 status=$?
625 if [ -n "$1" ]; then
626 while [ "$1" ]; do
627 dir=$(dirname "$here/$1")
628 mkdir -p "$dir" 2> /dev/null
629 mv "fs/$1" "$dir" 2> /dev/null
630 done
631 else
632 mv fs/* fs/.* $here 2> /dev/null
633 fi
634 cd $here
635 rm -rf $tmpcpio
636 exit $status
637 fi
638 done
639 for ext in $ISO_EXTS $SQUASHFS_EXTS $CROMFS_EXTS $CLOOP_EXTS $FS_EXTS; do
640 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
641 loop_fs $ext copy "$@"
642 exit 0
643 fi
644 done
645 exit $E_UNSUPPORTED
646 ;;
648 *) echo "error, option $opt not supported"
649 echo "use one of these:"
650 echo "-i #info"
651 echo "-o archive #open"
652 echo "-a archive files #add"
653 echo "-n archive file #new"
654 echo "-r archive files #remove"
655 echo "-e archive files #extract"
656 exit
657 esac