"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "scripts/to-zst.in" between
littleutils-1.2.3.tar.lz and littleutils-1.2.4.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

to-zst.in  (littleutils-1.2.3.tar.lz):to-zst.in  (littleutils-1.2.4.tar.lz)
#! PROGBASH #! PROGBASH
# vim: set filetype=bash: # vim: set filetype=bash:
# to-zst: Converts .Z, .lzo, .zip, .rar, .gz, and .bz2 files to zst format. # to-zst: Converts .Z, .lzo, .zip, .rar, .gz, and .bz2 files to zst format.
# Copyright (C) 2020 by Brian Lindholm. This file is part of the littleutils # Copyright (C) 2020-2021 by Brian Lindholm. This file is part of the
# utility set. # littleutils utility set.
# #
# The to-zst utility is free software; you can redistribute it and/or modify it # The to-zst utility is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free # under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later version. # Software Foundation; either version 3, or (at your option) any later version.
# #
# The to-zst utility is distributed in the hope that it will be useful, but # The to-zst utility is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
# #
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# the littleutils. If not, see <https://www.gnu.org/licenses/>. # the littleutils. If not, see <https://www.gnu.org/licenses/>.
# set up traps
trap 'rm -f ${TMPFILE} ; exit 1' 1 2 3 13 15
trap 'rm -f ${TMPFILE} ; exit 0' 0
# get command-line options # get command-line options
DELORIG='no' DELORIG='n'
MAXCOMP='no' MAXCOMP='n'
OVERWRITE='yes' OVERWRITE='y'
PREFER='' PREFER=''
RETAINTIME='no' RETAINTIME='n'
THREADS='1' declare -i THREADS=1
while getopts 46dhkmnp:t opts while getopts 46dhkmnp:t opts
do do
case $opts in case $opts in
4) PREFER='--prefer-family=IPv4' ;; 4) PREFER='--prefer-family=IPv4' ;;
6) PREFER='--prefer-family=IPv6' ;; 6) PREFER='--prefer-family=IPv6' ;;
d) DELORIG='yes' ;; d) DELORIG='y' ;;
h) echo 'to-zst LU_VERSION' h) echo 'to-zst LU_VERSION'
echo 'usage: to-zst [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]' echo 'usage: to-zst [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
echo ' [-m(ax_compression)] [-n(o_overwrite)] [-p threads]' echo ' [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
echo ' [-t(imestamp retain)] [-h(elp)] filename ...' echo ' [-t(imestamp retain)] [-h(elp)] filename ...'
exit 0 ;; exit 0 ;;
k) DELORIG='small' ;; k) DELORIG='small' ;;
m) MAXCOMP='yes' ;; m) MAXCOMP='y' ;;
n) OVERWRITE='no' ;; n) OVERWRITE='n' ;;
p) THREADS=${OPTARG} ;; p) THREADS=$OPTARG ;;
t) RETAINTIME='yes' ;; t) RETAINTIME='y' ;;
*) echo 'to-zst LU_VERSION' *) echo 'to-zst LU_VERSION'
echo 'usage: to-zst [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]' echo 'usage: to-zst [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
echo ' [-m(ax_compression)] [-n(o_overwrite)] [-p threads]' echo ' [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
echo ' [-t(imestamp retain)] [-h(elp)] filename ...' echo ' [-t(imestamp retain)] [-h(elp)] filename ...'
exit 1 ;; exit 1 ;;
esac esac
done done
shift `expr ${OPTIND} - 1` shift $((${OPTIND}-1))
if [ "$THREADS" = '1' ]; then if [ $THREADS -eq 1 ]; then
if [ "$MAXCOMP" = 'yes' ]; then if [ "$MAXCOMP" = 'y' ]; then
ZSTD_OPTS='--ultra -22' ZSTD_OPTS='--ultra -22'
else else
ZSTD_OPTS='-12' ZSTD_OPTS='-12'
fi fi
else else
if [ "$MAXCOMP" = 'yes' ]; then if [ "$MAXCOMP" = 'y' ]; then
ZSTD_OPTS="-T ${THREADS} --ultra -22" ZSTD_OPTS="-T $THREADS --ultra -22"
else else
ZSTD_OPTS="-T ${THREADS} -12" ZSTD_OPTS="-T $THREADS -12"
fi fi
fi fi
# set up traps
trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
# run through files # run through files
declare -i S0=0 S1=0
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
# if file is actually a URL, attempt to download, otherwise verify that it alr eady exists # if file is actually a URL, attempt to download, otherwise verify that it alr eady exists
NAME="$1" NAME="$1"
if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then
wget ${PREFER} -q ${NAME} wget $PREFER -q "$NAME"
NAME=${NAME##*/} NAME="${NAME##*/}"
if [ ! -f "$NAME" -o ! -r "$NAME" ]; then if [ ! -f "$NAME" -o ! -r "$NAME" ]; then
echo "${NAME} failed to download!" echo "$NAME failed to download!"
shift; continue shift; continue
fi fi
elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then
echo "${NAME} is not a readable file!" echo "$NAME is not a readable file!"
shift; continue shift; continue
fi fi
echo -n "${NAME}... " echo -n "${NAME}... "
# determine filepath and check for writeability # determine filepath and check for writeability
FILENAME="${NAME##*/}" FILENAME="${NAME##*/}"
FILEPATH="${NAME%${FILENAME}}" FILEPATH="${NAME%${FILENAME}}"
FILEPATH="${FILEPATH%/}" FILEPATH="${FILEPATH%/}"
if [ "X$FILEPATH" = 'X' ]; then if [ "X$FILEPATH" = 'X' ]; then
FILEPATH='.' FILEPATH='.'
skipping to change at line 124 skipping to change at line 124
NEWNAME="${NAME%.RAR}.tar.zst" NEWNAME="${NAME%.RAR}.tar.zst"
elif [ "${NAME#${NAME%.Z}}" = '.Z' ]; then elif [ "${NAME#${NAME%.Z}}" = '.Z' ]; then
NEWNAME="${NAME%.Z}.zst" NEWNAME="${NAME%.Z}.zst"
elif [ "${NAME#${NAME%.zip}}" = '.zip' ]; then elif [ "${NAME#${NAME%.zip}}" = '.zip' ]; then
NEWNAME="${NAME%.zip}.tar.zst" NEWNAME="${NAME%.zip}.tar.zst"
elif [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then elif [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then
NEWNAME="${NAME%.ZIP}.tar.zst" NEWNAME="${NAME%.ZIP}.tar.zst"
else else
NEWNAME="${NAME}.zst" NEWNAME="${NAME}.zst"
fi fi
if [ "$OVERWRITE" = 'no' -a -e "$NEWNAME" ]; then if [ "$OVERWRITE" = 'n' -a -e "$NEWNAME" ]; then
echo 'skipped because target already exists' echo 'skipped because target already exists'
shift; continue shift; continue
fi fi
# recompress based on filetype # recompress based on filetype
FILETYPE=`file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]'` FILETYPE=$(file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]')
TMPFILE=`tempname -d "$FILEPATH" .to-zst_$$` || exit 99 TMPFILE=$(tempname -d "$FILEPATH" .to-zst_$$) || exit 99
if [ "X$FILETYPE" = 'Xzsta' ]; then if [ "X$FILETYPE" = 'Xzsta' ]; then
echo 'already converted to zst' echo 'already converted to zst'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
elif [ "X$FILETYPE" = 'Xbzip' ]; then elif [ "X$FILETYPE" = 'Xbzip' ]; then
bzip2 -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > "$TMPFILE" bzip2 -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > $TMPFILE
OUT="$?" if [ $? -ne 0 ]; then
if [ "$OUT" -ne 0 ]; then
echo 'failed on bzip2-to-zst conversion' echo 'failed on bzip2-to-zst conversion'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
fi fi
elif [ "X$FILETYPE" = 'Xgzip' ]; then elif [ "X$FILETYPE" = 'Xgzip' ]; then
gzip -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > "$TMPFILE" gzip -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > $TMPFILE
OUT="$?" if [ $? -ne 0 ]; then
if [ "$OUT" -ne 0 ]; then
echo 'failed on gzip-to-zst conversion' echo 'failed on gzip-to-zst conversion'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
fi fi
elif [ "X$FILETYPE" = 'Xlzop' ]; then elif [ "X$FILETYPE" = 'Xlzop' ]; then
lzop -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > "$TMPFILE" lzop -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > $TMPFILE
OUT="$?" if [ $? -ne 0 ]; then
if [ "$OUT" -ne 0 ]; then
echo 'failed on lzop-to-zst conversion' echo 'failed on lzop-to-zst conversion'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
fi fi
elif [ "X$FILETYPE" = 'Xcomp' ]; then elif [ "X$FILETYPE" = 'Xcomp' ]; then
gzip -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > "$TMPFILE" gzip -c -d -q "$NAME" | zstd -q $ZSTD_OPTS > $TMPFILE
OUT="$?" if [ $? -ne 0 ]; then
if [ "$OUT" -ne 0 ]; then
echo 'failed on compress-to-zst conversion' echo 'failed on compress-to-zst conversion'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
fi fi
elif [ "X$FILETYPE" = 'Xrar ' ]; then elif [ "X$FILETYPE" = 'Xrar ' ]; then
rar2tarcat "$NAME" | zstd -q $ZSTD_OPTS > "$TMPFILE" rar2tarcat "$NAME" | zstd -q $ZSTD_OPTS > $TMPFILE
OUT="$?" if [ $? -ne 0 ]; then
if [ "$OUT" -ne 0 ]; then
echo 'failed on rar-to-zst conversion' echo 'failed on rar-to-zst conversion'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
fi fi
elif [ "X$FILETYPE" = 'Xzip ' ]; then elif [ "X$FILETYPE" = 'Xzip ' ]; then
zip2tarcat "$NAME" | zstd -q $ZSTD_OPTS > "$TMPFILE" zip2tarcat "$NAME" | zstd -q $ZSTD_OPTS > $TMPFILE
OUT="$?" if [ $? -ne 0 ]; then
if [ "$OUT" -ne 0 ]; then
echo 'failed on zip-to-zst conversion' echo 'failed on zip-to-zst conversion'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
fi fi
else else
echo 'skipped on unrecognized filetype' echo 'skipped on unrecognized filetype'
rm -f "$TMPFILE" rm -f $TMPFILE
shift; continue shift; continue
fi fi
# perform the final move, deleting old or new if necessary # perform the final move, deleting old or new if necessary
chmod --reference="$NAME" "$TMPFILE" chmod --reference="$NAME" $TMPFILE
mv "$TMPFILE" "$NEWNAME" mv $TMPFILE "$NEWNAME"
if [ "$RETAINTIME" = 'yes' ]; then if [ "$RETAINTIME" = 'y' ]; then
touch --reference="$NAME" "$NEWNAME" touch --reference="$NAME" "$NEWNAME"
fi fi
S0=`filesize "$NAME"` S0=$(filesize "$NAME")
S1=`filesize "$NEWNAME"` S1=$(filesize "$NEWNAME")
if [ "$S1" -lt "$S0" ]; then if [ $S1 -lt $S0 ]; then
if [ "$DELORIG" != 'no' ]; then if [ "$DELORIG" != 'n' ]; then
rm -f "$NAME" rm -f "$NAME"
echo "${S0} vs. ${S1} (deleting original)" echo "$S0 vs. $S1 (deleting original)"
else else
echo "${S0} vs. ${S1}" echo "$S0 vs. $S1"
fi fi
else else
if [ "$DELORIG" = 'no' ]; then if [ "$DELORIG" = 'n' ]; then
echo "${S0} vs. ${S1} (warning: new file is not smaller)" echo "$S0 vs. $S1 (warning: new file is not smaller)"
elif [ "$DELORIG" = 'yes' ]; then elif [ "$DELORIG" = 'y' ]; then
rm -f "$NAME" rm -f "$NAME"
echo "${S0} vs. ${S1} (deleting original: new file is not smaller)" echo "$S0 vs. $S1 (deleting original: new file is not smaller)"
else else
rm -f "$NEWNAME" rm -f "$NEWNAME"
echo 'new file deleted (not smaller than old)' echo 'new file deleted (not smaller than old)'
fi fi
fi fi
shift shift
done done
 End of changes. 36 change blocks. 
68 lines changed or deleted 62 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)