"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/to-zst.in" (28 Mar 2021, 6507 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. See also the latest Fossies "Diffs" side-by-side code changes report for "to-zst.in": 1.2.3_vs_1.2.4.

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