wok view xarchive/stuff/slitaz-wrap.sh @ rev 882
Up: sakura (2.1.2)
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Sat Jun 07 11:09:33 2008 +0200 (2008-06-07) |
parents | |
children | cee7a7cd19eb |
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"
36 # Setup awk program
37 AWK_PROGS="mawk gawk awk"
38 AWK_PROG=""
39 for awkprog in $AWK_PROGS; do
40 if [ "$(which $awkprog)" ]; then
41 AWK_PROG="$awkprog"
42 break
43 fi
44 done
46 # setup variables opt and archive.
47 # the shifting will leave the files passed as
48 # all the remaining args "$@"
49 opt="$1"
50 test -z $1 || shift 1
51 archive="$1"
52 test -z $1 || shift 1
54 # set up compression variables for our compression functions.
55 # translate archive name to lower case for pattern matching.
56 # use compressor -c option to output to stdout and direct it where we want
57 lc_archive="$(echo $archive|tr [:upper:] [:lower:])"
58 DECOMPRESS="cat"
59 COMPRESS="cat"
60 COMPRESS2=""
61 TAR_COMPRESS_OPT=""
62 for ext in $GZIP_EXTS $CPIOGZ_EXTS; do
63 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
64 DECOMPRESS="gzip -dc"
65 COMPRESS="gzip -c"
66 TAR_COMPRESS_OPT="z"
67 fi
68 done
69 for ext in $BZIP2_EXTS; do
70 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
71 DECOMPRESS="bzip2 -dc"
72 COMPRESS="bzip2 -c"
73 TAR_COMPRESS_OPT="j"
74 fi
75 done
76 for ext in $LZMA_EXTS; do
77 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
78 DECOMPRESS="unlzma -c"
79 COMPRESS="lzma e"
80 COMPRESS2=" -so"
81 TAR_COMPRESS_OPT="a"
82 fi
83 done
84 for ext in $COMPRESS_EXTS; do
85 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
86 DECOMPRESS="uncompress -dc"
87 COMPRESS="compress -c"
88 TAR_COMPRESS_OPT="--use-compress-prog=compress"
89 fi
90 done
92 # Compression functions
93 decompress_func()
94 {
95 if [ "$DECOMPRESS" ]; then
96 tmpname="$(mktemp -t tartmp.XXXXXX)"
97 if [ -f "$archive" ]; then
98 $DECOMPRESS "$archive" > "$tmpname"
99 fi
100 # store old name for when we recompress
101 oldarch="$archive"
102 # change working file to decompressed tmp
103 archive="$tmpname"
104 fi
105 }
107 compress_func()
108 {
109 if [ "$COMPRESS" ] && [ "$oldarch" ]; then
110 [ -f "$oldarch" ] && rm "$oldarch"
111 if $COMPRESS "$archive" $COMPRESS2 > "$oldarch"; then
112 rm "$archive"
113 fi
114 fi
115 }
117 not_busybox()
118 {
119 case "$(readlink $(which $1))" in
120 *busybox*) return 1;;
121 esac
122 return 0
123 }
125 # the option switches
126 case "$opt" in
127 -i) # info: output supported extentions for progs that exist
128 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS \
129 $CPIO_EXTS $CPIOGZ_EXTS $ZIP_EXTS $DEB_EXTS $RPM_EXTS \
130 $TAZPKG_EXTS ; do
131 printf "%s;" $ext
132 if [ "$ext" = "zip" -a ! "$(which zip)" ]; then
133 echo warning: zip not found, extract only >/dev/stderr
134 fi
135 if [ "$ext" = "tar" ] && ! not_busybox tar; then
136 echo warning: gnu/tar not found, extract only >/dev/stderr
137 fi
138 done
139 printf "\n"
140 exit
141 ;;
143 -o) # open: mangle output of tar cmd for xarchive
144 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
145 # format of tar output:
146 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
147 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
148 # 1 2 3 4 5 6
149 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
150 tar -tv${TAR_COMPRESS_OPT}f "$archive" | $AWK_PROG '
151 {
152 attr=$1
153 split($2,ids,"/") #split up the 2nd field to get uid/gid
154 uid=ids[1]
155 gid=ids[2]
156 size=$3
157 date=$4
158 time=$5
160 #this method works with filenames that start with a space (evil!)
161 #split line a time and a space
162 split($0,linesplit, $5 " ")
163 #then split the second item (name&link) at the space arrow space
164 split(linesplit[2], nlsplit, " -> ")
166 name=nlsplit[1]
167 link=nlsplit[2]
169 if (! link) {link="-"} #if there was no link set it to a dash
171 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
172 }'
173 exit
174 fi
175 done
177 for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
178 # format of cpio output:
179 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
180 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
181 # 1 2 3 4 5 6
182 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
183 $DECOMPRESS "$archive" | cpio -tv | $AWK_PROG '
184 {
185 attr=$1
186 split($2,ids,"/") #split up the 2nd field to get uid/gid
187 uid=ids[1]
188 gid=ids[2]
189 size=$3
190 date=$4
191 time=$5
193 #this method works with filenames that start with a space (evil!)
194 #split line a time and a space
195 split($0,linesplit, $5 " ")
196 #then split the second item (name&link) at the space arrow space
197 split(linesplit[2], nlsplit, " -> ")
199 name=nlsplit[1]
200 link=nlsplit[2]
202 if (! link) {link="-"} #if there was no link set it to a dash
204 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
205 }'
206 exit
207 fi
208 done
210 for ext in $ZIP_EXTS; do
211 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
212 if which zipinfo; then
213 # format of zipinfo -T -s-h- output:
214 # -rw-r--r-- 2.3 unx 11512 tx defN YYYYMMDD.HHMMSS file
215 # 1 2 3 4 5 6 7 8
216 zipinfo -T -s-h-t "$archive" | $AWK_PROG -v uuid=$(id -u -n) '
217 {
218 attr=$1; size=$4
220 year=substr($7,1,4)
221 month=substr($7,5,2)
222 day=substr($7,7,2)
223 date=year "-" month "-" day
225 hours=substr($7,10,2)
226 mins=substr($7,12,2)
227 secs=substr($7,14,2)
228 time=hours ":" mins ":" secs
230 uid=uuid; gid=uuid; link="-"
231 #split line at the time and a space, second item is our name
232 split($0, linesplit, ($7 " "))
233 name=linesplit[2]
234 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
235 }'
236 exit
237 else
238 # format of unzip -l output:
239 # 6622 2005-04-22 12:29:14 file
240 # 1 2 3 4
241 unzip -l "$archive" | $AWK_PROG -v uuid=$(id -u -n) '
242 BEGIN { n = 0}
243 {
244 n=n+1
245 attr=""
246 uid=uuid; gid=uuid
247 size=$1
248 date=$2
249 time=$3
251 #this method works with filenames that start with a space (evil!)
252 #split line a time and a space
253 split($0,linesplit, $3 " ")
254 #then split the second item (name&link) at the space arrow space
255 split(linesplit[2], nlsplit, " -> ")
257 name=nlsplit[1]
258 link=nlsplit[2]
260 if (! link) {link="-"} #if there was no link set it to a dash
262 if (name != "" && n > 3) printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
263 }'
264 exit
265 fi
266 fi
267 done
269 for ext in $RPM_EXTS; do
270 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
271 rpm2cpio "$archive" | cpio -tv | $AWK_PROG '
272 {
273 attr=$1
274 split($2,ids,"/") #split up the 2nd field to get uid/gid
275 uid=ids[1]
276 gid=ids[2]
277 size=$3
278 date=$4
279 time=$5
281 #this method works with filenames that start with a space (evil!)
282 #split line a time and a space
283 split($0,linesplit, $5 " ")
284 #then split the second item (name&link) at the space arrow space
285 split(linesplit[2], nlsplit, " -> ")
287 name=substr(nlsplit[1],2)
288 link=nlsplit[2]
290 if (! link) {link="-"} #if there was no link set it to a dash
292 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
293 }'
294 exit
295 fi
296 done
298 for ext in $DEB_EXTS; do
299 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
300 dpkg-deb -c "$archive" | $AWK_PROG '
301 {
302 attr=$1
303 split($2,ids,"/") #split up the 2nd field to get uid/gid
304 uid=ids[1]
305 gid=ids[2]
306 size=$3
307 date=$4
308 time=$5
310 #this method works with filenames that start with a space (evil!)
311 #split line a time and a space
312 split($0,linesplit, $5 " ")
313 #then split the second item (name&link) at the space arrow space
314 split(linesplit[2], nlsplit, " -> ")
316 name=substr(nlsplit[1],2)
317 link=nlsplit[2]
319 if (! link) {link="-"} #if there was no link set it to a dash
321 printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
322 }'
323 exit
324 fi
325 done
327 for ext in $TAZPKG_EXTS; do
328 # format of cpio output:
329 # lrwxrwxrwx USR/GRP 0 2005-05-12 00:32:03 file -> /path/to/link
330 # -rw-r--r-- USR/GRP 6622 2005-04-22 12:29:14 file
331 # 1 2 3 4 5 6
332 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
333 tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
334 here="$(pwd)"
335 cd $tmpcpio
336 cpio -i fs.cpio.gz > /dev/null < "$archive"
337 zcat fs.cpio.gz | cpio -tv | $AWK_PROG '
338 {
339 attr=$1
340 split($2,ids,"/") #split up the 2nd field to get uid/gid
341 uid=ids[1]
342 gid=ids[2]
343 size=$3
344 date=$4
345 time=$5
347 #this method works with filenames that start with a space (evil!)
348 #split line a time and a space
349 split($0,linesplit, $5 " ")
350 #then split the second item (name&link) at the space arrow space
351 split(linesplit[2], nlsplit, " -> ")
353 name=substr(nlsplit[1],3)
354 link=nlsplit[2]
356 if (! link) {link="-"} #if there was no link set it to a dash
358 if (name != "" && uid != "blocks") printf "%s;%s;%s;%s;%s;%s;%s;%s\n",name,size,attr,uid,gid,date,time,link
359 }'
360 cd $here
361 rm -rf $tmpcpio
362 exit
363 fi
364 done
365 exit $E_UNSUPPORTED
366 ;;
368 -a) # add to archive passed files
369 not_busybox tar && \
370 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
371 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
372 # we only want to add the file's basename, not
373 # the full path so...
374 decompress_func
375 while [ "$1" ]; do
376 cd "$(dirname "$1")"
377 tar -rf "$archive" "$(basename "$1")"
378 wrapper_status=$?
379 shift 1
380 done
381 compress_func
382 exit $wrapper_status
383 fi
384 done
385 which zip >/dev/null && for ext in $ZIP_EXTS; do
386 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
387 # we only want to add the file's basename, not
388 # the full path so...
389 while [ "$1" ]; do
390 cd "$(dirname "$1")"
391 zip -g -r "$archive" "$(basename "$1")"
392 wrapper_status=$?
393 shift 1
394 done
395 exit $wrapper_status
396 fi
397 done
398 exit $E_UNSUPPORTED
399 ;;
401 -n) # new: create new archive with passed files
402 not_busybox tar && \
403 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
404 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
405 # create will only be passed the first file, the
406 # rest will be "added" to the new archive
407 decompress_func
408 cd "$(dirname "$1")"
409 tar -cf "$archive" "$(basename "$1")"
410 wrapper_status=$?
411 compress_func
412 exit $wrapper_status
413 fi
414 done
415 which zip >/dev/null && for ext in $ZIP_EXTS; do
416 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
417 # create will only be passed the first file, the
418 # rest will be "added" to the new archive
419 cd "$(dirname "$1")"
420 zip -r "$archive" "$(basename "$1")"
421 fi
422 done
423 exit $E_UNSUPPORTED
424 ;;
426 -r) # remove: from archive passed files
427 not_busybox tar && \
428 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
429 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
430 decompress_func
431 tar --delete -f "$archive" "$@"
432 wrapper_status=$?
433 compress_func
434 exit $wrapper_status
435 fi
436 done
437 which zip >/dev/null && for ext in $ZIP_EXTS; do
438 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
439 zip -d "$archive" "$@"
440 fi
441 done
442 exit $E_UNSUPPORTED
443 ;;
445 -e) # extract: from archive passed files
446 for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
447 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
448 # xarchive will put is the right extract dir
449 # so we just have to extract.
450 tar -x${TAR_COMPRESS_OPT}f "$archive" "$@"
451 exit $?
452 fi
453 done
454 for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
455 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
456 if [ -n "$1" ]; then
457 while [ "$1" ]; do
458 $DECOMPRESS "$archive" | cpio -idv "$1"
459 shift 1
460 done
461 else
462 $DECOMPRESS "$archive" | cpio -idv
463 fi
464 exit $?
465 fi
466 done
467 for ext in $ZIP_EXTS; do
468 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
469 # xarchive will put is the right extract dir
470 # so we just have to extract.
471 unzip -n "$archive" "$@"
472 exit $?
473 fi
474 done
475 for ext in $RPM_EXTS; do
476 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
477 if [ -n "$1" ]; then
478 while [ "$1" ]; do
479 rpm2cpio "$archive" | cpio -idv "$1"
480 shift 1
481 done
482 else
483 rpm2cpio "$archive" | cpio -idv
484 fi
485 exit $?
486 fi
487 done
489 for ext in $DEB_EXTS; do
490 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
491 dpkg-deb -X "$archive" "$@"
492 exit $?
493 fi
494 done
495 for ext in $TAZPKG_EXTS; do
496 if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
497 tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
498 here="$(pwd)"
499 cd $tmpcpio
500 cpio -i < "$archive" > /dev/null
501 zcat fs.cpio.gz | cpio -id > /dev/null
502 status=$?
503 if [ -n "$1" ]; then
504 while [ "$1" ]; do
505 dir=$(dirname "$here/$1")
506 mkdir -p "$dir" 2> /dev/null
507 mv "fs/$1" "$dir" 2> /dev/null
508 done
509 else
510 mv fs/* fs/.* $here 2> /dev/null
511 fi
512 cd $here
513 rm -rf $tmpcpio
514 exit $status
515 fi
516 done
517 exit $E_UNSUPPORTED
518 ;;
520 *) echo "error, option $opt not supported"
521 echo "use one of these:"
522 echo "-i #info"
523 echo "-o archive #open"
524 echo "-a archive files #add"
525 echo "-n archive file #new"
526 echo "-r archive files #remove"
527 echo "-e archive files #extract"
528 exit
529 esac