wok rev 941

xarchive: enhance tar and cpio support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jun 24 13:47:32 2008 +0000 (2008-06-24)
parents 942d5b5c8ec1
children 19e156dfc1af
files xarchive/stuff/slitaz-wrap.sh
line diff
     1.1 --- a/xarchive/stuff/slitaz-wrap.sh	Mon Jun 23 20:33:44 2008 +0000
     1.2 +++ b/xarchive/stuff/slitaz-wrap.sh	Tue Jun 24 13:47:32 2008 +0000
     1.3 @@ -57,7 +57,6 @@
     1.4  lc_archive="$(echo $archive|tr [:upper:] [:lower:])"
     1.5  DECOMPRESS="cat"
     1.6  COMPRESS="cat"
     1.7 -COMPRESS2=""
     1.8  TAR_COMPRESS_OPT=""
     1.9  for ext in $GZIP_EXTS $CPIOGZ_EXTS; do
    1.10      if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
    1.11 @@ -76,8 +75,7 @@
    1.12  for ext in $LZMA_EXTS; do
    1.13      if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
    1.14          DECOMPRESS="unlzma -c" 
    1.15 -        COMPRESS="lzma e"
    1.16 -        COMPRESS2=" -so"
    1.17 +        COMPRESS="lzma e -si -so"
    1.18          TAR_COMPRESS_OPT="a"
    1.19      fi
    1.20  done
    1.21 @@ -108,7 +106,7 @@
    1.22  {
    1.23      if [ "$COMPRESS" ] && [ "$oldarch" ]; then
    1.24          [ -f "$oldarch" ] && rm "$oldarch"
    1.25 -        if $COMPRESS "$archive" $COMPRESS2 > "$oldarch"; then
    1.26 +        if $COMPRESS < "$archive" > "$oldarch"; then
    1.27              rm "$archive"
    1.28          fi
    1.29      fi
    1.30 @@ -122,6 +120,94 @@
    1.31      return 0
    1.32  }
    1.33  
    1.34 +add_file()
    1.35 +{
    1.36 +	( cd $2 ; tar -cf - $1 ) | tar -xf -
    1.37 +}
    1.38 +
    1.39 +remove_file()
    1.40 +{
    1.41 +	rm -rf ./$1
    1.42 +}
    1.43 +
    1.44 +update_tar_cpio()
    1.45 +{
    1.46 +    action=$1
    1.47 +    shift
    1.48 +    tardir="$(dirname "$archive")"
    1.49 +    for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
    1.50 +        if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
    1.51 +	    if [ "$action" = "new_archive" ]; then
    1.52 +	        decompress_func
    1.53 +		cd "$tardir"
    1.54 +		tar -cf "$archive" "${1#$tardir/}"
    1.55 +		status=$?
    1.56 +		compress_func
    1.57 +		exit $status
    1.58 +	    fi
    1.59 +	    if not_busybox tar; then
    1.60 +	        decompress_func
    1.61 +		case "$action" in
    1.62 +		remove_file)
    1.63 +			tar --delete -f "$archive" "$@";;
    1.64 +		add_file)
    1.65 +			cd "$tardir"
    1.66 +			tar -rf "$archive" "${1#$tardir/}";;
    1.67 +		*)	false;;
    1.68 +		esac
    1.69 +		status=$?
    1.70 +		compress_func
    1.71 +		exit $status
    1.72 +	    fi
    1.73 +            tmptar="$(mktemp -d -t tartmp.XXXXXX)"
    1.74 +	    here="$(pwd)"
    1.75 +	    cd $tmptar
    1.76 +            $DECOMPRESS < "$archive" | tar -xf -
    1.77 +            status=$?
    1.78 +	    if [ $status -eq 0 -a -n "$1" ]; then
    1.79 +                    while [ "$1" ]; do
    1.80 +		    	$action "${1#$tardir/}" $here
    1.81 +			shift
    1.82 +		    done
    1.83 +		tar -cf - $(ls -a | grep -v ^\.$  | grep -v ^\.\.$) | \
    1.84 +			$COMPRESS > "$archive"
    1.85 +                status=$?
    1.86 +	    fi
    1.87 +	    cd $here
    1.88 +	    rm -rf $tmptar
    1.89 +	    exit $status
    1.90 +	fi
    1.91 +    done
    1.92 +    for ext in $CPIO_EXTS $CPIOGZ_EXTS; do
    1.93 +        if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
    1.94 +	    if [ "$action" = "new_archive" ]; then
    1.95 +	        decompress_func
    1.96 +		cd "$tardir"
    1.97 +		echo "${1#$tardir/}" | cpio -o -H newc > "$archive"
    1.98 +		status=$?
    1.99 +		compress_func
   1.100 +		exit $status
   1.101 +	    fi
   1.102 +            tmpcpio="$(mktemp -d -t cpiotmp.XXXXXX)"
   1.103 +	    here="$(pwd)"
   1.104 +	    cd $tmpcpio
   1.105 +            $DECOMPRESS "$archive" | cpio -id > /dev/null
   1.106 +            status=$?
   1.107 +	    if [ $status -eq 0 -a -n "$1" ]; then
   1.108 +                    while [ "$1" ]; do
   1.109 +		    	$action "${1#$tardir/}" $here
   1.110 +			shift
   1.111 +		    done
   1.112 +		find . | cpio -o -H newc | $COMPRESS > "$archive"
   1.113 +                status=$?
   1.114 +	    fi
   1.115 +	    cd $here
   1.116 +	    rm -rf $tmpcpio
   1.117 +	    exit $status
   1.118 +	fi
   1.119 +    done
   1.120 +}
   1.121 +
   1.122  # the option switches
   1.123  case "$opt" in
   1.124      -i) # info: output supported extentions for progs that exist
   1.125 @@ -132,9 +218,6 @@
   1.126              if [ "$ext" = "zip" -a ! "$(which zip)" ]; then
   1.127                  echo warning: zip not found, extract only >/dev/stderr
   1.128              fi
   1.129 -            if [ "$ext" = "tar" ] && ! not_busybox tar; then
   1.130 -                echo warning: gnu/tar not found, extract only >/dev/stderr
   1.131 -            fi
   1.132          done
   1.133          printf "\n"
   1.134          exit
   1.135 @@ -366,22 +449,7 @@
   1.136          ;;
   1.137  
   1.138      -a) # add to archive passed files
   1.139 -    	not_busybox tar && \
   1.140 -        for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
   1.141 -            if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
   1.142 -                # we only want to add the file's basename, not
   1.143 -                # the full path so...
   1.144 -                decompress_func
   1.145 -                while [ "$1" ]; do
   1.146 -                    cd "$(dirname "$1")"
   1.147 -                    tar -rf "$archive" "$(basename "$1")"
   1.148 -                    wrapper_status=$?
   1.149 -                    shift 1
   1.150 -                done
   1.151 -                compress_func
   1.152 -                exit $wrapper_status
   1.153 -	    fi
   1.154 -	done
   1.155 +	update_tar_cpio add_file "$@"
   1.156          which zip >/dev/null && for ext in $ZIP_EXTS; do
   1.157              if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
   1.158                  # we only want to add the file's basename, not
   1.159 @@ -399,19 +467,7 @@
   1.160          ;;
   1.161  
   1.162      -n) # new: create new archive with passed files 
   1.163 -    	not_busybox tar && \
   1.164 -        for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
   1.165 -            if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
   1.166 -                # create will only be passed the first file, the
   1.167 -                # rest will be "added" to the new archive
   1.168 -                decompress_func
   1.169 -                cd "$(dirname "$1")"
   1.170 -                tar -cf "$archive" "$(basename "$1")"
   1.171 -                wrapper_status=$?
   1.172 -                compress_func
   1.173 -                exit $wrapper_status
   1.174 -	    fi
   1.175 -	done
   1.176 +	update_tar_cpio new_archive "$@"
   1.177          which zip >/dev/null && for ext in $ZIP_EXTS; do
   1.178              if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
   1.179                  # create will only be passed the first file, the
   1.180 @@ -424,16 +480,7 @@
   1.181          ;;
   1.182  
   1.183      -r) # remove: from archive passed files 
   1.184 -    	not_busybox tar && \
   1.185 -        for ext in $TAR_EXTS $GZIP_EXTS $BZIP2_EXTS $COMPRESS_EXTS $LZMA_EXTS; do
   1.186 -            if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
   1.187 -                decompress_func
   1.188 -                tar --delete -f "$archive" "$@"
   1.189 -                wrapper_status=$?
   1.190 -                compress_func
   1.191 -                exit $wrapper_status
   1.192 -	    fi
   1.193 -	done
   1.194 +	update_tar_cpio remove_file "$@"
   1.195          which zip >/dev/null && for ext in $ZIP_EXTS; do
   1.196              if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
   1.197                  zip -d "$archive" "$@"
   1.198 @@ -447,7 +494,7 @@
   1.199              if [ $(expr "$lc_archive" : ".*\."$ext"$") -gt 0 ]; then
   1.200                  # xarchive will put is the right extract dir
   1.201                  # so we just have to extract.
   1.202 -                tar -x${TAR_COMPRESS_OPT}f "$archive" "$@" 
   1.203 +		$DECOMPRESS < "$archive" | tar -xf - "$@"
   1.204                  exit $?
   1.205  	    fi
   1.206  	done