compress.sh: multiple correction and securisation

This commit is contained in:
Geoffray Levasseur-Brandin
2025-06-19 14:49:49 +02:00
parent f0f80e2924
commit 47e89b3b09

View File

@@ -35,7 +35,7 @@
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Smartly uncompress archives (zip only)
# Smartly uncompress archives (zip only for now)
# ------------------------------------------------------------------------------
utaz()
{
@@ -81,28 +81,32 @@ utaz()
esac
done
[[ $createdir && $nodir ]] && disp E "The --create-dir and --no-dir options are mutually exclusive."
[[ $createdir && $nodir ]] && \
disp E "The --create-dir and --no-dir options are mutually exclusive."
[[ ! $LIST ]] && local LIST="."
for zitem in $LIST; do
[[ $(ls $zitem/*.zip 2>/dev/null | wc -l) -eq 0 ]] &&
disp W "$zitem contains no supported archive file, skipping." &&
shopt -s nullglob
local zips=("$zitem"/*.zip)
shopt -u nullglob
[[ ${#zips[@]} -eq 0 ]] && \
disp W "$zitem contains no supported archive file, skipping." && \
continue
for f in $zitem/*.zip; do
disp I "Processing archive $zitem/$f... "
for f in "$zitem"/*.zip; do
disp I "Processing archive $f... "
local dir=${f::-4}
mkdir -p $dir
mkdir -p "$dir"
[[ $? -gt 0 ]] &&
disp E "The filesystem can't create directories, exit!" &&
return 1
unzip -o $f -d $dir >/dev/null 2>&1
unzip -o "$f" -d "$dir" >/dev/null 2>&1
case $? in
0)
[[ $willrm ]] && rm -f $f && disp I "File $zitem/$f deleted."
[[ $willrm ]] && rm -f "$f" && disp I "File $zitem/$f deleted."
;;
1)
@@ -110,7 +114,7 @@ utaz()
;;
*)
disp E "The zip file seems corrupted, failed."
rm -rf $dir >/dev/null 2>&1
rm -rf "$dir" >/dev/null 2>&1
continue
;;
esac
@@ -118,7 +122,7 @@ utaz()
if [[ $createdir ]]; then
disp I "Archive extracted successfully in subdirectory."
elif [[ $nodir ]]; then
mv ./$dir/* ./ && rmdir $dir
mv "./$dir/"* ./ && rmdir "$dir"
disp I "Archive extracted successfully, no subdirectory needed."
else
subdirs=$(find $dir -maxdepth 1 | wc -l)
@@ -167,7 +171,7 @@ taz()
disp E "Program 'plzip' or 'lzip' are not installed, aborting."
return 127
}
local command=lzip
command=lzip
local procopt=""
[[ $2 -gt 1 ]] &&
disp W "lzip doesn't support multithreading, falling back to 1 thread." &&
@@ -195,7 +199,7 @@ taz()
local procopt=""
[[ $2 -gt 1 ]] &&
disp W "gzip doesn't support multithreading, falling back to 1 thread." &&
disp W "Consitder installing pigz to obtain multithreading abilities."
disp W "Consider installing pigz to obtain multithreading abilities."
}
[[ $4 ]] && local verb="--verbose"
@@ -219,7 +223,7 @@ taz()
local procopt=""
[[ $2 -gt 1 ]] &&
disp W "bzip2 doesn't support multithreading, falling back to 1 thread." &&
disp W "Consitder installing pbzip2 to obtain multithreading abilities."
disp W "Consider installing pbzip2 to obtain multithreading abilities."
}
[[ $4 ]] && local verb="-v"
@@ -291,7 +295,7 @@ taz()
;;
"-q" | "--quiet")
QUIET=1
local quiet=1
;;
"-"*)
@@ -312,17 +316,17 @@ taz()
[[ ! $compform ]] && compform=lz # safe and efficient (unless data are already compressed)
[[ ! $nproc ]] && nproc=1
[[ ! $complevel ]] && complevel=6
[[ $verbose -gt 1 && $QUIET -gt 1 ]] &&
[[ $verbose -gt 1 && $quiet -gt 1 ]] &&
disp E "The --verbose and --quiet options can't be used together."
for item in $LIST; do
local donetar=0
disp I "Processing $item..."
if [[ -d $item ]]; then
if [[ -d "$item" ]]; then
disp I "\t Creating $item.tar... "
tar -cf $item{.tar,}
tar -cf "$item.tar" "$item"
if [[ ! $? -eq 0 ]]; then
disp E "tar file creation failed, skipping to next item."
continue
@@ -337,7 +341,7 @@ taz()
# Skip compression part if tar is asked
if [[ $compform != "tar" ]]; then
disp I "\t Compressing archive..."
_do$compform $fname $nproc $complevel $verbose
_do$compform "$fname" "$nproc" "$complevel" "$verbose"
[[ ! $? -eq 0 ]] && case $? in
127)
disp E "Compression program unavailable, aborting."
@@ -349,15 +353,15 @@ taz()
;;
esac
[[ $donetar -gt 0 ]] && rm $fname
[[ $donetar -gt 0 ]] && rm "$fname"
fi
if [[ $willrm ]]; then
disp I "\t Deleting original source as asked... "
rm -r $item
rm -r "$item"
fi
done
unset QUIET
unset quiet
}
export -f taz