"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/to-xz.in" (28 Mar 2021, 7355 Bytes) of package /linux/privat/littleutils-1.2.4.tar.lz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # to-xz: Converts .Z, .lzo, .zip, .rar, .gz, .bz2, .zst, and .lzma files to xz
    5 # format.
    6 
    7 # Copyright (C) 2009-2021 by Brian Lindholm and D. Gloger.  This file is part
    8 # of the littleutils utility set.
    9 #
   10 # The to-xz utility is free software; you can redistribute it and/or modify it
   11 # under the terms of the GNU General Public License as published by the Free
   12 # Software Foundation; either version 3, or (at your option) any later version.
   13 #
   14 # The to-xz utility is distributed in the hope that it will be useful, but
   15 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   16 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   17 # more details.
   18 #
   19 # You should have received a copy of the GNU General Public License along with
   20 # the littleutils.  If not, see <https://www.gnu.org/licenses/>.
   21 
   22 # get command-line options
   23 DELORIG='n'
   24 LZMAUTIL='PROGLZMA'
   25 MAXCOMP='n'
   26 OVERWRITE='y'
   27 PREFER=''
   28 RETAINTIME='n'
   29 declare -i THREADS=1
   30 while getopts 46dhkmnp:t opts
   31 do
   32   case $opts in
   33     4) PREFER='--prefer-family=IPv4' ;;
   34     6) PREFER='--prefer-family=IPv6' ;;
   35     d) DELORIG='y' ;;
   36     h) echo 'to-xz LU_VERSION'
   37        echo 'usage: to-xz [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   38        echo '             [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
   39        echo '             [-t(imestamp retain)] [-h(elp)] filename ...'
   40        exit 0 ;;
   41     k) DELORIG='small' ;;
   42     m) MAXCOMP='y' ;;
   43     n) OVERWRITE='n' ;;
   44     p) THREADS=$OPTARG ;;
   45     t) RETAINTIME='y' ;;
   46     *) echo 'to-xz LU_VERSION'
   47        echo 'usage: to-xz [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   48        echo '             [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
   49        echo '             [-t(imestamp retain)] [-h(elp)] filename ...'
   50        exit 1 ;;
   51   esac
   52 done
   53 shift $((${OPTIND}-1))
   54 if [ $THREADS -eq 1 ]; then
   55   if [ "$MAXCOMP" = 'y' ]; then
   56     XZ_OPTS='-9'
   57   else
   58     XZ_OPTS='-6'
   59   fi
   60 else
   61   if [ "$MAXCOMP" = 'y' ]; then
   62     XZ_OPTS="-T ${THREADS} -9"
   63   else
   64     XZ_OPTS="-T ${THREADS} -6"
   65   fi
   66 fi
   67 
   68 # set up traps
   69 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   70 
   71 # run through files
   72 declare -i S0=0 S1=0
   73 while [ $# -gt 0 ]; do
   74 
   75   # if file is actually a URL, attempt to download, otherwise verify that it already exists
   76   NAME="$1"
   77   if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then
   78     wget $PREFER -q "$NAME"
   79     NAME="${NAME##*/}"
   80     if [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   81       echo "$NAME failed to download!"
   82       shift; continue
   83     fi
   84   elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   85     echo "$NAME is not a readable file!"
   86     shift; continue
   87   fi
   88   echo -n "${NAME}... "
   89 
   90   # determine filepath and check for writeability
   91   FILENAME="${NAME##*/}"
   92   FILEPATH="${NAME%${FILENAME}}"
   93   FILEPATH="${FILEPATH%/}"
   94   if [ "X$FILEPATH" = 'X' ]; then
   95     FILEPATH='.'
   96   fi
   97   if [ ! -w "$FILEPATH" ]; then
   98     echo 'failed because target directory is non-writeable'
   99     shift; continue
  100   fi
  101 
  102   # determine new name and check for availability
  103   if  [ "${NAME#${NAME%.bz2}}" = '.bz2' ]; then
  104     NEWNAME="${NAME%.bz2}.xz"
  105   elif [ "${NAME#${NAME%.tbz}}" = '.tbz' ]; then
  106     NEWNAME="${NAME%.tbz}.txz"
  107   elif [ "${NAME#${NAME%.gz}}" = '.gz' ]; then
  108     NEWNAME="${NAME%.gz}.xz"
  109   elif [ "${NAME#${NAME%.tgz}}" = '.tgz' ]; then
  110     NEWNAME="${NAME%.tgz}.txz"
  111   elif [ "${NAME#${NAME%.lzma}}" = '.lzma' ]; then
  112     NEWNAME="${NAME%.lzma}.xz"
  113   elif [ "${NAME#${NAME%.lzo}}" = '.lzo' ]; then
  114     NEWNAME="${NAME%.lzo}.xz"
  115   elif  [ "${NAME#${NAME%.rar}}" = '.rar' ]; then
  116     NEWNAME="${NAME%.rar}.tar.xz"
  117   elif  [ "${NAME#${NAME%.RAR}}" = '.RAR' ]; then
  118     NEWNAME="${NAME%.RAR}.tar.xz"
  119   elif [ "${NAME#${NAME%.Z}}" = '.Z' ]; then
  120     NEWNAME="${NAME%.Z}.xz"
  121   elif  [ "${NAME#${NAME%.zip}}" = '.zip' ]; then
  122     NEWNAME="${NAME%.zip}.tar.xz"
  123   elif  [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then
  124     NEWNAME="${NAME%.ZIP}.tar.xz"
  125   elif [ "${NAME#${NAME%.zst}}" = '.zst' ]; then
  126     NEWNAME="${NAME%.zst}.xz"
  127   else
  128     NEWNAME="${NAME}.xz"
  129   fi
  130   if [ "$OVERWRITE" = 'n' -a -e "$NEWNAME" ]; then
  131     echo 'skipped because target already exists'
  132     shift; continue
  133   fi
  134 
  135   # recompress based on filetype
  136   FILETYPE=$(file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]')
  137   # file 5.02 and earlier cannot detect lzma, listing "data" instead
  138   if [ "$FILETYPE" = 'data' -a "${NAME#${NAME%.lzma}}" = '.lzma' ]; then
  139     FILETYPE='lzma'
  140   fi
  141   TMPFILE=$(tempname -d "$FILEPATH" .to-xz_$$) || exit 99
  142   if [ "X$FILETYPE" = 'Xxz c' ]; then
  143     echo 'already converted to xz'
  144     rm -f $TMPFILE
  145     shift; continue
  146   elif [ "X$FILETYPE" = 'Xbzip' ]; then
  147     bzip2 -c -d -q "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  148     if [ $? -ne 0 ]; then
  149       echo 'failed on bzip2-to-xz conversion'
  150       rm -f $TMPFILE
  151       shift; continue
  152     fi
  153   elif [ "X$FILETYPE" = 'Xgzip' ]; then
  154     gzip -c -d -q "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  155     if [ $? -ne 0 ]; then
  156       echo 'failed on gzip-to-xz conversion'
  157       rm -f $TMPFILE
  158       shift; continue
  159     fi
  160   elif [ "X$FILETYPE" = 'Xlzma' ]; then
  161     if [ "X$LZMAUTIL" = 'Xlzma_alone' ]; then
  162       lzma_alone d "$NAME" -so | xz -q $XZ_OPTS > $TMPFILE
  163     else
  164       lzma -c -d -q "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  165     fi
  166     if [ $? -ne 0 ]; then
  167       echo 'failed on lzma-to-xz conversion'
  168       rm -f $TMPFILE
  169     else
  170       CONVERTED='y'
  171     fi
  172   elif [ "X$FILETYPE" = 'Xlzop' ]; then
  173     lzop -c -d -q "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  174     if [ $? -ne 0 ]; then
  175       echo 'failed on lzop-to-xz conversion'
  176       rm -f $TMPFILE
  177       shift; continue
  178     fi
  179   elif [ "X$FILETYPE" = 'Xcomp' ]; then
  180     gzip -c -d -q "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  181     if [ $? -ne 0 ]; then
  182       echo 'failed on compress-to-xz conversion'
  183       rm -f $TMPFILE
  184       shift; continue
  185     fi
  186   elif [ "X$FILETYPE" = 'Xrar ' ]; then
  187     rar2tarcat "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  188     if [ $? -ne 0 ]; then
  189       echo 'failed on rar-to-xz conversion'
  190       rm -f $TMPFILE
  191       shift; continue
  192     fi
  193   elif [ "X$FILETYPE" = 'Xzip ' ]; then
  194     zip2tarcat "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  195     if [ $? -ne 0 ]; then
  196       echo 'failed on zip-to-xz conversion'
  197       rm -f $TMPFILE
  198       shift; continue
  199     fi
  200   elif [ "X$FILETYPE" = 'Xzsta' ]; then
  201     zstd -c -d -q "$NAME" | xz -q $XZ_OPTS > $TMPFILE
  202     if [ $? -ne 0 ]; then
  203       echo 'failed on zst-to-xz conversion'
  204       rm -f $TMPFILE
  205       shift; continue
  206     fi
  207   else
  208     echo 'skipped on unrecognized filetype'
  209     rm -f $TMPFILE
  210     shift; continue
  211   fi
  212 
  213   # perform the final move, deleting old or new if necessary
  214   chmod --reference="$NAME" $TMPFILE
  215   mv $TMPFILE "$NEWNAME"
  216   if [ "$RETAINTIME" = 'y' ]; then
  217     touch --reference="$NAME" "$NEWNAME"
  218   fi
  219   S0=$(filesize "$NAME")
  220   S1=$(filesize "$NEWNAME")
  221   if [ $S1 -lt $S0 ]; then
  222     if [ "$DELORIG" != 'n' ]; then
  223       rm -f "$NAME"
  224       echo "$S0 vs. $S1 (deleting original)"
  225     else
  226       echo "$S0 vs. $S1"
  227     fi
  228   else
  229     if [ "$DELORIG" = 'n' ]; then
  230       echo "$S0 vs. $S1 (warning: new file is not smaller)"
  231     elif [ "$DELORIG" = 'y' ]; then
  232       rm -f "$NAME"
  233       echo "$S0 vs. $S1 (deleting original: new file is not smaller)"
  234     else
  235       rm -f "$NEWNAME"
  236       echo 'new file deleted (not smaller than old)'
  237     fi
  238   fi
  239 
  240   shift
  241 done